Example usage for org.apache.poi.xwpf.usermodel XWPFRun getParagraph

List of usage examples for org.apache.poi.xwpf.usermodel XWPFRun getParagraph

Introduction

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

Prototype

@Deprecated
public XWPFParagraph getParagraph() 

Source Link

Document

Get the currently referenced paragraph, or null if a SDT object

Usage

From source file:com.maxl.java.aips2sqlite.PseudoExpertInfo.java

License:Open Source License

/**
 * Extracts all the important information from the pseudo "Fachinfo" file
 * @param pseudo_info_file// w  w w. ja v  a2 s  .c om
 */
public boolean extractInfo(int idx, FileInputStream pseudo_info_file) {
    mMedi = new MedicalInformations.MedicalInformation();

    mSectionContent = new ArrayList<String>();
    mSectionTitles = new ArrayList<String>();
    mBarCodes = new ArrayList<String>();
    m_list_of_packages = new ArrayList<String>();

    String mediTitle = "";
    String mediAuthor = "";
    String mediPseudoTag = "";
    String mediHtmlContent = "";

    StringBuilder content = new StringBuilder();

    try {
        // Read in docx file
        XWPFDocument docx = new XWPFDocument(pseudo_info_file);
        // Get iterator through all paragraphs
        Iterator<XWPFParagraph> para = docx.getParagraphsIterator();

        // Pre-process input stream to extract paragraph titles
        boolean goodToGo = false;
        while (para.hasNext()) {
            List<XWPFRun> runs = para.next().getRuns();
            if (!runs.isEmpty()) {
                for (XWPFRun r : runs) {
                    // bold and italics identifies section title!
                    if (r.isBold()) { // && r.isItalic()) {
                        String pText = r.getParagraph().getText();
                        // These are the first chapter titles (DE and FR)
                        if (pText.equals("Zusammensetzung") || pText.equals("Composition"))
                            goodToGo = true;
                        if (goodToGo == true)
                            mSectionTitles.add(pText);
                    }
                }
            }
        }
        // Add "nil" at the end
        mSectionTitles.add("nil");

        if (mLanguage.equals("de") && !mSectionTitles.get(0).equals("Zusammensetzung"))
            return false;
        if (mLanguage.equals("fr") && !mSectionTitles.get(0).equals("Composition"))
            return false;

        // Reset iterator
        para = docx.getParagraphsIterator();

        // Init list for section content 
        for (int i = 0; i < mSectionTitles.size(); ++i)
            mSectionContent.add(i, "");

        // Get title
        if (para.hasNext())
            mediTitle = para.next().getParagraphText();
        // Get author while using "Medizinprodukt" as tag
        String prevParaText = "";
        while (para.hasNext()) {
            String paraText = para.next().getParagraphText();
            // If this word is not found, then no pseudo FI will be produced
            if (paraText.equals("Medizinprodukt") || paraText.equals("Dispositif mdical")) {
                mediPseudoTag = paraText;
                mediAuthor = prevParaText;
                break;
            }
            prevParaText = paraText;
        }

        // Get section titles + sections + ean codes
        boolean isSectionPackungen = false;
        int numSection = 0;
        // Init with section1 and title
        String sectionId_str = "";
        String sectionTitle_str = "";
        mEanCodes_str = "";
        mSectionIds_str = "section1,";
        mSectionTitles_str = mediTitle + ",";
        m_pack_info_str = "";
        // This is the EAN code pattern
        Pattern pattern = Pattern.compile("^[0-9]{13}");
        // Loop through it, identifying medication title, author, section titles and corresponding titles
        while (para.hasNext()) {
            String paraText = para.next().getParagraphText();
            if (paraText.equals(mSectionTitles.get(numSection))) {
                // ->> Get section title
                isSectionPackungen = false;
                // Get section title
                if (numSection < mSectionTitles.size())
                    numSection++;
                // Section "Packungen" is special
                if (paraText.equals("Packungen") || paraText.equals("Prsentation")) {
                    isSectionPackungen = true;
                }
                // Close previous div
                if (numSection > 1)
                    content.append("</div>");
                // Create html
                sectionId_str = "section" + (numSection + 1); // section1 is reserved for the MonTitle
                sectionTitle_str = mSectionTitles.get(numSection - 1);
                content.append("<div class=\"paragraph\" id=\"" + sectionId_str + "\">");
                content.append("<div class=\"absTitle\">" + sectionTitle_str + "</div>");
                // Generate section id string
                mSectionIds_str += (sectionId_str + ",");
                // Generate titles string
                mSectionTitles_str += (sectionTitle_str + ";");
            } else {
                // ->> Get section content
                String s = mSectionContent.get(numSection - 1);
                mSectionContent.set(numSection - 1, s + paraText + " ");
                // Create html
                content.append("<p class=\"spacing1\">" + paraText + "</p>");
                // Extract EAN codes and start positions
                Matcher matcher = pattern.matcher(paraText);
                while (matcher.find()) {
                    String eanCode = matcher.group();
                    mEanCodes_str += (eanCode + ", ");
                    if (!eanCode.isEmpty()) {
                        String pup = "";
                        String efp = "";
                        String fep = "";
                        String fap = "";
                        String vat = "";
                        String size = "";
                        String units = "";
                        String swissmedic_cat = "";
                        String pharma_code = "";
                        int visible = 0xff;
                        int has_free_samples = 0x00; // by default no free samples
                        // Exctract fep and fap pricing information
                        // FAP = Fabrikabgabepreis = EFP?
                        // FEP = Fachhandelseinkaufspreis
                        // EFP = FAP < FEP < PUP                     
                        if (m_map_products != null && eanCode != null && m_map_products.containsKey(eanCode)) {
                            Product product = m_map_products.get(eanCode);
                            if (product.efp > 0.0f)
                                efp = String.format("CHF %.2f", product.efp);
                            if (product.pp > 0.0f)
                                pup = String.format("CHF %.2f", product.pp);
                            if (product.fap > 0.0f)
                                fap = String.format("CHF %.2f", product.fap);
                            if (product.fep > 0.0f)
                                fep = String.format("CHF %.2f", product.fep);
                            if (product.vat > 0.0f)
                                vat = String.format("%.2f", product.vat);
                            if (product.size != null && !product.size.isEmpty())
                                size = product.size;
                            if (product.units != null && product.units.length > 0)
                                units = product.units[0];
                            if (product.swissmedic_cat != null && !product.swissmedic_cat.isEmpty())
                                swissmedic_cat = product.swissmedic_cat;
                            if (product.pharmacode != null && !product.pharmacode.isEmpty())
                                pharma_code = product.pharmacode;
                            visible = product.visible;
                            has_free_samples = product.free_sample;
                        }
                        m_list_of_packages.add(mediTitle.toUpperCase() + ", " + units + ", " + size + "|" + size
                                + "|" + units + "|" + efp + "|" + pup + "|" + fap + "|" + fep + "|" + vat + "|"
                                + swissmedic_cat + ",,|" + eanCode + "|" + pharma_code + "|" + visible + "|"
                                + has_free_samples + "\n");
                        // Generate bar codes
                        BarCode bc = new BarCode();
                        String barcodeImg64 = bc.encode(eanCode);
                        mBarCodes.add("<p class=\"spacing1\">" + barcodeImg64 + "</p>");
                        content.append(barcodeImg64);
                    }
                }
                // Generate section Packungen for search result
                if (isSectionPackungen)
                    m_pack_info_str += (paraText + "\n");
            }
        }
        /*
        // Add chapter "Barcodes"
        content.append("<p class=\"paragraph\"></p><div class=\"absTitle\">" + "Barcodes" + "</div>");
        for (String bcode : mBarCodes)
           content.append(bcode);
        */
        // Remove last comma from mEanCodes_str
        if (!mEanCodes_str.isEmpty())
            mEanCodes_str = mEanCodes_str.substring(0, mEanCodes_str.length() - 2);
        // Remove last \n from mSectionPackungen_str
        if (!m_pack_info_str.isEmpty())
            m_pack_info_str = m_pack_info_str.substring(0, m_pack_info_str.length() - 1);

        // Set title, autor
        mMedi.setTitle(mediTitle);
        mMedi.setAuthHolder(mediAuthor);
        mMedi.setAtcCode("PSEUDO");
        mMedi.setSubstances(mediTitle);

        System.out.println(idx + " - " + mediTitle + ": " + mEanCodes_str);

        // Close previous div + monographie div
        content.append("</div></div>");
        String title = "<div class=\"MonTitle\" id=\"section1\">" + mediTitle + "</div>";
        String author = "<div class=\"ownerCompany\"><div style=\"text-align: right;\">" + mediAuthor
                + "</div></div>";
        // Set "Medizinprodukt" label
        String pseudo = "<p class=\"spacing1\">" + mediPseudoTag + "</p>";
        // Set medi content         
        mediHtmlContent = "<html><head></head><body><div id=\"monographie\">" + title + author + pseudo
                + content.toString() + "</div></body></html>";

        // Generate clean html file
        Document doc = Jsoup.parse(mediHtmlContent);
        doc.outputSettings().escapeMode(EscapeMode.xhtml);
        doc.outputSettings().charset("UTF-8");
        doc.outputSettings().prettyPrint(true);
        doc.outputSettings().indentAmount(1);
        mediHtmlContent = doc.html();

        // Set html content
        mMedi.setContent(mediHtmlContent);

        // Add to DB
        addToDB();

        return true;
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
}

From source file:com.qihang.winter.poi.word.parse.ParseWord07.java

License:Apache License

/**
 * //  w ww. ja v a2 s  . c o  m
 * 
 * @author Zerrion
 * @date 2013-11-20
 * @param obj
 * @param currentRun
 * @throws Exception
 */
private void addAnImage(WordImageEntity obj, XWPFRun currentRun) throws Exception {
    Object[] isAndType = PoiPublicUtil.getIsAndType(obj);
    String picId;
    try {
        picId = currentRun.getParagraph().getDocument().addPictureData((byte[]) isAndType[0],
                (Integer) isAndType[1]);
        ((MyXWPFDocument) currentRun.getParagraph().getDocument()).createPicture(currentRun, picId,
                currentRun.getParagraph().getDocument().getNextPicNameNumber((Integer) isAndType[1]),
                obj.getWidth(), obj.getHeight());

    } catch (Exception e) {
        LOGGER.error(e.getMessage(), e);
    }

}

From source file:fr.opensagres.poi.xwpf.converter.core.styles.run.AbstractRunValueProvider.java

License:Open Source License

@Override
protected String[] getStyleID(XWPFRun run) {
    XWPFParagraph paragraph = run.getParagraph();
    List<String> styleIDs = StylesHelper.getStyleIDs(paragraph);
    CTRPr rPr = getRPr(run);/*from www.  j  av a2s  . co  m*/
    if (rPr != null) {
        CTString style = rPr.getRStyle();
        if (style != null) {
            if (styleIDs == null) {
                styleIDs = new ArrayList<String>();
            }
            styleIDs.add(0, style.getVal());
        }
    }
    if (styleIDs != null) {
        return styleIDs.toArray(StringUtils.EMPTY_STRING_ARRAY);
    }
    return null;
}

From source file:fr.opensagres.poi.xwpf.converter.core.styles.run.AbstractRunValueProvider.java

License:Open Source License

@Override
protected XWPFTableCell getParentTableCell(XWPFRun run) {
    return StylesHelper.getEmbeddedTableCell(run.getParagraph());
}

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

License:Open Source License

protected void visitRun(XWPFRun run, boolean pageNumber, String url, T paragraphContainer) throws Exception

{
    CTR ctr = run.getCTR();//from www . j ava  2s  .  c om
    CTRPr rPr = ctr.getRPr();
    boolean hasTexStyles = rPr != null && (rPr.getHighlight() != null || rPr.getStrike() != null
            || rPr.getDstrike() != null || rPr.getVertAlign() != null);
    StringBuilder text = new StringBuilder();

    // Loop for each element of <w:run text, tab, image etc
    // to keep the order of thoses elements.
    XmlCursor c = ctr.newCursor();
    c.selectPath("./*");
    while (c.toNextSelection()) {
        XmlObject o = c.getObject();

        if (o instanceof CTText) {
            CTText ctText = (CTText) o;
            String tagName = o.getDomNode().getNodeName();
            // Field Codes (w:instrText, defined in spec sec. 17.16.23)
            // come up as instances of CTText, but we don't want them
            // in the normal text output
            if ("w:instrText".equals(tagName)) {

            } else {
                if (hasTexStyles) {
                    text.append(ctText.getStringValue());
                } else {
                    visitText(ctText, pageNumber, paragraphContainer);
                }
            }
        } else if (o instanceof CTPTab) {
            visitTab((CTPTab) o, paragraphContainer);
        } else if (o instanceof CTBr) {
            visitBR((CTBr) o, paragraphContainer);
        } else if (o instanceof CTEmpty) {
            // Some inline text elements get returned not as
            // themselves, but as CTEmpty, owing to some odd
            // definitions around line 5642 of the XSDs
            // This bit works around it, and replicates the above
            // rules for that case
            String tagName = o.getDomNode().getNodeName();
            if ("w:tab".equals(tagName)) {
                CTTabs tabs = stylesDocument.getParagraphTabs(run.getParagraph());
                visitTabs(tabs, paragraphContainer);
            }
            if ("w:br".equals(tagName)) {
                visitBR(null, paragraphContainer);
            }
            if ("w:cr".equals(tagName)) {
                visitBR(null, paragraphContainer);
            }
        } else if (o instanceof CTDrawing) {
            visitDrawing((CTDrawing) o, paragraphContainer);
        }
    }
    if (hasTexStyles && StringUtils.isNotEmpty(text.toString())) {
        visitStyleText(run, text.toString());
    }
    c.dispose();
}

From source file:fr.opensagres.poi.xwpf.converter.xhtml.internal.XHTMLMapper.java

License:Open Source License

@Override
protected void visitRun(XWPFRun run, boolean pageNumber, String url, Object paragraphContainer)
        throws Exception {
    if (run.getParent() instanceof XWPFParagraph) {
        this.currentParagraph = (XWPFParagraph) run.getParent();
    }/*from   w  w w .j  av a  2  s. c o  m*/

    XWPFParagraph paragraph = run.getParagraph();
    // 1) create attributes

    // 1.1) Create "class" attributes.
    this.currentRunAttributes = createClassAttribute(paragraph.getStyleID());

    // 1.2) Create "style" attributes.
    CTRPr rPr = run.getCTR().getRPr();
    CSSStyle cssStyle = getStylesDocument().createCSSStyle(rPr);
    if (cssStyle != null) {
        cssStyle.addProperty(CSSStylePropertyConstants.WHITE_SPACE, "pre-wrap");
    }
    this.currentRunAttributes = createStyleAttribute(cssStyle, currentRunAttributes);

    if (url != null) {
        // url is not null, generate a HTML a.
        AttributesImpl hyperlinkAttributes = new AttributesImpl();
        SAXHelper.addAttrValue(hyperlinkAttributes, HREF_ATTR, url);
        startElement(A_ELEMENT, hyperlinkAttributes);
    }

    super.visitRun(run, pageNumber, url, paragraphContainer);

    if (url != null) {
        // url is not null, close the HTML a.
        // TODO : for the moment generate space to be ensure that a has some content.
        characters(" ");
        endElement(A_ELEMENT);
    }
    this.currentRunAttributes = null;
    this.currentParagraph = null;
}

From source file:org.articleEditor.insertContent.DocumentUpdater1.java

License:Apache License

public void applyAttributes(XWPFRun run, AttributeSet attributes) {
    Enumeration attributeNames = attributes.getAttributeNames();
    while (attributeNames.hasMoreElements()) {
        Object attributeName = attributeNames.nextElement();
        Object attributeValue = attributes.getAttribute(attributeName);
        if (attributeName == Bold) {
            run.setBold((Boolean) attributeValue);
        } else if (attributeName == Italic) {
            run.setItalic((Boolean) attributeValue);
        } else if (attributeName == Underline) {
            run.setUnderline((Boolean) attributeValue ? UnderlinePatterns.SINGLE : UnderlinePatterns.NONE);
        } else if (attributeName == FontFamily || attributeName == Family) {
            run.setFontFamily((String) attributeValue);
        } else if (attributeName == FontSize) {
            run.setFontSize(((Number) attributeValue).intValue());
        } else if (attributeName == Foreground) {
            Color color = (Color) attributeValue;
            String rgb = Integer.toHexString((color.getRGB() & 0xffffff) | 0x1000000).substring(1);
            run.setColor(rgb);/*w ww  .  ja  va  2 s  .com*/
        } else if (attributeName == Background) {
            Color color = (Color) attributeValue;
            String rgb = Integer.toHexString((color.getRGB() & 0xffffff) | 0x1000000).substring(1);
            //run.getCTR().getRPr().setHighlight();
        } else if (attributeName == Subscript) {
            run.setSubscript((Boolean) attributeValue ? VerticalAlign.SUBSCRIPT : VerticalAlign.BASELINE);
        } else if (attributeName == Superscript) {
            run.setSubscript((Boolean) attributeValue ? VerticalAlign.SUPERSCRIPT : VerticalAlign.BASELINE);
        } else if (attributeName == StrikeThrough) {
            run.setStrike((Boolean) attributeValue);
        } else if (attributeName == Alignment) {
            ParagraphAlignment alignment = documentToPOI((Integer) attributeValue);
            run.getParagraph().setAlignment(alignment);
        }
    }
}

From source file:org.obeonetwork.m2doc.generator.TemplateProcessor.java

License:Open Source License

/**
 * Inserts a run in the generated document. The new run is a copy from the specified run.
 * /*w  w  w.  j a  v a  2 s .c  om*/
 * @param srcRun
 *            the run to copy
 * @return the inserted run.
 */
@SuppressWarnings("deprecation")
private XWPFRun insertRun(XWPFRun srcRun) {
    if (srcRun.getParagraph() != currentTemplateParagraph || forceNewParagraph) {
        createNewParagraph(srcRun.getParagraph());
        forceNewParagraph = false;
    }
    XWPFRun generatedRun = currentGeneratedParagraph.createRun();
    generatedRun.getCTR().set(srcRun.getCTR());
    return generatedRun;
}

From source file:org.obeonetwork.m2doc.generator.TemplateProcessor.java

License:Open Source License

/**
 * Inserts a run in the generated document and set it's text to the specified replacement. The new run is a copy from the specified run.
 * /* w  ww .  ja v a2 s . co m*/
 * @param srcRun
 *            the run to copy
 * @param replacement
 *            the text to set
 * @return the inserted run
 */
private XWPFRun insertFieldRunReplacement(XWPFRun srcRun, String replacement) {
    if (srcRun.getParagraph() != currentTemplateParagraph || forceNewParagraph) {
        createNewParagraph(srcRun.getParagraph());
        forceNewParagraph = false;
    }
    // creates as many paragraphs as there are '\n's in the string.
    String[] fragments = replacement.split("\n");
    XWPFRun result = null;
    int size = fragments.length;
    for (int i = 0; i < size - 1; i++) {
        XWPFRun generatedRun = insertFragment(fragments[i], srcRun);
        if (result == null) {
            result = generatedRun;
        }
        createNewParagraph(srcRun.getParagraph());
    }
    if (size > 0) {
        XWPFRun generatedRun = insertFragment(fragments[size - 1], srcRun);
        if (result == null) {
            result = generatedRun;
        }
    }
    return result;
}