Ignore:
Timestamp:
04/10/11 13:29:24 (14 months ago)
Author:
jharrop
Message:

Address issue reported in  http://dev.plutext.org/forums/viewtopic.php?f=6&t=667

Location:
trunk/docx4j/src/main/java/org/docx4j/openpackaging
Files:
5 edited

Legend:

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

    r1449 r1467  
    6262        } 
    6363 
    64         public ContentTypeManager ctm; 
     64        //public ContentTypeManager ctm; 
    6565 
    6666        protected boolean loadExternalTargets = false;  
     
    6969        } 
    7070         
    71         /** 
    72          * This HashMap is intended to prevent loops. 
    73          */ 
    74         protected HashMap<String, String> handled = new HashMap<String, String>(); 
    7571         
    7672        /** 
  • trunk/docx4j/src/main/java/org/docx4j/openpackaging/io/LoadFromVFSZipFile.java

    r1066 r1467  
    6969        } 
    7070 
    71         public LoadFromVFSZipFile(ContentTypeManager ctm, boolean loadExternalTargets) { 
    72                 _loadFromZipFile = new LoadFromZipFile(ctm); 
    73                 _loadFromZipFile.loadExternalTargets(loadExternalTargets); 
    74         } 
     71//      public LoadFromVFSZipFile(ContentTypeManager ctm, boolean loadExternalTargets) { 
     72//              _loadFromZipFile = new LoadFromZipFile(ctm); 
     73//              _loadFromZipFile.loadExternalTargets(loadExternalTargets); 
     74//      } 
    7575         
    7676        public OpcPackage get(String filepath) throws Docx4JException { 
  • trunk/docx4j/src/main/java/org/docx4j/openpackaging/io/LoadFromZipFile.java

    r1421 r1467  
    8686         // HashMap containing the names of all the zip entries, 
    8787        // so we can tell whether there are any orphans 
    88         public HashMap unusedZipEntries = null; 
     88//      public HashMap unusedZipEntries = null; 
    8989         
    9090        public LoadFromZipFile() { 
    91                 this(new ContentTypeManager() ); 
    92         } 
    93  
    94         public LoadFromZipFile(ContentTypeManager ctm) { 
    95                 this.ctm = ctm; 
    96         } 
     91//              this(new ContentTypeManager() ); 
     92        } 
     93 
     94//      public LoadFromZipFile(ContentTypeManager ctm) { 
     95//              this.ctm = ctm; 
     96//      } 
    9797         
    9898         
     
    123123//              entries, so we can tick them off. 
    124124                 
    125                 unusedZipEntries = new HashMap(); 
     125//              unusedZipEntries = new HashMap(); 
    126126                Enumeration entries = zf.entries(); 
    127127                while (entries.hasMoreElements()) { 
    128128                        ZipEntry entry = (ZipEntry) entries.nextElement(); 
    129                         unusedZipEntries.put(entry.getName(), new Boolean(true) );       
     129//                      unusedZipEntries.put(entry.getName(), new Boolean(true) );       
    130130                } 
    131131                 
     
    133133                //              Eventually, you'll also be able to create an Excel package etc 
    134134                //              but only the WordML package exists at present 
     135                ContentTypeManager ctm = new ContentTypeManager(); 
    135136                 
    136137                try { 
     
    147148//              each PartName, and use it in the Part constructor. 
    148149//              p.setContentTypeManager(ctm); - 20080111 - done by ctm.createPackage(); 
    149                 unusedZipEntries.put("[Content_Types].xml", new Boolean(false)); 
     150//              unusedZipEntries.put("[Content_Types].xml", new Boolean(false)); 
    150151                 
    151152                // 4. Start with _rels/.rels 
     
    161162                p.setRelationships(rp); 
    162163                //rp.setPackageRelationshipPart(true);           
    163                 unusedZipEntries.put(partName, new Boolean(false)); 
     164//              unusedZipEntries.put(partName, new Boolean(false)); 
    164165                 
    165166                 
     
    172173//              (ii) add the new Part to the package 
    173174//              (iii) cross the PartName off unusedZipEntries 
    174                 addPartsFromRelationships(zf, p, rp ); 
     175                addPartsFromRelationships(zf, p, rp, ctm ); 
    175176                 
    176177                 
    177178                // 6. Check unusedZipEntries is empty 
    178                 if (log.isDebugEnabled()) {              
    179                          Iterator myVeryOwnIterator = unusedZipEntries.keySet().iterator(); 
    180                          while(myVeryOwnIterator.hasNext()) { 
    181                              String key = (String)myVeryOwnIterator.next(); 
    182                              log.info( key + "  " + unusedZipEntries.get(key)); 
    183                          } 
    184                 }                 
     179//              if (log.isDebugEnabled()) {              
     180//                       Iterator myVeryOwnIterator = unusedZipEntries.keySet().iterator(); 
     181//                       while(myVeryOwnIterator.hasNext()) { 
     182//                           String key = (String)myVeryOwnIterator.next(); 
     183//                           log.info( key + "  " + unusedZipEntries.get(key)); 
     184//                       } 
     185//              }                 
    185186                 try { 
    186187                         zf.close(); 
     
    254255        (iii) cross the PartName off unusedZipEntries 
    255256        */ 
    256         private void addPartsFromRelationships(ZipFile zf, Base source, RelationshipsPart rp) 
     257        private void addPartsFromRelationships(ZipFile zf, Base source, RelationshipsPart rp, 
     258                        ContentTypeManager ctm) 
    257259                throws Docx4JException { 
    258260                 
     
    281283                                // a part, so start with a line break. 
    282284                        try {                            
    283                                 getPart(zf, pkg, rp, r); 
     285                                getPart(zf, pkg, rp, r, ctm); 
    284286                        } catch (Exception e) { 
    285287                                throw new Docx4JException("Failed to add parts from relationships", e); 
     
    306308//      private void getPart(ZipFile zf, Base source,  
    307309//                      Package pkg, String resolvedPartUri, String relationshipType) 
    308         private void getPart(ZipFile zf, OpcPackage pkg, RelationshipsPart rp, Relationship r) 
     310        private void getPart(ZipFile zf, OpcPackage pkg, RelationshipsPart rp, Relationship r, 
     311                        ContentTypeManager ctm) 
    309312                        throws Docx4JException, InvalidFormatException, URISyntaxException { 
    310313                 
     
    348351                } 
    349352                 
    350                 if (handled.get(resolvedPartUri)!=null) return; 
     353                if (pkg.handled.get(resolvedPartUri)!=null) return; 
    351354                 
    352355                String relationshipType = r.getType();           
     
    359362                }                
    360363                rp.loadPart(part, r); 
    361                 handled.put(resolvedPartUri, resolvedPartUri); 
     364                pkg.handled.put(resolvedPartUri, resolvedPartUri); 
    362365                 
    363366 
     
    369372                } 
    370373                 
    371                 unusedZipEntries.put(resolvedPartUri, new Boolean(false)); 
    372                 log.info(".. added." ); 
     374//              unusedZipEntries.put(resolvedPartUri, new Boolean(false)); 
     375//              log.info(".. added." ); 
    373376                 
    374377                RelationshipsPart rrp = getRelationshipsPart(zf, part); 
    375378                if (rrp!=null) { 
    376379                        // recurse via this parts relationships, if it has any 
    377                         addPartsFromRelationships(zf, part, rrp ); 
     380                        addPartsFromRelationships(zf, part, rrp, ctm ); 
    378381                        String relPart = PartName.getRelationshipsPartName( 
    379382                                        part.getPartName().getName().substring(1) ); 
    380                         unusedZipEntries.put(relPart, new Boolean(false));                                       
     383//                      unusedZipEntries.put(relPart, new Boolean(false));                                       
    381384                } 
    382385        } 
  • trunk/docx4j/src/main/java/org/docx4j/openpackaging/io/LoadFromZipNG.java

    r1421 r1467  
    8181public class LoadFromZipNG extends Load { 
    8282         
    83         public HashMap<String, ByteArray> partByteArrays = new HashMap<String, ByteArray>();     
     83        //public HashMap<String, ByteArray> partByteArrays = new HashMap<String, ByteArray>();   
    8484         
    8585        private static Logger log = Logger.getLogger(LoadFromZipNG.class); 
     
    9999         
    100100        public LoadFromZipNG() { 
    101                 this(new ContentTypeManager() ); 
    102         } 
    103  
    104         public LoadFromZipNG(ContentTypeManager ctm) { 
    105                 this.ctm = ctm; 
    106         } 
     101//              this(new ContentTypeManager() ); 
     102        } 
     103 
     104//      public LoadFromZipNG(ContentTypeManager ctm) { 
     105//              this.ctm = ctm; 
     106//      } 
    107107         
    108108         
     
    143143                } 
    144144                                 
     145                HashMap<String, ByteArray> partByteArrays = new HashMap<String, ByteArray>(); 
    145146                Enumeration entries = zf.entries(); 
    146147                while (entries.hasMoreElements()) { 
     
    163164                  
    164165                 
    165                 return process(); 
     166                return process(partByteArrays); 
    166167        } 
    167168 
    168169        public OpcPackage get(InputStream is) throws Docx4JException { 
    169170 
     171                HashMap<String, ByteArray> partByteArrays = new HashMap<String, ByteArray>();    
    170172       try { 
    171173            ZipInputStream zis = new ZipInputStream(is); 
     
    187189        // work the same way 
    188190                 
    189                 return process(); 
    190         } 
    191          
    192         private OpcPackage process() throws Docx4JException { 
     191                return process(partByteArrays); 
     192        } 
     193         
     194        private OpcPackage process(HashMap<String, ByteArray> partByteArrays) throws Docx4JException { 
    193195 
    194196                // 2. Create a new Package 
    195197                //              Eventually, you'll also be able to create an Excel package etc 
    196198                //              but only the WordML package exists at present 
     199                 
     200                ContentTypeManager ctm = new ContentTypeManager(); 
    197201 
    198202                try { 
     
    235239//              (ii) add the new Part to the package 
    236240//              (iii) cross the PartName off unusedZipEntries 
    237                 addPartsFromRelationships(partByteArrays, p, rp ); 
     241                addPartsFromRelationships(partByteArrays, p, rp, ctm ); 
    238242                 
    239243                 
     
    313317        //private void addPartsFromRelationships(ZipFile zf, Base source, RelationshipsPart rp) 
    314318        private void addPartsFromRelationships(HashMap<String, ByteArray> partByteArrays,  
    315                         Base source, RelationshipsPart rp) 
     319                        Base source, RelationshipsPart rp, ContentTypeManager ctm) 
    316320                throws Docx4JException { 
    317321                 
     
    342346                                // a part, so start with a line break. 
    343347                        try {                            
    344                                 getPart(partByteArrays, pkg, rp, r); 
     348                                getPart(partByteArrays, pkg, rp, r, ctm); 
    345349                        } catch (Exception e) { 
    346350                                throw new Docx4JException("Failed to add parts from relationships", e); 
     
    367371        //private void getPart(ZipFile zf, Package pkg, RelationshipsPart rp, Relationship r) 
    368372        private void getPart(HashMap<String, ByteArray> partByteArrays, OpcPackage pkg, RelationshipsPart rp,  
    369                         Relationship r) 
     373                        Relationship r, ContentTypeManager ctm) 
    370374                        throws Docx4JException, InvalidFormatException, URISyntaxException { 
    371375                 
     
    409413                } 
    410414                 
    411                 if (handled.get(resolvedPartUri)!=null) return; 
     415                if (pkg.handled.get(resolvedPartUri)!=null) return; 
    412416                 
    413417                String relationshipType = r.getType();           
     
    420424                } 
    421425                rp.loadPart(part, r); 
    422                 handled.put(resolvedPartUri, resolvedPartUri); 
     426                pkg.handled.put(resolvedPartUri, resolvedPartUri); 
    423427 
    424428                // The source Part (or Package) might have a convenience 
     
    434438                if (rrp!=null) { 
    435439                        // recurse via this parts relationships, if it has any 
    436                         addPartsFromRelationships(partByteArrays, part, rrp ); 
     440                        addPartsFromRelationships(partByteArrays, part, rrp, ctm ); 
    437441                        String relPart = PartName.getRelationshipsPartName( 
    438442                                        part.getPartName().getName().substring(1) ); 
  • trunk/docx4j/src/main/java/org/docx4j/openpackaging/packages/OpcPackage.java

    r1307 r1467  
    6363 
    6464        /** 
     65         * This HashMap is intended to prevent loops during the loading  
     66         * of this package. TODO This doesn't really tell us anything that 
     67         * the contents of Parts couldn't also tell us (except that 
     68         * that doesn't contain the rels parts), so consider removing. 
     69         * At least replace it with a method, so this implementation 
     70         * detail is hidden! 
     71         */ 
     72        public HashMap<String, String> handled = new HashMap<String, String>(); 
     73         
     74        /** 
    6575         * Package parts collection.  This is a collection of _all_ 
    6676         * parts in the package (_except_ relationship parts),  
Note: See TracChangeset for help on using the changeset viewer.