Ignore:
Timestamp:
08/29/11 13:50:35 (9 months ago)
Author:
jharrop
Message:

When binding, create hyperlinks out of text containing  http://

Location:
trunk/docx4j/src/main/java/org/docx4j/model/datastorage
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/docx4j/src/main/java/org/docx4j/model/datastorage/BindingHandler.java

    r1643 r1645  
    3131import org.docx4j.openpackaging.parts.XmlPart; 
    3232import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage; 
     33import org.docx4j.openpackaging.parts.relationships.Namespaces; 
    3334import org.docx4j.wml.CTSimpleField; 
    3435import org.docx4j.wml.RPr; 
     36import org.docx4j.wml.P.Hyperlink; 
    3537import org.w3c.dom.Document; 
    3638import org.w3c.dom.DocumentFragment; 
     
    175177                 */ 
    176178                public static DocumentFragment xpathGenerateRuns( 
    177                                 WordprocessingMLPackage pkg, Map<String, CustomXmlDataStoragePart> customXmlDataStorageParts, 
     179                                WordprocessingMLPackage pkg,  
     180                                JaxbXmlPart sourcePart,                          
     181                                Map<String, CustomXmlDataStoragePart> customXmlDataStorageParts, 
    178182                                String storeItemId, String xpath, String prefixMappings, 
    179183                                NodeIterator rPrNodeIt, boolean multiLine) { 
     
    191195                        if (r==null) return null; 
    192196 
    193                         DocumentFragment docfrag = null; // = document.createDocumentFragment(); 
     197                        org.w3c.dom.Document docContainer = XmlUtils.neww3cDomDocument(); 
     198                        DocumentFragment docfrag = docContainer.createDocumentFragment(); 
    194199                         
    195200                        try { 
     
    211216                                        while (st.hasMoreTokens()) {                                             
    212217                                                String line = (String) st.nextToken(); 
    213  
    214                                                 org.docx4j.wml.R  run = factory.createR();               
    215                                                 if (rPr!=null) { 
    216                                                         run.setRPr(rPr); 
    217                                                 } 
     218                                                 
    218219                                                if (firsttoken) { 
    219220                                                        firsttoken = false; 
    220221                                                } else { 
    221                                                         run.getRunContent().add(factory.createBr()); 
     222                                                        addBrRunToDocFrag(docfrag, rPr); 
    222223                                                } 
    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); 
    230224                                                 
    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);                                           
    243226                                        } 
    244227                                         
     
    250233                                                sb.append( st.nextToken() ); 
    251234                                        } 
    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                                }                                
    271238                                 
    272239                        } catch (Exception e) { 
    273                                 e.printStackTrace(); 
     240                                log.error(e); 
    274241                                return null; 
    275242                        } 
     
    277244                        return docfrag;                  
    278245                } 
     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                 
    279354                 
    280355                public static DocumentFragment xpathInjectImage(WordprocessingMLPackage wmlPackage, 
     
    389464                        }  
    390465                } 
     466                 
    391467 
    392468} 
  • trunk/docx4j/src/main/java/org/docx4j/model/datastorage/bind.xslt

    r1546 r1645  
    7171                                                                                        select="java:org.docx4j.model.datastorage.BindingHandler.xpathGenerateRuns( 
    7272                                                                                                                $wmlPackage, 
     73                                                                                                                $sourcePart, 
    7374                                                                                                                $customXmlDataStorageParts, 
    7475                                                                                                                string(w:sdtPr/w:dataBinding/@w:storeItemID), 
     
    9394                                                                                select="java:org.docx4j.model.datastorage.BindingHandler.xpathGenerateRuns( 
    9495                                                                                                        $wmlPackage, 
     96                                                                                                        $sourcePart, 
    9597                                                                                                        $customXmlDataStorageParts, 
    9698                                                                                                        string(w:sdtPr/w:dataBinding/@w:storeItemID), 
     
    115117                                                                        select="java:org.docx4j.model.datastorage.BindingHandler.xpathGenerateRuns( 
    116118                                                                                                $wmlPackage, 
     119                                                                                                $sourcePart, 
    117120                                                                                                $customXmlDataStorageParts, 
    118121                                                                                                string(w:sdtPr/w:dataBinding/@w:storeItemID), 
     
    133136                                                                select="java:org.docx4j.model.datastorage.BindingHandler.xpathGenerateRuns( 
    134137                                                                                        $wmlPackage, 
     138                                                                                        $sourcePart, 
    135139                                                                                        $customXmlDataStorageParts, 
    136140                                                                                        string(w:sdtPr/w:dataBinding/@w:storeItemID), 
     
    146150                                                        select="java:org.docx4j.model.datastorage.BindingHandler.xpathGenerateRuns( 
    147151                                                                                $wmlPackage, 
     152                                                                                $sourcePart, 
    148153                                                                                $customXmlDataStorageParts, 
    149154                                                                                string(w:sdtPr/w:dataBinding/@w:storeItemID), 
Note: See TracChangeset for help on using the changeset viewer.