Changeset 1645 for trunk/docx4j/src/main/java/org
- Timestamp:
- 08/29/11 13:50:35 (9 months ago)
- Location:
- trunk/docx4j/src/main/java/org/docx4j/model/datastorage
- Files:
-
- 2 edited
-
BindingHandler.java (modified) (7 diffs)
-
bind.xslt (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/docx4j/src/main/java/org/docx4j/model/datastorage/BindingHandler.java
r1643 r1645 31 31 import org.docx4j.openpackaging.parts.XmlPart; 32 32 import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage; 33 import org.docx4j.openpackaging.parts.relationships.Namespaces; 33 34 import org.docx4j.wml.CTSimpleField; 34 35 import org.docx4j.wml.RPr; 36 import org.docx4j.wml.P.Hyperlink; 35 37 import org.w3c.dom.Document; 36 38 import org.w3c.dom.DocumentFragment; … … 175 177 */ 176 178 public static DocumentFragment xpathGenerateRuns( 177 WordprocessingMLPackage pkg, Map<String, CustomXmlDataStoragePart> customXmlDataStorageParts, 179 WordprocessingMLPackage pkg, 180 JaxbXmlPart sourcePart, 181 Map<String, CustomXmlDataStoragePart> customXmlDataStorageParts, 178 182 String storeItemId, String xpath, String prefixMappings, 179 183 NodeIterator rPrNodeIt, boolean multiLine) { … … 191 195 if (r==null) return null; 192 196 193 DocumentFragment docfrag = null; // = document.createDocumentFragment(); 197 org.w3c.dom.Document docContainer = XmlUtils.neww3cDomDocument(); 198 DocumentFragment docfrag = docContainer.createDocumentFragment(); 194 199 195 200 try { … … 211 216 while (st.hasMoreTokens()) { 212 217 String line = (String) st.nextToken(); 213 214 org.docx4j.wml.R run = factory.createR(); 215 if (rPr!=null) { 216 run.setRPr(rPr); 217 } 218 218 219 if (firsttoken) { 219 220 firsttoken = false; 220 221 } else { 221 run.getRunContent().add(factory.createBr());222 addBrRunToDocFrag(docfrag, rPr); 222 223 } 223 org.docx4j.wml.Text text = factory.createText();224 run.getRunContent().add(text);225 if (line.startsWith(" ") || line.endsWith(" ") ) {226 // TODO: tab character?227 text.setSpace("preserve");228 }229 text.setValue(line);230 224 231 Document document = XmlUtils.marshaltoW3CDomDocument(run); 232 if (docfrag == null) { // will be for first line 233 docfrag = document.createDocumentFragment(); 234 docfrag.appendChild(document.getDocumentElement()); 235 } else { 236 // try to avoid WRONG_DOCUMENT_ERR: A node is used in a different document than the one that created it. 237 // but NOT_SUPPORTED_ERR: The implementation does not support the requested type of object or operation. 238 // at com.sun.org.apache.xerces.internal.dom.CoreDocumentImpl.importNode 239 // docfrag.appendChild(fragdoc.importNode(document, true)); 240 // so: 241 XmlUtils.treeCopy(document.getDocumentElement(), docfrag); 242 } 225 processString(sourcePart, docfrag, line, rPr); 243 226 } 244 227 … … 250 233 sb.append( st.nextToken() ); 251 234 } 252 String line = sb.toString(); 253 254 org.docx4j.wml.R run = factory.createR(); 255 if (rPr!=null) { 256 run.setRPr(rPr); 257 } 258 org.docx4j.wml.Text text = factory.createText(); 259 run.getRunContent().add(text); 260 if (line.startsWith(" ") || line.endsWith(" ") ) { 261 // TODO: tab character? 262 text.setSpace("preserve"); 263 } 264 text.setValue(line); 265 266 Document document = XmlUtils.marshaltoW3CDomDocument(run); 267 docfrag = document.createDocumentFragment(); 268 docfrag.appendChild(document.getDocumentElement()); 269 } 270 235 236 processString(sourcePart, docfrag, sb.toString(), rPr); 237 } 271 238 272 239 } catch (Exception e) { 273 e.printStackTrace();240 log.error(e); 274 241 return null; 275 242 } … … 277 244 return docfrag; 278 245 } 246 247 private static void addBrRunToDocFrag(DocumentFragment docfrag, RPr rPr) throws JAXBException { 248 249 // Not sure whether there is ever anything of interest in the rPr, 250 // but add it anyway 251 org.docx4j.wml.R run = Context.getWmlObjectFactory().createR(); 252 if (rPr!=null) { 253 run.setRPr(rPr); 254 } 255 run.getRunContent().add(Context.getWmlObjectFactory().createBr()); 256 257 Document tmpDoc = XmlUtils.marshaltoW3CDomDocument(run); 258 XmlUtils.treeCopy(tmpDoc.getDocumentElement(), docfrag); 259 } 260 261 private static void processString(JaxbXmlPart sourcePart, DocumentFragment docfrag, String text, RPr rPr) throws JAXBException { 262 263 int pos = text.indexOf("http://"); 264 265 if (pos==-1) { 266 addRunToDocFrag(sourcePart, docfrag, text, rPr); 267 return; 268 } 269 270 // There is a hyperlink to deal with 271 272 if (pos==0) { 273 int spacePos = text.indexOf(" "); 274 if (spacePos==-1) { 275 addHyperlinkToDocFrag(sourcePart, docfrag, text); 276 return; 277 } 278 279 // Could contain more than one hyperlink, so process recursively 280 String first = text.substring(0, spacePos); 281 String rest = text.substring(spacePos); 282 283 addHyperlinkToDocFrag( sourcePart, docfrag, first); 284 // .. now the recursive bit .. 285 processString(sourcePart, docfrag, rest, rPr); 286 return; 287 } 288 289 String first = text.substring(0, pos); 290 String rest = text.substring(pos); 291 292 addRunToDocFrag( sourcePart, docfrag, first, rPr); 293 // .. now the recursive bit .. 294 processString(sourcePart, docfrag, rest, rPr); 295 } 296 297 private static void addRunToDocFrag(JaxbXmlPart sourcePart, DocumentFragment docfrag, String string, RPr rPr) { 298 299 org.docx4j.wml.R run = Context.getWmlObjectFactory().createR(); 300 if (rPr!=null) { 301 run.setRPr(rPr); 302 } 303 org.docx4j.wml.Text text = Context.getWmlObjectFactory().createText(); 304 run.getRunContent().add(text); 305 if (string.startsWith(" ") || string.endsWith(" ") ) { 306 // TODO: tab character? 307 text.setSpace("preserve"); 308 } 309 text.setValue(string); 310 311 Document tmpDoc = XmlUtils.marshaltoW3CDomDocument(run); 312 313 // avoid WRONG_DOCUMENT_ERR: A node is used in a different document than the one that created it. 314 // but NOT_SUPPORTED_ERR: The implementation does not support the requested type of object or operation. 315 // at com.sun.org.apache.xerces.internal.dom.CoreDocumentImpl.importNode 316 // docfrag.appendChild(fragdoc.importNode(document, true)); 317 // so: 318 XmlUtils.treeCopy(tmpDoc.getDocumentElement(), docfrag); 319 } 320 321 private static void addHyperlinkToDocFrag(JaxbXmlPart sourcePart, DocumentFragment docfrag, String url) throws JAXBException { 322 323 // We need to add a relationship to word/_rels/document.xml.rels 324 // but since its external, we don't use the 325 // usual wordMLPackage.getMainDocumentPart().addTargetPart 326 // mechanism 327 org.docx4j.relationships.ObjectFactory factory = 328 new org.docx4j.relationships.ObjectFactory(); 329 330 org.docx4j.relationships.Relationship rel = factory.createRelationship(); 331 rel.setType( Namespaces.HYPERLINK ); 332 rel.setTarget(url); 333 rel.setTargetMode("External"); 334 335 sourcePart.getRelationshipsPart().addRelationship(rel); 336 337 // addRelationship sets the rel's @Id 338 339 String hpl = "<w:hyperlink r:id=\"" + rel.getId() + "\" xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" " + 340 "xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" >" + 341 "<w:r>" + 342 "<w:rPr>" + 343 "<w:rStyle w:val=\"Hyperlink\" />" + // TODO: enable this style in the document! 344 "</w:rPr>" + 345 "<w:t>" + url + "</w:t>" + 346 "</w:r>" + 347 "</w:hyperlink>"; 348 349 Document tmpDoc = XmlUtils.marshaltoW3CDomDocument( 350 (Hyperlink)XmlUtils.unmarshalString(hpl)); 351 XmlUtils.treeCopy(tmpDoc.getDocumentElement(), docfrag); 352 } 353 279 354 280 355 public static DocumentFragment xpathInjectImage(WordprocessingMLPackage wmlPackage, … … 389 464 } 390 465 } 466 391 467 392 468 } -
trunk/docx4j/src/main/java/org/docx4j/model/datastorage/bind.xslt
r1546 r1645 71 71 select="java:org.docx4j.model.datastorage.BindingHandler.xpathGenerateRuns( 72 72 $wmlPackage, 73 $sourcePart, 73 74 $customXmlDataStorageParts, 74 75 string(w:sdtPr/w:dataBinding/@w:storeItemID), … … 93 94 select="java:org.docx4j.model.datastorage.BindingHandler.xpathGenerateRuns( 94 95 $wmlPackage, 96 $sourcePart, 95 97 $customXmlDataStorageParts, 96 98 string(w:sdtPr/w:dataBinding/@w:storeItemID), … … 115 117 select="java:org.docx4j.model.datastorage.BindingHandler.xpathGenerateRuns( 116 118 $wmlPackage, 119 $sourcePart, 117 120 $customXmlDataStorageParts, 118 121 string(w:sdtPr/w:dataBinding/@w:storeItemID), … … 133 136 select="java:org.docx4j.model.datastorage.BindingHandler.xpathGenerateRuns( 134 137 $wmlPackage, 138 $sourcePart, 135 139 $customXmlDataStorageParts, 136 140 string(w:sdtPr/w:dataBinding/@w:storeItemID), … … 146 150 select="java:org.docx4j.model.datastorage.BindingHandler.xpathGenerateRuns( 147 151 $wmlPackage, 152 $sourcePart, 148 153 $customXmlDataStorageParts, 149 154 string(w:sdtPr/w:dataBinding/@w:storeItemID),
Note: See TracChangeset
for help on using the changeset viewer.
