tag 1678498 as 3.12-final
git-svn-id: https://svn.apache.org/repos/asf/poi/tags/REL_3_12_FINAL@1678500 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/build.xml b/build.xml
index 24c757c..9509ed3 100644
--- a/build.xml
+++ b/build.xml
@@ -406,6 +406,7 @@
<!-- remove previous versions of third-party jars to prevent them from lingering around,
we often had hard-to-find build/CI-problems because of these! -->
<mkdir dir="${main.lib}"/>
+ <mkdir dir="${compile.lib}"/>
<delete verbose="true">
<fileset dir="${main.lib}">
<include name="ant-1.8*"/>
@@ -1245,7 +1246,7 @@
description="Generates the API documentation">
<javadoc verbose="false" author="true" destdir="${apidocs.report.dir}"
windowtitle="POI API Documentation" use="true" version="true"
- maxmemory="384M" additionalparam="-notimestamp"
+ maxmemory="384M" additionalparam="-notimestamp" locale="en_US"
classpathref="javadoc.classpath">
<packageset dir="${main.src}" defaultexcludes="yes">
<include name="org/apache/poi/**"/>
@@ -1473,7 +1474,7 @@
<mkdir dir="${build.maven.javadocs}"/>
<javadoc verbose="false" author="false" destdir="${build.maven.javadocs}"
windowtitle="POI API Documentation" use="false" version="false"
- maxmemory="384M" additionalparam="-notimestamp -quiet"
+ maxmemory="384M" additionalparam="-notimestamp -quiet" locale="en_US"
classpathref="javadoc.classpath">
<packageset dir="${srcfolder}" defaultexcludes="yes">
<include name="org/apache/poi/**"/>
diff --git a/doap_POI.rdf b/doap_POI.rdf
index e0123c2..ce36424 100644
--- a/doap_POI.rdf
+++ b/doap_POI.rdf
@@ -21,20 +21,47 @@
<category rdf:resource="http://projects.apache.org/category/library" />
<release>
<Version>
+ <name>Apache POI 3.11</name>
+ <created>2014-12-21</created>
+ <revision>3.11</revision>
+ </Version>
+ </release>
+ <release>
+ <Version>
+ <name>Apache POI 3.10.1</name>
+ <created>2014-08-18</created>
+ <revision>3.10.1</revision>
+ </Version>
+ </release>
+ <release>
+ <Version>
+ <name>Apache POI 3.10</name>
+ <created>2014-02-08</created>
+ <revision>3.10</revision>
+ </Version>
+ </release>
+ <release>
+ <Version>
<name>Apache POI 3.9</name>
<created>2012-12-03</created>
<revision>3.9</revision>
</Version>
+ </release>
+ <release>
<Version>
<name>Apache POI 3.8</name>
<created>2012-03-26</created>
<revision>3.8</revision>
</Version>
+ </release>
+ <release>
<Version>
<name>Apache POI 3.7</name>
<created>2010-10-29</created>
<revision>3.7</revision>
</Version>
+ </release>
+ <release>
<Version>
<name>Apache POI 3.6</name>
<created>2009-12-14</created>
diff --git a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDefaultParagraphStyle.java b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDefaultParagraphStyle.java
new file mode 100644
index 0000000..9d16e26
--- /dev/null
+++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDefaultParagraphStyle.java
@@ -0,0 +1,42 @@
+/* ====================================================================
+ 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 org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPPr;
+
+/**
+ * Default Paragraph style, from which other styles will override
+ * TODO Share logic with {@link XWPFParagraph} which also uses CTPPr
+ */
+public class XWPFDefaultParagraphStyle {
+ private CTPPr ppr;
+
+ public XWPFDefaultParagraphStyle(CTPPr ppr) {
+ this.ppr = ppr;
+ }
+
+ protected CTPPr getPPr() {
+ return ppr;
+ }
+
+ public int getSpacingAfter() {
+ if (ppr.isSetSpacing())
+ return ppr.getSpacing().getAfter().intValue();
+ return -1;
+ }
+}
diff --git a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDefaultRunStyle.java b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDefaultRunStyle.java
new file mode 100644
index 0000000..210a951
--- /dev/null
+++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDefaultRunStyle.java
@@ -0,0 +1,42 @@
+/* ====================================================================
+ 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 org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRPr;
+
+/**
+ * Default Character Run style, from which other styles will override
+ * TODO Share logic with {@link XWPFRun} which also uses CTRPr
+ */
+public class XWPFDefaultRunStyle {
+ private CTRPr rpr;
+
+ public XWPFDefaultRunStyle(CTRPr rpr) {
+ this.rpr = rpr;
+ }
+
+ protected CTRPr getRPr() {
+ return rpr;
+ }
+
+ public int getFontSize() {
+ if (rpr.isSetSz())
+ return rpr.getSz().getVal().intValue() / 2;
+ return -1;
+ }
+}
diff --git a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFLatentStyles.java b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFLatentStyles.java
index 83b898c..e942991 100644
--- a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFLatentStyles.java
+++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFLatentStyles.java
@@ -20,31 +20,35 @@
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTLsdException;
public class XWPFLatentStyles {
- private CTLatentStyles latentStyles;
- protected XWPFStyles styles; //LatentStyle shall know styles
-
- protected XWPFLatentStyles(){
- }
-
- protected XWPFLatentStyles(CTLatentStyles latentStyles){
- this(latentStyles,null);
- }
-
- protected XWPFLatentStyles(CTLatentStyles latentStyles, XWPFStyles styles) {
- this.latentStyles=latentStyles;
- this.styles=styles;
- }
-
- /**
- * checks wheter specific LatentStyleID is a latentStyle
- */
+ private CTLatentStyles latentStyles;
+ protected XWPFStyles styles; //LatentStyle shall know styles
+
+ protected XWPFLatentStyles(){
+ }
+
+ protected XWPFLatentStyles(CTLatentStyles latentStyles){
+ this(latentStyles,null);
+ }
+
+ protected XWPFLatentStyles(CTLatentStyles latentStyles, XWPFStyles styles) {
+ this.latentStyles=latentStyles;
+ this.styles=styles;
+ }
+
+ public int getNumberOfStyles() {
+ return latentStyles.sizeOfLsdExceptionArray();
+ }
+
+ /**
+ * checks whether specific LatentStyleID is a latentStyle
+ */
@SuppressWarnings("deprecation")
- protected boolean isLatentStyle(String latentStyleID){
- for ( CTLsdException lsd: latentStyles.getLsdExceptionArray()) {
- if(lsd.getName().equals(latentStyleID)) {
- return true;
- }
- }
- return false;
- }
+ protected boolean isLatentStyle(String latentStyleID){
+ for ( CTLsdException lsd: latentStyles.getLsdExceptionArray()) {
+ if(lsd.getName().equals(latentStyleID)) {
+ return true;
+ }
+ }
+ return false;
+ }
}
diff --git a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFStyles.java b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFStyles.java
index 824d39d..8647cc9 100644
--- a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFStyles.java
+++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFStyles.java
@@ -34,6 +34,7 @@
import org.apache.poi.openxml4j.opc.PackageRelationship;
import org.apache.xmlbeans.XmlException;
import org.apache.xmlbeans.XmlOptions;
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPPrDefault;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTStyle;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTStyles;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.StylesDocument;
@@ -43,14 +44,18 @@
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFonts;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDocDefaults;
/**
- * @author Philipp Epp
- *
+ * Holds details of built-in, default and user styles, which
+ * apply to tables / paragraphs / lists etc.
+ * Text within one of those with custom stylings has the style
+ * information stored in the {@link XWPFRun}
*/
public class XWPFStyles extends POIXMLDocumentPart{
-
- private List<XWPFStyle> listStyle = new ArrayList<XWPFStyle>();
private CTStyles ctStyles;
- XWPFLatentStyles latentStyles;
+ private List<XWPFStyle> listStyle = new ArrayList<XWPFStyle>();
+
+ private XWPFLatentStyles latentStyles;
+ private XWPFDefaultRunStyle defaultRunStyle;
+ private XWPFDefaultParagraphStyle defaultParaStyle;
/**
* Construct XWPFStyles from a package part
@@ -58,254 +63,251 @@
* @param part the package part holding the data of the styles,
* @param rel the package relationship of type "http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles"
*/
+ public XWPFStyles(PackagePart part, PackageRelationship rel) throws IOException, OpenXML4JException{
+ super(part, rel);
+ }
- public XWPFStyles(PackagePart part, PackageRelationship rel) throws IOException, OpenXML4JException{
- super(part, rel);
- }
+ /**
+ * Construct XWPFStyles from scratch for a new document.
+ */
+ public XWPFStyles() {
+ }
- /**
- * Construct XWPFStyles from scratch for a new document.
- */
- public XWPFStyles() {
- }
+ /**
+ * Read document
+ */
+ @Override
+ protected void onDocumentRead() throws IOException{
+ StylesDocument stylesDoc;
+ try {
+ InputStream is = getPackagePart().getInputStream();
+ stylesDoc = StylesDocument.Factory.parse(is);
+ setStyles(stylesDoc.getStyles());
+ latentStyles = new XWPFLatentStyles(ctStyles.getLatentStyles(), this);
+ } catch (XmlException e) {
+ throw new POIXMLException("Unable to read styles", e);
+ }
+ }
- /**
- * Read document
- */
- @Override
- protected void onDocumentRead() throws IOException{
- StylesDocument stylesDoc;
- try {
- InputStream is = getPackagePart().getInputStream();
- stylesDoc = StylesDocument.Factory.parse(is);
- setStyles(stylesDoc.getStyles());
- latentStyles = new XWPFLatentStyles(ctStyles.getLatentStyles(), this);
- } catch (XmlException e) {
- throw new POIXMLException("Unable to read styles", e);
- }
-
-
- }
-
- @Override
- protected void commit() throws IOException {
- if (ctStyles == null) {
- throw new IllegalStateException("Unable to write out styles that were never read in!");
- }
-
- XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS);
- xmlOptions.setSaveSyntheticDocumentElement(new QName(CTStyles.type.getName().getNamespaceURI(), "styles"));
- Map<String,String> map = new HashMap<String,String>();
- map.put("http://schemas.openxmlformats.org/officeDocument/2006/relationships", "r");
- map.put("http://schemas.openxmlformats.org/wordprocessingml/2006/main", "w");
- xmlOptions.setSaveSuggestedPrefixes(map);
- PackagePart part = getPackagePart();
- OutputStream out = part.getOutputStream();
- ctStyles.save(out, xmlOptions);
- out.close();
- }
-
+ @Override
+ protected void commit() throws IOException {
+ if (ctStyles == null) {
+ throw new IllegalStateException("Unable to write out styles that were never read in!");
+ }
+
+ XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS);
+ xmlOptions.setSaveSyntheticDocumentElement(new QName(CTStyles.type.getName().getNamespaceURI(), "styles"));
+ Map<String,String> map = new HashMap<String,String>();
+ map.put("http://schemas.openxmlformats.org/officeDocument/2006/relationships", "r");
+ map.put("http://schemas.openxmlformats.org/wordprocessingml/2006/main", "w");
+ xmlOptions.setSaveSuggestedPrefixes(map);
+ PackagePart part = getPackagePart();
+ OutputStream out = part.getOutputStream();
+ ctStyles.save(out, xmlOptions);
+ out.close();
+ }
+
+ protected void ensureDocDefaults() {
+ if (! ctStyles.isSetDocDefaults()) {
+ ctStyles.addNewDocDefaults();
+ }
+
+ CTDocDefaults docDefaults = ctStyles.getDocDefaults();
+ if (! docDefaults.isSetPPrDefault())
+ docDefaults.addNewPPrDefault();
+ if (! docDefaults.isSetRPrDefault())
+ docDefaults.addNewRPrDefault();
+
+ CTPPrDefault pprd = docDefaults.getPPrDefault();
+ CTRPrDefault rprd = docDefaults.getRPrDefault();
+ if (!pprd.isSetPPr()) pprd.addNewPPr();
+ if (!rprd.isSetRPr()) rprd.addNewRPr();
+
+ defaultRunStyle = new XWPFDefaultRunStyle(rprd.getRPr());
+ defaultParaStyle = new XWPFDefaultParagraphStyle(pprd.getPPr());
+ }
+
/**
* Sets the ctStyles
* @param styles
*/
@SuppressWarnings("deprecation")
public void setStyles(CTStyles styles) {
- ctStyles = styles;
-
- // Build up all the style objects
- for(CTStyle style : ctStyles.getStyleArray()) {
- listStyle.add(new XWPFStyle(style, this));
- }
+ ctStyles = styles;
+
+ // Build up all the style objects
+ for(CTStyle style : ctStyles.getStyleArray()) {
+ listStyle.add(new XWPFStyle(style, this));
+ }
+ if (ctStyles.isSetDocDefaults()) {
+ CTDocDefaults docDefaults = ctStyles.getDocDefaults();
+ if (docDefaults.isSetRPrDefault() && docDefaults.getRPrDefault().isSetRPr()) {
+ defaultRunStyle = new XWPFDefaultRunStyle(
+ docDefaults.getRPrDefault().getRPr());
+ }
+ if (docDefaults.isSetPPrDefault() && docDefaults.getPPrDefault().isSetPPr()) {
+ defaultParaStyle = new XWPFDefaultParagraphStyle(
+ docDefaults.getPPrDefault().getPPr());
+ }
+ }
}
-
- /**
- * checks whether style with styleID exist
- * @param styleID styleID of the Style in the style-Document
- * @return true if style exist, false if style not exist
- */
- public boolean styleExist(String styleID){
- for (XWPFStyle style : listStyle) {
- if (style.getStyleId().equals(styleID))
- return true;
- }
- return false;
- }
- /**
- * add a style to the document
- * @param style
- * @throws IOException
- */
- public void addStyle(XWPFStyle style){
- listStyle.add(style);
- ctStyles.addNewStyle();
- int pos = ctStyles.sizeOfStyleArray() - 1;
- ctStyles.setStyleArray(pos, style.getCTStyle());
- }
- /**
- *get style by a styleID
- * @param styleID styleID of the searched style
- * @return style
- */
- public XWPFStyle getStyle(String styleID){
- for (XWPFStyle style : listStyle) {
- if(style.getStyleId().equals(styleID))
- return style;
- }
- return null;
- }
- /**
- * get the styles which are related to the parameter style and their relatives
- * this method can be used to copy all styles from one document to another document
- * @param style
- * @return a list of all styles which were used by this method
- */
- public List<XWPFStyle> getUsedStyleList(XWPFStyle style){
- List<XWPFStyle> usedStyleList = new ArrayList<XWPFStyle>();
- usedStyleList.add(style);
- return getUsedStyleList(style, usedStyleList);
- }
-
- /**
- * get the styles which are related to parameter style
- * @param style
- * @return all Styles of the parameterList
- */
- private List<XWPFStyle> getUsedStyleList(XWPFStyle style, List<XWPFStyle> usedStyleList){
- String basisStyleID = style.getBasisStyleID();
- XWPFStyle basisStyle = getStyle(basisStyleID);
- if((basisStyle!=null)&&(!usedStyleList.contains(basisStyle))){
- usedStyleList.add(basisStyle);
- getUsedStyleList(basisStyle, usedStyleList);
- }
- String linkStyleID = style.getLinkStyleID();
- XWPFStyle linkStyle = getStyle(linkStyleID);
- if((linkStyle!=null)&&(!usedStyleList.contains(linkStyle))){
- usedStyleList.add(linkStyle);
- getUsedStyleList(linkStyle, usedStyleList);
- }
-
- String nextStyleID = style.getNextStyleID();
- XWPFStyle nextStyle = getStyle(nextStyleID);
- if((nextStyle!=null)&&(!usedStyleList.contains(nextStyle))){
- usedStyleList.add(linkStyle);
- getUsedStyleList(linkStyle, 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;
+ /**
+ * checks whether style with styleID exist
+ * @param styleID styleID of the Style in the style-Document
+ * @return true if style exist, false if style not exist
+ */
+ public boolean styleExist(String styleID){
+ for (XWPFStyle style : listStyle) {
+ if (style.getStyleId().equals(styleID))
+ return true;
+ }
+ return false;
+ }
+ /**
+ * add a style to the document
+ * @param style
+ * @throws IOException
+ */
+ public void addStyle(XWPFStyle style){
+ listStyle.add(style);
+ ctStyles.addNewStyle();
+ int pos = ctStyles.sizeOfStyleArray() - 1;
+ ctStyles.setStyleArray(pos, style.getCTStyle());
+ }
+ /**
+ * Get style by a styleID
+ * @param styleID styleID of the searched style
+ * @return style
+ */
+ public XWPFStyle getStyle(String styleID){
+ for (XWPFStyle style : listStyle) {
+ if(style.getStyleId().equals(styleID))
+ return style;
+ }
+ return null;
+ }
+ public int getNumberOfStyles() {
+ return listStyle.size();
+ }
- // 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();
- }
- }
- }
+ /**
+ * get the styles which are related to the parameter style and their relatives
+ * this method can be used to copy all styles from one document to another document
+ * @param style
+ * @return a list of all styles which were used by this method
+ */
+ public List<XWPFStyle> getUsedStyleList(XWPFStyle style){
+ List<XWPFStyle> usedStyleList = new ArrayList<XWPFStyle>();
+ usedStyleList.add(style);
+ return getUsedStyleList(style, usedStyleList);
+ }
- if(docDefaults == null)
- docDefaults = ctStyles.addNewDocDefaults();
- if(runProps == null)
- runProps = docDefaults.addNewRPrDefault().addNewRPr();
- if(lang == null)
- lang = runProps.addNewLang();
+ /**
+ * get the styles which are related to parameter style
+ * @param style
+ * @return all Styles of the parameterList
+ */
+ private List<XWPFStyle> getUsedStyleList(XWPFStyle style, List<XWPFStyle> usedStyleList){
+ String basisStyleID = style.getBasisStyleID();
+ XWPFStyle basisStyle = getStyle(basisStyleID);
+ if((basisStyle!=null)&&(!usedStyleList.contains(basisStyle))){
+ usedStyleList.add(basisStyle);
+ getUsedStyleList(basisStyle, usedStyleList);
+ }
+ String linkStyleID = style.getLinkStyleID();
+ XWPFStyle linkStyle = getStyle(linkStyleID);
+ if((linkStyle!=null)&&(!usedStyleList.contains(linkStyle))){
+ usedStyleList.add(linkStyle);
+ getUsedStyleList(linkStyle, usedStyleList);
+ }
- lang.setVal(strSpellingLanguage);
- lang.setBidi(strSpellingLanguage);
- }
+ String nextStyleID = style.getNextStyleID();
+ XWPFStyle nextStyle = getStyle(nextStyleID);
+ if((nextStyle!=null)&&(!usedStyleList.contains(nextStyle))){
+ usedStyleList.add(linkStyle);
+ getUsedStyleList(linkStyle, usedStyleList);
+ }
+ return usedStyleList;
+ }
- /**
- * 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;
+ protected CTLanguage getCTLanguage() {
+ ensureDocDefaults();
+
+ CTLanguage lang = null;
+ if (defaultRunStyle.getRPr().isSetLang()) {
+ lang = defaultRunStyle.getRPr().getLang();
+ } else {
+ lang = defaultRunStyle.getRPr().addNewLang();
+ }
+
+ return lang;
+ }
+
+ /**
+ * Sets the default spelling language on ctStyles DocDefaults parameter
+ * @param strSpellingLanguage
+ */
+ public void setSpellingLanguage(String strSpellingLanguage) {
+ CTLanguage lang = getCTLanguage();
+ lang.setVal(strSpellingLanguage);
+ lang.setBidi(strSpellingLanguage);
+ }
+
+ /**
+ * Sets the default East Asia spelling language on ctStyles DocDefaults parameter
+ * @param strEastAsia
+ */
+ public void setEastAsia(String strEastAsia) {
+ CTLanguage lang = getCTLanguage();
+ lang.setEastAsia(strEastAsia);
+ }
- // 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();
- }
- }
- }
+ /**
+ * Sets the default font on ctStyles DocDefaults parameter
+ * TODO Replace this with specific setters for each type, possibly
+ * on XWPFDefaultRunStyle
+ */
+ public void setDefaultFonts(CTFonts fonts) {
+ ensureDocDefaults();
+
+ CTRPr runProps = defaultRunStyle.getRPr();
+ runProps.setRFonts(fonts);
+ }
- if(docDefaults == null)
- docDefaults = ctStyles.addNewDocDefaults();
- if(runProps == null)
- runProps = docDefaults.addNewRPrDefault().addNewRPr();
- if(lang == null)
- lang = runProps.addNewLang();
+ /**
+ * get the style with the same name
+ * if this style is not existing, return null
+ */
+ public XWPFStyle getStyleWithSameName(XWPFStyle style){
+ for (XWPFStyle ownStyle : listStyle) {
+ if(ownStyle.hasSameName(style)){
+ return ownStyle;
+ }
+ }
+ return null;
+ }
- lang.setEastAsia(strEastAsia);
- }
+ /**
+ * Get the default style which applies text runs in the document
+ */
+ public XWPFDefaultRunStyle getDefaultRunStyle() {
+ ensureDocDefaults();
+ return defaultRunStyle;
+ }
- /**
- * Sets the default font on ctStyles DocDefaults parameter
- * @param fonts
- */
- public void setDefaultFonts(CTFonts fonts) {
- CTDocDefaults docDefaults = null;
- CTRPr runProps = null;
+ /**
+ * Get the default paragraph style which applies to the document
+ */
+ public XWPFDefaultParagraphStyle getDefaultParagraphStyle() {
+ ensureDocDefaults();
+ return defaultParaStyle;
+ }
- // 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);
- }
-
-
- /**
- * get latentstyles
- */
- public XWPFLatentStyles getLatentStyles() {
- return latentStyles;
- }
-
- /**
- * get the style with the same name
- * if this style is not existing, return null
- */
- public XWPFStyle getStyleWithSameName(XWPFStyle style){
- for (XWPFStyle ownStyle : listStyle) {
- if(ownStyle.hasSameName(style)){
- return ownStyle;
- }
- }
- return null;
-
- }
-}//end class
+ /**
+ * Get the definition of all the Latent Styles
+ */
+ public XWPFLatentStyles getLatentStyles() {
+ return latentStyles;
+ }
+}
diff --git a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFStyles.java b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFStyles.java
index c29d502..c297bc4 100644
--- a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFStyles.java
+++ b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFStyles.java
@@ -138,4 +138,60 @@
styles = docIn.getStyles();
assertTrue(styles.styleExist(strStyleId));
}
+
+ public void testEasyAccessToStyles() throws IOException {
+ XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("SampleDoc.docx");
+ XWPFStyles styles = doc.getStyles();
+ assertNotNull(styles);
+
+ // Has 3 paragraphs on page one, a break, and 3 on page 2
+ assertEquals(7, doc.getParagraphs().size());
+
+ // Check the first three have no run styles, just default paragraph style
+ for (int i=0; i<3; i++) {
+ XWPFParagraph p = doc.getParagraphs().get(i);
+ assertEquals(null, p.getStyle());
+ assertEquals(null, p.getStyleID());
+ assertEquals(1, p.getRuns().size());
+
+ XWPFRun r = p.getRuns().get(0);
+ assertEquals(null, r.getColor());
+ assertEquals(null, r.getFontFamily());
+ assertEquals(null, r.getFontName());
+ assertEquals(-1, r.getFontSize());
+ }
+
+ // On page two, has explicit styles, but on runs not on
+ // the paragraph itself
+ for (int i=4; i<7; i++) {
+ XWPFParagraph p = doc.getParagraphs().get(i);
+ assertEquals(null, p.getStyle());
+ assertEquals(null, p.getStyleID());
+ assertEquals(1, p.getRuns().size());
+
+ XWPFRun r = p.getRuns().get(0);
+ assertEquals("Arial Black", r.getFontFamily());
+ assertEquals("Arial Black", r.getFontName());
+ assertEquals(16, r.getFontSize());
+ assertEquals("548DD4", r.getColor());
+ }
+
+ // Check the document styles
+ // Should have a style defined for each type
+ assertEquals(4, styles.getNumberOfStyles());
+ assertNotNull(styles.getStyle("Normal"));
+ assertNotNull(styles.getStyle("DefaultParagraphFont"));
+ assertNotNull(styles.getStyle("TableNormal"));
+ assertNotNull(styles.getStyle("NoList"));
+
+ // We can't do much yet with latent styles
+ assertEquals(137, styles.getLatentStyles().getNumberOfStyles());
+
+ // Check the default styles
+ assertNotNull(styles.getDefaultRunStyle());
+ assertNotNull(styles.getDefaultParagraphStyle());
+
+ assertEquals(11, styles.getDefaultRunStyle().getFontSize());
+ assertEquals(200, styles.getDefaultParagraphStyle().getSpacingAfter());
+ }
}