Ignore:
Timestamp:
05/29/11 11:33:42 (12 months ago)
Author:
jharrop
Message:

Fix to algorithm for id generation, to better satisfy what appears to be Word's buggy GUID equality test, which becomes evident if there are more than about 100 items. Symptom is that Word won't open the docx.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/docx4j/src/main/java/org/docx4j/openpackaging/parts/DrawingML/DiagramDataPart.java

    r1511 r1514  
    2424import java.util.HashMap; 
    2525import java.util.List; 
     26import java.util.UUID; 
    2627 
    2728import org.apache.log4j.Logger; 
     
    7677         */ 
    7778        protected static void setFriendlyIds(Object jaxbElement) { 
    78                  
     79                                 
    7980                map = new HashMap<String, String>(); 
    8081                 
     
    109110                                                        String from = pt.getModelId(); 
    110111                                                         
    111                                                         // Everything goes invisible in Word 2007 if you map these, 
     112                                                        // Everything goes invisible in Word 2007 if you map these 
     113                                                        // to an int (rather than a guid), 
    112114                                                        // and images disappear (not shown as broken) 
    113115                                                        if (pt.getType()!=null 
     
    118120                                                        } 
    119121                                                         
     122//                                                      String to = generateNiceGuid(index); 
    120123                                                        String to = "" + index; 
    121124                                                        index++;                                                                 
     
    127130                                                        CTCxn cxn = (CTCxn)o; 
    128131                                                        String from = cxn.getModelId(); 
     132//                                                      String to = generateNiceGuid(index); 
    129133                                                        String to = "" + index; 
    130134                                                        index++;                                                                 
     
    179183        private static String generateNiceGuid(int index) { 
    180184                 
     185                /* It seems that if there are more than about 100 
     186                 * items, Word won't open a docx with friendly ids 
     187                 * of the following form: 
     188                 *  
     189                        if (index<10) { 
     190                                return "{00000000-0000-0000-0000-00000000000" + index + "}"; 
     191                        } else if (index<100) { 
     192                                return "{00000000-0000-0000-0000-0000000000" + index + "}";                      
     193                        } else { 
     194                                return "{00000000-0000-0000-0000-000000000" + index + "}";                       
     195                        } 
     196 
     197                 * Speculate that this is because M$ internally 
     198                 * uses some broken equality test. 
     199                 *  
     200                 * The following works better ... 
     201                 */ 
     202                 
     203 
    181204                if (index<10) { 
    182                         return "{00000000-0000-0000-0000-00000000000" + index + "}"; 
     205                        return "{0000000" + index + "-0000-0000-0000-000000000000}"; 
    183206                } else if (index<100) { 
    184                         return "{00000000-0000-0000-0000-0000000000" + index + "}";                      
     207                        return "{000000" +  index + "-0000-0000-0000-000000000000}";                     
    185208                } else { 
    186                         return "{00000000-0000-0000-0000-000000000" + index + "}";                       
     209                        return "{00000" +   index + "-0000-0000-0000-000000000000}";                     
    187210                } 
     211                 
     212                // This works as well... 
     213//              return "{" + UUID.randomUUID().toString().toUpperCase() + "}"; 
     214                  
    188215                 
    189216        } 
     
    213240                                        @Override 
    214241                                        public List<Object> apply(Object o) { 
    215                                                  
     242                                                                                                 
    216243                                                if (o instanceof CTPt) { 
    217244                                                         
     
    219246                                                                                                                 
    220247                                                        pt.setModelId(  
    221                                                                         map.get(pt.getModelId() )); 
     248                                                                        mapGet(map, pt.getModelId() )); 
    222249                                                         
    223250                                                        if (pt.getPrSet()!=null) { 
     
    225252                                                                if (pr.getPresAssocID()!=null) { 
    226253                                                                        pr.setPresAssocID( 
    227                                                                                         map.get(pr.getPresAssocID() )); 
     254                                                                                        mapGet(map, pr.getPresAssocID() )); 
    228255                                                                } 
    229256                                                        } 
     
    231258                                                        if (!pt.getCxnId().equals("0")) { 
    232259                                                                pt.setCxnId(  
    233                                                                                 map.get(pt.getCxnId() )); 
     260                                                                                mapGet(map, pt.getCxnId() )); 
    234261                                                        }                                                        
    235262                                                } 
     
    239266                                                        CTCxn cxn = (CTCxn)o; 
    240267                                                        if (cxn.getModelId()!=null) { 
    241                                                                 cxn.setModelId(map.get(cxn.getModelId())); 
    242                                                         } 
    243                                                          
    244                                                         cxn.setSrcId(map.get(cxn.getSrcId())); 
     268                                                                cxn.setModelId(mapGet(map,cxn.getModelId())); 
     269                                                        } 
     270                                                         
     271                                                        cxn.setSrcId(mapGet(map, cxn.getSrcId())); 
    245272                                                        cxn.setDestId( mapGet(map,cxn.getDestId() )); 
    246273 
    247274                                                        if (!cxn.getSibTransId().equals("0")) { 
    248275                                                                cxn.setSibTransId(  
    249                                                                                 map.get(cxn.getSibTransId() )); 
     276                                                                                mapGet(map, cxn.getSibTransId() )); 
    250277                                                        } 
    251278                                                        if (!cxn.getParTransId().equals("0")) { 
    252279                                                                cxn.setParTransId(  
    253                                                                                 map.get(cxn.getParTransId() )); 
     280                                                                                mapGet(map, cxn.getParTransId() )); 
    254281                                                        } 
    255282//                                                      if (cxn.getPresId()!=null) { 
     
    309336                                .load(new java.io.File( 
    310337                                                System.getProperty("user.dir") 
    311                                                 + "/SmartArt/root-only.docx")); 
     338                                                + "/SmartArt/complex-nopic.docx")); 
    312339                 
    313340                Relationship r = wordMLPackage.getMainDocumentPart().getRelationshipsPart().getRelationshipByType(Namespaces.DRAWINGML_DIAGRAM_DATA); 
     
    337364                SaveToZipFile saver = new SaveToZipFile(wordMLPackage); 
    338365                saver.save(System.getProperty("user.dir") 
    339                                 + "/SmartArt/root-only-OUT.docx"); 
     366                                + "/SmartArt/complex-nopic-OUT.docx"); 
    340367                 
    341368        }        
Note: See TracChangeset for help on using the changeset viewer.