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.sun;
017:
018: import java.io.ByteArrayOutputStream;
019: import java.io.InputStream;
020: import java.io.ByteArrayInputStream;
021: import java.io.IOException;
022: import javax.xml.bind.JAXBException;
023: import javax.xml.bind.JAXBContext;
024: import javax.xml.bind.Marshaller;
025: import javax.xml.bind.Unmarshaller;
026: import javax.xml.bind.ValidationEventHandler;
027: import javax.xml.bind.ValidationEvent;
028: import javax.xml.parsers.ParserConfigurationException;
029: import javax.xml.parsers.SAXParserFactory;
030: import javax.xml.parsers.SAXParser;
031: import javax.xml.transform.sax.SAXSource;
032:
033: import org.xml.sax.SAXException;
034: import org.xml.sax.InputSource;
035: import org.xml.sax.XMLReader;
036: import org.xml.sax.helpers.XMLFilterImpl;
037:
038: /**
039: * @version $Rev: 607077 $ $Date: 2007-12-27 06:55:23 -0800 $
040: */
041: public class JaxbSun {
042:
043: public static <T> String marshal(Class<T> type, Object object)
044: throws JAXBException {
045: JAXBContext ctx2 = JAXBContext.newInstance(type);
046: Marshaller marshaller = ctx2.createMarshaller();
047:
048: marshaller.setProperty("jaxb.formatted.output", true);
049:
050: ByteArrayOutputStream baos = new ByteArrayOutputStream();
051: marshaller.marshal(object, baos);
052:
053: return new String(baos.toByteArray());
054: }
055:
056: public static <T> Object unmarshal(Class<T> type, InputStream in)
057: throws ParserConfigurationException, SAXException,
058: JAXBException {
059: return JaxbSun.unmarshal(type, in, true);
060: }
061:
062: public static <T> Object unmarshal(Class<T> type, InputStream in,
063: final boolean logErrors)
064: throws ParserConfigurationException, SAXException,
065: JAXBException {
066: // create a parser with validation disabled
067: SAXParserFactory factory = SAXParserFactory.newInstance();
068: factory.setNamespaceAware(true);
069: factory.setValidating(false);
070: SAXParser parser = factory.newSAXParser();
071:
072: // Get the JAXB context -- this should be cached
073: JAXBContext ctx = JAXBContext.newInstance(type);
074:
075: // get the unmarshaller
076: Unmarshaller unmarshaller = ctx.createUnmarshaller();
077:
078: // log errors?
079: unmarshaller.setEventHandler(new ValidationEventHandler() {
080: public boolean handleEvent(ValidationEvent validationEvent) {
081: if (logErrors) {
082: System.out.println(validationEvent);
083: }
084: return false;
085: }
086: });
087:
088: // add our XMLFilter which disables dtd downloading
089: NamespaceFilter xmlFilter = new NamespaceFilter(parser
090: .getXMLReader());
091: xmlFilter.setContentHandler(unmarshaller
092: .getUnmarshallerHandler());
093:
094: // Wrap the input stream with our filter
095: SAXSource source = new SAXSource(xmlFilter, new InputSource(in));
096:
097: // unmarshal the document
098: return unmarshaller.unmarshal(source);
099: }
100:
101: // todo Inject the proper namespace
102: public static class NamespaceFilter extends XMLFilterImpl {
103: private static final InputSource EMPTY_INPUT_SOURCE = new InputSource(
104: new ByteArrayInputStream(new byte[0]));
105:
106: public NamespaceFilter(XMLReader xmlReader) {
107: super (xmlReader);
108: }
109:
110: public InputSource resolveEntity(String publicId,
111: String systemId) throws SAXException, IOException {
112: return EMPTY_INPUT_SOURCE;
113: }
114: }
115: }
|