--- src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRun.java (revision 720187) +++ src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRun.java (working copy) @@ -18,6 +18,7 @@ import java.math.BigInteger; +import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBr; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFonts; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHpsMeasure; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTOnOff; @@ -27,6 +28,8 @@ import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTUnderline; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTVerticalAlignRun; +import org.openxmlformats.schemas.wordprocessingml.x2006.main.STBrClear; +import org.openxmlformats.schemas.wordprocessingml.x2006.main.STBrType; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STOnOff; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STUnderline; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STVerticalAlignRun; @@ -49,10 +52,18 @@ this.paragraph = p; } + /** + * Get the currently used CTR object + * @return ctr object + */ public CTR getCTR() { return run; } + /** + * Get the currenty referenced paragraph object + * @return current paragraph + */ public XWPFParagraph getParagraph() { return paragraph; } @@ -70,9 +81,8 @@ /** * Whether the bold property shall be applied to all non-complex script - * characters in the contents of this run when displayed in a document - *
- * + * characters in the contents of this run when displayed in a document. + ** This formatting property is a toggle property, which specifies that its * behavior differs between its use within a style definition and its use as * direct formatting. When used as part of a style definition, setting this @@ -83,7 +93,7 @@ * direct formatting, setting this property to true or false shall set the * absolute state of the resulting property. *
- * + *
* If this element is not present, the default value is to leave the
* formatting applied at previous level in the style hierarchy. If this
* element is never applied in the style hierarchy, then bold shall not be
@@ -104,8 +114,8 @@
*
* @return the text of this text run or null
if not set
*/
- public String getText() {
- return run.sizeOfTArray() == 0 ? null : run.getTArray(0)
+ public String getText(int pos) {
+ return run.sizeOfTArray() == 0 ? null : run.getTArray(pos)
.getStringValue();
}
@@ -115,10 +125,22 @@
* @param value the literal text which shall be displayed in the document
*/
public void setText(String value) {
- CTText t = run.sizeOfTArray() == 0 ? run.addNewT() : run.getTArray(0);
+ setText(value,run.getTArray().length);
+ }
+
+ /**
+ * Sets the text of this text run in the
+ *
+ * @param value the literal text which shall be displayed in the document
+ * @param pos - position in the text array (NB: 0 based)
+ */
+ public void setText(String value, int pos) {
+ if(pos > run.sizeOfTArray()) throw new ArrayIndexOutOfBoundsException("Value too large for the parameter position in XWPFRun.setText(String value,int pos)");
+ CTText t = (pos < run.sizeOfTArray() && pos >= 0) ? run.getTArray(pos) : run.addNewT();
t.setStringValue(value);
}
+
/**
* Whether the italic property should be applied to all non-complex script
* characters in the contents of this run when displayed in a document.
@@ -171,7 +193,7 @@
public UnderlinePatterns getUnderline() {
CTRPr pr = run.getRPr();
return (pr != null && pr.isSetU()) ? UnderlinePatterns.valueOf(pr
- .getU().getVal().intValue()) : null;
+ .getU().getVal().intValue()) : UnderlinePatterns.NONE;
}
/**
@@ -191,7 +213,7 @@
*/
public void setUnderline(UnderlinePatterns value) {
CTRPr pr = run.isSetRPr() ? run.getRPr() : run.addNewRPr();
- CTUnderline underline = pr.isSetU() ? pr.getU() : pr.addNewU();
+ CTUnderline underline = (pr.getU() == null) ? pr.addNewU() : pr.getU();
underline.setVal(STUnderline.Enum.forInt(value.getValue()));
}
@@ -248,7 +270,7 @@
public VerticalAlign getSubscript() {
CTRPr pr = run.getRPr();
return (pr != null && pr.isSetVertAlign()) ? VerticalAlign.valueOf(pr
- .getVertAlign().getVal().intValue()) : null;
+ .getVertAlign().getVal().intValue()) : VerticalAlign.BASELINE;
}
/**
@@ -305,9 +327,9 @@
*
* @return value representing the font size
*/
- public BigInteger getFontSize() {
+ public int getFontSize() {
CTRPr pr = run.getRPr();
- return (pr != null && pr.isSetSz()) ? pr.getSz().getVal().divide(new BigInteger("2")) : null;
+ return (pr != null && pr.isSetSz()) ? pr.getSz().getVal().divide(new BigInteger("2")).intValue() : -1;
}
/**
@@ -322,10 +344,11 @@
*
* @param size
*/
- public void setFontSize(BigInteger size) {
+ public void setFontSize(int size) {
+ BigInteger bint=new BigInteger(""+size);
CTRPr pr = run.isSetRPr() ? run.getRPr() : run.addNewRPr();
CTHpsMeasure ctSize = pr.isSetSz() ? pr.getSz() : pr.addNewSz();
- ctSize.setVal(size.multiply(new BigInteger("2")));
+ ctSize.setVal(bint.multiply(new BigInteger("2")));
}
/**
@@ -336,10 +359,10 @@
*
* @return a big integer representing the amount of text shall be "moved"
*/
- public BigInteger getTextPosition() {
+ public int getTextPosition() {
CTRPr pr = run.getRPr();
- return (pr != null && pr.isSetPosition()) ? pr.getPosition().getVal()
- : null;
+ return (pr != null && pr.isSetPosition()) ? pr.getPosition().getVal().intValue()
+ : -1;
}
/**
@@ -364,10 +387,87 @@
*
* @param val
*/
- public void setTextPosition(BigInteger val) {
+ public void setTextPosition(int val) {
+ BigInteger bint=new BigInteger(""+val);
CTRPr pr = run.isSetRPr() ? run.getRPr() : run.addNewRPr();
CTSignedHpsMeasure position = pr.isSetPosition() ? pr.getPosition() : pr.addNewPosition();
- position.setVal(val);
+ position.setVal(bint);
}
+ /**
+ *
+ */
+ public void removeBreak() {
+ // TODO
+ }
+
+ /**
+ * Specifies that a break shall be placed at the current location in the run
+ * content.
+ * A break is a special character which is used to override the
+ * normal line breaking that would be performed based on the normal layout
+ * of the documentÕs contents.
+ * @see addCarriageReturn()
+ */
+ public void addBreak() {
+ run.addNewBr();
+ }
+
+ /**
+ * Specifies that a break shall be placed at the current location in the run
+ * content.
+ * A break is a special character which is used to override the
+ * normal line breaking that would be performed based on the normal layout
+ * of the documentÕs contents.
+ *
+ * The behavior of this break character (the + * location where text shall be restarted after this break) shall be + * determined by its type values. + *
+ * @see BreakType + */ + public void addBreak(BreakType type){ + CTBr br=run.addNewBr(); + br.setType(STBrType.Enum.forInt(type.getValue())); + } + + + /** + * Specifies that a break shall be placed at the current location in the run + * content. A break is a special character which is used to override the + * normal line breaking that would be performed based on the normal layout + * of the documentÕs contents. + *+ * The behavior of this break character (the + * location where text shall be restarted after this break) shall be + * determined by its type (in this case is BreakType.TEXT_WRAPPING as default) and clear attribute values. + *
+ * @see BreakClear + */ + public void addBreak(BreakClear clear){ + CTBr br=run.addNewBr(); + br.setType(STBrType.Enum.forInt(BreakType.TEXT_WRAPPING.getValue())); + br.setClear(STBrClear.Enum.forInt(clear.getValue())); + } + + /** + * Specifies that a carriage return shall be placed at the + * current location in the run content. + * A carriage return is used to end the current line of text in + * Wordprocess. + * The behavior of a carriage return in run content shall be + * identical to a break character with null type and clear attributes, which + * shall end the current line and find the next available line on which to + * continue. + * The carriage return character forced the following text to be + * restarted on the next available line in the document. + */ + public void addCarriageReturn() { + run.addNewCr(); + } + + public void removeCarriageReturn() { + //TODO + } + } --- src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFRun.java (revision 720187) +++ src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFRun.java (working copy) @@ -20,8 +20,10 @@ import junit.framework.TestCase; +import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBr; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRPr; +import org.openxmlformats.schemas.wordprocessingml.x2006.main.STBrClear; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STOnOff; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STUnderline; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STVerticalAlignRun; @@ -39,9 +41,26 @@ p = doc.createParagraph(); this.ctRun = CTR.Factory.newInstance(); + + } + public void testSetGetText() { + ctRun.addNewT().setStringValue("TEST STRING"); + ctRun.addNewT().setStringValue("TEST2 STRING"); + ctRun.addNewT().setStringValue("TEST3 STRING"); + + assertEquals(3,ctRun.sizeOfTArray()); + XWPFRun run = new XWPFRun(ctRun, p); + + assertEquals("TEST2 STRING",run.getText(1)); + + run.setText("NEW STRING",0); + assertEquals("NEW STRING",run.getText(0)); + + //run.setText("xxx",14); + //fail("Position wrong"); } - + public void testSetGetBold() { CTRPr rpr = ctRun.addNewRPr(); rpr.addNewB().setVal(STOnOff.TRUE); @@ -118,9 +137,9 @@ rpr.addNewSz().setVal(new BigInteger("14")); XWPFRun run = new XWPFRun(ctRun, p); - assertEquals(7, run.getFontSize().longValue()); + assertEquals(7, run.getFontSize()); - run.setFontSize(new BigInteger("24")); + run.setFontSize(24); assertEquals(48, rpr.getSz().getVal().longValue()); } @@ -130,11 +149,48 @@ rpr.addNewPosition().setVal(new BigInteger("4000")); XWPFRun run = new XWPFRun(ctRun, p); - assertEquals(4000, run.getTextPosition().longValue()); + assertEquals(4000, run.getTextPosition()); - run.setTextPosition(new BigInteger("2400")); + run.setTextPosition(2400); assertEquals(2400, rpr.getPosition().getVal().longValue()); } + public void testAddCarriageReturn() { + + ctRun.addNewT().setStringValue("TEST STRING"); + ctRun.addNewCr(); + ctRun.addNewT().setStringValue("TEST2 STRING"); + ctRun.addNewCr(); + ctRun.addNewT().setStringValue("TEST3 STRING"); + assertEquals(2, ctRun.sizeOfCrArray()); + + XWPFRun run = new XWPFRun(CTR.Factory.newInstance(), p); + run.setText("T1"); + run.addCarriageReturn(); + run.addCarriageReturn(); + run.setText("T2"); + run.addCarriageReturn(); + assertEquals(3, run.getCTR().getCrArray().length); + + } + + public void testAddPageBreak() { + ctRun.addNewT().setStringValue("TEST STRING"); + ctRun.addNewBr(); + ctRun.addNewT().setStringValue("TEST2 STRING"); + CTBr breac=ctRun.addNewBr(); + breac.setClear(STBrClear.LEFT); + ctRun.addNewT().setStringValue("TEST3 STRING"); + assertEquals(2, ctRun.sizeOfBrArray()); + + XWPFRun run = new XWPFRun(CTR.Factory.newInstance(), p); + run.setText("TEXT1"); + run.addBreak(); + run.setText("TEXT2"); + run.addBreak(); + assertEquals(2, run.getCTR().sizeOfBrArray()); + } + + } --- src/examples/src/org/apache/poi/xwpf/usermodel/SimpleDocument.java (revision 720187) +++ src/examples/src/org/apache/poi/xwpf/usermodel/SimpleDocument.java (working copy) @@ -17,7 +17,6 @@ package org.apache.poi.xwpf.usermodel; import java.io.FileOutputStream; -import java.math.BigInteger; /** * A simple WOrdprocessingML document created by POI XWPF API @@ -46,7 +45,7 @@ r1.setBold(true); r1.setFontFamily("Courier"); r1.setUnderline(UnderlinePatterns.DOT_DOT_DASH); - r1.setTextPosition(new BigInteger("100")); + r1.setTextPosition(100); XWPFParagraph p2 = doc.createParagraph(); p2.setAlignment(ParagraphAlignment.RIGHT); @@ -61,51 +60,58 @@ XWPFRun r2 = p2.createRun(); r2.setText("jumped over the lazy dog"); r2.setStrike(true); - r2.setFontSize(new BigInteger("20")); + r2.setFontSize(20); XWPFRun r3 = p2.createRun(); r3.setText("and went away"); r3.setStrike(true); - r3.setFontSize(new BigInteger("20")); + r3.setFontSize(20); r3.setSubscript(VerticalAlign.SUPERSCRIPT); XWPFParagraph p3 = doc.createParagraph(); p3.setWordWrap(true); p3.setPageBreak(true); - p3.setAlignment(ParagraphAlignment.DISTRIBUTE); - p3.setIndentationFirstLine(new BigInteger("600")); - p3.setSpacingAfter(new BigInteger("250")); - p3.setSpacingBefore(new BigInteger("250")); + + //p3.setAlignment(ParagraphAlignment.DISTRIBUTE); + p3.setAlignment(ParagraphAlignment.BOTH); + p3.setSpacingLineRule(LineSpacingRule.EXACT); + p3.setIndentationFirstLine(600); + + XWPFRun r4 = p3.createRun(); - r4.setTextPosition(new BigInteger("20")); + r4.setTextPosition(20); r4.setText("To be, or not to be: that is the question: " + "Whether 'tis nobler in the mind to suffer " + "The slings and arrows of outrageous fortune, " + "Or to take arms against a sea of troubles, " - + "And by opposing end them? To die: to sleep; " - + "No more; and by a sleep to say we end " + + "And by opposing end them? To die: to sleep; "); + r4.addBreak(BreakType.PAGE); + r4.setText("No more; and by a sleep to say we end " + "The heart-ache and the thousand natural shocks " + "That flesh is heir to, 'tis a consummation " + "Devoutly to be wish'd. To die, to sleep; " + "To sleep: perchance to dream: ay, there's the rub; " + "......."); r4.setItalic(true); +//This would imply that this break shall be treated as a simple line break, and break the line after that word: - XWPFRun r5 = p3.createRun(); - r5.setTextPosition(new BigInteger("-10")); - r5.setText("For in that sleep of death what dreams may come" - + "When we have shuffled off this mortal coil," + r5.setTextPosition(-10); + r5.setText("For in that sleep of death what dreams may come"); + r5.addCarriageReturn(); + r5.setText("When we have shuffled off this mortal coil," + "Must give us pause: there's the respect" - + "That makes calamity of so long life;" - + "For who would bear the whips and scorns of time," - + "The oppressor's wrong, the proud man's contumely," - + "The pangs of despised love, the law's delay," + + "That makes calamity of so long life;"); + r5.addBreak(); + r5.setText("For who would bear the whips and scorns of time," + + "The oppressor's wrong, the proud man's contumely,"); + + r5.addBreak(BreakClear.ALL); + r5.setText("The pangs of despised love, the law's delay," + "The insolence of office and the spurns" + "......."); - FileOutputStream out = new FileOutputStream("simple.docx"); doc.write(out); out.close(); --- src/ooxml/java/org/apache/poi/xwpf/usermodel/BreakType.java (revision 0) +++ src/ooxml/java/org/apache/poi/xwpf/usermodel/BreakType.java (revision 0) @@ -0,0 +1,84 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ +package org.apache.poi.xwpf.usermodel; + +import java.util.HashMap; +import java.util.Map; + +/** + * Specifies the possible types of break characters in a WordprocessingML + * document. + * The break type determines the next location where text shall be + * placed after this manual break is applied to the text contents + * + * @author Gisella Bronzetti + */ +public enum BreakType { + + + /** + * Specifies that the current break shall restart itself on the next page of + * the document when the document is displayed in page view. + */ + PAGE(1), + + /** + * Specifies that the current break shall restart itself on the next column + * available on the current page when the document is displayed in page + * view. + *+ * If the current section is not divided into columns, or the column break + * occurs in the last column on the current page when displayed, then the + * restart location for text shall be the next page in the document. + *
+ */ + COLUMN(2), + + /** + * Specifies that the current break shall restart itself on the next line in + * the document when the document is displayed in page view. + * The determine of the next line shall be done subject to the value of the clear + * attribute on the specified break character. + */ + TEXT_WRAPPING(3); + + private final int value; + + private BreakType(int val) { + value = val; + } + + public int getValue() { + return value; + } + + private static Map