|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| JAXBSupport.java | 0% | 0% | 0% | 0% |
|
||||||||||||||
| 1 | /* | |
| 2 | * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved. | |
| 3 | * | |
| 4 | * This software is open source. | |
| 5 | * See the bottom of this file for the licence. | |
| 6 | * | |
| 7 | * $Id: JAXBSupport.java,v 1.1 2004/08/02 18:44:08 maartenc Exp $ | |
| 8 | */ | |
| 9 | ||
| 10 | package org.dom4j.jaxb; | |
| 11 | ||
| 12 | import java.io.StringReader; | |
| 13 | ||
| 14 | import javax.xml.bind.JAXBContext; | |
| 15 | import javax.xml.bind.JAXBException; | |
| 16 | import javax.xml.bind.Marshaller; | |
| 17 | import javax.xml.bind.Unmarshaller; | |
| 18 | import javax.xml.transform.Source; | |
| 19 | import javax.xml.transform.stream.StreamSource; | |
| 20 | ||
| 21 | import org.dom4j.dom.DOMDocument; | |
| 22 | ||
| 23 | /** | |
| 24 | * @author Wonne Keysers (Realsoftware.be) | |
| 25 | */ | |
| 26 | abstract class JAXBSupport { | |
| 27 | ||
| 28 | private String contextPath; | |
| 29 | private ClassLoader classloader; | |
| 30 | ||
| 31 | private JAXBContext jaxbContext; | |
| 32 | private Marshaller marshaller; | |
| 33 | private Unmarshaller unmarshaller; | |
| 34 | ||
| 35 | 0 | public JAXBSupport(String contextPath) { |
| 36 | 0 | this.contextPath = contextPath; |
| 37 | } | |
| 38 | ||
| 39 | 0 | public JAXBSupport(String contextPath, ClassLoader classloader) { |
| 40 | 0 | this.contextPath = contextPath; |
| 41 | 0 | this.classloader = classloader; |
| 42 | } | |
| 43 | ||
| 44 | /** | |
| 45 | * Marshals the given {@link javax.xml.bind.Element} in to its DOM4J counterpart. | |
| 46 | * | |
| 47 | * @param element JAXB Element to be marshalled | |
| 48 | * @return the marshalled DOM4J {@link org.dom4j.Element} | |
| 49 | * @throws JAXBException when an error occurs | |
| 50 | */ | |
| 51 | 0 | protected org.dom4j.Element marshal(javax.xml.bind.Element element) throws JAXBException { |
| 52 | 0 | Marshaller marshaller = getMarshaller(); |
| 53 | ||
| 54 | 0 | DOMDocument doc = new DOMDocument(); |
| 55 | 0 | marshaller.marshal(element, doc); |
| 56 | ||
| 57 | 0 | return doc.getRootElement(); |
| 58 | } | |
| 59 | ||
| 60 | /** | |
| 61 | * Unmarshalls the specified DOM4J {@link org.dom4j.Element} into a {@link javax.xml.bind.Element} | |
| 62 | * @param element the DOM4J element to unmarshall | |
| 63 | * @return the unmarshalled JAXB object | |
| 64 | * @throws JAXBException when an error occurs | |
| 65 | 0 | */protected javax.xml.bind.Element unmarshal(org.dom4j.Element element) throws JAXBException { |
| 66 | 0 | Unmarshaller unmarshaller = getUnmarshaller(); |
| 67 | ||
| 68 | 0 | Source source = new StreamSource(new StringReader(element.asXML())); |
| 69 | ||
| 70 | 0 | return (javax.xml.bind.Element) unmarshaller.unmarshal(source); |
| 71 | } | |
| 72 | ||
| 73 | 0 | private Marshaller getMarshaller() throws JAXBException { |
| 74 | 0 | if (marshaller == null) { |
| 75 | 0 | marshaller = getContext().createMarshaller(); |
| 76 | } | |
| 77 | 0 | return marshaller; |
| 78 | } | |
| 79 | ||
| 80 | 0 | private Unmarshaller getUnmarshaller() throws JAXBException { |
| 81 | 0 | if (unmarshaller == null) { |
| 82 | 0 | unmarshaller = getContext().createUnmarshaller(); |
| 83 | } | |
| 84 | 0 | return unmarshaller; |
| 85 | } | |
| 86 | ||
| 87 | 0 | private JAXBContext getContext() throws JAXBException { |
| 88 | 0 | if (jaxbContext == null) { |
| 89 | 0 | if (classloader == null) { |
| 90 | 0 | jaxbContext = JAXBContext.newInstance(contextPath); |
| 91 | } | |
| 92 | else { | |
| 93 | 0 | jaxbContext = JAXBContext.newInstance(contextPath, classloader); |
| 94 | } | |
| 95 | } | |
| 96 | 0 | return jaxbContext; |
| 97 | } | |
| 98 | ||
| 99 | } | |
| 100 | ||
| 101 | ||
| 102 | ||
| 103 | ||
| 104 | /* | |
| 105 | * Redistribution and use of this software and associated documentation | |
| 106 | * ("Software"), with or without modification, are permitted provided | |
| 107 | * that the following conditions are met: | |
| 108 | * | |
| 109 | * 1. Redistributions of source code must retain copyright | |
| 110 | * statements and notices. Redistributions must also contain a | |
| 111 | * copy of this document. | |
| 112 | * | |
| 113 | * 2. Redistributions in binary form must reproduce the | |
| 114 | * above copyright notice, this list of conditions and the | |
| 115 | * following disclaimer in the documentation and/or other | |
| 116 | * materials provided with the distribution. | |
| 117 | * | |
| 118 | * 3. The name "DOM4J" must not be used to endorse or promote | |
| 119 | * products derived from this Software without prior written | |
| 120 | * permission of MetaStuff, Ltd. For written permission, | |
| 121 | * please contact dom4j-info@metastuff.com. | |
| 122 | * | |
| 123 | * 4. Products derived from this Software may not be called "DOM4J" | |
| 124 | * nor may "DOM4J" appear in their names without prior written | |
| 125 | * permission of MetaStuff, Ltd. DOM4J is a registered | |
| 126 | * trademark of MetaStuff, Ltd. | |
| 127 | * | |
| 128 | * 5. Due credit should be given to the DOM4J Project - | |
| 129 | * http://www.dom4j.org | |
| 130 | * | |
| 131 | * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS | |
| 132 | * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT | |
| 133 | * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND | |
| 134 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL | |
| 135 | * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, | |
| 136 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
| 137 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
| 138 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
| 139 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
| 140 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
| 141 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
| 142 | * OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 143 | * | |
| 144 | * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved. | |
| 145 | * | |
| 146 | * $Id: JAXBSupport.java,v 1.1 2004/08/02 18:44:08 maartenc Exp $ | |
| 147 | */ |
|
||||||||||