| 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 | package org.docx4j.openpackaging.parts; |
|---|
| 21 | |
|---|
| 22 | import java.io.IOException; |
|---|
| 23 | |
|---|
| 24 | import javax.xml.bind.JAXBException; |
|---|
| 25 | import javax.xml.bind.Unmarshaller; |
|---|
| 26 | import javax.xml.transform.Source; |
|---|
| 27 | import javax.xml.transform.TransformerConfigurationException; |
|---|
| 28 | import javax.xml.transform.stream.StreamSource; |
|---|
| 29 | import javax.xml.xpath.XPath; |
|---|
| 30 | import javax.xml.xpath.XPathFactory; |
|---|
| 31 | |
|---|
| 32 | import org.apache.log4j.Logger; |
|---|
| 33 | import org.docx4j.XmlUtils; |
|---|
| 34 | import org.docx4j.docProps.core.CoreProperties; |
|---|
| 35 | import org.docx4j.jaxb.Context; |
|---|
| 36 | import org.docx4j.jaxb.NamespacePrefixMappings; |
|---|
| 37 | import org.docx4j.openpackaging.exceptions.Docx4JException; |
|---|
| 38 | import org.docx4j.openpackaging.exceptions.InvalidFormatException; |
|---|
| 39 | import org.docx4j.openpackaging.parts.relationships.Namespaces; |
|---|
| 40 | import org.w3c.dom.Document; |
|---|
| 41 | import org.w3c.dom.Element; |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | public class DocPropsCorePart extends JaxbXmlPart<CoreProperties> { |
|---|
| 45 | |
|---|
| 46 | /* |
|---|
| 47 | * <?xml version="1.0" encoding="UTF-8" standalone="yes"?> |
|---|
| 48 | * <cp:coreProperties |
|---|
| 49 | * xmlns:cp="http://schemas.openxmlformats.org/package/2006/metadata/core-properties" |
|---|
| 50 | * xmlns:dc="http://purl.org/dc/elements/1.1/" |
|---|
| 51 | * xmlns:dcterms="http://purl.org/dc/terms/" |
|---|
| 52 | * xmlns:dcmitype="http://purl.org/dc/dcmitype/" |
|---|
| 53 | * xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> |
|---|
| 54 | * <dc:creator>Jason Harrop</dc:creator> |
|---|
| 55 | * <cp:lastModifiedBy>Jason Harrop</cp:lastModifiedBy> |
|---|
| 56 | * <cp:revision>2</cp:revision> |
|---|
| 57 | * <dcterms:created xsi:type="dcterms:W3CDTF">2007-08-12T00:34:00Z</dcterms:created> |
|---|
| 58 | * <dcterms:modified xsi:type="dcterms:W3CDTF">2007-08-12T00:34:00Z</dcterms:modified> |
|---|
| 59 | * </cp:coreProperties> |
|---|
| 60 | */ |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | private static Logger log = Logger.getLogger(DocPropsCorePart.class); |
|---|
| 64 | |
|---|
| 65 | private static XPathFactory xPathFactory; |
|---|
| 66 | private static XPath xPath; |
|---|
| 67 | static { |
|---|
| 68 | xPathFactory = XPathFactory.newInstance(); |
|---|
| 69 | xPath = xPathFactory.newXPath(); |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | /** |
|---|
| 73 | * @throws InvalidFormatException |
|---|
| 74 | */ |
|---|
| 75 | public DocPropsCorePart(PartName partName) throws InvalidFormatException { |
|---|
| 76 | super(partName); |
|---|
| 77 | init(); |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | public DocPropsCorePart() throws InvalidFormatException { |
|---|
| 81 | super(new PartName("/docProps/core.xml")); |
|---|
| 82 | init(); |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | public void init() { |
|---|
| 86 | // Used if this Part is added to [Content_Types].xml |
|---|
| 87 | setContentType(new org.docx4j.openpackaging.contenttype.ContentType( |
|---|
| 88 | org.docx4j.openpackaging.contenttype.ContentTypes.PACKAGE_COREPROPERTIES)); |
|---|
| 89 | |
|---|
| 90 | // Used when this Part is added to a rels |
|---|
| 91 | setRelationshipType(Namespaces.PROPERTIES_CORE); |
|---|
| 92 | |
|---|
| 93 | setJAXBContext(Context.jcDocPropsCore); |
|---|
| 94 | |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | /** |
|---|
| 98 | * Unmarshal XML data from the specified InputStream and return the |
|---|
| 99 | * resulting content tree. Validation event location information may |
|---|
| 100 | * be incomplete when using this form of the unmarshal API. |
|---|
| 101 | * |
|---|
| 102 | * <p> |
|---|
| 103 | * Implements <a href="#unmarshalGlobal">Unmarshal Global Root Element</a>. |
|---|
| 104 | * |
|---|
| 105 | * @param is the InputStream to unmarshal XML data from |
|---|
| 106 | * @return the newly created root object of the java content tree |
|---|
| 107 | * |
|---|
| 108 | * @throws JAXBException |
|---|
| 109 | * If any unexpected errors occur while unmarshalling |
|---|
| 110 | */ |
|---|
| 111 | @Override |
|---|
| 112 | public CoreProperties unmarshal( java.io.InputStream is ) throws JAXBException { |
|---|
| 113 | |
|---|
| 114 | try { |
|---|
| 115 | |
|---|
| 116 | setJAXBContext(org.docx4j.jaxb.Context.jcDocPropsCore); |
|---|
| 117 | Unmarshaller u = jc.createUnmarshaller(); |
|---|
| 118 | |
|---|
| 119 | //u.setSchema(org.docx4j.jaxb.WmlSchema.schema); |
|---|
| 120 | u.setEventHandler(new org.docx4j.jaxb.JaxbValidationEventHandler()); |
|---|
| 121 | |
|---|
| 122 | log.info("unmarshalling " + this.getClass().getName()); |
|---|
| 123 | |
|---|
| 124 | jaxbElement = (CoreProperties) u.unmarshal( is ); |
|---|
| 125 | |
|---|
| 126 | } catch (Exception e ) { |
|---|
| 127 | e.printStackTrace(); |
|---|
| 128 | } |
|---|
| 129 | |
|---|
| 130 | return jaxbElement; |
|---|
| 131 | |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | // Core Props and Extended Props can both be bound as if they |
|---|
| 135 | // were a custom xml part. Copied from XmlPart. |
|---|
| 136 | |
|---|
| 137 | public String xpathGetString(String xpathString, String prefixMappings) throws Docx4JException { |
|---|
| 138 | |
|---|
| 139 | Document doc = XmlUtils.marshaltoW3CDomDocument( |
|---|
| 140 | getJaxbElement(), Context.jcDocPropsCore ); |
|---|
| 141 | |
|---|
| 142 | try { |
|---|
| 143 | getNamespaceContext().registerPrefixMappings(prefixMappings); |
|---|
| 144 | |
|---|
| 145 | String result = xPath.evaluate(xpathString, doc ); |
|---|
| 146 | log.debug(xpathString + " ---> " + result); |
|---|
| 147 | return result; |
|---|
| 148 | } catch (Exception e) { |
|---|
| 149 | throw new Docx4JException("Problems evaluating xpath '" + xpathString + "'", e); |
|---|
| 150 | } |
|---|
| 151 | } |
|---|
| 152 | private NamespacePrefixMappings nsContext; |
|---|
| 153 | private NamespacePrefixMappings getNamespaceContext() { |
|---|
| 154 | if (nsContext==null) { |
|---|
| 155 | nsContext = new NamespacePrefixMappings(); |
|---|
| 156 | xPath.setNamespaceContext(nsContext); |
|---|
| 157 | } |
|---|
| 158 | return nsContext; |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | |
|---|
| 162 | } |
|---|
| 163 | |
|---|
| 164 | |
|---|