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.oejb3;
017:
018: import org.xml.sax.SAXException;
019: import org.xml.sax.InputSource;
020: import org.xml.sax.XMLReader;
021: import org.xml.sax.Attributes;
022: import org.xml.sax.helpers.XMLFilterImpl;
023:
024: import javax.xml.bind.JAXBException;
025: import javax.xml.bind.JAXBContext;
026: import javax.xml.bind.Marshaller;
027: import javax.xml.bind.Unmarshaller;
028: import javax.xml.bind.ValidationEventHandler;
029: import javax.xml.bind.ValidationEvent;
030: import javax.xml.bind.JAXBElement;
031: import javax.xml.parsers.ParserConfigurationException;
032: import javax.xml.parsers.SAXParserFactory;
033: import javax.xml.parsers.SAXParser;
034: import javax.xml.transform.sax.SAXSource;
035: import java.io.ByteArrayOutputStream;
036: import java.io.InputStream;
037: import java.io.OutputStream;
038: import java.io.ByteArrayInputStream;
039: import java.io.IOException;
040:
041: /**
042: * @version $Rev: 607077 $ $Date: 2007-12-27 06:55:23 -0800 $
043: */
044: public class JaxbOpenejbJar3 {
045: private static JAXBContext jaxbContext;
046:
047: public static <T> String marshal(Class<T> type, Object object)
048: throws JAXBException {
049: ByteArrayOutputStream baos = new ByteArrayOutputStream();
050: marshal(type, object, baos);
051:
052: return new String(baos.toByteArray());
053: }
054:
055: public static <T> void marshal(Class<T> type, Object object,
056: OutputStream out) throws JAXBException {
057: JAXBContext ctx2 = getContext(type);
058: Marshaller marshaller = ctx2.createMarshaller();
059:
060: marshaller.setProperty("jaxb.formatted.output", true);
061:
062: marshaller.marshal(object, out);
063: }
064:
065: private static <T> JAXBContext getContext(Class<T> type)
066: throws JAXBException {
067: if (jaxbContext == null) {
068: jaxbContext = JAXBContext.newInstance(type);
069: }
070: return jaxbContext;
071: }
072:
073: public static <T> T unmarshal(Class<T> type, InputStream in)
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: // System.out.println(validationEvent);
088: return false;
089: }
090: });
091:
092: NamespaceFilter xmlFilter = new NamespaceFilter(parser
093: .getXMLReader());
094: xmlFilter.setContentHandler(unmarshaller
095: .getUnmarshallerHandler());
096:
097: SAXSource source = new SAXSource(xmlFilter, inputSource);
098:
099: Object o = unmarshaller.unmarshal(source);
100: if (o instanceof JAXBElement) {
101: JAXBElement element = (JAXBElement) o;
102: return (T) element.getValue();
103: }
104: return (T) o;
105: }
106:
107: public static class NamespaceFilter extends XMLFilterImpl {
108: private static final InputSource EMPTY_INPUT_SOURCE = new InputSource(
109: new ByteArrayInputStream(new byte[0]));
110:
111: public NamespaceFilter(XMLReader xmlReader) {
112: super (xmlReader);
113: }
114:
115: public InputSource resolveEntity(String publicId,
116: String systemId) throws SAXException, IOException {
117: return EMPTY_INPUT_SOURCE;
118: }
119:
120: public void startElement(String uri, String localName,
121: String qname, Attributes atts) throws SAXException {
122: super .startElement(
123: "http://www.openejb.org/openejb-jar/1.1",
124: localName, qname, atts);
125: }
126: }
127: }
|