/** * @return the hyperlinkResolver * @since 3.0.0 */ public static BindingHyperlinkResolver getHyperlinkResolver() { if (hyperlinkResolver==null) { hyperlinkResolver = new BindingHyperlinkResolver(); } return hyperlinkResolver; } /**
/** * @param bookmarkName * @param url * @return * @throws JAXBException */ public Hyperlink generateHyperlink(String bookmarkName, String url) throws JAXBException { if (url.startsWith(COMPONENT_REF)) { String hpl = "<w:hyperlink w:anchor=\"" + bookmarkName + "\" xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" >" + "<w:r>" + "<w:rPr>" + "<w:rStyle w:val=\"" + getHyperlinkStyleId() + "\" />" + "</w:rPr>" + "<w:t>" + url + "</w:t>" + "</w:r>" + "</w:hyperlink>"; return (Hyperlink)XmlUtils.unmarshalString(hpl); } else { return super.generateHyperlink(bookmarkName /* actually, relId here */, url); } }
/** * @deprecated */ public static String getHyperlinkStyleId() { return getHyperlinkResolver().getHyperlinkStyleId(); }
protected void processString(JaxbXmlPart sourcePart, DocumentFragment docfrag, String text, RPr rPr) throws Docx4JException { int pos = BindingHandler.getHyperlinkResolver().getIndexOfURL(text); if (pos==-1 ) { addRunToDocFrag(sourcePart, docfrag, text, rPr); return; } else if (BindingHandler.getHyperlinkResolver().getHyperlinkStyleId() == null){ log.warn("No Hyperlink style set, not linking"); addRunToDocFrag(sourcePart, docfrag, text, rPr);
/** * Return the index of the position you wish to treat as a hyperlink. * The characters from there to the first whitespace following will be treated as * the URL * * @param text * @return */ public int getIndexOfURL(String text) { /* For OpenAPI, treat a string starting with: * * #/components * * as a hyperlink to a bookmark. */ if (text.startsWith(COMPONENT_REF)) return 0; return super.getIndexOfURL(text); }
getHyperlinkResolver().activateHyperlinkStyle(wordMLPackage);
/** * Configure, how the handler handles links found in Custom XML. * * If hyperlinkStyleId is set to <code>null</code>, strings * containing 'http://' or 'https://' are not converted to * w:hyperlink. This is the default behavior. * * If hyperlinkStyleId is set to <code>"someWordHyperlinkStyleName"</code>, * strings containing 'http://' or 'https://' or or 'mailto:' are converted to w:hyperlink. * The default Word hyperlink style name is "Hyperlink". * * Due to the architecture of this class, this is a static flag changing the * behavior of all following calls to {@link #applyBindings}. * * @param hyperlinkStyleID * The style to use for hyperlinks (eg Hyperlink) * @deprecated */ public static void setHyperlinkStyle ( String hyperlinkStyleID) { getHyperlinkResolver().setHyperlinkStyle(hyperlinkStyleID); } /**
protected void processString(JaxbXmlPart sourcePart, DocumentFragment docfrag, String text, RPr rPr) throws Docx4JException { int pos = BindingHandler.getHyperlinkResolver().getIndexOfURL(text); if (pos==-1 ) { addRunToDocFrag(sourcePart, docfrag, text, rPr); return; } else if (BindingHandler.getHyperlinkResolver().getHyperlinkStyleId() == null){ log.warn("No Hyperlink style set, not linking"); addRunToDocFrag(sourcePart, docfrag, text, rPr);
private void processString(JaxbXmlPart sourcePart, List<Object> contents, String text, SdtPr sdtPr, RPr rPr) throws JAXBException { int pos = BindingHandler.getHyperlinkResolver().getIndexOfURL(text); if (pos==-1 || BindingHandler.getHyperlinkStyleId() == null) { addRunToDocFrag(sourcePart, contents, text, rPr);
public void applyBindings() throws Docx4JException { // A component can apply in both the main document part, // and in headers/footers. See further // http://forums.opendope.org/Support-components-in-headers-footers-tp2964174p2964174.html getHyperlinkResolver().activateHyperlinkStyle(wordMLPackage); applyBindings(wordMLPackage.getMainDocumentPart()); // Add headers/footers RelationshipsPart rp = wordMLPackage.getMainDocumentPart() .getRelationshipsPart(); for (Relationship r : rp.getRelationships().getRelationship()) { if (r.getType().equals(Namespaces.HEADER)) { applyBindings((HeaderPart) rp.getPart(r)); } else if (r.getType().equals(Namespaces.FOOTER)) { applyBindings((FooterPart) rp.getPart(r)); } } }
/** * Configure, how the handler handles links found in Custom XML. * * If hyperlinkStyleId is set to <code>null</code>, strings * containing 'http://' or 'https://' are not converted to * w:hyperlink. This is the default behavior. * * If hyperlinkStyleId is set to <code>"someWordHyperlinkStyleName"</code>, * strings containing 'http://' or 'https://' or or 'mailto:' are converted to w:hyperlink. * The default Word hyperlink style name is "Hyperlink". * * Due to the architecture of this class, this is a static flag changing the * behavior of all following calls to {@link #applyBindings}. * * @param hyperlinkStyleID * The style to use for hyperlinks (eg Hyperlink) * @deprecated */ public static void setHyperlinkStyle ( String hyperlinkStyleID) { getHyperlinkResolver().setHyperlinkStyle(hyperlinkStyleID); } /**
/** * @param relId * @param url * @return * @throws JAXBException */ public Hyperlink generateHyperlink(String relId, String url) throws JAXBException { String hpl = "<w:hyperlink r:id=\"" + relId + "\" xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" " + "xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" >" + "<w:r>" + "<w:rPr>" + "<w:rStyle w:val=\"" + getHyperlinkStyleId() + "\" />" + "</w:rPr>" + "<w:t>" + url + "</w:t>" + "</w:r>" + "</w:hyperlink>"; return (Hyperlink)XmlUtils.unmarshalString(hpl); }
protected void addHyperlinkToDocFrag(JaxbXmlPart sourcePart, DocumentFragment docfrag, String url) throws Docx4JException { if (url.startsWith("http") || url.startsWith("mailto")) { super.addHyperlinkToDocFrag(sourcePart, docfrag, url); } else { // Handle eg #/components/schemas/Pets String bookmarkName = refToBookmarkName(url); try { Document tmpDoc = XmlUtils.marshaltoW3CDomDocument( BindingHandler.getHyperlinkResolver().generateHyperlink(bookmarkName, url)); XmlUtils.treeCopy(tmpDoc.getDocumentElement(), docfrag); } catch (JAXBException e) { throw new Docx4JException(e.getMessage(), e); } } }
/** * Return the index of the position you wish to treat as a hyperlink. * The characters from there to the first whitespace following will be treated as * the URL * * @param text * @return */ public int getIndexOfURL(String text) { /* For OpenAPI, treat a string starting with: * * #/components * * as a hyperlink to a bookmark. */ if (text.startsWith(COMPONENT_REF)) return 0; return super.getIndexOfURL(text); }
getHyperlinkResolver().activateHyperlinkStyle(wordMLPackage);
/** * @return the hyperlinkResolver * @since 3.0.0 */ public static BindingHyperlinkResolver getHyperlinkResolver() { if (hyperlinkResolver==null) { hyperlinkResolver = new BindingHyperlinkResolver(); } return hyperlinkResolver; } /**
/** * Enable the hyperlinkStyle in the docx. * * @param wordMLPackage * @param hyperlinkStyleId */ public void activateHyperlinkStyle(WordprocessingMLPackage wordMLPackage) { if (hyperlinkStyleId !=null) { wordMLPackage.getMainDocumentPart().getPropertyResolver().activateStyle(getHyperlinkStyleId()); } }
protected void addHyperlinkToDocFrag(JaxbXmlPart sourcePart, DocumentFragment docfrag, String url) throws Docx4JException { // We need to add a relationship to word/_rels/document.xml.rels // but since its external, we don't use the // usual wordMLPackage.getMainDocumentPart().addTargetPart // mechanism org.docx4j.relationships.ObjectFactory factory = new org.docx4j.relationships.ObjectFactory(); org.docx4j.relationships.Relationship rel = factory.createRelationship(); rel.setType( Namespaces.HYPERLINK ); rel.setTarget(url); rel.setTargetMode("External"); sourcePart.getRelationshipsPart().addRelationship(rel); // addRelationship sets the rel's @Id try { Document tmpDoc = XmlUtils.marshaltoW3CDomDocument( BindingHandler.getHyperlinkResolver().generateHyperlink(rel.getId(), url)); XmlUtils.treeCopy(tmpDoc.getDocumentElement(), docfrag); } catch (JAXBException e) { throw new Docx4JException(e.getMessage(), e); } }
private void processString(JaxbXmlPart sourcePart, List<Object> contents, String text, SdtPr sdtPr, RPr rPr) throws JAXBException { int pos = BindingHandler.getHyperlinkResolver().getIndexOfURL(text); if (pos==-1 || BindingHandler.getHyperlinkStyleId() == null) { addRunToDocFrag(sourcePart, contents, text, rPr);
public void applyBindings() throws Docx4JException { // A component can apply in both the main document part, // and in headers/footers. See further // http://forums.opendope.org/Support-components-in-headers-footers-tp2964174p2964174.html getHyperlinkResolver().activateHyperlinkStyle(wordMLPackage); applyBindings(wordMLPackage.getMainDocumentPart()); // Add headers/footers RelationshipsPart rp = wordMLPackage.getMainDocumentPart() .getRelationshipsPart(); for (Relationship r : rp.getRelationships().getRelationship()) { if (r.getType().equals(Namespaces.HEADER)) { applyBindings((HeaderPart) rp.getPart(r)); } else if (r.getType().equals(Namespaces.FOOTER)) { applyBindings((FooterPart) rp.getPart(r)); } } }