You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

XWPFStyles.java 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.xwpf.usermodel;
  16. import java.io.IOException;
  17. import java.io.InputStream;
  18. import java.io.OutputStream;
  19. import java.util.ArrayList;
  20. import java.util.HashMap;
  21. import java.util.List;
  22. import java.util.Map;
  23. import java.lang.String;
  24. import javax.xml.namespace.QName;
  25. import org.apache.poi.POIXMLDocumentPart;
  26. import org.apache.poi.POIXMLException;
  27. import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
  28. import org.apache.poi.openxml4j.opc.PackagePart;
  29. import org.apache.poi.openxml4j.opc.PackageRelationship;
  30. import org.apache.xmlbeans.XmlException;
  31. import org.apache.xmlbeans.XmlOptions;
  32. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTStyle;
  33. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTStyles;
  34. import org.openxmlformats.schemas.wordprocessingml.x2006.main.StylesDocument;
  35. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRPr;
  36. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRPrDefault;
  37. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTLanguage;
  38. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFonts;
  39. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDocDefaults;
  40. /**
  41. * @author Philipp Epp
  42. *
  43. */
  44. public class XWPFStyles extends POIXMLDocumentPart{
  45. private CTStyles ctStyles;
  46. protected XWPFLatentStyles latentStyles;
  47. protected List<XWPFStyle> listStyle;
  48. /**
  49. * Construct XWPFStyles from a package part
  50. *
  51. * @param part the package part holding the data of the styles,
  52. * @param rel the package relationship of type "http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles"
  53. */
  54. public XWPFStyles(PackagePart part, PackageRelationship rel) throws IOException, OpenXML4JException{
  55. super(part, rel);
  56. onDocumentRead();
  57. }
  58. /**
  59. * Construct XWPFStyles from scratch for a new document.
  60. */
  61. public XWPFStyles() {
  62. listStyle = new ArrayList<XWPFStyle>();
  63. }
  64. /**
  65. * Read document
  66. */
  67. @Override
  68. protected void onDocumentRead ()throws IOException{
  69. listStyle = new ArrayList<XWPFStyle>();
  70. StylesDocument stylesDoc;
  71. try {
  72. InputStream is = getPackagePart().getInputStream();
  73. stylesDoc = StylesDocument.Factory.parse(is);
  74. ctStyles = stylesDoc.getStyles();
  75. latentStyles = new XWPFLatentStyles(ctStyles.getLatentStyles(), this);
  76. } catch (XmlException e) {
  77. throw new POIXMLException();
  78. }
  79. //get any Style
  80. for(CTStyle style : ctStyles.getStyleList()) {
  81. listStyle.add(new XWPFStyle(style, this));
  82. }
  83. }
  84. @Override
  85. protected void commit() throws IOException {
  86. XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS);
  87. xmlOptions.setSaveSyntheticDocumentElement(new QName(CTStyles.type.getName().getNamespaceURI(), "styles"));
  88. Map map = new HashMap();
  89. map.put("http://schemas.openxmlformats.org/officeDocument/2006/relationships", "r");
  90. map.put("http://schemas.openxmlformats.org/wordprocessingml/2006/main", "w");
  91. xmlOptions.setSaveSuggestedPrefixes(map);
  92. PackagePart part = getPackagePart();
  93. OutputStream out = part.getOutputStream();
  94. ctStyles.save(out, xmlOptions);
  95. out.close();
  96. }
  97. /**
  98. * Sets the ctStyles
  99. * @param styles
  100. */
  101. public void setStyles(CTStyles styles) {
  102. ctStyles = styles;
  103. }
  104. /**
  105. * checks whether style with styleID exist
  106. * @param styleID styleID of the Style in the style-Document
  107. * @return true if style exist, false if style not exist
  108. */
  109. public boolean styleExist(String styleID){
  110. for (XWPFStyle style : listStyle) {
  111. if (style.getStyleId().equals(styleID))
  112. return true;
  113. }
  114. return false;
  115. }
  116. /**
  117. * add a style to the document
  118. * @param style
  119. * @throws IOException
  120. */
  121. public void addStyle(XWPFStyle style){
  122. listStyle.add(style);
  123. ctStyles.addNewStyle();
  124. int pos = (ctStyles.getStyleList().size()) - 1;
  125. ctStyles.setStyleArray(pos, style.getCTStyle());
  126. }
  127. /**
  128. *get style by a styleID
  129. * @param styleID styleID of the searched style
  130. * @return style
  131. */
  132. public XWPFStyle getStyle(String styleID){
  133. for (XWPFStyle style : listStyle) {
  134. if(style.getStyleId().equals(styleID))
  135. return style;
  136. }
  137. return null;
  138. }
  139. /**
  140. * get the styles which are related to the parameter style and their relatives
  141. * this method can be used to copy all styles from one document to another document
  142. * @param style
  143. * @return a list of all styles which were used by this method
  144. */
  145. public List<XWPFStyle> getUsedStyleList(XWPFStyle style){
  146. List<XWPFStyle> usedStyleList = new ArrayList<XWPFStyle>();
  147. usedStyleList.add(style);
  148. return getUsedStyleList(style, usedStyleList);
  149. }
  150. /**
  151. * get the styles which are related to parameter style
  152. * @param style
  153. * @return all Styles of the parameterList
  154. */
  155. private List<XWPFStyle> getUsedStyleList(XWPFStyle style, List<XWPFStyle> usedStyleList){
  156. String basisStyleID = style.getBasisStyleID();
  157. XWPFStyle basisStyle = getStyle(basisStyleID);
  158. if((basisStyle!=null)&&(!usedStyleList.contains(basisStyle))){
  159. usedStyleList.add(basisStyle);
  160. getUsedStyleList(basisStyle, usedStyleList);
  161. }
  162. String linkStyleID = style.getLinkStyleID();
  163. XWPFStyle linkStyle = getStyle(linkStyleID);
  164. if((linkStyle!=null)&&(!usedStyleList.contains(linkStyle))){
  165. usedStyleList.add(linkStyle);
  166. getUsedStyleList(linkStyle, usedStyleList);
  167. }
  168. String nextStyleID = style.getNextStyleID();
  169. XWPFStyle nextStyle = getStyle(nextStyleID);
  170. if((nextStyle!=null)&&(!usedStyleList.contains(nextStyle))){
  171. usedStyleList.add(linkStyle);
  172. getUsedStyleList(linkStyle, usedStyleList);
  173. }
  174. return usedStyleList;
  175. }
  176. /**
  177. * Sets the default spelling language on ctStyles DocDefaults parameter
  178. * @param strSpellingLanguage
  179. */
  180. public void setSpellingLanguage(String strSpellingLanguage) {
  181. CTDocDefaults docDefaults = null;
  182. CTRPr runProps = null;
  183. CTLanguage lang = null;
  184. // Just making sure we use the members that have already been defined
  185. if(ctStyles.isSetDocDefaults()) {
  186. docDefaults = ctStyles.getDocDefaults();
  187. if(docDefaults.isSetRPrDefault()) {
  188. CTRPrDefault RPrDefault = docDefaults.getRPrDefault();
  189. if(RPrDefault.isSetRPr()) {
  190. runProps = RPrDefault.getRPr();
  191. if(runProps.isSetLang())
  192. lang = runProps.getLang();
  193. }
  194. }
  195. }
  196. if(docDefaults == null)
  197. docDefaults = ctStyles.addNewDocDefaults();
  198. if(runProps == null)
  199. runProps = docDefaults.addNewRPrDefault().addNewRPr();
  200. if(lang == null)
  201. lang = runProps.addNewLang();
  202. lang.setVal(strSpellingLanguage);
  203. lang.setBidi(strSpellingLanguage);
  204. }
  205. /**
  206. * Sets the default East Asia spelling language on ctStyles DocDefaults parameter
  207. * @param strEastAsia
  208. */
  209. public void setEastAsia(String strEastAsia) {
  210. CTDocDefaults docDefaults = null;
  211. CTRPr runProps = null;
  212. CTLanguage lang = null;
  213. // Just making sure we use the members that have already been defined
  214. if(ctStyles.isSetDocDefaults()) {
  215. docDefaults = ctStyles.getDocDefaults();
  216. if(docDefaults.isSetRPrDefault()) {
  217. CTRPrDefault RPrDefault = docDefaults.getRPrDefault();
  218. if(RPrDefault.isSetRPr()) {
  219. runProps = RPrDefault.getRPr();
  220. if(runProps.isSetLang())
  221. lang = runProps.getLang();
  222. }
  223. }
  224. }
  225. if(docDefaults == null)
  226. docDefaults = ctStyles.addNewDocDefaults();
  227. if(runProps == null)
  228. runProps = docDefaults.addNewRPrDefault().addNewRPr();
  229. if(lang == null)
  230. lang = runProps.addNewLang();
  231. lang.setEastAsia(strEastAsia);
  232. }
  233. /**
  234. * Sets the default font on ctStyles DocDefaults parameter
  235. * @param fonts
  236. */
  237. public void setDefaultFonts(CTFonts fonts) {
  238. CTDocDefaults docDefaults = null;
  239. CTRPr runProps = null;
  240. // Just making sure we use the members that have already been defined
  241. if(ctStyles.isSetDocDefaults()) {
  242. docDefaults = ctStyles.getDocDefaults();
  243. if(docDefaults.isSetRPrDefault()) {
  244. CTRPrDefault RPrDefault = docDefaults.getRPrDefault();
  245. if(RPrDefault.isSetRPr()) {
  246. runProps = RPrDefault.getRPr();
  247. }
  248. }
  249. }
  250. if(docDefaults == null)
  251. docDefaults = ctStyles.addNewDocDefaults();
  252. if(runProps == null)
  253. runProps = docDefaults.addNewRPrDefault().addNewRPr();
  254. runProps.setRFonts(fonts);
  255. }
  256. /**
  257. * get latentstyles
  258. */
  259. public XWPFLatentStyles getLatentStyles() {
  260. return latentStyles;
  261. }
  262. /**
  263. * get the style with the same name
  264. * if this style is not existing, return null
  265. */
  266. public XWPFStyle getStyleWithSameName(XWPFStyle style){
  267. for (XWPFStyle ownStyle : listStyle) {
  268. if(ownStyle.hasSameName(style)){
  269. return ownStyle;
  270. }
  271. }
  272. return null;
  273. }
  274. }//end class