Changeset 532
- Timestamp:
- 08/22/08 03:02:48 (4 years ago)
- Location:
- trunk/docx4j/src/main/java/org/docx4j
- Files:
-
- 5 added
- 2 deleted
- 11 edited
-
convert/in/XmlPackage.java (modified) (7 diffs)
-
convert/out/xmlPackage/XmlPackage.java (modified) (5 diffs)
-
customxml/ObjectFactory.java (modified) (1 diff)
-
customxml/SchemaLibrary.java (modified) (1 diff)
-
customxml/package-info.java (modified) (1 diff)
-
jaxb/Context.java (modified) (2 diffs)
-
openpackaging/io/LoadFromJCR.java (modified) (7 diffs)
-
openpackaging/io/LoadFromZipFile.java (modified) (8 diffs)
-
openpackaging/io/SaveToJCR.java (modified) (7 diffs)
-
openpackaging/io/SaveToZipFile.java (modified) (5 diffs)
-
openpackaging/parts/relationships/Relationship.java (deleted)
-
openpackaging/parts/relationships/RelationshipsPart.java (modified) (19 diffs)
-
openpackaging/parts/relationships/TargetMode.java (deleted)
-
relationships (added)
-
relationships/ObjectFactory.java (added)
-
relationships/Relationship.java (added)
-
relationships/Relationships.java (added)
-
relationships/package-info.java (added)
Legend:
- Unmodified
- Added
- Removed
-
trunk/docx4j/src/main/java/org/docx4j/convert/in/XmlPackage.java
r511 r532 23 23 24 24 25 import java.net.URI; 25 26 import java.net.URISyntaxException; 26 27 import java.util.HashMap; … … 47 48 import org.docx4j.openpackaging.parts.PartName; 48 49 import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPart; 49 import org.docx4j.openpackaging.parts.relationships.Relationship;50 50 import org.docx4j.openpackaging.parts.relationships.RelationshipsPart; 51 import org.docx4j.openpackaging.parts.relationships.TargetMode; 51 import org.docx4j.relationships.Relationships; 52 import org.docx4j.relationships.Relationship; 52 53 53 54 … … 165 166 org.w3c.dom.Element el = part.getXmlData().getAny(); 166 167 167 // Convert it to a Dom4J element 168 169 thePart = new RelationshipsPart( p, new PartName( partName), convertW3CtoDom4J(el) ); 170 // PartName already starts with a '/', so no need to add it 168 RelationshipsPart rp = new RelationshipsPart(new PartName(partName) ); 169 // PartName already starts with a '/', so no need to add it 170 rp.setSourceP(p); 171 rp.unmarshal(el); 172 173 // // Convert it to a Dom4J element 174 // thePart = new RelationshipsPart( p, new PartName( partName), convertW3CtoDom4J(el) ); 175 // // PartName already starts with a '/', so no need to add it 171 176 172 177 } catch (Exception e) { … … 209 214 Package pkg = source.getPackage(); 210 215 211 for (Iterator it = rp.iterator(); it.hasNext(); ) { 212 Relationship r = (Relationship)it.next(); 213 log.info("For Relationship Id=" + r.getId() + " Source is " 214 + r.getSource().getPartName() 215 + ", Target is " + r.getTargetURI() ); 216 try { 217 216 // for (Iterator it = rp.iterator(); it.hasNext(); ) { 217 // Relationship r = (Relationship)it.next(); 218 // log.info("For Relationship Id=" + r.getId() + " Source is " 219 // + r.getSource().getPartName() 220 // + ", Target is " + r.getTargetURI() ); 221 // try { 222 // 223 // getPart(pkg, rp, r); 224 // 225 // } catch (Exception e) { 226 // throw new Docx4JException("Failed to add parts from relationships", e); 227 // } 228 // } 229 230 for ( Relationship r : rp.getRelationships().getRelationship() ) { 231 232 log.info("For Relationship Id=" + r.getId() 233 + " Source is " + rp.getSourceP().getPartName() 234 + ", Target is " + r.getTarget() ); 235 try { 218 236 getPart(pkg, rp, r); 219 220 237 } catch (Exception e) { 221 238 throw new Docx4JException("Failed to add parts from relationships", e); 222 239 } 223 240 } 241 224 242 225 243 … … 241 259 */ 242 260 private void getPart( Package pkg, RelationshipsPart rp, Relationship r) 243 throws Docx4JException, InvalidFormatException {261 throws Docx4JException, InvalidFormatException, URISyntaxException { 244 262 245 263 Base source = null; 246 264 String resolvedPartUri = null; 247 265 248 if (r.getTargetMode().equals(TargetMode.INTERNAL) ) { 266 if (r.getTargetMode() == null 267 || !r.getTargetMode().equals("External") ) { 249 268 250 269 // Usual case 251 270 252 source = r.getSource(); 253 resolvedPartUri = URIHelper.resolvePartUri(r.getSourceURI(), r.getTargetURI() ).toString(); 271 // source = r.getSource(); 272 // resolvedPartUri = URIHelper.resolvePartUri(r.getSourceURI(), r.getTargetURI() ).toString(); 273 source = rp.getSourceP(); 274 resolvedPartUri = URIHelper.resolvePartUri(rp.getSourceURI(), new URI(r.getTarget() ) ).toString(); 254 275 255 276 // Don't drop leading "/' in Xml Package … … 265 286 */ 266 287 267 log.warn("Encountered external resource " + r.getTarget URI()268 + " of type " + r.get RelationshipType() );288 log.warn("Encountered external resource " + r.getTarget() 289 + " of type " + r.getType() ); 269 290 270 291 // As of 1 May 2008, we don't do anything with these yet. … … 279 300 } 280 301 281 String relationshipType = r.get RelationshipType();302 String relationshipType = r.getType(); 282 303 283 304 Part part = getRawPart(ctm, resolvedPartUri); -
trunk/docx4j/src/main/java/org/docx4j/convert/out/xmlPackage/XmlPackage.java
r511 r532 22 22 23 23 import java.io.IOException; 24 import java.net.URI; 24 25 import java.util.Iterator; 25 26 … … 34 35 import org.docx4j.openpackaging.parts.PartName; 35 36 import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPart; 36 import org.docx4j.openpackaging.parts.relationships.Relationship;37 37 import org.docx4j.openpackaging.parts.relationships.RelationshipsPart; 38 import org.docx4j.openpackaging.parts.relationships.TargetMode; 38 import org.docx4j.relationships.Relationships; 39 import org.docx4j.relationships.Relationship; 39 40 40 41 /** … … 207 208 throws Docx4JException { 208 209 209 for (Iterator it = rp.iterator(); it.hasNext(); ) { 210 Relationship r = (Relationship)it.next(); 211 log.info("For Relationship Id=" + r.getId() + " Source is " + r.getSource().getPartName() + ", Target is " + r.getTargetURI() ); 212 213 if (!r.getTargetMode().equals(TargetMode.INTERNAL) ) { 210 // for (Iterator it = rp.iterator(); it.hasNext(); ) { 211 // Relationship r = (Relationship)it.next(); 212 // log.info("For Relationship Id=" + r.getId() + " Source is " + r.getSource().getPartName() + ", Target is " + r.getTargetURI() ); 213 214 for ( Relationship r : rp.getRelationships().getRelationship() ) { 215 216 log.info("For Relationship Id=" + r.getId() 217 + " Source is " + rp.getSourceP().getPartName() 218 + ", Target is " + r.getTarget() ); 219 220 // if (!r.getTargetMode().equals(TargetMode.INTERNAL) ) { 221 if (r.getTargetMode() != null 222 && r.getTargetMode().equals("External") ) { 214 223 215 224 // ie its EXTERNAL … … 217 226 // there is just the relationship. 218 227 219 log.warn("Encountered external resource " + r.getTarget URI()220 + " of type " + r.get RelationshipType() );228 log.warn("Encountered external resource " + r.getTarget() 229 + " of type " + r.getType() ); 221 230 222 231 // So … … 225 234 226 235 try { 227 String resolvedPartUri = URIHelper.resolvePartUri(r.getSourceURI(), r.getTargetURI() ).toString(); 228 236 //String resolvedPartUri = URIHelper.resolvePartUri(r.getSourceURI(), r.getTargetURI() ).toString(); 237 238 String resolvedPartUri = URIHelper.resolvePartUri(rp.getSourceURI(), new URI(r.getTarget() ) ).toString(); 239 229 240 // Now drop leading "/' 230 241 resolvedPartUri = resolvedPartUri.substring(1); -
trunk/docx4j/src/main/java/org/docx4j/customxml/ObjectFactory.java
r517 r532 1 /* 2 * Copyright 2007-2008, Plutext Pty Ltd. 3 * 4 * This file is part of docx4j. 5 6 docx4j is licensed under the Apache License, Version 2.0 (the "License"); 7 you may not use this file except in compliance with the License. 8 9 You may obtain a copy of the License at 10 11 http://www.apache.org/licenses/LICENSE-2.0 12 13 Unless required by applicable law or agreed to in writing, software 14 distributed under the License is distributed on an "AS IS" BASIS, 15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 See the License for the specific language governing permissions and 17 limitations under the License. 18 19 */ 20 1 21 2 22 package org.docx4j.customxml; -
trunk/docx4j/src/main/java/org/docx4j/customxml/SchemaLibrary.java
r517 r532 1 /* 2 * Copyright 2007-2008, Plutext Pty Ltd. 3 * 4 * This file is part of docx4j. 5 6 docx4j is licensed under the Apache License, Version 2.0 (the "License"); 7 you may not use this file except in compliance with the License. 8 9 You may obtain a copy of the License at 10 11 http://www.apache.org/licenses/LICENSE-2.0 12 13 Unless required by applicable law or agreed to in writing, software 14 distributed under the License is distributed on an "AS IS" BASIS, 15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 See the License for the specific language governing permissions and 17 limitations under the License. 18 19 */ 20 1 21 2 22 package org.docx4j.customxml; -
trunk/docx4j/src/main/java/org/docx4j/customxml/package-info.java
r517 r532 1 /* 2 * Copyright 2007-2008, Plutext Pty Ltd. 3 * 4 * This file is part of docx4j. 5 6 docx4j is licensed under the Apache License, Version 2.0 (the "License"); 7 you may not use this file except in compliance with the License. 8 9 You may obtain a copy of the License at 10 11 http://www.apache.org/licenses/LICENSE-2.0 12 13 Unless required by applicable law or agreed to in writing, software 14 distributed under the License is distributed on an "AS IS" BASIS, 15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 See the License for the specific language governing permissions and 17 limitations under the License. 18 19 */ 20 1 21 @javax.xml.bind.annotation.XmlSchema(namespace = "http://schemas.openxmlformats.org/schemaLibrary/2006/main", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED) 2 22 package org.docx4j.customxml; -
trunk/docx4j/src/main/java/org/docx4j/jaxb/Context.java
r511 r532 31 31 public static JAXBContext jcDocPropsExtended; 32 32 public static JAXBContext jcXmlPackage; 33 public static JAXBContext jcRelationships; 33 34 34 35 static { … … 41 42 jcDocPropsExtended = JAXBContext.newInstance("org.docx4j.docProps.extended"); 42 43 jcXmlPackage = JAXBContext.newInstance("org.docx4j.xmlPackage"); 44 jcRelationships = JAXBContext.newInstance("org.docx4j.relationships"); 43 45 } catch (Exception ex) { 44 46 ex.printStackTrace(); -
trunk/docx4j/src/main/java/org/docx4j/openpackaging/io/LoadFromJCR.java
r350 r532 57 57 import org.docx4j.openpackaging.parts.PartName; 58 58 import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPart; 59 import org.docx4j.openpackaging.parts.relationships.Relationship;60 59 import org.docx4j.openpackaging.parts.relationships.RelationshipsPart; 61 import org.docx4j.openpackaging.parts.relationships.TargetMode; 60 import org.docx4j.relationships.Relationships; 61 import org.docx4j.relationships.Relationship; 62 62 63 63 import org.docx4j.openpackaging.exceptions.Docx4JException; … … 247 247 248 248 249 return new RelationshipsPart(p, new PartName("/" + partName), is ); 249 //return new RelationshipsPart(p, new PartName("/" + partName), is ); 250 251 RelationshipsPart rp = new RelationshipsPart(new PartName("/" + partName) ); 252 rp.setSourceP(p); 253 rp.unmarshal(is); 254 return rp; 255 250 256 251 257 } catch (Exception e) { … … 412 418 Package pkg = source.getPackage(); 413 419 414 for (Iterator it = rp.iterator(); it.hasNext(); ) { 415 Relationship r = (Relationship)it.next(); 420 // for (Iterator it = rp.iterator(); it.hasNext(); ) { 421 // Relationship r = (Relationship)it.next(); 422 // log.info("For Relationship Id=" + r.getId() 423 // + " Source is " + r.getSource().getPartName() 424 // + ", Target is " + r.getTargetURI() ); 425 // try { 426 // getPart(jcrSession, docxNode, pkg, rp, r); 427 // } catch (Exception e) { 428 // throw new Docx4JException("Failed to add parts from relationships", e); 429 // } 430 // } 431 432 433 for ( Relationship r : rp.getRelationships().getRelationship() ) { 434 416 435 log.info("For Relationship Id=" + r.getId() 417 + " Source is " + r .getSource().getPartName()418 + ", Target is " + r.getTarget URI() );436 + " Source is " + rp.getSourceP().getPartName() 437 + ", Target is " + r.getTarget() ); 419 438 try { 420 439 getPart(jcrSession, docxNode, pkg, rp, r); … … 423 442 } 424 443 } 425 426 444 427 445 } … … 444 462 public void getPart(Session jcrSession, Node docxNode, 445 463 Package pkg, RelationshipsPart rp, Relationship r) 446 throws Docx4JException, RepositoryException, InvalidFormatException {464 throws Docx4JException, RepositoryException, InvalidFormatException, URISyntaxException { 447 465 448 466 Base source = null; 449 467 String resolvedPartUri = null; 450 468 451 if (r.getTargetMode().equals(TargetMode.INTERNAL) ) { 469 if (r.getTargetMode() == null 470 || !r.getTargetMode().equals("External") ) { 452 471 453 472 // Usual case 454 473 455 source = r .getSource();456 resolvedPartUri = URIHelper.resolvePartUri(r .getSourceURI(), r.getTargetURI() ).toString();474 source = rp.getSourceP(); 475 resolvedPartUri = URIHelper.resolvePartUri(rp.getSourceURI(), new URI(r.getTarget() ) ).toString(); 457 476 458 477 // Now drop leading "/' … … 472 491 */ 473 492 474 log.warn("Encountered external resource " + r.getTarget URI()475 + " of type " + r.get RelationshipType() );493 log.warn("Encountered external resource " + r.getTarget() 494 + " of type " + r.getType() ); 476 495 477 496 // As of 1 May 2008, we don't do anything with these yet. … … 486 505 } 487 506 488 String relationshipType = r.get RelationshipType();507 String relationshipType = r.getType(); 489 508 490 509 Part part = getRawPart(jcrSession, nodeMapper, docxNode, ctm, resolvedPartUri); -
trunk/docx4j/src/main/java/org/docx4j/openpackaging/io/LoadFromZipFile.java
r350 r532 26 26 import java.io.IOException; 27 27 import java.io.InputStream; 28 import java.net.URI; 28 29 import java.net.URISyntaxException; 29 30 import java.util.Enumeration; … … 46 47 import org.docx4j.openpackaging.parts.PartName; 47 48 import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPart; 48 import org.docx4j.openpackaging.parts.relationships.Relationship;49 49 import org.docx4j.openpackaging.parts.relationships.RelationshipsPart; 50 import org.docx4j.openpackaging.parts.relationships.TargetMode; 50 import org.docx4j.relationships.Relationships; 51 import org.docx4j.relationships.Relationship; 51 52 52 53 import org.dom4j.Document; … … 197 198 // return new RelationshipsPart( p, new PartName("/" + partName), contents ); 198 199 199 RelationshipsPart thePart= null;200 RelationshipsPart rp = null; 200 201 201 202 InputStream is = null; 202 203 try { 203 204 is = getInputStreamFromZippedPart( zf, partName); 204 thePart = new RelationshipsPart( p, new PartName("/" + partName), is ); 205 //thePart = new RelationshipsPart( p, new PartName("/" + partName), is ); 206 rp = new RelationshipsPart(new PartName("/" + partName) ); 207 rp.setSourceP(p); 208 rp.unmarshal(is); 205 209 206 210 } catch (Exception e) { … … 218 222 } 219 223 220 return thePart;224 return rp; 221 225 // debugPrint(contents); 222 226 // TODO - why don't any of the part names in this document start with "/"? … … 270 274 Package pkg = source.getPackage(); 271 275 272 for (Iterator it = rp.iterator(); it.hasNext(); ) { 273 Relationship r = (Relationship)it.next(); 274 log.info("For Relationship Id=" + r.getId() + " Source is " 275 + r.getSource().getPartName() 276 + ", Target is " + r.getTargetURI() ); 277 try { 278 276 // for (Iterator it = rp.iterator(); it.hasNext(); ) { 277 // Relationship r = (Relationship)it.next(); 278 // log.info("For Relationship Id=" + r.getId() + " Source is " 279 // + r.getSource().getPartName() 280 // + ", Target is " + r.getTargetURI() ); 281 // try { 282 // 283 // getPart(zf, pkg, rp, r); 284 // 285 // } catch (Exception e) { 286 // throw new Docx4JException("Failed to add parts from relationships", e); 287 // } 288 // } 289 290 for ( Relationship r : rp.getRelationships().getRelationship() ) { 291 292 log.info("For Relationship Id=" + r.getId() 293 + " Source is " + rp.getSourceP().getPartName() 294 + ", Target is " + r.getTarget() ); 295 try { 279 296 getPart(zf, pkg, rp, r); 280 281 297 } catch (Exception e) { 282 298 throw new Docx4JException("Failed to add parts from relationships", e); … … 304 320 // Package pkg, String resolvedPartUri, String relationshipType) 305 321 private void getPart(ZipFile zf, Package pkg, RelationshipsPart rp, Relationship r) 306 throws Docx4JException, InvalidFormatException {322 throws Docx4JException, InvalidFormatException, URISyntaxException { 307 323 308 324 Base source = null; 309 325 String resolvedPartUri = null; 310 326 311 if (r.getTargetMode().equals(TargetMode.INTERNAL) ) { 327 if (r.getTargetMode() == null 328 || !r.getTargetMode().equals("External") ) { 312 329 313 330 // Usual case 314 331 315 source = r .getSource();316 resolvedPartUri = URIHelper.resolvePartUri(r .getSourceURI(), r.getTargetURI() ).toString();332 source = rp.getSourceP(); 333 resolvedPartUri = URIHelper.resolvePartUri(rp.getSourceURI(), new URI(r.getTarget() ) ).toString(); 317 334 318 335 // Now drop leading "/' … … 335 352 */ 336 353 337 log.warn("Encountered external resource " + r.getTarget URI()338 + " of type " + r.get RelationshipType() );354 log.warn("Encountered external resource " + r.getTarget() 355 + " of type " + r.getType() ); 339 356 340 357 // As of 1 May 2008, we don't do anything with these yet. … … 349 366 } 350 367 351 String relationshipType = r.get RelationshipType();368 String relationshipType = r.getType(); 352 369 353 370 Part part = getRawPart(zf, ctm, resolvedPartUri); -
trunk/docx4j/src/main/java/org/docx4j/openpackaging/io/SaveToJCR.java
r460 r532 25 25 import java.io.InputStream; 26 26 import java.io.IOException; 27 import java.net.URI; 27 28 28 29 import java.util.Iterator; … … 56 57 import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPart; 57 58 import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart; 58 import org.docx4j.openpackaging.parts.relationships.Relationship;59 59 import org.docx4j.openpackaging.parts.relationships.RelationshipsPart; 60 import org.docx4j.openpackaging.parts.relationships.TargetMode; 60 import org.docx4j.relationships.Relationships; 61 import org.docx4j.relationships.Relationship; 61 62 62 63 import org.docx4j.openpackaging.exceptions.Docx4JException; … … 140 141 // TODO - replace with saveRawXmlPart(baseNode, rp) 141 142 // once we know that partName resolves correctly 142 saveRawXmlPart(baseNode, partName, rp.getW3cDocument() ); 143 // saveRawXmlPart(baseNode, partName, rp.getW3cDocument() ); 144 saveRawXmlPart(baseNode, rp ); 143 145 144 146 … … 401 403 Node baseNode, RelationshipsPart rp ) throws Docx4JException { 402 404 403 for (Iterator it = rp.iterator(); it.hasNext(); ) { 404 Relationship r = (Relationship)it.next(); 405 // for (Iterator it = rp.iterator(); it.hasNext(); ) { 406 // Relationship r = (Relationship)it.next(); 407 // log.info("For Relationship Id=" + r.getId() 408 // + " Source is " + r.getSource().getPartName() 409 // + ", Target is " + r.getTargetURI() ); 410 for ( Relationship r : rp.getRelationships().getRelationship() ) { 411 405 412 log.info("For Relationship Id=" + r.getId() 406 + " Source is " + r.getSource().getPartName() 407 + ", Target is " + r.getTargetURI() ); 408 409 if (!r.getTargetMode().equals(TargetMode.INTERNAL) ) { 413 + " Source is " + rp.getSourceP().getPartName() 414 + ", Target is " + r.getTarget() ); 415 416 417 // if (!r.getTargetMode().equals(TargetMode.INTERNAL) ) { 418 if (r.getTargetMode() != null 419 && r.getTargetMode().equals("External") ) { 410 420 411 421 // ie its EXTERNAL … … 413 423 // there is just the relationship. 414 424 415 log.warn("Encountered external resource " + r.getTarget URI()416 + " of type " + r.get RelationshipType() );425 log.warn("Encountered external resource " + r.getTarget() 426 + " of type " + r.getType() ); 417 427 418 428 // So … … 421 431 422 432 try { 423 String resolvedPartUri = URIHelper.resolvePartUri(r.getSourceURI(), r.getTargetURI() ).toString(); 433 //String resolvedPartUri = URIHelper.resolvePartUri(r.getSourceURI(), r.getTargetURI() ).toString(); 434 435 String resolvedPartUri = URIHelper.resolvePartUri(rp.getSourceURI(), new URI(r.getTarget() ) ).toString(); 436 424 437 // Now drop leading "/' 425 438 resolvedPartUri = resolvedPartUri.substring(1); … … 480 493 String relPart = PartName.getRelationshipsPartName(resolvedPartUri); 481 494 log.info("Found relationships " + relPart ); 482 saveRawXmlPart(baseNode, relPart, rrp.getW3cDocument() ); 495 496 //saveRawXmlPart(baseNode, relPart, rrp.getW3cDocument() ); 497 saveRawXmlPart(baseNode, rrp ); 498 483 499 log.info("Recursing ... " ); 484 500 addPartsFromRelationships( baseNode, rrp ); -
trunk/docx4j/src/main/java/org/docx4j/openpackaging/io/SaveToZipFile.java
r466 r532 28 28 import java.io.InputStream; 29 29 import java.io.OutputStream; 30 import java.net.URI; 30 31 import java.util.Enumeration; 31 32 import java.util.Iterator; … … 42 43 import org.docx4j.openpackaging.parts.PartName; 43 44 import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPart; 44 import org.docx4j.openpackaging.parts.relationships.Relationship;45 45 import org.docx4j.openpackaging.parts.relationships.RelationshipsPart; 46 import org.docx4j.openpackaging.parts.relationships.TargetMode; 46 import org.docx4j.relationships.Relationships; 47 import org.docx4j.relationships.Relationship; 48 47 49 import org.dom4j.Document; 48 50 import org.dom4j.DocumentException; … … 238 240 throws Docx4JException { 239 241 240 for (Iterator it = rp.iterator(); it.hasNext(); ) { 241 Relationship r = (Relationship)it.next(); 242 log.info("For Relationship Id=" + r.getId() + " Source is " + r.getSource().getPartName() + ", Target is " + r.getTargetURI() ); 243 244 if (!r.getTargetMode().equals(TargetMode.INTERNAL) ) { 242 // for (Iterator it = rp.iterator(); it.hasNext(); ) { 243 // Relationship r = (Relationship)it.next(); 244 // log.info("For Relationship Id=" + r.getId() + " Source is " + r.getSource().getPartName() + ", Target is " + r.getTargetURI() ); 245 for ( Relationship r : rp.getRelationships().getRelationship() ) { 246 247 log.info("For Relationship Id=" + r.getId() 248 + " Source is " + rp.getSourceP().getPartName() 249 + ", Target is " + r.getTarget() ); 250 251 // if (!r.getTargetMode().equals(TargetMode.INTERNAL) ) { 252 if (r.getTargetMode() != null 253 && r.getTargetMode().equals("External") ) { 245 254 246 255 // ie its EXTERNAL … … 248 257 // there is just the relationship. 249 258 250 log.warn("Encountered external resource " + r.getTarget URI()251 + " of type " + r.get RelationshipType() );259 log.warn("Encountered external resource " + r.getTarget() 260 + " of type " + r.getType() ); 252 261 253 262 // So … … 256 265 257 266 try { 258 String resolvedPartUri = URIHelper.resolvePartUri(r.getSourceURI(), r.getTargetURI() ).toString(); 259 267 //String resolvedPartUri = URIHelper.resolvePartUri(r.getSourceURI(), r.getTargetURI() ).toString(); 268 269 String resolvedPartUri = URIHelper.resolvePartUri(rp.getSourceURI(), new URI(r.getTarget() ) ).toString(); 270 260 271 // Now drop leading "/' 261 272 resolvedPartUri = resolvedPartUri.substring(1); -
trunk/docx4j/src/main/java/org/docx4j/openpackaging/parts/relationships/RelationshipsPart.java
r511 r532 51 51 package org.docx4j.openpackaging.parts.relationships; 52 52 53 import javax.xml.bind.JAXBException; 54 import javax.xml.bind.Unmarshaller; 55 56 import org.docx4j.relationships.Relationships; 57 import org.docx4j.relationships.Relationship; 58 59 import org.docx4j.jaxb.Context; 60 import org.docx4j.openpackaging.parts.JaxbXmlPart; 61 53 62 import java.io.InputStream; 54 63 import java.net.URI; 55 64 import java.net.URISyntaxException; 56 import java.util.ArrayList;57 import java.util.Iterator;58 import java.util.TreeMap;59 import java.util.zip.ZipOutputStream;60 65 61 66 import org.apache.log4j.Logger; … … 71 76 import org.docx4j.openpackaging.parts.PartName; 72 77 73 import org.docx4j.openpackaging.parts.Dom4jXmlPart; 74 75 import org.dom4j.Attribute; 76 import org.dom4j.Document; 77 import org.dom4j.DocumentHelper; 78 import org.dom4j.Element; 79 import org.dom4j.Namespace; 80 import org.dom4j.QName; 81 import org.dom4j.io.SAXReader; 82 //import org.openxml4j.opc.PackageAccess; 78 83 79 84 80 … … 91 87 * @version 0.1 92 88 */ 93 public final class RelationshipsPart extends Dom4jXmlPart implements 94 Iterable<Relationship> { 95 89 public final class RelationshipsPart extends JaxbXmlPart { 90 // implements Iterable<Relationship> { 91 92 private static Logger logger = Logger.getLogger(RelationshipsPart.class); 93 96 94 /* Example: 97 95 * … … 142 140 */ 143 141 144 145 private static Logger logger = Logger.getLogger(RelationshipsPart.class); 146 147 /** 148 * Package relationships ordered by ID. 149 */ 150 private TreeMap<String, Relationship> relationshipsByID; 151 152 /** 153 * Package relationships ordered by type. 154 */ 155 private TreeMap<String, Relationship> relationshipsByType; 156 157 142 /** 143 * Constructor. 144 */ 145 public RelationshipsPart(PartName partName) throws InvalidFormatException { 146 super(partName); 147 init(); 148 } 149 150 public RelationshipsPart() throws InvalidFormatException { 151 super(new PartName("/rels/.rels")); 152 init(); 153 } 154 155 public void init() { 156 // Used if this Part is added to [Content_Types].xml 157 setContentType(new org.docx4j.openpackaging.contenttype.ContentType( 158 org.docx4j.openpackaging.contenttype.ContentTypes.RELATIONSHIPS_PART)); 159 160 setJAXBContext(Context.jcRelationships); 161 162 } 163 164 private Relationships relationships; 165 public Relationships getRelationships() { 166 return relationships; 167 } 168 169 public void setRelationships(Relationships relationships) { 170 this.relationships = relationships; 171 } 172 173 // NB partName is the partName of this relationship part, 174 // not the source Part. sourceP above has the 175 // sourcePartName, which will be required in order to resolve 176 // relative targets 177 158 178 /** 159 179 * Source part for these relationships … … 164 184 return sourceP; 165 185 } 186 public void setSourceP( Base sourcePart) { 187 sourceP = sourcePart; 188 } 189 190 public URI getSourceURI() { 191 if (sourceP == null) { 192 return URIHelper.PACKAGE_ROOT_URI; 193 } 194 return sourceP.getPartName().getURI(); 195 } 196 166 197 167 198 /** This Relationship Part is the package relationship part … … 172 203 } 173 204 174 // public void setPackageRelationshipPart(boolean isPackageRelationshipPart) {175 // this.isPackageRelationshipPart = isPackageRelationshipPart;176 // }177 178 179 /**180 * Constructor.181 *182 * DO NOT USE. Doesn't set source Part!183 */184 public RelationshipsPart(PartName partName) throws InvalidFormatException {185 super(partName);186 // NB partName is the partName of this relationship part,187 // not the source Part. sourceP above has the188 // sourcePartName, which will be required in order to resolve189 // relative targets190 init();191 192 //throw new InvalidFormatException();193 }194 205 195 206 /** … … 217 228 // in this class. 218 229 219 } 220 221 222 /** 223 * Constructor. Parses the .rels XML document. 224 * 225 * @param partName 226 * The part name, relative to the parent Package root. 227 * @param contents 228 * The XML Document contents of the part. 229 * @throws InvalidFormatException 230 * If the specified URI is not valid. 231 */ 232 public RelationshipsPart(Base sourceP, PartName partName, InputStream in) 233 throws InvalidFormatException { 234 super(partName); 235 setDocument(in); 236 this.sourceP = sourceP; 237 init(); 238 Element root = document.getRootElement(); 239 parseRelationshipsDocument(root); 240 241 // this.container = (Package) pack; 242 // isRelationshipPart = partName.isRelationshipPartURI(); 243 } 244 245 // This constructor used when input is a Word 2007 Xml Package file 246 public RelationshipsPart(Base sourceP, PartName partName, Element root) 247 throws InvalidFormatException { 248 249 super(partName); 250 251 // setDocument(in); // nb - not set 252 this.sourceP = sourceP; 253 init(); 254 parseRelationshipsDocument(root); 255 256 // this.container = (Package) pack; 257 // isRelationshipPart = partName.isRelationshipPartURI(); 258 } 259 260 261 private void init() { 262 263 setContentType(new org.docx4j.openpackaging.contenttype.ContentType( 264 org.docx4j.openpackaging.contenttype.ContentTypes.RELATIONSHIPS_PART)); 265 266 relationshipsByID = new TreeMap<String, Relationship>(); 267 relationshipsByType = new TreeMap<String, Relationship>(); 230 231 // TODO - create jaxbElement|relationships from factory? 232 268 233 } 269 234 … … 315 280 } 316 281 317 log.info(" source is " + r.getSourceURI() );318 log.info(id + " points to " + r.getTarget URI());282 log.info(" source is " + sourceP.getPartName().toString() ); 283 log.info(id + " points to " + r.getTarget()); 319 284 // eg rId1 points to fonts/font1.odttf 285 286 URI uri = null; 287 288 try { 289 uri = org.docx4j.openpackaging.URIHelper 290 .resolvePartUri(sourceP.partName.getURI(), new URI( 291 r.getTarget())); 292 } catch (URISyntaxException e) { 293 log.error("Cannot convert " + r.getTarget() 294 + " in a valid relationship URI-> ignored", e); 295 } 320 296 321 URI uri = org.docx4j.openpackaging.URIHelper.resolvePartUri(r.getSourceURI(), r.getTargetURI()); 322 297 323 298 try { 324 299 return getPackage().getParts().get( new PartName(uri, true )); … … 355 330 log.debug("Result " + result); 356 331 357 Relationship rel = new Relationship(sourceP, result, 358 TargetMode.INTERNAL, part.getRelationshipType(), id); 332 org.docx4j.relationships.ObjectFactory factory = 333 new org.docx4j.relationships.ObjectFactory(); 334 335 Relationship rel = factory.createRelationship(); 336 relationships.getRelationship().add(rel); 337 338 rel.setTarget(result.toString() ); 339 //rel.setTargetMode( TargetMode.INTERNAL ); 340 rel.setType( part.getRelationshipType() ); 341 rel.setId( id ); 342 343 344 // Relationship rel = new Relationship(sourceP, result, 345 // TargetMode.INTERNAL, part.getRelationshipType(), id); 359 346 addRelationship(rel ); 360 347 … … 372 359 // Make a list in order to avoid concurrent modification exception 373 360 java.util.ArrayList<Relationship> relationshipsToGo = new java.util.ArrayList<Relationship>(); 374 for (Relationship r : relationships ByID.values() ) {361 for (Relationship r : relationships.getRelationship() ) { 375 362 relationshipsToGo.add(r); 376 363 } … … 378 365 for (Relationship r : relationshipsToGo ) { 379 366 380 String resolvedPartUri = URIHelper.resolvePartUri(r.getSourceURI(), r.getTargetURI() ).toString();381 382 log.info("Removing part: " + resolvedPartUri);383 384 367 try { 368 String resolvedPartUri = URIHelper.resolvePartUri( 369 getSourceURI(), new URI(r.getTarget())).toString(); 370 371 log.info("Removing part: " + resolvedPartUri); 372 385 373 removePart(new PartName(resolvedPartUri)); 374 } catch (URISyntaxException e) { 375 log.error("Cannot convert " + r.getTarget() 376 + " in a valid relationship URI-> ignored", e); 386 377 } catch (InvalidFormatException e) { 387 378 // TODO Auto-generated catch block … … 416 407 // Throw an error if this can't be found! 417 408 Relationship relToBeRemoved = null; 418 for (Relationship rel : relationshipsByID.values() ) { 419 420 URI resolvedTargetURI = org.docx4j.openpackaging.URIHelper.resolvePartUri( sourceP.partName.getURI(), rel.getTargetURI() ); 409 // for (Relationship rel : relationshipsByID.values() ) { 410 for (Relationship rel : relationships.getRelationship() ) { 411 412 URI resolvedTargetURI = null; 413 414 try { 415 resolvedTargetURI = org.docx4j.openpackaging.URIHelper 416 .resolvePartUri(sourceP.partName.getURI(), new URI( 417 rel.getTarget())); 418 } catch (URISyntaxException e) { 419 log.error("Cannot convert " + rel.getTarget() 420 + " in a valid relationship URI-> ignored", e); 421 } 422 421 423 log.debug("Comparing " + resolvedTargetURI + " == " + partName.getName()); 422 424 423 425 if (partName.getName().equals(resolvedTargetURI.toString()) ) { // was rel.getTargetURI() 424 426 425 log.info("True - will delete relationship with target " + rel.getTarget URI());427 log.info("True - will delete relationship with target " + rel.getTarget()); 426 428 relToBeRemoved = rel; // Avoid java.util.ConcurrentModificationException 427 429 break; … … 455 457 456 458 457 458 459 460 /**461 * Parse the relationship part and add all relationship in this collection.462 *463 * @param relPart464 * The package part to parse.465 * @throws InvalidFormatException466 * Throws if the relationship part is invalid.467 */468 private void parseRelationshipsDocument(Element root)469 throws InvalidFormatException {470 try {471 472 // Browse default types473 474 // Check OPC compliance M4.1 rule475 boolean fCorePropertiesRelationship = false;476 477 for (Iterator i = root478 .elementIterator(Relationship.RELATIONSHIP_TAG_NAME);479 i.hasNext();) {480 Element element = (Element) i.next();481 482 Relationship rel = new Relationship(sourceP, element, fCorePropertiesRelationship);483 484 if (rel.getId() == null) {485 // Generate a unique ID if id parameter is null.486 int j = 0;487 do {488 rel.setId("rId" + ++j);489 } while (relationshipsByID.get(rel.getId()) != null);490 }491 492 //addRelationship(target, targetMode, type, id);493 addRelationship(rel);494 495 }496 } catch (Exception e) {497 e.printStackTrace();498 logger.error(e);499 throw new InvalidFormatException(e.getMessage());500 }501 }502 503 /**504 * Return an XML representation of this part.505 */506 public Document getDocument() {507 508 // Building xml509 Document xmlOutDoc = DocumentHelper.createDocument();510 // make something like <Relationships511 // xmlns="http://schemas.openxmlformats.org/package/2006/relationships">512 Namespace dfNs = Namespace.get("", Namespaces.RELATIONSHIPS);513 Element root = xmlOutDoc.addElement(new QName(514 Relationship.RELATIONSHIPS_TAG_NAME, dfNs));515 516 // <Relationship517 // TargetMode="External"518 // Id="rIdx"519 // Target="http://www.custom.com/images/pic1.jpg"520 // Type="http://www.custom.com/external-resource"/>521 522 log.debug("Partname is " + partName);523 // URI sourcePartURI = URIHelper524 // .getSourcePartUriFromRelationshipPartUri(partName.getURI());525 526 for (Relationship rel : relationshipsByID.values()) {527 // The relationship element528 Element relElem = root529 .addElement(Relationship.RELATIONSHIP_TAG_NAME);530 531 //rel.marshall(sourcePartURI, relElem);532 533 rel.marshall(relElem);534 535 }536 537 538 xmlOutDoc.normalize();539 540 return xmlOutDoc;541 }542 543 459 /** 544 460 * Add the specified relationship to the collection. … … 548 464 */ 549 465 public void addRelationship(Relationship rel) { 550 relationshipsByID.put(rel.getId(), rel); 551 relationshipsByType.put(rel.getRelationshipType(), rel); 552 } 553 554 555 556 557 /** 558 * Remove a relationship by its ID. 559 * 560 * @param id 561 * The relationship ID to remove. 562 */ 563 // private void removeRelationship(String id) { 564 // if (relationshipsByID != null && relationshipsByType != null) { 565 // Relationship rel = relationshipsByID.get(id); 566 // if (rel != null) { 567 // relationshipsByID.remove(rel.getId()); 568 // relationshipsByType.values().remove(rel); 569 // } 570 // } 571 // } 466 467 relationships.getRelationship().add(rel); 468 } 469 572 470 573 471 /** … … 580 478 if (rel == null) 581 479 throw new IllegalArgumentException("rel"); 582 583 relationshipsByID.values().remove(rel); 584 relationshipsByType.values().remove(rel); 585 } 586 587 /** 588 * Retrieves a relationship by its index in the collection. 589 * 590 * @param index 591 * Must be a value between [0-relationships_count-1] 592 */ 593 public Relationship getRelationship(int index) { 594 if (index < 0 || index > relationshipsByID.values().size()) 595 throw new IllegalArgumentException("index"); 596 597 Relationship retRel = null; 598 int i = 0; 599 for (Relationship rel : relationshipsByID.values()) { 600 if (index == i++) 601 return rel; 602 } 603 return retRel; 604 } 480 481 relationships.getRelationship().remove(rel); 482 483 } 484 605 485 606 486 /** … … 612 492 */ 613 493 public Relationship getRelationshipByID(String id) { 614 return relationshipsByID.get(id); 494 495 for ( Relationship r : relationships.getRelationship() ) { 496 497 if (r.getId().equals(id) ) { 498 return r; 499 } 500 501 } 502 503 return null; 615 504 } 616 505 617 506 public Relationship getRelationshipByType(String type) { 618 return relationshipsByType.get(type); 507 508 for ( Relationship r : relationships.getRelationship() ) { 509 510 if (r.getType().equals(type) ) { 511 return r; 512 } 513 514 } 515 516 return null; 619 517 } 620 518 … … 623 521 */ 624 522 public int size() { 625 return relationships ByID.values().size();523 return relationships.getRelationship().size(); 626 524 } 627 525 … … 629 527 * Get this collection's iterator. 630 528 */ 631 public Iterator<Relationship> iterator() {632 return relationshipsByID.values().iterator();633 }529 // public Iterator<Relationship> iterator() { 530 // return relationshipsByID.values().iterator(); 531 // } 634 532 635 533 /** … … 642 540 * specified type contain in this collection. 643 541 */ 644 public Iterator<Relationship> iterator(String typeFilter) { 645 ArrayList<Relationship> retArr = new ArrayList<Relationship>(); 646 for (Relationship rel : relationshipsByID.values()) { 647 if (rel.getRelationshipType().equals(typeFilter)) 648 retArr.add(rel); 649 } 650 return retArr.iterator(); 651 } 652 653 /** 654 * Clear all relationships. 655 */ 656 public void clear() { 657 relationshipsByID.clear(); 658 relationshipsByType.clear(); 659 } 660 661 @Override 662 public String toString() { 663 664 String str; 665 if (relationshipsByID == null) { 666 str = "relationshipsByID=null"; 667 } else { 668 str = relationshipsByID.size() + " relationship(s) = [ \n"; 669 } 670 671 ArrayList<Relationship> retArr = new ArrayList<Relationship>(); 672 for (Relationship rel : relationshipsByID.values()) { 673 str = str + rel.toString() + "\n"; 674 } 675 676 // if ( (partName != null)) { 677 // str = str + "," + partName; 678 // } else { 679 // str = str + ",relationshipPart=null"; 542 // public Iterator<Relationship> iterator(String typeFilter) { 543 // ArrayList<Relationship> retArr = new ArrayList<Relationship>(); 544 // for (Relationship rel : relationshipsByID.values()) { 545 // if (rel.getRelationshipType().equals(typeFilter)) 546 // retArr.add(rel); 680 547 // } 681 // 682 // // Source of this relationship 683 // if ((sourcePart != null) && (sourcePart.getPartName() != null)) { 684 // str = str + "," + sourcePart.getPartName(); 685 // } else { 686 // str = str + ",sourcePart=null"; 687 // } 688 // if (partName != null) { 689 // str = str + "," + partName; 690 // } else { 691 // str = str + ",uri=null)"; 692 // } 693 return str + "]"; 694 } 695 548 // return retArr.iterator(); 549 // } 550 551 552 /** 553 * Unmarshal XML data from the specified InputStream and return the 554 * resulting content tree. Validation event location information may 555 * be incomplete when using this form of the unmarshal API. 556 * 557 * <p> 558 * Implements <a href="#unmarshalGlobal">Unmarshal Global Root Element</a>. 559 * 560 * @param is the InputStream to unmarshal XML data from 561 * @return the newly created root object of the java content tree 562 * 563 * @throws JAXBException 564 * If any unexpected errors occur while unmarshalling 565 */ 566 public Object unmarshal( java.io.InputStream is ) throws JAXBException { 567 568 try { 569 570 // if (jc==null) { 571 // setJAXBContext(Context.jc); 572 // } 573 574 Unmarshaller u = jc.createUnmarshaller(); 575 576 //u.setSchema(org.docx4j.jaxb.WmlSchema.schema); 577 u.setEventHandler(new org.docx4j.jaxb.JaxbValidationEventHandler()); 578 579 System.out.println("unmarshalling " + this.getClass().getName() + " \n\n" ); 580 581 jaxbElement = u.unmarshal( is ); 582 583 584 System.out.println("\n\n" + this.getClass().getName() + " unmarshalled \n\n" ); 585 586 } catch (Exception e ) { 587 e.printStackTrace(); 588 } 589 590 relationships = (Relationships)jaxbElement; 591 592 return jaxbElement; 593 594 } 595 596 696 597 }
Note: See TracChangeset
for help on using the changeset viewer.
