log.debug("Result " + target);
// Check whether we already have a rel with this target
// This code is a bit more efficient than getRel, since it
// doesn't unrelativise each existing rel!
Relationship existsAlready = null;
for (Relationship rel : jaxbElement.getRelationship() ) {
if (rel.getTarget().equals(target)) {
existsAlready = rel;
break; // Assumes at most 1 existing rel with this target
}
}
if (log.isDebugEnabled()) {
boolean exists = (existsAlready!=null);
log.debug("rel exists: " + exists);
}
if (existsAlready!=null
&& mode.equals(AddPartBehaviour.REUSE_EXISTING)) {
log.debug("Returning preexisting rel");
return existsAlready;
}
// sanity check
if (existsAlready!=null && mode.equals(AddPartBehaviour.RENAME_IF_NAME_EXISTS)) {
// Shouldn't happen
throw new InvalidFormatException("Found existing rel, and yet constructed part name should be globally unique!");
}
if (this.getPackage().getParts().get( newPartName )!=null
&& mode.equals(AddPartBehaviour.OVERWRITE_IF_NAME_EXISTS)) {
// ie we have the same part in the package already, not
// necessarily referenced by a rel in this rp.
// overwrite the part
if (existsAlready!=null) {
// we reuse it. can't assume its the same type, though, so
existsAlready.setType( part.getRelationshipType() );
loadPart(part, existsAlready);
return existsAlready;
}
// case where rel doesn't exist (it might not in this rp), create a rel to it
// is handled below
}
// OK, create the new rel
org.docx4j.relationships.ObjectFactory factory =
new org.docx4j.relationships.ObjectFactory();
Relationship rel = factory.createRelationship();
rel.setTarget(target );
//rel.setTargetMode( TargetMode.INTERNAL );
rel.setType( part.getRelationshipType() );
if (relId!=null) {
rel.setId( relId );
}
addRelationship(rel );
String ext = part.getPartName().getExtension();