| 1 | /* |
|---|
| 2 | * Copyright 2007-2008, Plutext Pty Ltd. |
|---|
| 3 | * |
|---|
| 4 | * This file is part of docx4j. |
|---|
| 5 | |
|---|
| 6 | docx4j is licensed under the Apache License, Version 2.0 (the "License"); |
|---|
| 7 | you may not use this file except in compliance with the License. |
|---|
| 8 | |
|---|
| 9 | You may obtain a copy of the License at |
|---|
| 10 | |
|---|
| 11 | http://www.apache.org/licenses/LICENSE-2.0 |
|---|
| 12 | |
|---|
| 13 | Unless required by applicable law or agreed to in writing, software |
|---|
| 14 | distributed under the License is distributed on an "AS IS" BASIS, |
|---|
| 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|---|
| 16 | See the License for the specific language governing permissions and |
|---|
| 17 | limitations under the License. |
|---|
| 18 | |
|---|
| 19 | */ |
|---|
| 20 | |
|---|
| 21 | package org.docx4j.openpackaging.parts.WordprocessingML; |
|---|
| 22 | |
|---|
| 23 | //import java.io.InputStream; |
|---|
| 24 | //import java.io.OutputStream; |
|---|
| 25 | //import java.net.URI; |
|---|
| 26 | // |
|---|
| 27 | //import javax.xml.bind.JAXBContext; |
|---|
| 28 | //import java.net.URI; |
|---|
| 29 | |
|---|
| 30 | import org.docx4j.XmlUtils; |
|---|
| 31 | import org.docx4j.jaxb.Context; |
|---|
| 32 | import org.docx4j.openpackaging.exceptions.InvalidFormatException; |
|---|
| 33 | import org.docx4j.openpackaging.packages.WordprocessingMLPackage; |
|---|
| 34 | import org.docx4j.openpackaging.parts.JaxbXmlPart; |
|---|
| 35 | import org.docx4j.openpackaging.parts.Part; |
|---|
| 36 | import org.docx4j.openpackaging.parts.PartName; |
|---|
| 37 | import org.docx4j.openpackaging.parts.ThemePart; |
|---|
| 38 | import org.docx4j.openpackaging.parts.opendope.ComponentsPart; |
|---|
| 39 | import org.docx4j.openpackaging.parts.opendope.ConditionsPart; |
|---|
| 40 | import org.docx4j.openpackaging.parts.opendope.QuestionsPart; |
|---|
| 41 | import org.docx4j.openpackaging.parts.opendope.XPathsPart; |
|---|
| 42 | import org.docx4j.openpackaging.parts.relationships.Namespaces; |
|---|
| 43 | import org.docx4j.wml.CTEndnotes; |
|---|
| 44 | import org.docx4j.wml.CTFootnotes; |
|---|
| 45 | import org.docx4j.wml.CTFtnEdn; |
|---|
| 46 | import org.docx4j.wml.Hdr; |
|---|
| 47 | import org.w3c.dom.Document; |
|---|
| 48 | import org.w3c.dom.Node; |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | public abstract class DocumentPart<E> extends JaxbXmlPart<E> { |
|---|
| 53 | |
|---|
| 54 | /** Parts which can be the target of a relationship from either |
|---|
| 55 | * the Main Document or the Glossary Document |
|---|
| 56 | * |
|---|
| 57 | * Microsoft's Microsoft.Office.DocumentFormat.OpenXML has |
|---|
| 58 | * no equivalent of this. |
|---|
| 59 | * |
|---|
| 60 | */ |
|---|
| 61 | |
|---|
| 62 | protected CommentsPart commentsPart; |
|---|
| 63 | protected DocumentSettingsPart documentSettingsPart; |
|---|
| 64 | protected EndnotesPart endNotesPart; |
|---|
| 65 | protected FontTablePart fontTablePart; |
|---|
| 66 | protected ThemePart themePart; |
|---|
| 67 | protected FootnotesPart footnotesPart; |
|---|
| 68 | protected NumberingDefinitionsPart numberingDefinitionsPart; |
|---|
| 69 | protected StyleDefinitionsPart styleDefinitionsPart; |
|---|
| 70 | protected WebSettingsPart webSettingsPart; |
|---|
| 71 | |
|---|
| 72 | // To access headerParts and footerParts, instead |
|---|
| 73 | // use the DocumentModel. |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | public boolean setPartShortcut(Part part) { |
|---|
| 77 | |
|---|
| 78 | if (part == null ){ |
|---|
| 79 | return false; |
|---|
| 80 | } else { |
|---|
| 81 | return setPartShortcut(part, part.getRelationshipType() ); |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | public boolean setPartShortcut(Part part, String relationshipType) { |
|---|
| 87 | |
|---|
| 88 | // Since each part knows its relationshipsType, |
|---|
| 89 | // why is this passed in as an arg? |
|---|
| 90 | // Answer: where the relationshipType is ascertained |
|---|
| 91 | // from the rel itself, it is the most authoritative. |
|---|
| 92 | // Note that we normally use the info in [Content_Types] |
|---|
| 93 | // to create a part of the correct type. This info |
|---|
| 94 | // will not necessary correspond to the info in the rel! |
|---|
| 95 | |
|---|
| 96 | if (relationshipType==null) { |
|---|
| 97 | log.warn("trying to set part shortcut against a null relationship type."); |
|---|
| 98 | return false; |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | if (relationshipType.equals(Namespaces.FONT_TABLE)) { |
|---|
| 102 | fontTablePart = (FontTablePart)part; |
|---|
| 103 | return true; |
|---|
| 104 | } else if (relationshipType.equals(Namespaces.THEME)) { |
|---|
| 105 | themePart = (ThemePart)part; |
|---|
| 106 | return true; |
|---|
| 107 | } else if (relationshipType.equals(Namespaces.STYLES)) { |
|---|
| 108 | styleDefinitionsPart = (StyleDefinitionsPart)part; |
|---|
| 109 | return true; |
|---|
| 110 | } else if (relationshipType.equals(Namespaces.WEB_SETTINGS)) { |
|---|
| 111 | webSettingsPart = (WebSettingsPart)part; |
|---|
| 112 | return true; |
|---|
| 113 | } else if (relationshipType.equals(Namespaces.SETTINGS)) { |
|---|
| 114 | documentSettingsPart = (DocumentSettingsPart)part; |
|---|
| 115 | return true; |
|---|
| 116 | } else if (relationshipType.equals(Namespaces.COMMENTS)) { |
|---|
| 117 | commentsPart = (CommentsPart)part; |
|---|
| 118 | return true; |
|---|
| 119 | } else if (relationshipType.equals(Namespaces.ENDNOTES)) { |
|---|
| 120 | endNotesPart = (EndnotesPart)part; |
|---|
| 121 | return true; |
|---|
| 122 | } else if (relationshipType.equals(Namespaces.FOOTNOTES)) { |
|---|
| 123 | footnotesPart = (FootnotesPart)part; |
|---|
| 124 | return true; |
|---|
| 125 | } else if (relationshipType.equals(Namespaces.NUMBERING)) { |
|---|
| 126 | numberingDefinitionsPart = (NumberingDefinitionsPart)part; |
|---|
| 127 | return true; |
|---|
| 128 | } else if (part instanceof ConditionsPart) { |
|---|
| 129 | conditionsPart = ((ConditionsPart)part); |
|---|
| 130 | return true; |
|---|
| 131 | } else if (part instanceof QuestionsPart) { |
|---|
| 132 | questionsPart = ((QuestionsPart)part); |
|---|
| 133 | return true; |
|---|
| 134 | } else if (part instanceof XPathsPart) { |
|---|
| 135 | xPathsPart = ((XPathsPart)part); |
|---|
| 136 | return true; |
|---|
| 137 | } else if (part instanceof ComponentsPart) { |
|---|
| 138 | componentsPart = ((ComponentsPart)part); |
|---|
| 139 | return true; |
|---|
| 140 | } else if (part instanceof BibliographyPart) { |
|---|
| 141 | bibliographyPart = ((BibliographyPart)part); |
|---|
| 142 | return true; |
|---|
| 143 | |
|---|
| 144 | // TODO - to be completed. |
|---|
| 145 | } else { |
|---|
| 146 | return false; |
|---|
| 147 | } |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | |
|---|
| 151 | /** Other characteristics which both the Main Document and the |
|---|
| 152 | * Glossary Document have. |
|---|
| 153 | */ |
|---|
| 154 | //private VML background; // [1.2.1] |
|---|
| 155 | |
|---|
| 156 | |
|---|
| 157 | public DocumentPart(PartName partName) throws InvalidFormatException { |
|---|
| 158 | super(partName); |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | |
|---|
| 162 | |
|---|
| 163 | public CommentsPart getCommentsPart() { |
|---|
| 164 | return commentsPart; |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | |
|---|
| 168 | public DocumentSettingsPart getDocumentSettingsPart() { |
|---|
| 169 | return documentSettingsPart; |
|---|
| 170 | } |
|---|
| 171 | |
|---|
| 172 | |
|---|
| 173 | public EndnotesPart getEndNotesPart() { |
|---|
| 174 | return endNotesPart; |
|---|
| 175 | } |
|---|
| 176 | public static Node getEndnotes(WordprocessingMLPackage wmlPackage) { |
|---|
| 177 | return XmlUtils.marshaltoW3CDomDocument( |
|---|
| 178 | wmlPackage.getMainDocumentPart().getEndNotesPart().getJaxbElement()); |
|---|
| 179 | } |
|---|
| 180 | |
|---|
| 181 | /** |
|---|
| 182 | * Does this package contain an endnotes part, with real endnotes |
|---|
| 183 | * in it? |
|---|
| 184 | * @param wmlPackage |
|---|
| 185 | * @return |
|---|
| 186 | */ |
|---|
| 187 | public static boolean hasEndnotesPart(WordprocessingMLPackage wmlPackage) { |
|---|
| 188 | if (wmlPackage.getMainDocumentPart().getEndNotesPart()==null) { |
|---|
| 189 | return false; |
|---|
| 190 | } else { |
|---|
| 191 | // Word seems to add an endnotes part when it adds a footnotes part, |
|---|
| 192 | // so existence of part is not determinative |
|---|
| 193 | CTEndnotes endnotes = wmlPackage.getMainDocumentPart().getEndNotesPart().getJaxbElement(); |
|---|
| 194 | |
|---|
| 195 | if (endnotes.getEndnote().size()<3) { |
|---|
| 196 | // id's 0 & 1 are: |
|---|
| 197 | // <w:endnote w:type="separator" w:id="0"> |
|---|
| 198 | // <w:endnote w:type="continuationSeparator" w:id="1"> |
|---|
| 199 | return false; |
|---|
| 200 | } |
|---|
| 201 | return true; |
|---|
| 202 | } |
|---|
| 203 | } |
|---|
| 204 | |
|---|
| 205 | public FootnotesPart getFootnotesPart() { |
|---|
| 206 | return footnotesPart; |
|---|
| 207 | } |
|---|
| 208 | |
|---|
| 209 | public static Node getFootnotes(WordprocessingMLPackage wmlPackage) { |
|---|
| 210 | return XmlUtils.marshaltoW3CDomDocument( |
|---|
| 211 | wmlPackage.getMainDocumentPart().getFootnotesPart().getJaxbElement()); |
|---|
| 212 | } |
|---|
| 213 | |
|---|
| 214 | public static boolean hasFootnotesPart(WordprocessingMLPackage wmlPackage) { |
|---|
| 215 | if (wmlPackage.getMainDocumentPart().getFootnotesPart()==null) { |
|---|
| 216 | return false; |
|---|
| 217 | } else { |
|---|
| 218 | return true; |
|---|
| 219 | } |
|---|
| 220 | } |
|---|
| 221 | |
|---|
| 222 | public static Node getFootnote(WordprocessingMLPackage wmlPackage, String id) { |
|---|
| 223 | |
|---|
| 224 | CTFootnotes footnotes = wmlPackage.getMainDocumentPart().getFootnotesPart().getJaxbElement(); |
|---|
| 225 | int pos = Integer.parseInt(id); |
|---|
| 226 | |
|---|
| 227 | // No @XmlRootElement on CTFtnEdn, so .. |
|---|
| 228 | CTFtnEdn ftn = footnotes.getFootnote().get(pos); |
|---|
| 229 | Document d = XmlUtils.marshaltoW3CDomDocument( ftn, |
|---|
| 230 | Context.jc, Namespaces.NS_WORD12, "footnote", CTFtnEdn.class ); |
|---|
| 231 | log.debug("Footnote " + id + ": " + XmlUtils.w3CDomNodeToString(d)); |
|---|
| 232 | return d; |
|---|
| 233 | } |
|---|
| 234 | |
|---|
| 235 | public FontTablePart getFontTablePart() { |
|---|
| 236 | return fontTablePart; |
|---|
| 237 | } |
|---|
| 238 | |
|---|
| 239 | |
|---|
| 240 | // public List getFooterParts() { |
|---|
| 241 | // return footerPart; |
|---|
| 242 | // } |
|---|
| 243 | |
|---|
| 244 | // public List getHeaderParts() { |
|---|
| 245 | // return headerPart; |
|---|
| 246 | // } |
|---|
| 247 | |
|---|
| 248 | |
|---|
| 249 | public NumberingDefinitionsPart getNumberingDefinitionsPart() { |
|---|
| 250 | return numberingDefinitionsPart; |
|---|
| 251 | } |
|---|
| 252 | |
|---|
| 253 | |
|---|
| 254 | public StyleDefinitionsPart getStyleDefinitionsPart() { |
|---|
| 255 | return styleDefinitionsPart; |
|---|
| 256 | } |
|---|
| 257 | |
|---|
| 258 | public ThemePart getThemePart() { |
|---|
| 259 | return themePart; |
|---|
| 260 | } |
|---|
| 261 | |
|---|
| 262 | |
|---|
| 263 | public WebSettingsPart getWebSettingsPart() { |
|---|
| 264 | return webSettingsPart; |
|---|
| 265 | } |
|---|
| 266 | |
|---|
| 267 | private ConditionsPart conditionsPart; |
|---|
| 268 | public ConditionsPart getConditionsPart() { |
|---|
| 269 | return conditionsPart; |
|---|
| 270 | } |
|---|
| 271 | // public void setConditionsPart(ConditionsPart conditionsPart) { |
|---|
| 272 | // this.conditionsPart = conditionsPart; |
|---|
| 273 | // } |
|---|
| 274 | |
|---|
| 275 | private XPathsPart xPathsPart; |
|---|
| 276 | public XPathsPart getXPathsPart() { |
|---|
| 277 | return xPathsPart; |
|---|
| 278 | } |
|---|
| 279 | // public void setXPathsPart(XPathsPart xPathsPart) { |
|---|
| 280 | // this.xPathsPart = xPathsPart; |
|---|
| 281 | // } |
|---|
| 282 | |
|---|
| 283 | private QuestionsPart questionsPart; |
|---|
| 284 | public QuestionsPart getQuestionsPart() { |
|---|
| 285 | return questionsPart; |
|---|
| 286 | } |
|---|
| 287 | |
|---|
| 288 | private ComponentsPart componentsPart; |
|---|
| 289 | public ComponentsPart getComponentsPart() { |
|---|
| 290 | return componentsPart; |
|---|
| 291 | } |
|---|
| 292 | |
|---|
| 293 | private BibliographyPart bibliographyPart; |
|---|
| 294 | public BibliographyPart getBibliographyPart() { |
|---|
| 295 | return bibliographyPart; |
|---|
| 296 | } |
|---|
| 297 | |
|---|
| 298 | } |
|---|