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;
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.JAXBContext;
025: import javax.xml.bind.JAXBException;
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.parsers.ParserConfigurationException;
031: import javax.xml.parsers.SAXParserFactory;
032: import javax.xml.parsers.SAXParser;
033: import javax.xml.transform.sax.SAXSource;
034: import java.io.ByteArrayOutputStream;
035: import java.io.InputStream;
036: import java.io.OutputStream;
037: import java.io.IOException;
038: import java.io.ByteArrayInputStream;
039: import java.util.Map;
040: import java.util.HashMap;
041: import java.util.Set;
042: import java.util.TreeSet;
043:
044: /**
045: * @version $Rev: 607077 $ $Date: 2007-12-27 06:55:23 -0800 $
046: */
047: public class JaxbJavaee {
048: public static final ThreadLocal<Set<String>> currentPublicId = new ThreadLocal<Set<String>>();
049:
050: private static Map<Class<?>, JAXBContext> jaxbContexts = new HashMap<Class<?>, JAXBContext>();
051:
052: public static <T> String marshal(Class<T> type, Object object)
053: throws JAXBException {
054: ByteArrayOutputStream baos = new ByteArrayOutputStream();
055:
056: marshal(type, object, baos);
057:
058: return new String(baos.toByteArray());
059: }
060:
061: public static <T> void marshal(Class<T> type, Object object,
062: OutputStream out) throws JAXBException {
063: JAXBContext ctx2 = JaxbJavaee.getContext(type);
064: Marshaller marshaller = ctx2.createMarshaller();
065:
066: marshaller.setProperty("jaxb.formatted.output", true);
067:
068: marshaller.marshal(object, out);
069: }
070:
071: private static <T> JAXBContext getContext(Class<T> type)
072: throws JAXBException {
073: JAXBContext jaxbContext = jaxbContexts.get(type);
074: if (jaxbContext == null) {
075: jaxbContext = JAXBContext.newInstance(type);
076: jaxbContexts.put(type, jaxbContext);
077: }
078: return jaxbContext;
079: }
080:
081: public static <T> Object unmarshal(Class<T> type, InputStream in)
082: throws ParserConfigurationException, SAXException,
083: JAXBException {
084: InputSource inputSource = new InputSource(in);
085:
086: SAXParserFactory factory = SAXParserFactory.newInstance();
087: factory.setNamespaceAware(true);
088: factory.setValidating(false);
089: SAXParser parser = factory.newSAXParser();
090:
091: JAXBContext ctx = JaxbJavaee.getContext(type);
092: Unmarshaller unmarshaller = ctx.createUnmarshaller();
093: unmarshaller.setEventHandler(new ValidationEventHandler() {
094: public boolean handleEvent(ValidationEvent validationEvent) {
095: System.out.println(validationEvent);
096: return false;
097: }
098: });
099:
100: JaxbJavaee.NamespaceFilter xmlFilter = new JaxbJavaee.NamespaceFilter(
101: parser.getXMLReader());
102: xmlFilter.setContentHandler(unmarshaller
103: .getUnmarshallerHandler());
104:
105: SAXSource source = new SAXSource(xmlFilter, inputSource);
106:
107: currentPublicId.set(new TreeSet<String>());
108: try {
109: return unmarshaller.unmarshal(source);
110: } finally {
111: currentPublicId.set(null);
112: }
113: }
114:
115: public static class NamespaceFilter extends XMLFilterImpl {
116: private static final InputSource EMPTY_INPUT_SOURCE = new InputSource(
117: new ByteArrayInputStream(new byte[0]));
118:
119: public NamespaceFilter(XMLReader xmlReader) {
120: super (xmlReader);
121: }
122:
123: public InputSource resolveEntity(String publicId,
124: String systemId) throws SAXException, IOException {
125: Set<String> publicIds = currentPublicId.get();
126: if (publicIds != null) {
127: publicIds.add(publicId);
128: }
129: return EMPTY_INPUT_SOURCE;
130: }
131:
132: public void startElement(String uri, String localName,
133: String qname, Attributes atts) throws SAXException {
134: super .startElement("http://java.sun.com/xml/ns/javaee",
135: localName, qname, atts);
136: }
137: }
138: }
|