mirror of https://github.com/apache/poi.git
Apply patch from bug #51134 from Mike McEuen - Ability to add XWPFStyles and XWPFNumbering to a XWPF file that lacks them
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1128296 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
044c992853
commit
5bc714ba81
|
@ -34,6 +34,7 @@
|
||||||
|
|
||||||
<changes>
|
<changes>
|
||||||
<release version="3.8-beta3" date="2011-??-??">
|
<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="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="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>
|
<action dev="poi-developers" type="fix">51236 - XSSF set colour support for black/white to match getter</action>
|
||||||
|
|
|
@ -62,6 +62,7 @@ import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSdtBlock;
|
||||||
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTStyles;
|
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTStyles;
|
||||||
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl;
|
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl;
|
||||||
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTc;
|
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.CommentsDocument;
|
||||||
import org.openxmlformats.schemas.wordprocessingml.x2006.main.DocumentDocument;
|
import org.openxmlformats.schemas.wordprocessingml.x2006.main.DocumentDocument;
|
||||||
import org.openxmlformats.schemas.wordprocessingml.x2006.main.EndnotesDocument;
|
import org.openxmlformats.schemas.wordprocessingml.x2006.main.EndnotesDocument;
|
||||||
|
@ -644,6 +645,23 @@ public class XWPFDocument extends POIXMLDocument implements Document, IBody {
|
||||||
out.close();
|
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
|
* Appends a new paragraph to this document
|
||||||
* @return a new paragraph
|
* @return a new paragraph
|
||||||
|
@ -655,6 +673,57 @@ public class XWPFDocument extends POIXMLDocument implements Document, IBody {
|
||||||
return 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
|
* remove a BodyElement from bodyElements array list
|
||||||
* @param pos
|
* @param pos
|
||||||
|
|
|
@ -58,6 +58,15 @@ public class XWPFNumbering extends POIXMLDocumentPart {
|
||||||
onDocumentRead();
|
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
|
* read numbering form an existing package
|
||||||
*/
|
*/
|
||||||
|
@ -109,6 +118,14 @@ public class XWPFNumbering extends POIXMLDocumentPart {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the ctNumbering
|
||||||
|
* @param numbering
|
||||||
|
*/
|
||||||
|
public void setNumbering(CTNumbering numbering){
|
||||||
|
ctNumbering = numbering;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks whether number with numID exists
|
* Checks whether number with numID exists
|
||||||
|
@ -149,6 +166,19 @@ public class XWPFNumbering extends POIXMLDocumentPart {
|
||||||
return ctNum.getNumId();
|
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
|
* get Num by NumID
|
||||||
|
@ -207,9 +237,13 @@ public class XWPFNumbering extends POIXMLDocumentPart {
|
||||||
*/
|
*/
|
||||||
public BigInteger addAbstractNum(XWPFAbstractNum abstractNum){
|
public BigInteger addAbstractNum(XWPFAbstractNum abstractNum){
|
||||||
int pos = abstractNums.size();
|
int pos = abstractNums.size();
|
||||||
|
if(abstractNum.getAbstractNum() != null){ // Use the current CTAbstractNum if it exists
|
||||||
|
ctNumbering.addNewAbstractNum().set(abstractNum.getAbstractNum());
|
||||||
|
} else {
|
||||||
ctNumbering.addNewAbstractNum();
|
ctNumbering.addNewAbstractNum();
|
||||||
abstractNum.getAbstractNum().setAbstractNumId(BigInteger.valueOf(pos));
|
abstractNum.getAbstractNum().setAbstractNumId(BigInteger.valueOf(pos));
|
||||||
ctNumbering.setAbstractNumArray(pos, abstractNum.getAbstractNum());
|
ctNumbering.setAbstractNumArray(pos, abstractNum.getAbstractNum());
|
||||||
|
}
|
||||||
abstractNums.add(abstractNum);
|
abstractNums.add(abstractNum);
|
||||||
return abstractNum.getCTAbstractNum().getAbstractNumId();
|
return abstractNum.getCTAbstractNum().getAbstractNumId();
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,6 +24,7 @@ import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.lang.String;
|
||||||
|
|
||||||
import javax.xml.namespace.QName;
|
import javax.xml.namespace.QName;
|
||||||
|
|
||||||
|
@ -38,6 +39,11 @@ import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTStyle;
|
||||||
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTStyles;
|
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTStyles;
|
||||||
import org.openxmlformats.schemas.wordprocessingml.x2006.main.StylesDocument;
|
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
|
* @author Philipp Epp
|
||||||
*
|
*
|
||||||
|
@ -58,6 +64,14 @@ public class XWPFStyles extends POIXMLDocumentPart{
|
||||||
super(part, rel);
|
super(part, rel);
|
||||||
onDocumentRead();
|
onDocumentRead();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Construct XWPFStyles from scratch for a new document.
|
||||||
|
*/
|
||||||
|
public XWPFStyles() {
|
||||||
|
listStyle = new ArrayList<XWPFStyle>();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Read document
|
* Read document
|
||||||
*/
|
*/
|
||||||
|
@ -95,7 +109,13 @@ public class XWPFStyles extends POIXMLDocumentPart{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the ctStyles
|
||||||
|
* @param styles
|
||||||
|
*/
|
||||||
|
public void setStyles(CTStyles styles) {
|
||||||
|
ctStyles = styles;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* checks whether style with styleID exist
|
* checks whether style with styleID exist
|
||||||
|
@ -173,6 +193,97 @@ public class XWPFStyles extends POIXMLDocumentPart{
|
||||||
return usedStyleList;
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue