| 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.jaxb; |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | import javax.xml.bind.JAXBContext; |
|---|
| 24 | import javax.xml.bind.JAXBException; |
|---|
| 25 | |
|---|
| 26 | import org.apache.log4j.Logger; |
|---|
| 27 | import org.docx4j.utils.Log4jConfigurator; |
|---|
| 28 | |
|---|
| 29 | public class Context { |
|---|
| 30 | |
|---|
| 31 | public static JAXBContext jc; |
|---|
| 32 | public static JAXBContext jcThemePart; |
|---|
| 33 | public static JAXBContext jcDocPropsCore; |
|---|
| 34 | public static JAXBContext jcDocPropsCustom; |
|---|
| 35 | public static JAXBContext jcDocPropsExtended; |
|---|
| 36 | public static JAXBContext jcRelationships; |
|---|
| 37 | public static JAXBContext jcCustomXmlProperties; |
|---|
| 38 | public static JAXBContext jcContentTypes; |
|---|
| 39 | |
|---|
| 40 | public static JAXBContext jcXmlPackage; |
|---|
| 41 | |
|---|
| 42 | private static JAXBContext jcXslFo; |
|---|
| 43 | public static JAXBContext jcSectionModel; |
|---|
| 44 | |
|---|
| 45 | public static JAXBContext jcXmlDSig; |
|---|
| 46 | |
|---|
| 47 | private static Logger log = Logger.getLogger(Context.class); |
|---|
| 48 | |
|---|
| 49 | static { |
|---|
| 50 | |
|---|
| 51 | Log4jConfigurator.configure(); |
|---|
| 52 | |
|---|
| 53 | // Display diagnostic info about version of JAXB being used. |
|---|
| 54 | Object namespacePrefixMapper; |
|---|
| 55 | try { |
|---|
| 56 | namespacePrefixMapper = NamespacePrefixMapperUtils.getPrefixMapper(); |
|---|
| 57 | if ( namespacePrefixMapper.getClass().getName().equals("org.docx4j.jaxb.NamespacePrefixMapperSunInternal") ) { |
|---|
| 58 | // Java 6 |
|---|
| 59 | log.info("Using Java 6/7 JAXB implementation"); |
|---|
| 60 | } else { |
|---|
| 61 | log.info("Using JAXB Reference Implementation"); |
|---|
| 62 | } |
|---|
| 63 | } catch (JAXBException e) { |
|---|
| 64 | log.error("PANIC! No suitable JAXB implementation available"); |
|---|
| 65 | e.printStackTrace(); |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | try { |
|---|
| 69 | |
|---|
| 70 | // JBOSS might use a different class loader to load JAXBContext, which causes problems, |
|---|
| 71 | // so explicitly specify our class loader. |
|---|
| 72 | Context tmp = new Context(); |
|---|
| 73 | java.lang.ClassLoader classLoader = tmp.getClass().getClassLoader(); |
|---|
| 74 | //log.info("\n\nClassloader: " + classLoader.toString() ); |
|---|
| 75 | |
|---|
| 76 | log.info("loading Context jc"); |
|---|
| 77 | jc = JAXBContext.newInstance("org.docx4j.wml:" + |
|---|
| 78 | "org.docx4j.dml:org.docx4j.dml.chart:org.docx4j.dml.chartDrawing:org.docx4j.dml.compatibility:org.docx4j.dml.diagram:org.docx4j.dml.lockedCanvas:org.docx4j.dml.picture:org.docx4j.dml.wordprocessingDrawing:org.docx4j.dml.spreadsheetdrawing:org.docx4j.dml.diagram2008:" + |
|---|
| 79 | // All VML stuff is here, since compiling it requires WML and DML (and MathML), but not PML or SML |
|---|
| 80 | "org.docx4j.vml:org.docx4j.vml.officedrawing:org.docx4j.vml.wordprocessingDrawing:org.docx4j.vml.presentationDrawing:org.docx4j.vml.spreadsheetDrawing:org.docx4j.vml.root:" + |
|---|
| 81 | "org.opendope.xpaths:org.opendope.conditions:org.opendope.questions:org.opendope.components:org.opendope.SmartArt.dataHierarchy:" + |
|---|
| 82 | "org.docx4j.math:" + |
|---|
| 83 | "org.docx4j.sharedtypes:org.docx4j.bibliography",classLoader ); |
|---|
| 84 | log.info("loaded " + jc.getClass().getName() + " .. loading others .."); |
|---|
| 85 | |
|---|
| 86 | jcThemePart = jc; //JAXBContext.newInstance("org.docx4j.dml",classLoader ); |
|---|
| 87 | jcDocPropsCore = JAXBContext.newInstance("org.docx4j.docProps.core:org.docx4j.docProps.core.dc.elements:org.docx4j.docProps.core.dc.terms",classLoader ); |
|---|
| 88 | jcDocPropsCustom = JAXBContext.newInstance("org.docx4j.docProps.custom",classLoader ); |
|---|
| 89 | jcDocPropsExtended = JAXBContext.newInstance("org.docx4j.docProps.extended",classLoader ); |
|---|
| 90 | jcXmlPackage = JAXBContext.newInstance("org.docx4j.xmlPackage",classLoader ); |
|---|
| 91 | jcRelationships = JAXBContext.newInstance("org.docx4j.relationships",classLoader ); |
|---|
| 92 | jcCustomXmlProperties = JAXBContext.newInstance("org.docx4j.customXmlProperties",classLoader ); |
|---|
| 93 | jcContentTypes = JAXBContext.newInstance("org.docx4j.openpackaging.contenttype",classLoader ); |
|---|
| 94 | |
|---|
| 95 | jcSectionModel = JAXBContext.newInstance("org.docx4j.model.structure.jaxb",classLoader ); |
|---|
| 96 | |
|---|
| 97 | jcXmlDSig = JAXBContext.newInstance("org.plutext.jaxb.xmldsig",classLoader ); |
|---|
| 98 | |
|---|
| 99 | log.info(".. others loaded .."); |
|---|
| 100 | |
|---|
| 101 | } catch (Exception ex) { |
|---|
| 102 | log.error("Cannot initialize context", ex); |
|---|
| 103 | } |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | private static org.docx4j.wml.ObjectFactory wmlObjectFactory; |
|---|
| 107 | |
|---|
| 108 | public static org.docx4j.wml.ObjectFactory getWmlObjectFactory() { |
|---|
| 109 | |
|---|
| 110 | if (wmlObjectFactory==null) { |
|---|
| 111 | wmlObjectFactory = new org.docx4j.wml.ObjectFactory(); |
|---|
| 112 | } |
|---|
| 113 | return wmlObjectFactory; |
|---|
| 114 | |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | public static JAXBContext getXslFoContext() { |
|---|
| 118 | if (jcXslFo==null) { |
|---|
| 119 | try { |
|---|
| 120 | Context tmp = new Context(); |
|---|
| 121 | java.lang.ClassLoader classLoader = tmp.getClass().getClassLoader(); |
|---|
| 122 | |
|---|
| 123 | jcXslFo = JAXBContext.newInstance("org.plutext.jaxb.xslfo",classLoader ); |
|---|
| 124 | |
|---|
| 125 | } catch (JAXBException ex) { |
|---|
| 126 | log.error("Cannot determine XSL-FO context", ex); |
|---|
| 127 | } |
|---|
| 128 | } |
|---|
| 129 | return jcXslFo; |
|---|
| 130 | } |
|---|
| 131 | } |
|---|