if (fileDom instanceof OdfContentDom) {
xpath = ((OdfContentDom) fileDom).getXPath();
} else {
xpath = ((OdfStylesDom) fileDom).getXPath();
}
OdfPackageDocument srcDoc = fileDom.getDocument();
// new a map to put the original name and the rename string, in case
// that the same name might be referred by the slide several times.
HashMap<String, String> objectRenameMap = new HashMap<String, String>();
NodeList linkNodes = (NodeList) xpath.evaluate(".//*[@xlink:href]", sourceCloneEle, XPathConstants.NODESET);
for (int i = 0; i <= linkNodes.getLength(); i++) {
OdfElement object = null;
if (linkNodes.getLength() == i) {
if (sourceCloneEle.hasAttributeNS(OdfDocumentNamespace.XLINK.getUri(), "href")) {
object = sourceCloneEle;
} else {
break;
}
} else {
object = (OdfElement) linkNodes.item(i);
}
String refObjPath = object.getAttributeNS(OdfDocumentNamespace.XLINK.getUri(), "href");
if (refObjPath != null && refObjPath.length() > 0) {
// the path of the object is start with "./"
boolean hasPrefix = false;
String prefix = "./";
if (refObjPath.startsWith(prefix)) {
refObjPath = refObjPath.substring(2);
hasPrefix = true;
}
// check if the current document contains the same path
OdfFileEntry fileEntry = getPackage().getFileEntry(refObjPath);
// note: if refObjPath is a directory, it must end with '/'
if (fileEntry == null) {
fileEntry = getPackage().getFileEntry(refObjPath + "/");
}
String newObjPath = refObjPath;
if (fileEntry != null) {
// rename the object path
newObjPath = objectRenameMap.get(refObjPath);
if (newObjPath == null) {
// if refObjPath still contains ".", it means that
// it has the suffix
// then change the name before the suffix string
int dotIndex = refObjPath.indexOf(".");
if (dotIndex != -1) {
newObjPath = refObjPath.substring(0, dotIndex) + "-" + makeUniqueName()
+ refObjPath.substring(dotIndex);
} else {
newObjPath = refObjPath + "-" + makeUniqueName();
}
objectRenameMap.put(refObjPath, newObjPath);
}
object.setAttributeNS(OdfDocumentNamespace.XLINK.getUri(), "xlink:href",
hasPrefix ? (prefix + newObjPath) : newObjPath);
}
InputStream is = srcDoc.getPackage().getInputStream(refObjPath);
if (is != null) {
String mediaType = srcDoc.getPackage().getFileEntry(refObjPath).getMediaTypeString();
getPackage().insert(is, newObjPath, mediaType);
} else {
Document embedDoc = ((Document) srcDoc).getEmbeddedDocument(refObjPath);
if (embedDoc != null) {
insertDocument(embedDoc, newObjPath);