| 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; |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | import javax.xml.bind.JAXBException; |
|---|
| 25 | import javax.xml.bind.Unmarshaller; |
|---|
| 26 | import javax.xml.xpath.XPath; |
|---|
| 27 | import javax.xml.xpath.XPathFactory; |
|---|
| 28 | |
|---|
| 29 | import org.apache.log4j.Logger; |
|---|
| 30 | import org.docx4j.XmlUtils; |
|---|
| 31 | import org.docx4j.docProps.extended.Properties; |
|---|
| 32 | import org.docx4j.jaxb.Context; |
|---|
| 33 | import org.docx4j.jaxb.NamespacePrefixMappings; |
|---|
| 34 | import org.docx4j.openpackaging.exceptions.Docx4JException; |
|---|
| 35 | import org.docx4j.openpackaging.exceptions.InvalidFormatException; |
|---|
| 36 | import org.docx4j.openpackaging.parts.relationships.Namespaces; |
|---|
| 37 | import org.w3c.dom.Document; |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | public class DocPropsExtendedPart extends JaxbXmlPart<Properties> { |
|---|
| 43 | |
|---|
| 44 | /* |
|---|
| 45 | * <?xml version="1.0" encoding="UTF-8" standalone="yes"?> |
|---|
| 46 | * <Properties xmlns="http://schemas.openxmlformats.org/officeDocument/2006/extended-properties" |
|---|
| 47 | * xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes"> |
|---|
| 48 | * <Template>Normal.dotm</Template> |
|---|
| 49 | * <TotalTime>0</TotalTime> |
|---|
| 50 | * <Pages>1</Pages><Words>3</Words><Characters>20</Characters> |
|---|
| 51 | * <Application>Microsoft Office Word</Application> |
|---|
| 52 | * <DocSecurity>0</DocSecurity> |
|---|
| 53 | * <Lines>1</Lines><Paragraphs>1</Paragraphs> |
|---|
| 54 | * <ScaleCrop>false</ScaleCrop> |
|---|
| 55 | * <Company>Plutext Pty Ltd</Company> |
|---|
| 56 | * <LinksUpToDate>false</LinksUpToDate><CharactersWithSpaces>22</CharactersWithSpaces> |
|---|
| 57 | * <SharedDoc>false</SharedDoc> |
|---|
| 58 | * <HyperlinksChanged>false</HyperlinksChanged> |
|---|
| 59 | * <AppVersion>12.0000</AppVersion> |
|---|
| 60 | * </Properties> |
|---|
| 61 | */ |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | private static Logger log = Logger.getLogger(DocPropsExtendedPart.class); |
|---|
| 65 | |
|---|
| 66 | private static XPathFactory xPathFactory; |
|---|
| 67 | private static XPath xPath; |
|---|
| 68 | static { |
|---|
| 69 | xPathFactory = XPathFactory.newInstance(); |
|---|
| 70 | xPath = xPathFactory.newXPath(); |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | /** |
|---|
| 74 | * @throws InvalidFormatException |
|---|
| 75 | */ |
|---|
| 76 | public DocPropsExtendedPart(PartName partName) throws InvalidFormatException { |
|---|
| 77 | super(partName); |
|---|
| 78 | init(); |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | public DocPropsExtendedPart() throws InvalidFormatException { |
|---|
| 82 | super(new PartName("/docProps/app.xml")); |
|---|
| 83 | init(); |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | public void init() { |
|---|
| 87 | |
|---|
| 88 | jc = Context.jcDocPropsExtended; |
|---|
| 89 | |
|---|
| 90 | // Used if this Part is added to [Content_Types].xml |
|---|
| 91 | setContentType(new org.docx4j.openpackaging.contenttype.ContentType( |
|---|
| 92 | org.docx4j.openpackaging.contenttype.ContentTypes.OFFICEDOCUMENT_EXTENDEDPROPERTIES)); |
|---|
| 93 | |
|---|
| 94 | // Used when this Part is added to a rels |
|---|
| 95 | setRelationshipType(Namespaces.PROPERTIES_EXTENDED); |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | |
|---|
| 99 | /** |
|---|
| 100 | * Unmarshal XML data from the specified InputStream and return the |
|---|
| 101 | * resulting content tree. Validation event location information may |
|---|
| 102 | * be incomplete when using this form of the unmarshal API. |
|---|
| 103 | * |
|---|
| 104 | * <p> |
|---|
| 105 | * Implements <a href="#unmarshalGlobal">Unmarshal Global Root Element</a>. |
|---|
| 106 | * |
|---|
| 107 | * @param is the InputStream to unmarshal XML data from |
|---|
| 108 | * @return the newly created root object of the java content tree |
|---|
| 109 | * |
|---|
| 110 | * @throws JAXBException |
|---|
| 111 | * If any unexpected errors occur while unmarshalling |
|---|
| 112 | */ |
|---|
| 113 | @Override |
|---|
| 114 | public Properties unmarshal( java.io.InputStream is ) throws JAXBException { |
|---|
| 115 | |
|---|
| 116 | try { |
|---|
| 117 | |
|---|
| 118 | // if (jc==null) { |
|---|
| 119 | // setJAXBContext(Context.jc); |
|---|
| 120 | // } |
|---|
| 121 | |
|---|
| 122 | setJAXBContext(org.docx4j.jaxb.Context.jcDocPropsExtended); |
|---|
| 123 | Unmarshaller u = jc.createUnmarshaller(); |
|---|
| 124 | |
|---|
| 125 | //u.setSchema(org.docx4j.jaxb.WmlSchema.schema); |
|---|
| 126 | u.setEventHandler(new org.docx4j.jaxb.JaxbValidationEventHandler()); |
|---|
| 127 | |
|---|
| 128 | log.info("unmarshalling " + this.getClass().getName() ); |
|---|
| 129 | jaxbElement = (Properties) u.unmarshal( is ); |
|---|
| 130 | |
|---|
| 131 | } catch (Exception e ) { |
|---|
| 132 | e.printStackTrace(); |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | return jaxbElement; |
|---|
| 136 | |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | // Core Props and Extended Props can both be bound as if they |
|---|
| 140 | // were a custom xml part. Copied from XmlPart. |
|---|
| 141 | |
|---|
| 142 | public String xpathGetString(String xpathString, String prefixMappings) throws Docx4JException { |
|---|
| 143 | |
|---|
| 144 | Document doc = XmlUtils.marshaltoW3CDomDocument( |
|---|
| 145 | getJaxbElement(), Context.jcDocPropsExtended ); |
|---|
| 146 | |
|---|
| 147 | try { |
|---|
| 148 | getNamespaceContext().registerPrefixMappings(prefixMappings); |
|---|
| 149 | |
|---|
| 150 | String result = xPath.evaluate(xpathString, doc ); |
|---|
| 151 | log.debug(xpathString + " ---> " + result); |
|---|
| 152 | return result; |
|---|
| 153 | } catch (Exception e) { |
|---|
| 154 | throw new Docx4JException("Problems evaluating xpath '" + xpathString + "'", e); |
|---|
| 155 | } |
|---|
| 156 | } |
|---|
| 157 | private NamespacePrefixMappings nsContext; |
|---|
| 158 | private NamespacePrefixMappings getNamespaceContext() { |
|---|
| 159 | if (nsContext==null) { |
|---|
| 160 | nsContext = new NamespacePrefixMappings(); |
|---|
| 161 | xPath.setNamespaceContext(nsContext); |
|---|
| 162 | } |
|---|
| 163 | return nsContext; |
|---|
| 164 | } |
|---|
| 165 | |
|---|
| 166 | } |
|---|
| 167 | |
|---|
| 168 | |
|---|