001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.openejb.jee.oejb2;
017:
018: import java.io.ByteArrayOutputStream;
019: import java.io.InputStream;
020: import java.util.HashMap;
021: import java.util.Map;
022: import javax.xml.bind.JAXBContext;
023: import javax.xml.bind.JAXBException;
024: import javax.xml.bind.Marshaller;
025: import javax.xml.bind.Unmarshaller;
026: import javax.xml.bind.ValidationEvent;
027: import javax.xml.bind.ValidationEventHandler;
028: import javax.xml.parsers.ParserConfigurationException;
029: import javax.xml.parsers.SAXParser;
030: import javax.xml.parsers.SAXParserFactory;
031: import javax.xml.transform.sax.SAXSource;
032:
033: import org.xml.sax.InputSource;
034: import org.xml.sax.SAXException;
035:
036: /**
037: * @version $Rev: 607077 $ $Date: 2007-12-27 06:55:23 -0800 $
038: */
039: public class JaxbOpenejbJar2 {
040:
041: private static final Map<Class<?>, JAXBContext> contexts = new HashMap<Class<?>, JAXBContext>();
042:
043: private static JAXBContext getContext(Class<?> type)
044: throws JAXBException {
045: JAXBContext jaxbContext = contexts.get(type);
046: if (jaxbContext == null) {
047: jaxbContext = JAXBContext.newInstance(type);
048: contexts.put(type, jaxbContext);
049: }
050: return jaxbContext;
051: }
052:
053: public static <T> String marshal(Class<T> type, Object object)
054: throws JAXBException {
055: JAXBContext ctx2 = getContext(type);
056: Marshaller marshaller = ctx2.createMarshaller();
057:
058: marshaller.setProperty("jaxb.formatted.output", true);
059:
060: ByteArrayOutputStream baos = new ByteArrayOutputStream();
061: marshaller.marshal(object, baos);
062:
063: return new String(baos.toByteArray());
064: }
065:
066: public static <T> Object unmarshal(Class<T> type, InputStream in)
067: throws ParserConfigurationException, SAXException,
068: JAXBException {
069: return unmarshal(type, in, true);
070: }
071:
072: public static <T> Object unmarshal(Class<T> type, InputStream in,
073: final boolean logErrors)
074: throws ParserConfigurationException, SAXException,
075: JAXBException {
076: InputSource inputSource = new InputSource(in);
077:
078: SAXParserFactory factory = SAXParserFactory.newInstance();
079: factory.setNamespaceAware(true);
080: factory.setValidating(false);
081: SAXParser parser = factory.newSAXParser();
082:
083: JAXBContext ctx = getContext(type);
084: Unmarshaller unmarshaller = ctx.createUnmarshaller();
085: unmarshaller.setEventHandler(new ValidationEventHandler() {
086: public boolean handleEvent(ValidationEvent validationEvent) {
087: if (logErrors) {
088: System.out.println(validationEvent);
089: }
090: return false;
091: }
092: });
093:
094: unmarshaller.setListener(new Unmarshaller.Listener() {
095: public void afterUnmarshal(Object object, Object object1) {
096: super .afterUnmarshal(object, object1);
097: }
098:
099: public void beforeUnmarshal(Object target, Object parent) {
100: super .beforeUnmarshal(target, parent);
101: }
102: });
103:
104: NamespaceFilter xmlFilter = new NamespaceFilter(parser
105: .getXMLReader());
106: xmlFilter.setContentHandler(unmarshaller
107: .getUnmarshallerHandler());
108:
109: SAXSource source = new SAXSource(xmlFilter, inputSource);
110:
111: return unmarshaller.unmarshal(source);
112: }
113: }
|