Ignore:
Timestamp:
09/02/11 07:16:44 (9 months ago)
Author:
jharrop
Message:

Improved control over what addTargetPart does in the event of a part name collision.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/docx4j/src/main/java/org/docx4j/openpackaging/Base.java

    r1648 r1649  
    2727import org.docx4j.openpackaging.contenttype.ContentType; 
    2828import org.docx4j.openpackaging.contenttype.ObjectFactory; 
     29import org.docx4j.openpackaging.exceptions.Docx4JException; 
    2930import org.docx4j.openpackaging.exceptions.InvalidFormatException; 
    3031import org.docx4j.openpackaging.packages.OpcPackage; 
     
    3233import org.docx4j.openpackaging.parts.PartName; 
    3334import org.docx4j.openpackaging.parts.relationships.RelationshipsPart; 
     35import org.docx4j.openpackaging.parts.relationships.RelationshipsPart.AddPartBehaviour; 
    3436import org.docx4j.relationships.Relationship; 
    3537 
     
    171173//      } 
    172174 
     175        /** 
     176         * Convenience method to add a part to this Part's 
     177         * relationships.  The package must be set on this 
     178         * part in order for this to work. 
     179         *  
     180         * @since 2.7.1 
     181         *  
     182         * @param targetpart The part to add to this part's relationships 
     183         * @param mode whether to overwrite, rename or abort if the part name already exists 
     184         * @param proposedRelId 
     185         * @return 
     186         * @throws InvalidFormatException 
     187         */      
     188        public Relationship addTargetPart(Part targetpart, AddPartBehaviour mode 
     189                        ) throws InvalidFormatException { 
     190                return addTargetPart(targetpart, mode, null); 
     191        } 
    173192         
    174193        /** 
     
    187206         */ 
    188207        public Relationship addTargetPart(Part targetpart) throws InvalidFormatException { 
    189                 return this.addTargetPart(targetpart, null); 
    190         } 
    191          
     208                return this.addTargetPart(targetpart, AddPartBehaviour.OVERWRITE_IF_NAME_EXISTS, null); 
     209        } 
     210 
    192211        /** 
    193212         * Convenience method to add a part to this Part's 
     
    207226                        ) throws InvalidFormatException { 
    208227                 
     228                return addTargetPart( targetpart, AddPartBehaviour.OVERWRITE_IF_NAME_EXISTS, proposedRelId); 
     229        } 
     230         
     231         
     232        /** 
     233         * Convenience method to add a part to this Part's 
     234         * relationships.  The package must be set on this 
     235         * part in order for this to work. 
     236         *  
     237         * The added part will replace any existing part 
     238         * with the same name (ie same target in the rels  
     239         * part).  In other words, if you want to use the 
     240         * one image as the target of 2 rels, don't use 
     241         * this method.  
     242         *  
     243         * @since 2.7.1 
     244         *  
     245         * @param targetpart The part to add to this part's relationships 
     246         * @param mode whether to overwrite, rename or abort if the part name already exists 
     247         * @param proposedRelId 
     248         * @return 
     249         * @throws InvalidFormatException 
     250         */ 
     251        public Relationship addTargetPart(Part targetpart, AddPartBehaviour mode, String proposedRelId 
     252                        ) throws InvalidFormatException { 
     253                 
    209254                if ( this.getPackage()==null ) {                                                 
    210255                        throw new InvalidFormatException("Package not set; if you are adding part2 to part1, make sure part1 is added first."); 
     
    215260                 
    216261                // Now add the targetpart to the relationships 
    217                 Relationship rel = this.getRelationshipsPart().addPart(targetpart, true,  
     262                Relationship rel = this.getRelationshipsPart().addPart(targetpart, mode,  
    218263                                getPackage().getContentTypeManager(), proposedRelId); 
    219264                 
     
    227272                 
    228273        } 
    229  
    230          
    231274         
    232275} 
Note: See TracChangeset for help on using the changeset viewer.