diff options
Diffstat (limited to 'src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java')
-rw-r--r-- | src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java | 189 |
1 files changed, 111 insertions, 78 deletions
diff --git a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java index c56aef00d4..f0dc207081 100644 --- a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java +++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java @@ -18,89 +18,83 @@ package org.apache.poi.xwpf.usermodel; import java.util.ArrayList; -import org.apache.poi.xwpf.XWPFDocument; -import org.apache.poi.xwpf.model.XMLParagraph; import org.apache.xmlbeans.XmlCursor; import org.apache.xmlbeans.XmlObject; -import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP; -import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPTab; -import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPicture; -import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR; -import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRPr; -import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSdtBlock; -import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSdtContentRun; -import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSdtRun; -import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText; +import org.openxmlformats.schemas.wordprocessingml.x2006.main.*; import org.w3c.dom.NodeList; import org.w3c.dom.Text; /** * Sketch of XWPF paragraph class */ -public class XWPFParagraph extends XMLParagraph +public class XWPFParagraph { - protected XWPFDocument docRef; // XXX: we'd like to have access to document's hyperlink, comments and other tables + private CTP paragraph; + protected XWPFDocument document; // XXX: we'd like to have access to document's hyperlink, comments and other tables /** * TODO - replace with RichText String */ private StringBuffer text = new StringBuffer(); private StringBuffer pictureText = new StringBuffer(); - public XWPFParagraph(CTP prgrph, XWPFDocument docRef) + protected XWPFParagraph(CTP prgrph, XWPFDocument docRef) { - super(prgrph); - this.docRef = docRef; - - // All the runs to loop over - // TODO - replace this with some sort of XPath expression - // to directly find all the CTRs, in the right order - ArrayList<CTR> rs = new ArrayList<CTR>(); - CTR[] tmp; - - // Get the main text runs - tmp = paragraph.getRArray(); - for(int i=0; i<tmp.length; i++) { - rs.add(tmp[i]); - } - - // Not sure quite what these are, but they hold - // more text runs - CTSdtRun[] sdts = paragraph.getSdtArray(); - for(int i=0; i<sdts.length; i++) { - CTSdtContentRun run = sdts[i].getSdtContent(); - tmp = run.getRArray(); - for(int j=0; j<tmp.length; j++) { - rs.add(tmp[j]); + this.paragraph = prgrph; + this.document = docRef; + + if(!isEmpty()) { + // All the runs to loop over + // TODO - replace this with some sort of XPath expression + // to directly find all the CTRs, in the right order + ArrayList<CTR> rs = new ArrayList<CTR>(); + CTR[] tmp; + + // Get the main text runs + tmp = paragraph.getRArray(); + for(int i=0; i<tmp.length; i++) { + rs.add(tmp[i]); } - } - - - // Get text of the paragraph - for (int j = 0; j < rs.size(); j++) { - // Grab the text and tabs of the paragraph - // Do so in a way that preserves the ordering - XmlCursor c = rs.get(j).newCursor(); - c.selectPath( "./*" ); - while(c.toNextSelection()) { - XmlObject o = c.getObject(); - if(o instanceof CTText) { - text.append( ((CTText)o).getStringValue() ); - } - if(o instanceof CTPTab) { - text.append("\t"); - } - } - - // Loop over pictures inside our - // paragraph, looking for text in them - CTPicture[] picts = rs.get(j).getPictArray(); - for (int k = 0; k < picts.length; k++) { - XmlObject[] t = picts[k].selectPath("declare namespace w='http://schemas.openxmlformats.org/wordprocessingml/2006/main' .//w:t"); - for (int m = 0; m < t.length; m++) { - NodeList kids = t[m].getDomNode().getChildNodes(); - for (int n = 0; n < kids.getLength(); n++) { - if (kids.item(n) instanceof Text) { - pictureText.append("\n" + kids.item(n).getNodeValue()); + + // Not sure quite what these are, but they hold + // more text runs + CTSdtRun[] sdts = paragraph.getSdtArray(); + for(int i=0; i<sdts.length; i++) { + CTSdtContentRun run = sdts[i].getSdtContent(); + tmp = run.getRArray(); + for(int j=0; j<tmp.length; j++) { + rs.add(tmp[j]); + } + } + + + // Get text of the paragraph + for (int j = 0; j < rs.size(); j++) { + // Grab the text and tabs of the paragraph + // Do so in a way that preserves the ordering + XmlCursor c = rs.get(j).newCursor(); + c.selectPath( "./*" ); + while(c.toNextSelection()) { + XmlObject o = c.getObject(); + if(o instanceof CTText) { + text.append( ((CTText)o).getStringValue() ); + } + if(o instanceof CTPTab) { + text.append("\t"); + } + } + + // Loop over pictures inside our + // paragraph, looking for text in them + CTPicture[] picts = rs.get(j).getPictArray(); + for (int k = 0; k < picts.length; k++) { + XmlObject[] t = picts[k].selectPath("declare namespace w='http://schemas.openxmlformats.org/wordprocessingml/2006/main' .//w:t"); + for (int m = 0; m < t.length; m++) { + NodeList kids = t[m].getDomNode().getChildNodes(); + for (int n = 0; n < kids.getLength(); n++) { + if (kids.item(n) instanceof Text) { + pictureText.append("\n"); + pictureText.append(kids.item(n).getNodeValue()); + } } } } @@ -108,22 +102,16 @@ public class XWPFParagraph extends XMLParagraph } } - public XWPFParagraph(CTP prgrph) { - this(prgrph, null); + public CTP getCTP() { + return paragraph; } - - public XWPFParagraph(XMLParagraph paragraph) { - this(paragraph.getCTP()); - } - - + public boolean isEmpty() { return !paragraph.getDomNode().hasChildNodes(); } - - - public XWPFDocument getDocRef() { - return docRef; + + public XWPFDocument getDocument() { + return document; } /** @@ -147,4 +135,49 @@ public class XWPFParagraph extends XMLParagraph public String getPictureText() { return pictureText.toString(); } + + /** + * Appends a new run to this paragraph + * + * @return a new text run + */ + public XWPFRun createRun(){ + return new XWPFRun(paragraph.addNewR(), this); + } + + /** + * Returns the paragraph alignment which shall be applied to text in this paragraph. + * + * <p> + * If this element is not set on a given paragraph, its value is determined by the setting previously + * set at any level of the style hierarchy (i.e. that previous setting remains unchanged). + * If this setting is never specified in the style hierarchy, then no alignment is applied to the paragraph. + * </p> + * + * @return the paragraph alignment of this paragraph. + */ + public ParagraphAlignment getAlignment(){ + CTPPr pr = paragraph.getPPr(); + return pr == null || !pr.isSetJc() ? ParagraphAlignment.LEFT : + ParagraphAlignment.valueOf(pr.getJc().getVal().intValue()); + } + + /** + * Specifies the paragraph alignment which shall be applied to text in this paragraph. + * + * <p> + * If this element is not set on a given paragraph, its value is determined by the setting previously + * set at any level of the style hierarchy (i.e. that previous setting remains unchanged). + * If this setting is never specified in the style hierarchy, then no alignment is applied to the paragraph. + * </p> + * + * @param align the paragraph alignment to apply to this paragraph. + */ + public void setAlignment(ParagraphAlignment align){ + CTPPr pr = paragraph.isSetPPr() ? paragraph.getPPr() : paragraph.addNewPPr(); + CTJc jc = pr.isSetJc() ? pr.getJc() : pr.addNewJc(); + STJc.Enum en = STJc.Enum.forInt(align.getValue()); + jc.setVal(en); + } + } |