| 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 | import javax.xml.bind.JAXBException; |
|---|
| 24 | import javax.xml.bind.Unmarshaller; |
|---|
| 25 | |
|---|
| 26 | import org.apache.log4j.Logger; |
|---|
| 27 | import org.docx4j.dml.Theme; |
|---|
| 28 | import org.docx4j.jaxb.Context; |
|---|
| 29 | import org.docx4j.openpackaging.exceptions.InvalidFormatException; |
|---|
| 30 | import org.docx4j.openpackaging.parts.relationships.Namespaces; |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | public final class ThemePart extends JaxbXmlPart<Theme> { |
|---|
| 35 | |
|---|
| 36 | private static Logger log = Logger.getLogger(ThemePart.class); |
|---|
| 37 | |
|---|
| 38 | public ThemePart(PartName partName) throws InvalidFormatException { |
|---|
| 39 | super(partName); |
|---|
| 40 | init(); |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | public ThemePart() throws InvalidFormatException { |
|---|
| 44 | super(new PartName("/word/theme/theme1.xml")); |
|---|
| 45 | init(); |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | public void init() { |
|---|
| 50 | |
|---|
| 51 | // Used if this Part is added to [Content_Types].xml |
|---|
| 52 | setContentType(new org.docx4j.openpackaging.contenttype.ContentType( |
|---|
| 53 | org.docx4j.openpackaging.contenttype.ContentTypes.OFFICEDOCUMENT_THEME)); |
|---|
| 54 | |
|---|
| 55 | // Used when this Part is added to a rels |
|---|
| 56 | setRelationshipType(Namespaces.THEME); |
|---|
| 57 | |
|---|
| 58 | setJAXBContext(Context.jcThemePart); |
|---|
| 59 | |
|---|
| 60 | |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | private org.docx4j.dml.BaseStyles.FontScheme fontScheme = null; |
|---|
| 64 | |
|---|
| 65 | public org.docx4j.dml.BaseStyles.FontScheme getFontScheme() { |
|---|
| 66 | if (fontScheme == null) { // ie we haven't done this already |
|---|
| 67 | org.docx4j.dml.Theme theme = (org.docx4j.dml.Theme) this |
|---|
| 68 | .getJaxbElement(); |
|---|
| 69 | if (theme.getThemeElements() != null |
|---|
| 70 | && theme.getThemeElements().getFontScheme() != null) { |
|---|
| 71 | fontScheme = theme.getThemeElements().getFontScheme(); |
|---|
| 72 | } |
|---|
| 73 | } |
|---|
| 74 | return fontScheme; |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | /** |
|---|
| 78 | * Unmarshal XML data from the specified InputStream and return the |
|---|
| 79 | * resulting content tree. Validation event location information may |
|---|
| 80 | * be incomplete when using this form of the unmarshal API. |
|---|
| 81 | * |
|---|
| 82 | * <p> |
|---|
| 83 | * Implements <a href="#unmarshalGlobal">Unmarshal Global Root Element</a>. |
|---|
| 84 | * |
|---|
| 85 | * @param is the InputStream to unmarshal XML data from |
|---|
| 86 | * @return the newly created root object of the java content tree |
|---|
| 87 | * |
|---|
| 88 | * @throws JAXBException |
|---|
| 89 | * If any unexpected errors occur while unmarshalling |
|---|
| 90 | */ |
|---|
| 91 | @Override |
|---|
| 92 | public Theme unmarshal( java.io.InputStream is ) throws JAXBException { |
|---|
| 93 | |
|---|
| 94 | try { |
|---|
| 95 | |
|---|
| 96 | setJAXBContext(org.docx4j.jaxb.Context.jcThemePart); |
|---|
| 97 | |
|---|
| 98 | Unmarshaller u = jc.createUnmarshaller(); |
|---|
| 99 | |
|---|
| 100 | //u.setSchema(org.docx4j.jaxb.WmlSchema.schema); |
|---|
| 101 | u.setEventHandler(new org.docx4j.jaxb.JaxbValidationEventHandler()); |
|---|
| 102 | |
|---|
| 103 | log.info("unmarshalling " + this.getClass().getName() ); |
|---|
| 104 | jaxbElement = (Theme) u.unmarshal( is ); |
|---|
| 105 | |
|---|
| 106 | } catch (Exception e ) { |
|---|
| 107 | e.printStackTrace(); |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | return jaxbElement; |
|---|
| 111 | |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | } |
|---|