Example usage for org.apache.poi.xwpf.usermodel XWPFHyperlink getURL

List of usage examples for org.apache.poi.xwpf.usermodel XWPFHyperlink getURL

Introduction

In this page you can find the example usage for org.apache.poi.xwpf.usermodel XWPFHyperlink getURL.

Prototype

public String getURL() 

Source Link

Usage

From source file:fr.opensagres.poi.xwpf.converter.core.XWPFDocumentVisitor.java

License:Open Source License

private void visitRun(XWPFParagraph paragraph, XmlObject o, T paragraphContainer) throws Exception {
    if (o instanceof CTHyperlink) {
        CTHyperlink link = (CTHyperlink) o;
        String anchor = link.getAnchor();
        String href = null;/*from   w  w  w .  j a v  a  2  s  .c  o  m*/
        // Test if the is an id for hyperlink
        String hyperlinkId = link.getId();
        if (StringUtils.isNotEmpty(hyperlinkId)) {
            XWPFHyperlink hyperlink = document.getHyperlinkByID(hyperlinkId);
            href = hyperlink != null ? hyperlink.getURL() : null;
        }
        for (CTR r : link.getRList()) {
            XWPFRun run = new XWPFHyperlinkRun(link, r, paragraph);
            visitRun(run, false, href != null ? href : "#" + anchor, paragraphContainer);
        }
    } else if (o instanceof CTSdtRun) {
        CTSdtContentRun run = ((CTSdtRun) o).getSdtContent();
        for (CTR r : run.getRList()) {
            XWPFRun ru = new XWPFRun(r, paragraph);
            visitRun(ru, false, null, paragraphContainer);
        }
    } else if (o instanceof CTRunTrackChange) {
        for (CTR r : ((CTRunTrackChange) o).getRList()) {
            XWPFRun run = new XWPFRun(r, paragraph);
            visitRun(run, false, null, paragraphContainer);
        }
    } else if (o instanceof CTSimpleField) {
        CTSimpleField simpleField = (CTSimpleField) o;
        String instr = simpleField.getInstr();
        // 1) test if it's page number
        // <w:fldSimple w:instr=" PAGE \* MERGEFORMAT "> <w:r> <w:rPr> <w:noProof/>
        // </w:rPr> <w:t>- 1 -</w:t> </w:r> </w:fldSimple>
        boolean fieldPageNumber = XWPFRunHelper.isInstrTextPage(instr);
        String fieldHref = null;
        if (!fieldPageNumber) {
            // not page number, test if it's hyperlink :
            // <w:instrText>HYPERLINK "http://code.google.com/p/xdocrepor"</w:instrText>
            fieldHref = XWPFRunHelper.getInstrTextHyperlink(instr);
        }
        for (CTR r : simpleField.getRList()) {
            XWPFRun run = new XWPFRun(r, paragraph);
            visitRun(run, fieldPageNumber, fieldHref, paragraphContainer);
        }
    } else if (o instanceof CTSmartTagRun) {
        // Smart Tags can be nested many times.
        // This implementation does not preserve the tagging information
        // buildRunsInOrderFromXml(o);
    } else if (o instanceof CTBookmark) {
        CTBookmark bookmark = (CTBookmark) o;
        visitBookmark(bookmark, paragraph, paragraphContainer);
    }
}

From source file:mj.ocraptor.extraction.tika.parser.microsoft.ooxml.XWPFWordExtractorDecorator.java

License:Apache License

private TmpFormatting processRun(XWPFRun run, XWPFParagraph paragraph, XHTMLContentHandler xhtml,
        TmpFormatting tfmtg) throws SAXException, XmlException, IOException {
    // True if we are currently in the named style tag:
    if (run.isBold() != tfmtg.isBold()) {
        if (tfmtg.isItalic()) {
            xhtml.endElement("i");
            tfmtg.setItalic(false);//  ww w.  j a  v a2 s .c  o m
        }
        if (run.isBold()) {
            xhtml.startElement("b");
        } else {
            xhtml.endElement("b");
        }
        tfmtg.setBold(run.isBold());
    }

    if (run.isItalic() != tfmtg.isItalic()) {
        if (run.isItalic()) {
            xhtml.startElement("i");
        } else {
            xhtml.endElement("i");
        }
        tfmtg.setItalic(run.isItalic());
    }

    boolean addedHREF = false;
    if (run instanceof XWPFHyperlinkRun) {
        XWPFHyperlinkRun linkRun = (XWPFHyperlinkRun) run;
        XWPFHyperlink link = linkRun.getHyperlink(document);
        if (link != null && link.getURL() != null) {
            xhtml.startElement("a", "href", link.getURL());
            addedHREF = true;
        } else if (linkRun.getAnchor() != null && linkRun.getAnchor().length() > 0) {
            xhtml.startElement("a", "href", "#" + linkRun.getAnchor());
            addedHREF = true;
        }
    }

    xhtml.characters(run.toString());

    // If we have any pictures, output them
    for (XWPFPicture picture : run.getEmbeddedPictures()) {
        if (paragraph.getDocument() != null) {
            XWPFPictureData data = picture.getPictureData();
            if (data != null) {
                AttributesImpl attr = new AttributesImpl();

                attr.addAttribute("", "src", "src", "CDATA", "embedded:" + data.getFileName());
                attr.addAttribute("", "alt", "alt", "CDATA", picture.getDescription());

                xhtml.startElement("img", attr);
                xhtml.endElement("img");
            }
        }
    }

    if (addedHREF) {
        xhtml.endElement("a");
    }

    return tfmtg;
}