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.

XWPFDocument.java 53KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477
  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.ByteArrayOutputStream;
  17. import java.io.IOException;
  18. import java.io.InputStream;
  19. import java.io.OutputStream;
  20. import java.lang.reflect.Method;
  21. import java.util.ArrayList;
  22. import java.util.Arrays;
  23. import java.util.Collection;
  24. import java.util.Collections;
  25. import java.util.HashMap;
  26. import java.util.Iterator;
  27. import java.util.LinkedList;
  28. import java.util.List;
  29. import java.util.Map;
  30. import javax.xml.namespace.QName;
  31. import org.apache.poi.POIXMLDocument;
  32. import org.apache.poi.POIXMLDocumentPart;
  33. import org.apache.poi.POIXMLException;
  34. import org.apache.poi.POIXMLProperties;
  35. import org.apache.poi.POIXMLRelation;
  36. import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
  37. import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
  38. import org.apache.poi.openxml4j.opc.OPCPackage;
  39. import org.apache.poi.openxml4j.opc.PackagePart;
  40. import org.apache.poi.openxml4j.opc.PackagePartName;
  41. import org.apache.poi.openxml4j.opc.PackageRelationship;
  42. import org.apache.poi.openxml4j.opc.PackageRelationshipTypes;
  43. import org.apache.poi.openxml4j.opc.PackagingURIHelper;
  44. import org.apache.poi.openxml4j.opc.TargetMode;
  45. import org.apache.poi.poifs.crypt.HashAlgorithm;
  46. import org.apache.poi.util.IOUtils;
  47. import org.apache.poi.util.IdentifierManager;
  48. import org.apache.poi.util.Internal;
  49. import org.apache.poi.util.PackageHelper;
  50. import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;
  51. import org.apache.xmlbeans.XmlCursor;
  52. import org.apache.xmlbeans.XmlException;
  53. import org.apache.xmlbeans.XmlObject;
  54. import org.apache.xmlbeans.XmlOptions;
  55. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTComment;
  56. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDocument1;
  57. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFtnEdn;
  58. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
  59. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRow;
  60. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSdtBlock;
  61. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTStyles;
  62. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl;
  63. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTc;
  64. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CommentsDocument;
  65. import org.openxmlformats.schemas.wordprocessingml.x2006.main.DocumentDocument;
  66. import org.openxmlformats.schemas.wordprocessingml.x2006.main.EndnotesDocument;
  67. import org.openxmlformats.schemas.wordprocessingml.x2006.main.FootnotesDocument;
  68. import org.openxmlformats.schemas.wordprocessingml.x2006.main.NumberingDocument;
  69. import org.openxmlformats.schemas.wordprocessingml.x2006.main.STDocProtect;
  70. import org.openxmlformats.schemas.wordprocessingml.x2006.main.StylesDocument;
  71. /**
  72. * <p>High(ish) level class for working with .docx files.</p>
  73. *
  74. * <p>This class tries to hide some of the complexity
  75. * of the underlying file format, but as it's not a
  76. * mature and stable API yet, certain parts of the
  77. * XML structure come through. You'll therefore almost
  78. * certainly need to refer to the OOXML specifications
  79. * from
  80. * http://www.ecma-international.org/publications/standards/Ecma-376.htm
  81. * at some point in your use.</p>
  82. */
  83. public class XWPFDocument extends POIXMLDocument implements Document, IBody {
  84. private CTDocument1 ctDocument;
  85. private XWPFSettings settings;
  86. /**
  87. * Keeps track on all id-values used in this document and included parts, like headers, footers, etc.
  88. */
  89. private IdentifierManager drawingIdManager = new IdentifierManager(1L,4294967295L);
  90. protected List<XWPFFooter> footers = new ArrayList<XWPFFooter>();
  91. protected List<XWPFHeader> headers = new ArrayList<XWPFHeader>();
  92. protected List<XWPFComment> comments = new ArrayList<XWPFComment>();
  93. protected List<XWPFHyperlink> hyperlinks = new ArrayList<XWPFHyperlink>();
  94. protected List<XWPFParagraph> paragraphs = new ArrayList<XWPFParagraph>();
  95. protected List<XWPFTable> tables = new ArrayList<XWPFTable>();
  96. protected List<XWPFSDT> contentControls = new ArrayList<XWPFSDT>();
  97. protected List<IBodyElement> bodyElements = new ArrayList<IBodyElement>();
  98. protected List<XWPFPictureData> pictures = new ArrayList<XWPFPictureData>();
  99. protected Map<Long, List<XWPFPictureData>> packagePictures = new HashMap<Long, List<XWPFPictureData>>();
  100. protected Map<Integer, XWPFFootnote> endnotes = new HashMap<Integer, XWPFFootnote>();
  101. protected XWPFNumbering numbering;
  102. protected XWPFStyles styles;
  103. protected XWPFFootnotes footnotes;
  104. /** Handles the joy of different headers/footers for different pages */
  105. private XWPFHeaderFooterPolicy headerFooterPolicy;
  106. public XWPFDocument(OPCPackage pkg) throws IOException {
  107. super(pkg);
  108. //build a tree of POIXMLDocumentParts, this document being the root
  109. load(XWPFFactory.getInstance());
  110. }
  111. public XWPFDocument(InputStream is) throws IOException {
  112. super(PackageHelper.open(is));
  113. //build a tree of POIXMLDocumentParts, this workbook being the root
  114. load(XWPFFactory.getInstance());
  115. }
  116. public XWPFDocument(){
  117. super(newPackage());
  118. onDocumentCreate();
  119. }
  120. @Override
  121. protected void onDocumentRead() throws IOException {
  122. try {
  123. DocumentDocument doc = DocumentDocument.Factory.parse(getPackagePart().getInputStream());
  124. ctDocument = doc.getDocument();
  125. initFootnotes();
  126. // parse the document with cursor and add
  127. // the XmlObject to its lists
  128. XmlCursor cursor = ctDocument.getBody().newCursor();
  129. cursor.selectPath("./*");
  130. while (cursor.toNextSelection()) {
  131. XmlObject o = cursor.getObject();
  132. if (o instanceof CTP) {
  133. XWPFParagraph p = new XWPFParagraph((CTP) o, this);
  134. bodyElements.add(p);
  135. paragraphs.add(p);
  136. } else if (o instanceof CTTbl) {
  137. XWPFTable t = new XWPFTable((CTTbl) o, this);
  138. bodyElements.add(t);
  139. tables.add(t);
  140. } else if (o instanceof CTSdtBlock){
  141. XWPFSDT c = new XWPFSDT((CTSdtBlock)o, this);
  142. bodyElements.add(c);
  143. contentControls.add(c);
  144. }
  145. }
  146. cursor.dispose();
  147. // Sort out headers and footers
  148. if (doc.getDocument().getBody().getSectPr() != null)
  149. headerFooterPolicy = new XWPFHeaderFooterPolicy(this);
  150. // Create for each XML-part in the Package a PartClass
  151. for (POIXMLDocumentPart p : getRelations()) {
  152. String relation = p.getPackageRelationship().getRelationshipType();
  153. if (relation.equals(XWPFRelation.STYLES.getRelation())) {
  154. this.styles = (XWPFStyles) p;
  155. this.styles.onDocumentRead();
  156. } else if (relation.equals(XWPFRelation.NUMBERING.getRelation())) {
  157. this.numbering = (XWPFNumbering) p;
  158. this.numbering.onDocumentRead();
  159. } else if (relation.equals(XWPFRelation.FOOTER.getRelation())) {
  160. XWPFFooter footer = (XWPFFooter) p;
  161. footers.add(footer);
  162. footer.onDocumentRead();
  163. } else if (relation.equals(XWPFRelation.HEADER.getRelation())) {
  164. XWPFHeader header = (XWPFHeader) p;
  165. headers.add(header);
  166. header.onDocumentRead();
  167. } else if (relation.equals(XWPFRelation.COMMENT.getRelation())) {
  168. // TODO Create according XWPFComment class, extending POIXMLDocumentPart
  169. CommentsDocument cmntdoc = CommentsDocument.Factory.parse(p.getPackagePart().getInputStream());
  170. for (CTComment ctcomment : cmntdoc.getComments().getCommentArray()) {
  171. comments.add(new XWPFComment(ctcomment, this));
  172. }
  173. } else if (relation.equals(XWPFRelation.SETTINGS.getRelation())) {
  174. settings = (XWPFSettings) p;
  175. settings.onDocumentRead();
  176. } else if (relation.equals(XWPFRelation.IMAGES.getRelation())) {
  177. XWPFPictureData picData = (XWPFPictureData) p;
  178. picData.onDocumentRead();
  179. registerPackagePictureData(picData);
  180. pictures.add(picData);
  181. } else if (relation.equals(XWPFRelation.GLOSSARY_DOCUMENT.getRelation())) {
  182. // We don't currently process the glossary itself
  183. // Until we do, we do need to load the glossary child parts of it
  184. for (POIXMLDocumentPart gp : p.getRelations()) {
  185. // Trigger the onDocumentRead for all the child parts
  186. // Otherwise we'll hit issues on Styles, Settings etc on save
  187. try {
  188. Method onDocumentRead = gp.getClass().getDeclaredMethod("onDocumentRead");
  189. onDocumentRead.setAccessible(true);
  190. onDocumentRead.invoke(gp);
  191. } catch(Exception e) {
  192. throw new POIXMLException(e);
  193. }
  194. }
  195. }
  196. }
  197. initHyperlinks();
  198. } catch (XmlException e) {
  199. throw new POIXMLException(e);
  200. }
  201. }
  202. private void initHyperlinks(){
  203. // Get the hyperlinks
  204. // TODO: make me optional/separated in private function
  205. try {
  206. Iterator<PackageRelationship> relIter =
  207. getPackagePart().getRelationshipsByType(XWPFRelation.HYPERLINK.getRelation()).iterator();
  208. while(relIter.hasNext()) {
  209. PackageRelationship rel = relIter.next();
  210. hyperlinks.add(new XWPFHyperlink(rel.getId(), rel.getTargetURI().toString()));
  211. }
  212. } catch (InvalidFormatException e){
  213. throw new POIXMLException(e);
  214. }
  215. }
  216. @SuppressWarnings("deprecation")
  217. private void initFootnotes() throws XmlException, IOException {
  218. for(POIXMLDocumentPart p : getRelations()){
  219. String relation = p.getPackageRelationship().getRelationshipType();
  220. if (relation.equals(XWPFRelation.FOOTNOTE.getRelation())) {
  221. this.footnotes = (XWPFFootnotes)p;
  222. this.footnotes.onDocumentRead();
  223. } else if (relation.equals(XWPFRelation.ENDNOTE.getRelation())){
  224. EndnotesDocument endnotesDocument = EndnotesDocument.Factory.parse(p.getPackagePart().getInputStream());
  225. for(CTFtnEdn ctFtnEdn : endnotesDocument.getEndnotes().getEndnoteArray()) {
  226. endnotes.put(ctFtnEdn.getId().intValue(), new XWPFFootnote(this, ctFtnEdn));
  227. }
  228. }
  229. }
  230. }
  231. /**
  232. * Create a new WordProcessingML package and setup the default minimal content
  233. */
  234. protected static OPCPackage newPackage() {
  235. try {
  236. OPCPackage pkg = OPCPackage.create(new ByteArrayOutputStream());
  237. // Main part
  238. PackagePartName corePartName = PackagingURIHelper.createPartName(XWPFRelation.DOCUMENT.getDefaultFileName());
  239. // Create main part relationship
  240. pkg.addRelationship(corePartName, TargetMode.INTERNAL, PackageRelationshipTypes.CORE_DOCUMENT);
  241. // Create main document part
  242. pkg.createPart(corePartName, XWPFRelation.DOCUMENT.getContentType());
  243. pkg.getPackageProperties().setCreatorProperty(DOCUMENT_CREATOR);
  244. return pkg;
  245. } catch (Exception e){
  246. throw new POIXMLException(e);
  247. }
  248. }
  249. /**
  250. * Create a new CTWorkbook with all values set to default
  251. */
  252. @Override
  253. protected void onDocumentCreate() {
  254. ctDocument = CTDocument1.Factory.newInstance();
  255. ctDocument.addNewBody();
  256. settings = (XWPFSettings) createRelationship(XWPFRelation.SETTINGS,XWPFFactory.getInstance());
  257. POIXMLProperties.ExtendedProperties expProps = getProperties().getExtendedProperties();
  258. expProps.getUnderlyingProperties().setApplication(DOCUMENT_CREATOR);
  259. }
  260. /**
  261. * Returns the low level document base object
  262. */
  263. @Internal
  264. public CTDocument1 getDocument() {
  265. return ctDocument;
  266. }
  267. IdentifierManager getDrawingIdManager() {
  268. return drawingIdManager;
  269. }
  270. /**
  271. * returns an Iterator with paragraphs and tables
  272. * @see org.apache.poi.xwpf.usermodel.IBody#getBodyElements()
  273. */
  274. @Override
  275. public List<IBodyElement> getBodyElements() {
  276. return Collections.unmodifiableList(bodyElements);
  277. }
  278. public Iterator<IBodyElement> getBodyElementsIterator() {
  279. return bodyElements.iterator();
  280. }
  281. /**
  282. * @see org.apache.poi.xwpf.usermodel.IBody#getParagraphs()
  283. */
  284. @Override
  285. public List<XWPFParagraph> getParagraphs(){
  286. return Collections.unmodifiableList(paragraphs);
  287. }
  288. /**
  289. * @see org.apache.poi.xwpf.usermodel.IBody#getTables()
  290. */
  291. @Override
  292. public List<XWPFTable> getTables(){
  293. return Collections.unmodifiableList(tables);
  294. }
  295. /**
  296. * @see org.apache.poi.xwpf.usermodel.IBody#getTableArray(int)
  297. */
  298. @Override
  299. public XWPFTable getTableArray(int pos) {
  300. if (pos > 0 && pos < tables.size()){
  301. return tables.get(pos);
  302. }
  303. return null;
  304. }
  305. /**
  306. *
  307. * @return the list of footers
  308. */
  309. public List<XWPFFooter> getFooterList(){
  310. return Collections.unmodifiableList(footers);
  311. }
  312. public XWPFFooter getFooterArray(int pos){
  313. return footers.get(pos);
  314. }
  315. /**
  316. *
  317. * @return the list of headers
  318. */
  319. public List<XWPFHeader> getHeaderList(){
  320. return Collections.unmodifiableList(headers);
  321. }
  322. public XWPFHeader getHeaderArray(int pos){
  323. return headers.get(pos);
  324. }
  325. public String getTblStyle(XWPFTable table){
  326. return table.getStyleID();
  327. }
  328. public XWPFHyperlink getHyperlinkByID(String id) {
  329. for (XWPFHyperlink link : hyperlinks) {
  330. if (link.getId().equals(id))
  331. return link;
  332. }
  333. return null;
  334. }
  335. public XWPFFootnote getFootnoteByID(int id) {
  336. if(footnotes == null) return null;
  337. return footnotes.getFootnoteById(id);
  338. }
  339. public XWPFFootnote getEndnoteByID(int id) {
  340. if(endnotes == null) return null;
  341. return endnotes.get(id);
  342. }
  343. public List<XWPFFootnote> getFootnotes() {
  344. if(footnotes == null) {
  345. return Collections.emptyList();
  346. }
  347. return footnotes.getFootnotesList();
  348. }
  349. public XWPFHyperlink[] getHyperlinks() {
  350. return hyperlinks.toArray(new XWPFHyperlink[hyperlinks.size()]);
  351. }
  352. public XWPFComment getCommentByID(String id) {
  353. for (XWPFComment comment : comments) {
  354. if (comment.getId().equals(id))
  355. return comment;
  356. }
  357. return null;
  358. }
  359. public XWPFComment[] getComments() {
  360. return comments.toArray(new XWPFComment[comments.size()]);
  361. }
  362. /**
  363. * Get the document part that's defined as the
  364. * given relationship of the core document.
  365. */
  366. public PackagePart getPartById(String id) {
  367. try {
  368. PackagePart corePart = getCorePart();
  369. return corePart.getRelatedPart(corePart.getRelationship(id));
  370. } catch (InvalidFormatException e) {
  371. throw new IllegalArgumentException(e);
  372. }
  373. }
  374. /**
  375. * Returns the policy on headers and footers, which
  376. * also provides a way to get at them.
  377. */
  378. public XWPFHeaderFooterPolicy getHeaderFooterPolicy() {
  379. return headerFooterPolicy;
  380. }
  381. /**
  382. * Returns the styles object used
  383. */
  384. @Internal
  385. public CTStyles getStyle() throws XmlException, IOException {
  386. PackagePart[] parts;
  387. try {
  388. parts = getRelatedByType(XWPFRelation.STYLES.getRelation());
  389. } catch(InvalidFormatException e) {
  390. throw new IllegalStateException(e);
  391. }
  392. if(parts.length != 1) {
  393. throw new IllegalStateException("Expecting one Styles document part, but found " + parts.length);
  394. }
  395. StylesDocument sd = StylesDocument.Factory.parse(parts[0].getInputStream());
  396. return sd.getStyles();
  397. }
  398. /**
  399. * Get the document's embedded files.
  400. */
  401. @Override
  402. public List<PackagePart> getAllEmbedds() throws OpenXML4JException {
  403. List<PackagePart> embedds = new LinkedList<PackagePart>();
  404. // Get the embeddings for the workbook
  405. PackagePart part = getPackagePart();
  406. for (PackageRelationship rel : getPackagePart().getRelationshipsByType(OLE_OBJECT_REL_TYPE)) {
  407. embedds.add(part.getRelatedPart(rel));
  408. }
  409. for (PackageRelationship rel : getPackagePart().getRelationshipsByType(PACK_OBJECT_REL_TYPE)) {
  410. embedds.add(part.getRelatedPart(rel));
  411. }
  412. return embedds;
  413. }
  414. /**
  415. * Finds that for example the 2nd entry in the body list is the 1st paragraph
  416. */
  417. private int getBodyElementSpecificPos(int pos, List<? extends IBodyElement> list) {
  418. // If there's nothing to find, skip it
  419. if(list.size() == 0) {
  420. return -1;
  421. }
  422. if(pos >= 0 && pos < bodyElements.size()) {
  423. // Ensure the type is correct
  424. IBodyElement needle = bodyElements.get(pos);
  425. if(needle.getElementType() != list.get(0).getElementType()) {
  426. // Wrong type
  427. return -1;
  428. }
  429. // Work back until we find it
  430. int startPos = Math.min(pos, list.size()-1);
  431. for(int i=startPos; i>=0; i--) {
  432. if(list.get(i) == needle) {
  433. return i;
  434. }
  435. }
  436. }
  437. // Couldn't be found
  438. return -1;
  439. }
  440. /**
  441. * Look up the paragraph at the specified position in the body elements list
  442. * and return this paragraphs position in the paragraphs list
  443. *
  444. * @param pos
  445. * The position of the relevant paragraph in the body elements
  446. * list
  447. * @return the position of the paragraph in the paragraphs list, if there is
  448. * a paragraph at the position in the bodyelements list. Else it
  449. * will return -1
  450. *
  451. */
  452. public int getParagraphPos(int pos) {
  453. return getBodyElementSpecificPos(pos, paragraphs);
  454. }
  455. /**
  456. * get with the position of a table in the bodyelement array list
  457. * the position of this table in the table array list
  458. * @param pos position of the table in the bodyelement array list
  459. * @return if there is a table at the position in the bodyelement array list,
  460. * else it will return null.
  461. */
  462. public int getTablePos(int pos) {
  463. return getBodyElementSpecificPos(pos, tables);
  464. }
  465. /**
  466. * Add a new paragraph at position of the cursor. The cursor must be on the
  467. * {@link org.apache.xmlbeans.XmlCursor.TokenType#START} tag of an subelement
  468. * of the documents body. When this method is done, the cursor passed as
  469. * parameter points to the {@link org.apache.xmlbeans.XmlCursor.TokenType#END}
  470. * of the newly inserted paragraph.
  471. *
  472. * @param cursor
  473. * @return the {@link XWPFParagraph} object representing the newly inserted
  474. * CTP object
  475. */
  476. @Override
  477. public XWPFParagraph insertNewParagraph(XmlCursor cursor) {
  478. if (isCursorInBody(cursor)) {
  479. String uri = CTP.type.getName().getNamespaceURI();
  480. /*
  481. * TODO DO not use a coded constant, find the constant in the OOXML
  482. * classes instead, as the child of type CT_Paragraph is defined in the
  483. * OOXML schema as 'p'
  484. */
  485. String localPart = "p";
  486. // creates a new Paragraph, cursor is positioned inside the new
  487. // element
  488. cursor.beginElement(localPart, uri);
  489. // move the cursor to the START token to the paragraph just created
  490. cursor.toParent();
  491. CTP p = (CTP) cursor.getObject();
  492. XWPFParagraph newP = new XWPFParagraph(p, this);
  493. XmlObject o = null;
  494. /*
  495. * move the cursor to the previous element until a) the next
  496. * paragraph is found or b) all elements have been passed
  497. */
  498. while (!(o instanceof CTP) && (cursor.toPrevSibling())) {
  499. o = cursor.getObject();
  500. }
  501. /*
  502. * if the object that has been found is a) not a paragraph or b) is
  503. * the paragraph that has just been inserted, as the cursor in the
  504. * while loop above was not moved as there were no other siblings,
  505. * then the paragraph that was just inserted is the first paragraph
  506. * in the body. Otherwise, take the previous paragraph and calculate
  507. * the new index for the new paragraph.
  508. */
  509. if ((!(o instanceof CTP)) || (CTP) o == p) {
  510. paragraphs.add(0, newP);
  511. } else {
  512. int pos = paragraphs.indexOf(getParagraph((CTP) o)) + 1;
  513. paragraphs.add(pos, newP);
  514. }
  515. /*
  516. * create a new cursor, that points to the START token of the just
  517. * inserted paragraph
  518. */
  519. XmlCursor newParaPos = p.newCursor();
  520. try {
  521. /*
  522. * Calculate the paragraphs index in the list of all body
  523. * elements
  524. */
  525. int i = 0;
  526. cursor.toCursor(newParaPos);
  527. while (cursor.toPrevSibling()) {
  528. o = cursor.getObject();
  529. if (o instanceof CTP || o instanceof CTTbl)
  530. i++;
  531. }
  532. bodyElements.add(i, newP);
  533. cursor.toCursor(newParaPos);
  534. cursor.toEndToken();
  535. return newP;
  536. } finally {
  537. newParaPos.dispose();
  538. }
  539. }
  540. return null;
  541. }
  542. @Override
  543. public XWPFTable insertNewTbl(XmlCursor cursor) {
  544. if (isCursorInBody(cursor)) {
  545. String uri = CTTbl.type.getName().getNamespaceURI();
  546. String localPart = "tbl";
  547. cursor.beginElement(localPart, uri);
  548. cursor.toParent();
  549. CTTbl t = (CTTbl) cursor.getObject();
  550. XWPFTable newT = new XWPFTable(t, this);
  551. XmlObject o = null;
  552. while (!(o instanceof CTTbl) && (cursor.toPrevSibling())) {
  553. o = cursor.getObject();
  554. }
  555. if (!(o instanceof CTTbl)) {
  556. tables.add(0, newT);
  557. } else {
  558. int pos = tables.indexOf(getTable((CTTbl) o)) + 1;
  559. tables.add(pos, newT);
  560. }
  561. int i = 0;
  562. XmlCursor tableCursor = t.newCursor();
  563. try {
  564. cursor.toCursor(tableCursor);
  565. while (cursor.toPrevSibling()) {
  566. o = cursor.getObject();
  567. if (o instanceof CTP || o instanceof CTTbl)
  568. i++;
  569. }
  570. bodyElements.add(i, newT);
  571. cursor.toCursor(tableCursor);
  572. cursor.toEndToken();
  573. return newT;
  574. }
  575. finally {
  576. tableCursor.dispose();
  577. }
  578. }
  579. return null;
  580. }
  581. /**
  582. * verifies that cursor is on the right position
  583. * @param cursor
  584. */
  585. private boolean isCursorInBody(XmlCursor cursor) {
  586. XmlCursor verify = cursor.newCursor();
  587. verify.toParent();
  588. try {
  589. return (verify.getObject() == this.ctDocument.getBody());
  590. } finally {
  591. verify.dispose();
  592. }
  593. }
  594. private int getPosOfBodyElement(IBodyElement needle) {
  595. BodyElementType type = needle.getElementType();
  596. IBodyElement current;
  597. for(int i=0; i<bodyElements.size(); i++) {
  598. current = bodyElements.get(i);
  599. if(current.getElementType() == type) {
  600. if(current.equals(needle)) {
  601. return i;
  602. }
  603. }
  604. }
  605. return -1;
  606. }
  607. /**
  608. * Get the position of the paragraph, within the list
  609. * of all the body elements.
  610. * @param p The paragraph to find
  611. * @return The location, or -1 if the paragraph couldn't be found
  612. */
  613. public int getPosOfParagraph(XWPFParagraph p){
  614. return getPosOfBodyElement(p);
  615. }
  616. /**
  617. * Get the position of the table, within the list of
  618. * all the body elements.
  619. * @param t The table to find
  620. * @return The location, or -1 if the table couldn't be found
  621. */
  622. public int getPosOfTable(XWPFTable t){
  623. return getPosOfBodyElement(t);
  624. }
  625. /**
  626. * commit and saves the document
  627. */
  628. @Override
  629. protected void commit() throws IOException {
  630. XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS);
  631. xmlOptions.setSaveSyntheticDocumentElement(new QName(CTDocument1.type.getName().getNamespaceURI(), "document"));
  632. Map<String, String> map = new HashMap<String, String>();
  633. map.put("http://schemas.openxmlformats.org/officeDocument/2006/math", "m");
  634. map.put("urn:schemas-microsoft-com:office:office", "o");
  635. map.put("http://schemas.openxmlformats.org/officeDocument/2006/relationships", "r");
  636. map.put("urn:schemas-microsoft-com:vml", "v");
  637. map.put("http://schemas.openxmlformats.org/markup-compatibility/2006", "ve");
  638. map.put("http://schemas.openxmlformats.org/wordprocessingml/2006/main", "w");
  639. map.put("urn:schemas-microsoft-com:office:word", "w10");
  640. map.put("http://schemas.microsoft.com/office/word/2006/wordml", "wne");
  641. map.put("http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing", "wp");
  642. xmlOptions.setSaveSuggestedPrefixes(map);
  643. PackagePart part = getPackagePart();
  644. OutputStream out = part.getOutputStream();
  645. ctDocument.save(out, xmlOptions);
  646. out.close();
  647. }
  648. /**
  649. * Gets the index of the relation we're trying to create
  650. * @param relation
  651. * @return i
  652. */
  653. private int getRelationIndex(XWPFRelation relation) {
  654. List<POIXMLDocumentPart> relations = getRelations();
  655. int i = 1;
  656. for (Iterator<POIXMLDocumentPart> it = relations.iterator(); it.hasNext() ; ) {
  657. POIXMLDocumentPart item = it.next();
  658. if (item.getPackageRelationship().getRelationshipType().equals(relation.getRelation())) {
  659. i++;
  660. }
  661. }
  662. return i;
  663. }
  664. /**
  665. * Appends a new paragraph to this document
  666. * @return a new paragraph
  667. */
  668. public XWPFParagraph createParagraph() {
  669. XWPFParagraph p = new XWPFParagraph(ctDocument.getBody().addNewP(), this);
  670. bodyElements.add(p);
  671. paragraphs.add(p);
  672. return p;
  673. }
  674. /**
  675. * Creates an empty numbering if one does not already exist and sets the numbering member
  676. * @return numbering
  677. */
  678. public XWPFNumbering createNumbering() {
  679. if(numbering == null) {
  680. NumberingDocument numberingDoc = NumberingDocument.Factory.newInstance();
  681. XWPFRelation relation = XWPFRelation.NUMBERING;
  682. int i = getRelationIndex(relation);
  683. XWPFNumbering wrapper = (XWPFNumbering)createRelationship(relation, XWPFFactory.getInstance(), i);
  684. wrapper.setNumbering(numberingDoc.addNewNumbering());
  685. numbering = wrapper;
  686. }
  687. return numbering;
  688. }
  689. /**
  690. * Creates an empty styles for the document if one does not already exist
  691. * @return styles
  692. */
  693. public XWPFStyles createStyles() {
  694. if(styles == null) {
  695. StylesDocument stylesDoc = StylesDocument.Factory.newInstance();
  696. XWPFRelation relation = XWPFRelation.STYLES;
  697. int i = getRelationIndex(relation);
  698. XWPFStyles wrapper = (XWPFStyles)createRelationship(relation, XWPFFactory.getInstance(), i);
  699. wrapper.setStyles(stylesDoc.addNewStyles());
  700. styles = wrapper;
  701. }
  702. return styles;
  703. }
  704. /**
  705. * Creates an empty footnotes element for the document if one does not already exist
  706. * @return footnotes
  707. */
  708. public XWPFFootnotes createFootnotes() {
  709. if(footnotes == null) {
  710. FootnotesDocument footnotesDoc = FootnotesDocument.Factory.newInstance();
  711. XWPFRelation relation = XWPFRelation.FOOTNOTE;
  712. int i = getRelationIndex(relation);
  713. XWPFFootnotes wrapper = (XWPFFootnotes)createRelationship(relation, XWPFFactory.getInstance(), i);
  714. wrapper.setFootnotes(footnotesDoc.addNewFootnotes());
  715. footnotes = wrapper;
  716. }
  717. return footnotes;
  718. }
  719. public XWPFFootnote addFootnote(CTFtnEdn note) {
  720. return footnotes.addFootnote(note);
  721. }
  722. public XWPFFootnote addEndnote(CTFtnEdn note) {
  723. XWPFFootnote endnote = new XWPFFootnote(this, note);
  724. endnotes.put(note.getId().intValue(), endnote);
  725. return endnote;
  726. }
  727. /**
  728. * remove a BodyElement from bodyElements array list
  729. * @param pos
  730. * @return true if removing was successfully, else return false
  731. */
  732. public boolean removeBodyElement(int pos) {
  733. if (pos >= 0 && pos < bodyElements.size()) {
  734. BodyElementType type = bodyElements.get(pos).getElementType();
  735. if (type == BodyElementType.TABLE){
  736. int tablePos = getTablePos(pos);
  737. tables.remove(tablePos);
  738. ctDocument.getBody().removeTbl(tablePos);
  739. }
  740. if (type == BodyElementType.PARAGRAPH) {
  741. int paraPos = getParagraphPos(pos);
  742. paragraphs.remove(paraPos);
  743. ctDocument.getBody().removeP(paraPos);
  744. }
  745. bodyElements.remove(pos);
  746. return true;
  747. }
  748. return false;
  749. }
  750. /**
  751. * copies content of a paragraph to a existing paragraph in the list paragraphs at position pos
  752. * @param paragraph
  753. * @param pos
  754. */
  755. public void setParagraph(XWPFParagraph paragraph, int pos) {
  756. paragraphs.set(pos, paragraph);
  757. ctDocument.getBody().setPArray(pos, paragraph.getCTP());
  758. /* TODO update body element, update xwpf element, verify that
  759. * incoming paragraph belongs to this document or if not, XML was
  760. * copied properly (namespace-abbreviations, etc.)
  761. */
  762. }
  763. /**
  764. * @return the LastParagraph of the document
  765. */
  766. public XWPFParagraph getLastParagraph() {
  767. int lastPos = paragraphs.toArray().length - 1;
  768. return paragraphs.get(lastPos);
  769. }
  770. /**
  771. * Create an empty table with one row and one column as default.
  772. * @return a new table
  773. */
  774. public XWPFTable createTable() {
  775. XWPFTable table = new XWPFTable(ctDocument.getBody().addNewTbl(), this);
  776. bodyElements.add(table);
  777. tables.add(table);
  778. return table;
  779. }
  780. /**
  781. * Create an empty table with a number of rows and cols specified
  782. * @param rows
  783. * @param cols
  784. * @return table
  785. */
  786. public XWPFTable createTable(int rows, int cols) {
  787. XWPFTable table = new XWPFTable(ctDocument.getBody().addNewTbl(), this, rows, cols);
  788. bodyElements.add(table);
  789. tables.add(table);
  790. return table;
  791. }
  792. /**
  793. *
  794. */
  795. public void createTOC() {
  796. CTSdtBlock block = this.getDocument().getBody().addNewSdt();
  797. TOC toc = new TOC(block);
  798. for (XWPFParagraph par : paragraphs) {
  799. String parStyle = par.getStyle();
  800. if (parStyle != null && parStyle.startsWith("Heading")) {
  801. try {
  802. int level = Integer.valueOf(parStyle.substring("Heading".length())).intValue();
  803. toc.addRow(level, par.getText(), 1, "112723803");
  804. } catch (NumberFormatException e) {
  805. e.printStackTrace();
  806. }
  807. }
  808. }
  809. }
  810. /**Replace content of table in array tables at position pos with a
  811. * @param pos
  812. * @param table
  813. */
  814. public void setTable(int pos, XWPFTable table) {
  815. tables.set(pos, table);
  816. ctDocument.getBody().setTblArray(pos, table.getCTTbl());
  817. }
  818. /**
  819. * Verifies that the documentProtection tag in settings.xml file <br/>
  820. * specifies that the protection is enforced (w:enforcement="1") <br/>
  821. * and that the kind of protection is readOnly (w:edit="readOnly")<br/>
  822. * <br/>
  823. * sample snippet from settings.xml
  824. * <pre>
  825. * &lt;w:settings ... &gt;
  826. * &lt;w:documentProtection w:edit=&quot;readOnly&quot; w:enforcement=&quot;1&quot;/&gt;
  827. * </pre>
  828. *
  829. * @return true if documentProtection is enforced with option readOnly
  830. */
  831. public boolean isEnforcedReadonlyProtection() {
  832. return settings.isEnforcedWith(STDocProtect.READ_ONLY);
  833. }
  834. /**
  835. * Verifies that the documentProtection tag in settings.xml file <br/>
  836. * specifies that the protection is enforced (w:enforcement="1") <br/>
  837. * and that the kind of protection is forms (w:edit="forms")<br/>
  838. * <br/>
  839. * sample snippet from settings.xml
  840. * <pre>
  841. * &lt;w:settings ... &gt;
  842. * &lt;w:documentProtection w:edit=&quot;forms&quot; w:enforcement=&quot;1&quot;/&gt;
  843. * </pre>
  844. *
  845. * @return true if documentProtection is enforced with option forms
  846. */
  847. public boolean isEnforcedFillingFormsProtection() {
  848. return settings.isEnforcedWith(STDocProtect.FORMS);
  849. }
  850. /**
  851. * Verifies that the documentProtection tag in settings.xml file <br/>
  852. * specifies that the protection is enforced (w:enforcement="1") <br/>
  853. * and that the kind of protection is comments (w:edit="comments")<br/>
  854. * <br/>
  855. * sample snippet from settings.xml
  856. * <pre>
  857. * &lt;w:settings ... &gt;
  858. * &lt;w:documentProtection w:edit=&quot;comments&quot; w:enforcement=&quot;1&quot;/&gt;
  859. * </pre>
  860. *
  861. * @return true if documentProtection is enforced with option comments
  862. */
  863. public boolean isEnforcedCommentsProtection() {
  864. return settings.isEnforcedWith(STDocProtect.COMMENTS);
  865. }
  866. /**
  867. * Verifies that the documentProtection tag in settings.xml file <br/>
  868. * specifies that the protection is enforced (w:enforcement="1") <br/>
  869. * and that the kind of protection is trackedChanges (w:edit="trackedChanges")<br/>
  870. * <br/>
  871. * sample snippet from settings.xml
  872. * <pre>
  873. * &lt;w:settings ... &gt;
  874. * &lt;w:documentProtection w:edit=&quot;trackedChanges&quot; w:enforcement=&quot;1&quot;/&gt;
  875. * </pre>
  876. *
  877. * @return true if documentProtection is enforced with option trackedChanges
  878. */
  879. public boolean isEnforcedTrackedChangesProtection() {
  880. return settings.isEnforcedWith(STDocProtect.TRACKED_CHANGES);
  881. }
  882. public boolean isEnforcedUpdateFields() {
  883. return settings.isUpdateFields();
  884. }
  885. /**
  886. * Enforces the readOnly protection.<br/>
  887. * In the documentProtection tag inside settings.xml file, <br/>
  888. * it sets the value of enforcement to "1" (w:enforcement="1") <br/>
  889. * and the value of edit to readOnly (w:edit="readOnly")<br/>
  890. * <br/>
  891. * sample snippet from settings.xml
  892. * <pre>
  893. * &lt;w:settings ... &gt;
  894. * &lt;w:documentProtection w:edit=&quot;readOnly&quot; w:enforcement=&quot;1&quot;/&gt;
  895. * </pre>
  896. */
  897. public void enforceReadonlyProtection() {
  898. settings.setEnforcementEditValue(STDocProtect.READ_ONLY);
  899. }
  900. /**
  901. * Enforces the readOnly protection with a password.<br/>
  902. * <br/>
  903. * sample snippet from settings.xml
  904. * <pre>
  905. * &lt;w:documentProtection w:edit=&quot;readOnly&quot; w:enforcement=&quot;1&quot;
  906. * w:cryptProviderType=&quot;rsaAES&quot; w:cryptAlgorithmClass=&quot;hash&quot;
  907. * w:cryptAlgorithmType=&quot;typeAny&quot; w:cryptAlgorithmSid=&quot;14&quot;
  908. * w:cryptSpinCount=&quot;100000&quot; w:hash=&quot;...&quot; w:salt=&quot;....&quot;
  909. * /&gt;
  910. * </pre>
  911. *
  912. * @param password the plaintext password, if null no password will be applied
  913. * @param hashAlgo the hash algorithm - only md2, m5, sha1, sha256, sha384 and sha512 are supported.
  914. * if null, it will default default to sha1
  915. */
  916. public void enforceReadonlyProtection(String password, HashAlgorithm hashAlgo) {
  917. settings.setEnforcementEditValue(STDocProtect.READ_ONLY, password, hashAlgo);
  918. }
  919. /**
  920. * Enforce the Filling Forms protection.<br/>
  921. * In the documentProtection tag inside settings.xml file, <br/>
  922. * it sets the value of enforcement to "1" (w:enforcement="1") <br/>
  923. * and the value of edit to forms (w:edit="forms")<br/>
  924. * <br/>
  925. * sample snippet from settings.xml
  926. * <pre>
  927. * &lt;w:settings ... &gt;
  928. * &lt;w:documentProtection w:edit=&quot;forms&quot; w:enforcement=&quot;1&quot;/&gt;
  929. * </pre>
  930. */
  931. public void enforceFillingFormsProtection() {
  932. settings.setEnforcementEditValue(STDocProtect.FORMS);
  933. }
  934. /**
  935. * Enforce the Filling Forms protection.<br/>
  936. * <br/>
  937. * sample snippet from settings.xml
  938. * <pre>
  939. * &lt;w:documentProtection w:edit=&quot;forms&quot; w:enforcement=&quot;1&quot;
  940. * w:cryptProviderType=&quot;rsaAES&quot; w:cryptAlgorithmClass=&quot;hash&quot;
  941. * w:cryptAlgorithmType=&quot;typeAny&quot; w:cryptAlgorithmSid=&quot;14&quot;
  942. * w:cryptSpinCount=&quot;100000&quot; w:hash=&quot;...&quot; w:salt=&quot;....&quot;
  943. * /&gt;
  944. * </pre>
  945. *
  946. * @param password the plaintext password, if null no password will be applied
  947. * @param hashAlgo the hash algorithm - only md2, m5, sha1, sha256, sha384 and sha512 are supported.
  948. * if null, it will default default to sha1
  949. */
  950. public void enforceFillingFormsProtection(String password, HashAlgorithm hashAlgo) {
  951. settings.setEnforcementEditValue(STDocProtect.FORMS, password, hashAlgo);
  952. }
  953. /**
  954. * Enforce the Comments protection.<br/>
  955. * In the documentProtection tag inside settings.xml file,<br/>
  956. * it sets the value of enforcement to "1" (w:enforcement="1") <br/>
  957. * and the value of edit to comments (w:edit="comments")<br/>
  958. * <br/>
  959. * sample snippet from settings.xml
  960. * <pre>
  961. * &lt;w:settings ... &gt;
  962. * &lt;w:documentProtection w:edit=&quot;comments&quot; w:enforcement=&quot;1&quot;/&gt;
  963. * </pre>
  964. */
  965. public void enforceCommentsProtection() {
  966. settings.setEnforcementEditValue(STDocProtect.COMMENTS);
  967. }
  968. /**
  969. * Enforce the Comments protection.<br/>
  970. * <br/>
  971. * sample snippet from settings.xml
  972. * <pre>
  973. * &lt;w:documentProtection w:edit=&quot;comments&quot; w:enforcement=&quot;1&quot;
  974. * w:cryptProviderType=&quot;rsaAES&quot; w:cryptAlgorithmClass=&quot;hash&quot;
  975. * w:cryptAlgorithmType=&quot;typeAny&quot; w:cryptAlgorithmSid=&quot;14&quot;
  976. * w:cryptSpinCount=&quot;100000&quot; w:hash=&quot;...&quot; w:salt=&quot;....&quot;
  977. * /&gt;
  978. * </pre>
  979. *
  980. * @param password the plaintext password, if null no password will be applied
  981. * @param hashAlgo the hash algorithm - only md2, m5, sha1, sha256, sha384 and sha512 are supported.
  982. * if null, it will default default to sha1
  983. */
  984. public void enforceCommentsProtection(String password, HashAlgorithm hashAlgo) {
  985. settings.setEnforcementEditValue(STDocProtect.COMMENTS, password, hashAlgo);
  986. }
  987. /**
  988. * Enforce the Tracked Changes protection.<br/>
  989. * In the documentProtection tag inside settings.xml file, <br/>
  990. * it sets the value of enforcement to "1" (w:enforcement="1") <br/>
  991. * and the value of edit to trackedChanges (w:edit="trackedChanges")<br/>
  992. * <br/>
  993. * sample snippet from settings.xml
  994. * <pre>
  995. * &lt;w:settings ... &gt;
  996. * &lt;w:documentProtection w:edit=&quot;trackedChanges&quot; w:enforcement=&quot;1&quot;/&gt;
  997. * </pre>
  998. */
  999. public void enforceTrackedChangesProtection() {
  1000. settings.setEnforcementEditValue(STDocProtect.TRACKED_CHANGES);
  1001. }
  1002. /**
  1003. * Enforce the Tracked Changes protection.<br/>
  1004. * <br/>
  1005. * sample snippet from settings.xml
  1006. * <pre>
  1007. * &lt;w:documentProtection w:edit=&quot;trackedChanges&quot; w:enforcement=&quot;1&quot;
  1008. * w:cryptProviderType=&quot;rsaAES&quot; w:cryptAlgorithmClass=&quot;hash&quot;
  1009. * w:cryptAlgorithmType=&quot;typeAny&quot; w:cryptAlgorithmSid=&quot;14&quot;
  1010. * w:cryptSpinCount=&quot;100000&quot; w:hash=&quot;...&quot; w:salt=&quot;....&quot;
  1011. * /&gt;
  1012. * </pre>
  1013. *
  1014. * @param password the plaintext password, if null no password will be applied
  1015. * @param hashAlgo the hash algorithm - only md2, m5, sha1, sha256, sha384 and sha512 are supported.
  1016. * if null, it will default default to sha1
  1017. */
  1018. public void enforceTrackedChangesProtection(String password, HashAlgorithm hashAlgo) {
  1019. settings.setEnforcementEditValue(STDocProtect.TRACKED_CHANGES, password, hashAlgo);
  1020. }
  1021. /**
  1022. * Validates the existing password
  1023. *
  1024. * @param password
  1025. * @return true, only if password was set and equals, false otherwise
  1026. */
  1027. public boolean validateProtectionPassword(String password) {
  1028. return settings.validateProtectionPassword(password);
  1029. }
  1030. /**
  1031. * Remove protection enforcement.<br/>
  1032. * In the documentProtection tag inside settings.xml file <br/>
  1033. * it sets the value of enforcement to "0" (w:enforcement="0") <br/>
  1034. */
  1035. public void removeProtectionEnforcement() {
  1036. settings.removeEnforcement();
  1037. }
  1038. /**
  1039. * Enforces fields update on document open (in Word).
  1040. * In the settings.xml file <br/>
  1041. * sets the updateSettings value to true (w:updateSettings w:val="true")
  1042. *
  1043. * NOTICES:
  1044. * <ul>
  1045. * <li>Causing Word to ask on open: "This document contains fields that may refer to other files. Do you want to update the fields in this document?"
  1046. * (if "Update automatic links at open" is enabled)</li>
  1047. * <li>Flag is removed after saving with changes in Word </li>
  1048. * </ul>
  1049. */
  1050. public void enforceUpdateFields() {
  1051. settings.setUpdateFields();
  1052. }
  1053. /**
  1054. * Check if revision tracking is turned on.
  1055. *
  1056. * @return <code>true</code> if revision tracking is turned on
  1057. */
  1058. public boolean isTrackRevisions() {
  1059. return settings.isTrackRevisions();
  1060. }
  1061. /**
  1062. * Enable or disable revision tracking.
  1063. *
  1064. * @param enable <code>true</code> to turn on revision tracking, <code>false</code> to turn off revision tracking
  1065. */
  1066. public void setTrackRevisions(boolean enable) {
  1067. settings.setTrackRevisions(enable);
  1068. }
  1069. /**
  1070. * inserts an existing XWPFTable to the arrays bodyElements and tables
  1071. * @param pos
  1072. * @param table
  1073. */
  1074. @Override
  1075. @SuppressWarnings("deprecation")
  1076. public void insertTable(int pos, XWPFTable table) {
  1077. bodyElements.add(pos, table);
  1078. int i = 0;
  1079. for (CTTbl tbl : ctDocument.getBody().getTblArray()) {
  1080. if (tbl == table.getCTTbl()) {
  1081. break;
  1082. }
  1083. i++;
  1084. }
  1085. tables.add(i, table);
  1086. }
  1087. /**
  1088. * Returns all Pictures, which are referenced from the document itself.
  1089. * @return a {@link List} of {@link XWPFPictureData}. The returned {@link List} is unmodifiable. Use #a
  1090. */
  1091. public List<XWPFPictureData> getAllPictures() {
  1092. return Collections.unmodifiableList(pictures);
  1093. }
  1094. /**
  1095. * @return all Pictures in this package
  1096. */
  1097. public List<XWPFPictureData> getAllPackagePictures() {
  1098. List<XWPFPictureData> result = new ArrayList<XWPFPictureData>();
  1099. Collection<List<XWPFPictureData>> values = packagePictures.values();
  1100. for (List<XWPFPictureData> list : values) {
  1101. result.addAll(list);
  1102. }
  1103. return Collections.unmodifiableList(result);
  1104. }
  1105. void registerPackagePictureData(XWPFPictureData picData) {
  1106. List<XWPFPictureData> list = packagePictures.get(picData.getChecksum());
  1107. if (list == null) {
  1108. list = new ArrayList<XWPFPictureData>(1);
  1109. packagePictures.put(picData.getChecksum(), list);
  1110. }
  1111. if (!list.contains(picData))
  1112. {
  1113. list.add(picData);
  1114. }
  1115. }
  1116. XWPFPictureData findPackagePictureData(byte[] pictureData, int format)
  1117. {
  1118. long checksum = IOUtils.calculateChecksum(pictureData);
  1119. XWPFPictureData xwpfPicData = null;
  1120. /*
  1121. * Try to find PictureData with this checksum. Create new, if none
  1122. * exists.
  1123. */
  1124. List<XWPFPictureData> xwpfPicDataList = packagePictures.get(checksum);
  1125. if (xwpfPicDataList != null) {
  1126. Iterator<XWPFPictureData> iter = xwpfPicDataList.iterator();
  1127. while (iter.hasNext() && xwpfPicData == null) {
  1128. XWPFPictureData curElem = iter.next();
  1129. if (Arrays.equals(pictureData, curElem.getData())) {
  1130. xwpfPicData = curElem;
  1131. }
  1132. }
  1133. }
  1134. return xwpfPicData;
  1135. }
  1136. public String addPictureData(byte[] pictureData,int format) throws InvalidFormatException
  1137. {
  1138. XWPFPictureData xwpfPicData = findPackagePictureData(pictureData, format);
  1139. POIXMLRelation relDesc = XWPFPictureData.RELATIONS[format];
  1140. if (xwpfPicData == null)
  1141. {
  1142. /* Part doesn't exist, create a new one */
  1143. int idx = getNextPicNameNumber(format);
  1144. xwpfPicData = (XWPFPictureData) createRelationship(relDesc, XWPFFactory.getInstance(),idx);
  1145. /* write bytes to new part */
  1146. PackagePart picDataPart = xwpfPicData.getPackagePart();
  1147. OutputStream out = null;
  1148. try {
  1149. out = picDataPart.getOutputStream();
  1150. out.write(pictureData);
  1151. } catch (IOException e) {
  1152. throw new POIXMLException(e);
  1153. } finally {
  1154. try {
  1155. if (out != null) out.close();
  1156. } catch (IOException e) {
  1157. // ignore
  1158. }
  1159. }
  1160. registerPackagePictureData(xwpfPicData);
  1161. pictures.add(xwpfPicData);
  1162. return getRelationId(xwpfPicData);
  1163. }
  1164. else if (!getRelations().contains(xwpfPicData))
  1165. {
  1166. /*
  1167. * Part already existed, but was not related so far. Create
  1168. * relationship to the already existing part and update
  1169. * POIXMLDocumentPart data.
  1170. */
  1171. PackagePart picDataPart = xwpfPicData.getPackagePart();
  1172. // TODO add support for TargetMode.EXTERNAL relations.
  1173. TargetMode targetMode = TargetMode.INTERNAL;
  1174. PackagePartName partName = picDataPart.getPartName();
  1175. String relation = relDesc.getRelation();
  1176. PackageRelationship relShip = getPackagePart().addRelationship(partName,targetMode,relation);
  1177. String id = relShip.getId();
  1178. addRelation(id,xwpfPicData);
  1179. pictures.add(xwpfPicData);
  1180. return id;
  1181. }
  1182. else
  1183. {
  1184. /* Part already existed, get relation id and return it */
  1185. return getRelationId(xwpfPicData);
  1186. }
  1187. }
  1188. public String addPictureData(InputStream is,int format) throws InvalidFormatException
  1189. {
  1190. try {
  1191. byte[] data = IOUtils.toByteArray(is);
  1192. return addPictureData(data, format);
  1193. } catch (IOException e) {
  1194. throw new POIXMLException(e);
  1195. }
  1196. }
  1197. /**
  1198. * get the next free ImageNumber
  1199. * @param format
  1200. * @return the next free ImageNumber
  1201. * @throws InvalidFormatException
  1202. */
  1203. public int getNextPicNameNumber(int format) throws InvalidFormatException {
  1204. int img = getAllPackagePictures().size() + 1;
  1205. String proposal = XWPFPictureData.RELATIONS[format].getFileName(img);
  1206. PackagePartName createPartName = PackagingURIHelper.createPartName(proposal);
  1207. while (this.getPackage().getPart(createPartName) != null) {
  1208. img++;
  1209. proposal = XWPFPictureData.RELATIONS[format].getFileName(img);
  1210. createPartName = PackagingURIHelper.createPartName(proposal);
  1211. }
  1212. return img;
  1213. }
  1214. /**
  1215. * returns the PictureData by blipID
  1216. * @param blipID
  1217. * @return XWPFPictureData of a specificID
  1218. */
  1219. public XWPFPictureData getPictureDataByID(String blipID) {
  1220. POIXMLDocumentPart relatedPart = getRelationById(blipID);
  1221. if (relatedPart instanceof XWPFPictureData) {
  1222. XWPFPictureData xwpfPicData = (XWPFPictureData) relatedPart;
  1223. return xwpfPicData;
  1224. }
  1225. return null;
  1226. }
  1227. /**
  1228. * getNumbering
  1229. * @return numbering
  1230. */
  1231. public XWPFNumbering getNumbering() {
  1232. return numbering;
  1233. }
  1234. /**
  1235. * get Styles
  1236. * @return styles for this document
  1237. */
  1238. public XWPFStyles getStyles() {
  1239. return styles;
  1240. }
  1241. /**
  1242. * get the paragraph with the CTP class p
  1243. *
  1244. * @param p
  1245. * @return the paragraph with the CTP class p
  1246. */
  1247. @Override
  1248. public XWPFParagraph getParagraph(CTP p) {
  1249. for (int i = 0; i < getParagraphs().size(); i++) {
  1250. if (getParagraphs().get(i).getCTP() == p) {
  1251. return getParagraphs().get(i);
  1252. }
  1253. }
  1254. return null;
  1255. }
  1256. /**
  1257. * get a table by its CTTbl-Object
  1258. * @param ctTbl
  1259. * @see org.apache.poi.xwpf.usermodel.IBody#getTable(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl)
  1260. * @return a table by its CTTbl-Object or null
  1261. */
  1262. @Override
  1263. public XWPFTable getTable(CTTbl ctTbl) {
  1264. for (int i = 0; i < tables.size(); i++) {
  1265. if (getTables().get(i).getCTTbl() == ctTbl) {
  1266. return getTables().get(i);
  1267. }
  1268. }
  1269. return null;
  1270. }
  1271. public Iterator<XWPFTable> getTablesIterator() {
  1272. return tables.iterator();
  1273. }
  1274. public Iterator<XWPFParagraph> getParagraphsIterator() {
  1275. return paragraphs.iterator();
  1276. }
  1277. /**
  1278. * Returns the paragraph that of position pos
  1279. * @see org.apache.poi.xwpf.usermodel.IBody#getParagraphArray(int)
  1280. */
  1281. @Override
  1282. public XWPFParagraph getParagraphArray(int pos) {
  1283. if (pos >= 0 && pos < paragraphs.size()) {
  1284. return paragraphs.get(pos);
  1285. }
  1286. return null;
  1287. }
  1288. /**
  1289. * returns the Part, to which the body belongs, which you need for adding relationship to other parts
  1290. * Actually it is needed of the class XWPFTableCell. Because you have to know to which part the tableCell
  1291. * belongs.
  1292. * @see org.apache.poi.xwpf.usermodel.IBody#getPart()
  1293. */
  1294. @Override
  1295. public POIXMLDocumentPart getPart() {
  1296. return this;
  1297. }
  1298. /**
  1299. * get the PartType of the body, for example
  1300. * DOCUMENT, HEADER, FOOTER, FOOTNOTE,
  1301. *
  1302. * @see org.apache.poi.xwpf.usermodel.IBody#getPartType()
  1303. */
  1304. @Override
  1305. public BodyType getPartType() {
  1306. return BodyType.DOCUMENT;
  1307. }
  1308. /**
  1309. * get the TableCell which belongs to the TableCell
  1310. * @param cell
  1311. */
  1312. @Override
  1313. public XWPFTableCell getTableCell(CTTc cell) {
  1314. XmlCursor cursor = cell.newCursor();
  1315. cursor.toParent();
  1316. XmlObject o = cursor.getObject();
  1317. if(!(o instanceof CTRow)){
  1318. return null;
  1319. }
  1320. CTRow row = (CTRow)o;
  1321. cursor.toParent();
  1322. o = cursor.getObject();
  1323. cursor.dispose();
  1324. if(! (o instanceof CTTbl)){
  1325. return null;
  1326. }
  1327. CTTbl tbl = (CTTbl) o;
  1328. XWPFTable table = getTable(tbl);
  1329. if(table == null){
  1330. return null;
  1331. }
  1332. XWPFTableRow tableRow = table.getRow(row);
  1333. if (tableRow == null) {
  1334. return null;
  1335. }
  1336. return tableRow.getTableCell(cell);
  1337. }
  1338. @Override
  1339. public XWPFDocument getXWPFDocument() {
  1340. return this;
  1341. }
  1342. }