Changeset 1499 for trunk/docx4j/src/main/java/org/docx4j/openpackaging
- Timestamp:
- 05/20/11 01:39:06 (12 months ago)
- Location:
- trunk/docx4j/src/main/java/org/docx4j/openpackaging/parts
- Files:
-
- 3 edited
-
DrawingML/DiagramDataPart.java (modified) (2 diffs)
-
DrawingML/DiagramDrawingPart.java (modified) (3 diffs)
-
relationships/Namespaces.java (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
trunk/docx4j/src/main/java/org/docx4j/openpackaging/parts/DrawingML/DiagramDataPart.java
r1201 r1499 22 22 23 23 24 import java.util.HashMap; 25 import java.util.List; 26 24 27 import org.apache.log4j.Logger; 28 import org.docx4j.TraversalUtil; 29 import org.docx4j.XmlUtils; 30 import org.docx4j.TraversalUtil.Callback; 31 import org.docx4j.dml.diagram.CTCxn; 25 32 import org.docx4j.dml.diagram.CTDataModel; 33 import org.docx4j.dml.diagram.CTElemPropSet; 34 import org.docx4j.dml.diagram.CTPt; 35 import org.docx4j.dml.diagram.STCxnType; 36 import org.docx4j.dml.diagram.STPtType; 26 37 import org.docx4j.openpackaging.exceptions.InvalidFormatException; 38 import org.docx4j.openpackaging.io.SaveToZipFile; 39 import org.docx4j.openpackaging.packages.WordprocessingMLPackage; 27 40 import org.docx4j.openpackaging.parts.JaxbXmlPart; 28 41 import org.docx4j.openpackaging.parts.PartName; 29 42 import org.docx4j.openpackaging.parts.relationships.Namespaces; 43 import org.docx4j.relationships.Relationship; 30 44 31 45 … … 55 69 } 56 70 71 72 /** 73 * This method changes the IDs from GUID to ints, 74 * so that the contents of the part is easier to understand. 75 * 76 * That's all its for. Use at your own risk. 77 */ 78 @Deprecated 79 public void setFriendlyIds() { 80 81 // Go through once creating a map of IDs 82 generateIdMap(); 83 84 // Go through again, replacing 85 ReplaceIds(); 86 87 } 88 89 HashMap<String, String> map = new HashMap<String, String>(); 90 int index = 1; 91 private void generateIdMap() { 92 93 new TraversalUtil(getJaxbElement(), 94 95 new Callback() { 96 97 // Unfortunately, the schema says 98 // a model id must be an int or a GUID. 99 // Word 2007 won't open a docx which violates that. 100 final static boolean IGNORE_XSD_RESTRICTION = false; 101 102 @Override 103 public List<Object> apply(Object o) { 104 105 if (o instanceof CTPt) { 106 107 CTPt pt = (CTPt)o; 108 String from = pt.getModelId(); 109 String to = null; 110 111 if (IGNORE_XSD_RESTRICTION) { 112 113 if (pt.getType().equals(STPtType.DOC)) { 114 to = "doc"; 115 } else if (pt.getType().equals(STPtType.PAR_TRANS)) { 116 to="parTrans"+index; 117 index++; 118 } else if (pt.getType().equals(STPtType.SIB_TRANS)) { 119 to="sibTrans"+index; 120 index++; 121 } else if (pt.getType().equals(STPtType.PRES)) { 122 to="pres"+index; 123 index++; 124 } else { 125 to="pt"+index; 126 index++; 127 } 128 129 } else { 130 to = "" + index; 131 index++; 132 } 133 map.put(from, to); 134 } 135 136 if (o instanceof CTCxn) { 137 138 CTCxn cxn = (CTCxn)o; 139 String from = cxn.getModelId(); 140 String to = null; 141 142 if (IGNORE_XSD_RESTRICTION) { 143 144 if (cxn.getType().equals(STCxnType.PRES_OF)) { 145 to="presOf"+index; 146 index++; 147 } else if (cxn.getType().equals(STCxnType.PAR_OF)) { 148 to="parOf"+index; 149 index++; 150 } else if (cxn.getType().equals(STCxnType.PRES_PAR_OF)) { 151 to="presParOf"+index; 152 index++; 153 } else { 154 to="cxn"+index; 155 index++; 156 } 157 158 } else { 159 to = "" + index; 160 index++; 161 } 162 163 map.put(from, to); 164 } 165 166 return null; 167 } 168 169 @Override 170 public boolean shouldTraverse(Object o) { 171 return true; 172 } 173 174 // Depth first 175 @Override 176 public void walkJAXBElements(Object parent) { 177 178 179 List children = getChildren(parent); 180 if (children != null) { 181 182 for (Object o : children) { 183 184 // if its wrapped in javax.xml.bind.JAXBElement, get its 185 // value 186 o = XmlUtils.unwrap(o); 187 188 this.apply(o); 189 190 if (this.shouldTraverse(o)) { 191 walkJAXBElements(o); 192 } 193 194 } 195 } 196 197 } 198 199 @Override 200 public List<Object> getChildren(Object o) { 201 return TraversalUtil.getChildrenImpl(o); 202 } 203 204 } 205 206 ); 207 208 209 } 210 211 private void ReplaceIds() { 212 213 new TraversalUtil(getJaxbElement(), 214 215 new Callback() { 216 217 @Override 218 public List<Object> apply(Object o) { 219 220 if (o instanceof CTPt) { 221 222 CTPt pt = (CTPt)o; 223 pt.setModelId( 224 map.get(pt.getModelId() )); 225 226 if (pt.getPrSet()!=null) { 227 CTElemPropSet pr = (CTElemPropSet)pt.getPrSet(); 228 if (pr.getPresAssocID()!=null) { 229 pr.setPresAssocID( 230 map.get(pr.getPresAssocID() )); 231 } 232 } 233 234 if (pt.getCxnId()!=null) { 235 pt.setCxnId( 236 map.get(pt.getCxnId() )); 237 } 238 } 239 240 if (o instanceof CTCxn) { 241 242 CTCxn cxn = (CTCxn)o; 243 cxn.setModelId(map.get(cxn.getModelId())); 244 245 cxn.setSrcId(map.get(cxn.getSrcId())); 246 cxn.setDestId(map.get(cxn.getDestId())); 247 248 if (cxn.getSibTransId()!=null) { 249 cxn.setSibTransId( 250 map.get(cxn.getSibTransId() )); 251 } 252 if (cxn.getParTransId()!=null) { 253 cxn.setParTransId( 254 map.get(cxn.getParTransId() )); 255 } 256 if (cxn.getPresId()!=null) { 257 cxn.setPresId( 258 map.get(cxn.getPresId() )); 259 } 260 } 261 262 return null; 263 } 264 265 @Override 266 public boolean shouldTraverse(Object o) { 267 return true; 268 } 269 270 // Depth first 271 @Override 272 public void walkJAXBElements(Object parent) { 273 274 275 List children = getChildren(parent); 276 if (children != null) { 277 278 for (Object o : children) { 279 280 // if its wrapped in javax.xml.bind.JAXBElement, get its 281 // value 282 o = XmlUtils.unwrap(o); 283 284 this.apply(o); 285 286 if (this.shouldTraverse(o)) { 287 walkJAXBElements(o); 288 } 289 290 } 291 } 292 293 } 294 295 @Override 296 public List<Object> getChildren(Object o) { 297 return TraversalUtil.getChildrenImpl(o); 298 } 299 300 } 301 302 ); 303 304 305 } 306 307 public static void main(String[] args) throws Exception { 308 309 WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage 310 .load(new java.io.File( 311 System.getProperty("user.dir") 312 + "/glox5.docx")); 313 314 Relationship r = wordMLPackage.getMainDocumentPart().getRelationshipsPart().getRelationshipByType(Namespaces.DRAWINGML_DIAGRAM_DATA); 315 316 if (r==null) { 317 System.out.println("No DDP!"); 318 return; 319 } 320 321 DiagramDataPart thisPart = (DiagramDataPart)wordMLPackage.getMainDocumentPart().getRelationshipsPart().getPart(r); 322 323 thisPart.setFriendlyIds(); 324 325 System.out.println( XmlUtils.marshaltoString(thisPart.getJaxbElement(), true, true)); 326 327 // Now fix the IDs in the drawing part to match 328 Relationship r2 = wordMLPackage.getMainDocumentPart().getRelationshipsPart().getRelationshipByType(Namespaces.DRAWINGML_DIAGRAM_DRAWING); 329 if (r2==null) { 330 System.out.println("No DDrawingP!"); 331 return; 332 } 333 334 DiagramDrawingPart drawingPart = (DiagramDrawingPart)wordMLPackage.getMainDocumentPart().getRelationshipsPart().getPart(r2); 335 drawingPart.setFriendlyIds(thisPart.map); 336 337 System.out.println( XmlUtils.marshaltoString(drawingPart.getJaxbElement(), true, true)); 338 339 SaveToZipFile saver = new SaveToZipFile(wordMLPackage); 340 saver.save(System.getProperty("user.dir") 341 + "/glox5-OUT.docx"); 342 343 } 344 57 345 } -
trunk/docx4j/src/main/java/org/docx4j/openpackaging/parts/DrawingML/DiagramDrawingPart.java
r1498 r1499 22 22 23 23 24 import java.util.HashMap; 25 import java.util.List; 26 24 27 import org.apache.log4j.Logger; 28 import org.docx4j.TraversalUtil; 29 import org.docx4j.XmlUtils; 30 import org.docx4j.TraversalUtil.Callback; 31 import org.docx4j.dml.diagram.CTCxn; 25 32 import org.docx4j.dml.diagram.CTDataModel; 33 import org.docx4j.dml.diagram.CTElemPropSet; 34 import org.docx4j.dml.diagram.CTPt; 35 import org.docx4j.dml.diagram2008.CTShape; 26 36 import org.docx4j.openpackaging.exceptions.InvalidFormatException; 27 37 import org.docx4j.openpackaging.parts.JaxbXmlPart; … … 31 41 32 42 /** 43 * The last successful layout for a diagram". 44 * See MS-ODRAWXML. 45 * 33 46 * When present, will be a rel of document.xml, 34 47 * and pointed to from dgm:dataModel 35 * 48 * 49 * In MS-ODRAWXML, this part is called "Diagram 50 * Layout Part". Is that a typo, or just 51 * confusing? I'm not gonna call it that. 52 * 53 * At http://msdn.microsoft.com/en-us/library/documentformat.openxml.office.drawing.drawing.aspx 54 * it seems to be called DiagramPersistLayoutPart, 55 * which would be better. 36 56 */ 37 57 public final class DiagramDrawingPart extends JaxbDmlPart<CTDataModel> { 58 59 /* From http://msdn.microsoft.com/en-us/library/dd909877(v=office.12).aspx 60 * 61 * The last successful layout for a diagram is stored in documents as an extension 62 * by using a Diagram Layout part. The part is referenced by the relationship id 63 * attribute of a DataModelExt extension to the Data Model. For more information, 64 * see [ISO/IEC-29500-1] section 21.4.2.10. The content of the part contains XML 65 * as defined by Diagram Layout (section 2.1.1). 66 * 67 * The DataModelExt contains a version URI that represents the minimum version 68 * required to run the layout. If an application version is insufficient to perform 69 * layout, the Diagram Layout can be used to display the diagram. 70 * 71 * See also http://msdn.microsoft.com/en-us/library/dd910819(v=office.12).aspx 72 * 73 */ 38 74 39 75 /* … … 69 105 } 70 106 107 108 @Deprecated 109 public void setFriendlyIds(final HashMap<String, String> map) { 110 111 new TraversalUtil(getJaxbElement(), 112 113 new Callback() { 114 115 @Override 116 public List<Object> apply(Object o) { 117 118 if (o instanceof org.docx4j.dml.diagram2008.CTShape) { 119 120 CTShape sp = (CTShape)o; 121 if (sp.getModelId()!=null) { 122 sp.setModelId( 123 map.get(sp.getModelId() )); 124 } 125 } 126 127 return null; 128 } 129 130 @Override 131 public boolean shouldTraverse(Object o) { 132 return true; 133 } 134 135 // Depth first 136 @Override 137 public void walkJAXBElements(Object parent) { 138 139 140 List children = getChildren(parent); 141 if (children != null) { 142 143 for (Object o : children) { 144 145 // if its wrapped in javax.xml.bind.JAXBElement, get its 146 // value 147 o = XmlUtils.unwrap(o); 148 149 this.apply(o); 150 151 if (this.shouldTraverse(o)) { 152 walkJAXBElements(o); 153 } 154 155 } 156 } 157 158 } 159 160 @Override 161 public List<Object> getChildren(Object o) { 162 return TraversalUtil.getChildrenImpl(o); 163 } 164 165 } 166 167 ); 168 } 169 71 170 } -
trunk/docx4j/src/main/java/org/docx4j/openpackaging/parts/relationships/Namespaces.java
r1498 r1499 153 153 // "/word/diagrams/drawing1.xml" 154 154 public final static String DRAWINGML_DIAGRAM_DRAWING = 155 "http://schemas. openxmlformats.org/officeDocument/2007/relationships/diagramDrawing";155 "http://schemas.microsoft.com/office/2007/relationships/diagramDrawing"; 156 156 157 157
Note: See TracChangeset
for help on using the changeset viewer.
