NodeList destStyleNodeList = (NodeList) xpath.evaluate("//*[@" + styleQName + "]", dom, XPathConstants.NODESET);
// HashMap<String, String> styleRenameMap = new HashMap<String, String>();
Iterator<OdfElement> iter = srcStyleCloneEleList.keySet().iterator();
while (iter.hasNext()) {
OdfElement styleElement = iter.next();
OdfElement cloneStyleElement = appendStyleList.get(styleElement);
if (cloneStyleElement == null) {
cloneStyleElement = (OdfElement) styleElement.cloneNode(true);
appendStyleList.put(styleElement, cloneStyleElement);
}
String styleName = styleElement.getAttributeNS(styleURI, styleLocalName);
List<String> newStyleNameList = styleRenameMap.get(styleName);
// if the newStyleNameList != null, means that styleName exists in dest document
// and it has already been renamed
if ((newStyleNameList != null)
|| (isStyleNameExist(destStyleNodeList, styleName) != null)) {
String newStyleName = null;
if (newStyleNameList == null) {
newStyleNameList = new ArrayList<String>();
newStyleName = styleName + "-" + makeUniqueName();
newStyleNameList.add(newStyleName);
styleRenameMap.put(styleName, newStyleNameList);
} else {
for (int i = 0; i < newStyleNameList.size(); i++) {
String styleNameIter = newStyleNameList.get(i);
OdfElement destStyleElementWithNewName = isStyleNameExist(destStyleNodeList, styleNameIter);
//check if the two style elements have the same content
//if not, the cloneStyleElement should rename, rather than reuse the new style name
cloneStyleElement.setAttributeNS(styleURI, styleQName, styleNameIter);
if ((destStyleElementWithNewName != null) && destStyleElementWithNewName.equals(cloneStyleElement)) {
newStyleName = styleNameIter;
break;
}
}
if (newStyleName == null) {
newStyleName = styleName + "-" + makeUniqueName();
newStyleNameList.add(newStyleName);
}
}
// if newStyleName has been set in the element as the new name
// which means that the newStyleName is conform to the odf spec
// then change element style reference name
if (changeStyleRefName(srcStyleCloneEleList.get(styleElement), styleName, newStyleName)) {
cloneStyleElement.setAttributeNS(styleURI, styleQName, newStyleName);
//if display name should also be renamed
String displayName = cloneStyleElement.getAttributeNS(styleURI, "display-name");
if ((displayName != null) && (displayName.length() > 0)) {
cloneStyleElement.setAttributeNS(styleURI, stylePrefix + ":display-name",
displayName + newStyleName.substring(newStyleName.length() - 8));
}
}
}
}
iter = appendStyleList.keySet().iterator();
while (iter.hasNext()) {
OdfElement styleElement = iter.next();
OdfElement cloneStyleElement = appendStyleList.get(styleElement);
String newStyleName = cloneStyleElement.getAttributeNS(styleURI, styleLocalName);
Boolean isAppended = styleAppendMap.get(newStyleName);
//if styleAppendMap contain the newStyleName,
//means that cloneStyleElement has already been appended
if ((isAppended != null) && isAppended.booleanValue() == true) {
continue;
} else {
styleAppendMap.put(newStyleName, true);
}
OdfElement cloneForeignStyleElement = (OdfElement) cloneForeignElement(cloneStyleElement, dom, true);
String styleElePath = getElementPath(styleElement);
appendForeignStyleElement(cloneForeignStyleElement, dom, styleElePath);
copyForeignLinkRef(cloneStyleElement);
}
} catch (Exception e) {