您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

XWPFNumbering.java 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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 static org.apache.poi.ooxml.POIXMLTypeLoader.DEFAULT_XML_OPTIONS;
  17. import java.io.IOException;
  18. import java.io.InputStream;
  19. import java.io.OutputStream;
  20. import java.math.BigInteger;
  21. import java.util.ArrayList;
  22. import java.util.Collections;
  23. import java.util.List;
  24. import javax.xml.namespace.QName;
  25. import org.apache.poi.ooxml.POIXMLDocumentPart;
  26. import org.apache.poi.ooxml.POIXMLException;
  27. import org.apache.poi.openxml4j.opc.PackagePart;
  28. import org.apache.xmlbeans.XmlException;
  29. import org.apache.xmlbeans.XmlOptions;
  30. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTAbstractNum;
  31. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTNum;
  32. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTNumbering;
  33. import org.openxmlformats.schemas.wordprocessingml.x2006.main.NumberingDocument;
  34. public class XWPFNumbering extends POIXMLDocumentPart {
  35. protected List<XWPFAbstractNum> abstractNums = new ArrayList<>();
  36. protected List<XWPFNum> nums = new ArrayList<>();
  37. boolean isNew;
  38. private CTNumbering ctNumbering;
  39. /**
  40. * create a new styles object with an existing document
  41. *
  42. * @since POI 3.14-Beta1
  43. */
  44. public XWPFNumbering(PackagePart part) {
  45. super(part);
  46. isNew = true;
  47. }
  48. /**
  49. * create a new XWPFNumbering object for use in a new document
  50. */
  51. public XWPFNumbering() {
  52. abstractNums = new ArrayList<>();
  53. nums = new ArrayList<>();
  54. isNew = true;
  55. }
  56. /**
  57. * read numbering form an existing package
  58. */
  59. @Override
  60. protected void onDocumentRead() throws IOException {
  61. NumberingDocument numberingDoc;
  62. InputStream is;
  63. is = getPackagePart().getInputStream();
  64. try {
  65. numberingDoc = NumberingDocument.Factory.parse(is, DEFAULT_XML_OPTIONS);
  66. ctNumbering = numberingDoc.getNumbering();
  67. //get any Nums
  68. for (CTNum ctNum : ctNumbering.getNumArray()) {
  69. nums.add(new XWPFNum(ctNum, this));
  70. }
  71. for (CTAbstractNum ctAbstractNum : ctNumbering.getAbstractNumArray()) {
  72. abstractNums.add(new XWPFAbstractNum(ctAbstractNum, this));
  73. }
  74. isNew = false;
  75. } catch (XmlException e) {
  76. throw new POIXMLException();
  77. } finally {
  78. is.close();
  79. }
  80. }
  81. /**
  82. * save and commit numbering
  83. */
  84. @Override
  85. protected void commit() throws IOException {
  86. XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS);
  87. xmlOptions.setSaveSyntheticDocumentElement(new QName(CTNumbering.type.getName().getNamespaceURI(), "numbering"));
  88. PackagePart part = getPackagePart();
  89. try (OutputStream out = part.getOutputStream()) {
  90. ctNumbering.save(out, xmlOptions);
  91. }
  92. }
  93. /**
  94. * Sets the ctNumbering
  95. */
  96. public void setNumbering(CTNumbering numbering) {
  97. ctNumbering = numbering;
  98. }
  99. /**
  100. * Checks whether number with numID exists
  101. *
  102. * @return boolean true if num exist, false if num not exist
  103. */
  104. public boolean numExist(BigInteger numID) {
  105. for (XWPFNum num : nums) {
  106. if (num.getCTNum().getNumId().equals(numID))
  107. return true;
  108. }
  109. return false;
  110. }
  111. /**
  112. * add a new number to the numbering document
  113. */
  114. public BigInteger addNum(XWPFNum num) {
  115. ctNumbering.addNewNum();
  116. int pos = ctNumbering.sizeOfNumArray() - 1;
  117. ctNumbering.setNumArray(pos, num.getCTNum());
  118. nums.add(num);
  119. return num.getCTNum().getNumId();
  120. }
  121. /**
  122. * Add a new num with an abstractNumID
  123. *
  124. * @return return NumId of the added num
  125. */
  126. public BigInteger addNum(BigInteger abstractNumID) {
  127. CTNum ctNum = this.ctNumbering.addNewNum();
  128. ctNum.addNewAbstractNumId();
  129. ctNum.getAbstractNumId().setVal(abstractNumID);
  130. ctNum.setNumId(BigInteger.valueOf(nums.size() + 1L));
  131. XWPFNum num = new XWPFNum(ctNum, this);
  132. nums.add(num);
  133. return ctNum.getNumId();
  134. }
  135. /**
  136. * Add a new num with an abstractNumID and a numID
  137. */
  138. public void addNum(BigInteger abstractNumID, BigInteger numID) {
  139. CTNum ctNum = this.ctNumbering.addNewNum();
  140. ctNum.addNewAbstractNumId();
  141. ctNum.getAbstractNumId().setVal(abstractNumID);
  142. ctNum.setNumId(numID);
  143. XWPFNum num = new XWPFNum(ctNum, this);
  144. nums.add(num);
  145. }
  146. /**
  147. * get Num by NumID
  148. *
  149. * @return abstractNum with NumId if no Num exists with that NumID
  150. * null will be returned
  151. */
  152. public XWPFNum getNum(BigInteger numID) {
  153. for (XWPFNum num : nums) {
  154. if (num.getCTNum().getNumId().equals(numID))
  155. return num;
  156. }
  157. return null;
  158. }
  159. /**
  160. * get AbstractNum by abstractNumID
  161. *
  162. * @return abstractNum with abstractNumId if no abstractNum exists with that abstractNumID
  163. * null will be returned
  164. */
  165. public XWPFAbstractNum getAbstractNum(BigInteger abstractNumID) {
  166. for (XWPFAbstractNum abstractNum : abstractNums) {
  167. if (abstractNum.getAbstractNum().getAbstractNumId().equals(abstractNumID)) {
  168. return abstractNum;
  169. }
  170. }
  171. return null;
  172. }
  173. /**
  174. * Compare AbstractNum with abstractNums of this numbering document.
  175. * If the content of abstractNum equals with an abstractNum of the List in numbering
  176. * the BigInteger Value of it will be returned.
  177. * If no equal abstractNum is existing null will be returned
  178. *
  179. * @return BigInteger
  180. */
  181. public BigInteger getIdOfAbstractNum(XWPFAbstractNum abstractNum) {
  182. CTAbstractNum copy = (CTAbstractNum) abstractNum.getCTAbstractNum().copy();
  183. XWPFAbstractNum newAbstractNum = new XWPFAbstractNum(copy, this);
  184. int i;
  185. for (i = 0; i < abstractNums.size(); i++) {
  186. newAbstractNum.getCTAbstractNum().setAbstractNumId(BigInteger.valueOf(i));
  187. newAbstractNum.setNumbering(this);
  188. if (newAbstractNum.getCTAbstractNum().valueEquals(abstractNums.get(i).getCTAbstractNum())) {
  189. return newAbstractNum.getCTAbstractNum().getAbstractNumId();
  190. }
  191. }
  192. return null;
  193. }
  194. /**
  195. * add a new AbstractNum and return its AbstractNumID
  196. */
  197. public BigInteger addAbstractNum(XWPFAbstractNum abstractNum) {
  198. int pos = abstractNums.size();
  199. if (abstractNum.getAbstractNum() != null) { // Use the current CTAbstractNum if it exists
  200. ctNumbering.addNewAbstractNum().set(abstractNum.getAbstractNum());
  201. } else {
  202. abstractNum.setCtAbstractNum(ctNumbering.addNewAbstractNum());
  203. abstractNum.getAbstractNum().setAbstractNumId(BigInteger.valueOf(pos));
  204. ctNumbering.setAbstractNumArray(pos, abstractNum.getAbstractNum());
  205. }
  206. abstractNums.add(abstractNum);
  207. return abstractNum.getCTAbstractNum().getAbstractNumId();
  208. }
  209. /**
  210. * remove an existing abstractNum
  211. *
  212. * @return true if abstractNum with abstractNumID exists in NumberingArray,
  213. * false if abstractNum with abstractNumID not exists
  214. */
  215. public boolean removeAbstractNum(BigInteger abstractNumID) {
  216. for (XWPFAbstractNum abstractNum : abstractNums) {
  217. BigInteger foundNumId = abstractNum.getAbstractNum().getAbstractNumId();
  218. if(abstractNumID.equals(foundNumId)) {
  219. abstractNums.remove(abstractNum);
  220. break;
  221. }
  222. }
  223. for (int i = 0; i < ctNumbering.sizeOfAbstractNumArray(); i++) {
  224. CTAbstractNum ctAbstractNum = ctNumbering.getAbstractNumArray(i);
  225. BigInteger foundNumId = ctAbstractNum.getAbstractNumId();
  226. if(abstractNumID.equals(foundNumId)) {
  227. ctNumbering.removeAbstractNum(i);
  228. return true;
  229. }
  230. }
  231. return false;
  232. }
  233. /**
  234. * return the abstractNumID
  235. * If the AbstractNumID not exists
  236. * return null
  237. *
  238. * @return abstractNumID
  239. */
  240. public BigInteger getAbstractNumID(BigInteger numID) {
  241. XWPFNum num = getNum(numID);
  242. if (num == null)
  243. return null;
  244. if (num.getCTNum() == null)
  245. return null;
  246. if (num.getCTNum().getAbstractNumId() == null)
  247. return null;
  248. return num.getCTNum().getAbstractNumId().getVal();
  249. }
  250. /**
  251. * @return all abstractNums
  252. */
  253. public List<XWPFAbstractNum> getAbstractNums() {
  254. return Collections.unmodifiableList(abstractNums);
  255. }
  256. /**
  257. * @return all nums
  258. */
  259. public List<XWPFNum> getNums() {
  260. return Collections.unmodifiableList(nums);
  261. }
  262. }