PageRenderTime 25ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/docx4j-samples-docx4j/src/main/java/org/docx4j/samples/BookmarksDeleter.java

http://github.com/plutext/docx4j
Java | 140 lines | 87 code | 21 blank | 32 comment | 14 complexity | de265ce03bf1422ec680ae71ed50fb80 MD5 | raw file
Possible License(s): Apache-2.0
  1. package org.docx4j.samples;
  2. import java.util.List;
  3. import org.docx4j.Docx4J;
  4. import org.docx4j.TraversalUtil;
  5. import org.docx4j.XmlUtils;
  6. import org.docx4j.finders.RangeFinder;
  7. import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
  8. import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
  9. import org.docx4j.wml.Body;
  10. import org.docx4j.wml.CTBookmark;
  11. import org.docx4j.wml.CTMarkupRange;
  12. import org.docx4j.wml.ContentAccessor;
  13. import org.slf4j.Logger;
  14. import org.slf4j.LoggerFactory;
  15. public class BookmarksDeleter {
  16. protected static Logger log = LoggerFactory.getLogger(BookmarksDeleter.class);
  17. public static void main(String[] args) throws Exception {
  18. WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage
  19. .load(new java.io.File(System.getProperty("user.dir")
  20. + "/BookmarksDeleter.docx"));
  21. MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
  22. // Before..
  23. // System.out.println(XmlUtils.marshaltoString(documentPart.getJaxbElement(), true, true));
  24. org.docx4j.wml.Document wmlDocumentEl = (org.docx4j.wml.Document) documentPart
  25. .getJaxbElement();
  26. Body body = wmlDocumentEl.getBody();
  27. fixRange(body.getContent(), "CTBookmark", "CTMarkupRange");
  28. // After
  29. // System.out.println(XmlUtils.marshaltoString(documentPart.getJaxbElement(), true, true));
  30. // or save the docx...
  31. Docx4J.save(wordMLPackage, new java.io.File(System.getProperty("user.dir")
  32. + "/OUT_BookmarksDeleter.docx"));
  33. }
  34. private static void fixRange(List<Object> paragraphs, String startElement,
  35. String endElement) throws Exception {
  36. RangeFinder rt = new RangeFinder(startElement, endElement);
  37. new TraversalUtil(paragraphs, rt);
  38. for (CTBookmark bm : rt.getStarts()) {
  39. try {
  40. // Can't just remove the object from the parent,
  41. // since in the parent, it may be wrapped in a JAXBElement
  42. List<Object> theList = null;
  43. if (bm.getParent() instanceof List) {
  44. theList = (List)bm.getParent(); // eg body.getContent()
  45. } else {
  46. theList = ((ContentAccessor)(bm.getParent())).getContent();
  47. }
  48. Object deleteMe = null;
  49. for (Object ox : theList) {
  50. if (XmlUtils.unwrap(ox).equals(bm)) {
  51. deleteMe = ox;
  52. break;
  53. }
  54. }
  55. if (deleteMe!=null) {
  56. theList.remove(deleteMe);
  57. }
  58. } catch (ClassCastException cce) {
  59. log.error(cce.getMessage(), cce);
  60. }
  61. }
  62. for (CTMarkupRange mr : rt.getEnds()) {
  63. try {
  64. // Can't just remove the object from the parent,
  65. // since in the parent, it may be wrapped in a JAXBElement
  66. List<Object> theList = null;
  67. if (mr.getParent() instanceof List) {
  68. theList = (List)mr.getParent(); // eg body.getContent()
  69. } else {
  70. theList = ((ContentAccessor)(mr.getParent())).getContent();
  71. }
  72. Object deleteMe = null;
  73. for (Object ox : theList) {
  74. if (XmlUtils.unwrap(ox).equals(mr)) {
  75. deleteMe = ox;
  76. break;
  77. }
  78. }
  79. if (deleteMe!=null) {
  80. theList.remove(deleteMe);
  81. }
  82. } catch (ClassCastException cce) {
  83. log.info(mr.getParent().getClass().getName());
  84. log.error(cce.getMessage(), cce);
  85. }
  86. }
  87. // NB: this leaves begin|separate|end behind!
  88. // It would be better to delete the whole field, or just leave it,
  89. // so this is commented out. Result in Word will be "bookmark undefined!"
  90. // for (Text instrText : rt.refs) {
  91. // try {
  92. // List<Object> theList = ((ContentAccessor)(instrText.getParent())).getContent();
  93. // Object deleteMe = null;
  94. // for (Object ox : theList) {
  95. // if (XmlUtils.unwrap(ox).equals(instrText)) {
  96. // deleteMe = ox;
  97. // break;
  98. // }
  99. // }
  100. // if (deleteMe!=null) {
  101. // theList.remove(deleteMe);
  102. // }
  103. // } catch (ClassCastException cce) {
  104. // log.info(instrText.getParent().getClass().getName());
  105. // log.error(cce);
  106. // }
  107. // }
  108. }
  109. private static boolean remove(List<Object> theList, Object bm) {
  110. // Can't just remove the object from the parent,
  111. // since in the parent, it may be wrapped in a JAXBElement
  112. for (Object ox : theList) {
  113. if (XmlUtils.unwrap(ox).equals(bm)) {
  114. return theList.remove(ox);
  115. }
  116. }
  117. return false;
  118. }
  119. }