001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.xml.ws.message.jaxb;
038:
039: import com.sun.xml.bind.api.Bridge;
040: import org.xml.sax.*;
041: import org.xml.sax.ext.LexicalHandler;
042: import org.xml.sax.helpers.XMLFilterImpl;
043:
044: import javax.xml.bind.JAXBException;
045: import javax.xml.transform.Source;
046: import javax.xml.transform.sax.SAXSource;
047:
048: /**
049: * Wraps a bridge and JAXB object into a pseudo-{@link Source}.
050: * @author Kohsuke Kawaguchi
051: */
052: final class JAXBBridgeSource extends SAXSource {
053:
054: public JAXBBridgeSource(Bridge bridge, Object contentObject) {
055: this .bridge = bridge;
056: this .contentObject = contentObject;
057:
058: super .setXMLReader(pseudoParser);
059: // pass a dummy InputSource. We don't care
060: super .setInputSource(new InputSource());
061: }
062:
063: private final Bridge bridge;
064: private final Object contentObject;
065:
066: // this object will pretend as an XMLReader.
067: // no matter what parameter is specified to the parse method,
068: // it just parse the contentObject.
069: private final XMLReader pseudoParser = new XMLFilterImpl() {
070: public boolean getFeature(String name)
071: throws SAXNotRecognizedException {
072: if (name.equals("http://xml.org/sax/features/namespaces"))
073: return true;
074: if (name
075: .equals("http://xml.org/sax/features/namespace-prefixes"))
076: return false;
077: throw new SAXNotRecognizedException(name);
078: }
079:
080: public void setFeature(String name, boolean value)
081: throws SAXNotRecognizedException {
082: if (name.equals("http://xml.org/sax/features/namespaces")
083: && value)
084: return;
085: if (name
086: .equals("http://xml.org/sax/features/namespace-prefixes")
087: && !value)
088: return;
089: throw new SAXNotRecognizedException(name);
090: }
091:
092: public Object getProperty(String name)
093: throws SAXNotRecognizedException {
094: if ("http://xml.org/sax/properties/lexical-handler"
095: .equals(name)) {
096: return lexicalHandler;
097: }
098: throw new SAXNotRecognizedException(name);
099: }
100:
101: public void setProperty(String name, Object value)
102: throws SAXNotRecognizedException {
103: if ("http://xml.org/sax/properties/lexical-handler"
104: .equals(name)) {
105: this .lexicalHandler = (LexicalHandler) value;
106: return;
107: }
108: throw new SAXNotRecognizedException(name);
109: }
110:
111: private LexicalHandler lexicalHandler;
112:
113: public void parse(InputSource input) throws SAXException {
114: parse();
115: }
116:
117: public void parse(String systemId) throws SAXException {
118: parse();
119: }
120:
121: public void parse() throws SAXException {
122: // parses a content object by using the given bridge
123: // SAX events will be sent to the repeater, and the repeater
124: // will further forward it to an appropriate component.
125: try {
126: startDocument();
127: // this method only writes a fragment, so need start/end document
128: bridge.marshal(contentObject, this );
129: endDocument();
130: } catch (JAXBException e) {
131: // wrap it to a SAXException
132: SAXParseException se = new SAXParseException(e
133: .getMessage(), null, null, -1, -1, e);
134:
135: // if the consumer sets an error handler, it is our responsibility
136: // to notify it.
137: fatalError(se);
138:
139: // this is a fatal error. Even if the error handler
140: // returns, we will abort anyway.
141: throw se;
142: }
143: }
144: };
145: }
|