<changes>
<release version="3.8-beta3" date="2011-??-??">
+ <action dev="poi-developers" type="add">51134 - Support for adding Numbering and Styles to a XWPF document that doesn't already have them</action>
<action dev="poi-developers" type="fix">51273 - Formula Value Cache fix for repeated evaluations</action>
<action dev="poi-developers" type="add">51171 - Improved performance of SharedValueManager </action>
<action dev="poi-developers" type="fix">51236 - XSSF set colour support for black/white to match getter</action>
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTStyles;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTc;
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.NumberingDocument;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CommentsDocument;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.DocumentDocument;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.EndnotesDocument;
out.close();
}
+ /**
+ * Gets the index of the relation we're trying to create
+ * @param relation
+ * @return i
+ */
+ private int getRelationIndex(XWPFRelation relation) {
+ List<POIXMLDocumentPart> relations = getRelations();
+ int i = 1;
+ for (Iterator<POIXMLDocumentPart> it = relations.iterator(); it.hasNext() ; ) {
+ POIXMLDocumentPart item = it.next();
+ if (item.getPackageRelationship().getRelationshipType().equals(relation.getRelation())) {
+ i++;
+ }
+ }
+ return i;
+ }
+
/**
* Appends a new paragraph to this document
* @return a new paragraph
paragraphs.add(p);
return p;
}
-
+
+ /**
+ * Creates an empty numbering if one does not already exist and sets the numbering member
+ * @return numbering
+ */
+ public XWPFNumbering createNumbering() {
+ if(numbering == null) {
+ NumberingDocument numberingDoc = NumberingDocument.Factory.newInstance();
+
+ XWPFRelation relation = XWPFRelation.NUMBERING;
+ int i = getRelationIndex(relation);
+
+ XWPFNumbering wrapper = (XWPFNumbering)createRelationship(relation, XWPFFactory.getInstance(), i);
+ wrapper.setNumbering(numberingDoc.addNewNumbering());
+ numbering = wrapper;
+ }
+
+ return numbering;
+ }
+
+ /**
+ * Creates an empty styles for the document if one does not already exist
+ * @return styles
+ */
+ public XWPFStyles createStyles() {
+ if(styles == null) {
+ StylesDocument stylesDoc = StylesDocument.Factory.newInstance();
+
+ XWPFRelation relation = XWPFRelation.STYLES;
+ int i = getRelationIndex(relation);
+
+ XWPFStyles wrapper = (XWPFStyles)createRelationship(relation, XWPFFactory.getInstance(), i);
+ wrapper.setStyles(stylesDoc.addNewStyles());
+ styles = wrapper;
+ }
+
+ return styles;
+ }
+
+
+ public XWPFFootnote addEndnote(CTFtnEdn note) {
+ XWPFFootnote footnote = new XWPFFootnote(this, note);
+ footnotes.put(note.getId().intValue(), footnote);
+ return footnote;
+ }
+
+ public XWPFFootnote addFootnote(CTFtnEdn note) {
+ XWPFFootnote endnote = new XWPFFootnote(this, note);
+ endnotes.put(note.getId().intValue(), endnote);
+ return endnote;
+ }
+
/**
* remove a BodyElement from bodyElements array list
* @param pos
isNew = true;
onDocumentRead();
}
-
+
+ /**
+ * create a new XWPFNumbering object for use in a new document
+ */
+ public XWPFNumbering(){
+ abstractNums = new ArrayList<XWPFAbstractNum>();
+ nums = new ArrayList<XWPFNum>();
+ isNew = true;
+ }
+
/**
* read numbering form an existing package
*/
out.close();
}
+
+ /**
+ * Sets the ctNumbering
+ * @param numbering
+ */
+ public void setNumbering(CTNumbering numbering){
+ ctNumbering = numbering;
+ }
/**
return ctNum.getNumId();
}
+ /**
+ * Add a new num with an abstractNumID and a numID
+ * @param abstractNumId
+ * @param numID
+ */
+ public void addNum(BigInteger abstractNumID, BigInteger numID){
+ CTNum ctNum = this.ctNumbering.addNewNum();
+ ctNum.addNewAbstractNumId();
+ ctNum.getAbstractNumId().setVal(abstractNumID);
+ ctNum.setNumId(numID);
+ XWPFNum num = new XWPFNum(ctNum, this);
+ nums.add(num);
+ }
/**
* get Num by NumID
*/
public BigInteger addAbstractNum(XWPFAbstractNum abstractNum){
int pos = abstractNums.size();
- ctNumbering.addNewAbstractNum();
- abstractNum.getAbstractNum().setAbstractNumId(BigInteger.valueOf(pos));
- ctNumbering.setAbstractNumArray(pos, abstractNum.getAbstractNum());
+ if(abstractNum.getAbstractNum() != null){ // Use the current CTAbstractNum if it exists
+ ctNumbering.addNewAbstractNum().set(abstractNum.getAbstractNum());
+ } else {
+ ctNumbering.addNewAbstractNum();
+ abstractNum.getAbstractNum().setAbstractNumId(BigInteger.valueOf(pos));
+ ctNumbering.setAbstractNumArray(pos, abstractNum.getAbstractNum());
+ }
abstractNums.add(abstractNum);
return abstractNum.getCTAbstractNum().getAbstractNumId();
}
import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import java.lang.String;
import javax.xml.namespace.QName;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTStyles;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.StylesDocument;
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRPr;
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRPrDefault;
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTLanguage;
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFonts;
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDocDefaults;
/**
* @author Philipp Epp
*
super(part, rel);
onDocumentRead();
}
+
+ /**
+ * Construct XWPFStyles from scratch for a new document.
+ */
+ public XWPFStyles() {
+ listStyle = new ArrayList<XWPFStyle>();
+ }
+
/**
* Read document
*/
}
-
+ /**
+ * Sets the ctStyles
+ * @param styles
+ */
+ public void setStyles(CTStyles styles) {
+ ctStyles = styles;
+ }
/**
* checks whether style with styleID exist
return usedStyleList;
}
+ /**
+ * Sets the default spelling language on ctStyles DocDefaults parameter
+ * @param strSpellingLanguage
+ */
+ public void setSpellingLanguage(String strSpellingLanguage) {
+ CTDocDefaults docDefaults = null;
+ CTRPr runProps = null;
+ CTLanguage lang = null;
+
+ // Just making sure we use the members that have already been defined
+ if(ctStyles.isSetDocDefaults()) {
+ docDefaults = ctStyles.getDocDefaults();
+ if(docDefaults.isSetRPrDefault()) {
+ CTRPrDefault RPrDefault = docDefaults.getRPrDefault();
+ if(RPrDefault.isSetRPr()) {
+ runProps = RPrDefault.getRPr();
+ if(runProps.isSetLang())
+ lang = runProps.getLang();
+ }
+ }
+ }
+
+ if(docDefaults == null)
+ docDefaults = ctStyles.addNewDocDefaults();
+ if(runProps == null)
+ runProps = docDefaults.addNewRPrDefault().addNewRPr();
+ if(lang == null)
+ lang = runProps.addNewLang();
+
+ lang.setVal(strSpellingLanguage);
+ lang.setBidi(strSpellingLanguage);
+ }
+
+ /**
+ * Sets the default East Asia spelling language on ctStyles DocDefaults parameter
+ * @param strEastAsia
+ */
+ public void setEastAsia(String strEastAsia) {
+ CTDocDefaults docDefaults = null;
+ CTRPr runProps = null;
+ CTLanguage lang = null;
+
+ // Just making sure we use the members that have already been defined
+ if(ctStyles.isSetDocDefaults()) {
+ docDefaults = ctStyles.getDocDefaults();
+ if(docDefaults.isSetRPrDefault()) {
+ CTRPrDefault RPrDefault = docDefaults.getRPrDefault();
+ if(RPrDefault.isSetRPr()) {
+ runProps = RPrDefault.getRPr();
+ if(runProps.isSetLang())
+ lang = runProps.getLang();
+ }
+ }
+ }
+
+ if(docDefaults == null)
+ docDefaults = ctStyles.addNewDocDefaults();
+ if(runProps == null)
+ runProps = docDefaults.addNewRPrDefault().addNewRPr();
+ if(lang == null)
+ lang = runProps.addNewLang();
+
+ lang.setEastAsia(strEastAsia);
+ }
+
+ /**
+ * Sets the default font on ctStyles DocDefaults parameter
+ * @param fonts
+ */
+ public void setDefaultFonts(CTFonts fonts) {
+ CTDocDefaults docDefaults = null;
+ CTRPr runProps = null;
+
+ // Just making sure we use the members that have already been defined
+ if(ctStyles.isSetDocDefaults()) {
+ docDefaults = ctStyles.getDocDefaults();
+ if(docDefaults.isSetRPrDefault()) {
+ CTRPrDefault RPrDefault = docDefaults.getRPrDefault();
+ if(RPrDefault.isSetRPr()) {
+ runProps = RPrDefault.getRPr();
+ }
+ }
+ }
+
+ if(docDefaults == null)
+ docDefaults = ctStyles.addNewDocDefaults();
+ if(runProps == null)
+ runProps = docDefaults.addNewRPrDefault().addNewRPr();
+
+ runProps.setRFonts(fonts);
+ }
/**