1/*
2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package com.sun.xml.internal.ws.message.jaxb;
27
28import com.sun.xml.internal.ws.spi.db.XMLBridge;
29
30import org.xml.sax.*;
31import org.xml.sax.ext.LexicalHandler;
32import org.xml.sax.helpers.XMLFilterImpl;
33
34import javax.xml.bind.JAXBException;
35import javax.xml.transform.Source;
36import javax.xml.transform.sax.SAXSource;
37
38/**
39 * Wraps a bridge and JAXB object into a pseudo-{@link Source}.
40 * @author Kohsuke Kawaguchi
41 */
42final class JAXBBridgeSource extends SAXSource {
43
44    public JAXBBridgeSource( XMLBridge bridge, Object contentObject ) {
45        this.bridge = bridge;
46        this.contentObject = contentObject;
47
48        super.setXMLReader(pseudoParser);
49        // pass a dummy InputSource. We don't care
50        super.setInputSource(new InputSource());
51    }
52
53    private final XMLBridge bridge;
54    private final Object contentObject;
55
56    // this object will pretend as an XMLReader.
57    // no matter what parameter is specified to the parse method,
58    // it just parse the contentObject.
59    private final XMLReader pseudoParser = new XMLFilterImpl() {
60        public boolean getFeature(String name) throws SAXNotRecognizedException {
61            if(name.equals("http://xml.org/sax/features/namespaces"))
62                return true;
63            if(name.equals("http://xml.org/sax/features/namespace-prefixes"))
64                return false;
65            throw new SAXNotRecognizedException(name);
66        }
67
68        public void setFeature(String name, boolean value) throws SAXNotRecognizedException {
69            if(name.equals("http://xml.org/sax/features/namespaces") && value)
70                return;
71            if(name.equals("http://xml.org/sax/features/namespace-prefixes") && !value)
72                return;
73            throw new SAXNotRecognizedException(name);
74        }
75
76        public Object getProperty(String name) throws SAXNotRecognizedException {
77            if( "http://xml.org/sax/properties/lexical-handler".equals(name) ) {
78                return lexicalHandler;
79            }
80            throw new SAXNotRecognizedException(name);
81        }
82
83        public void setProperty(String name, Object value) throws SAXNotRecognizedException {
84            if( "http://xml.org/sax/properties/lexical-handler".equals(name) ) {
85                this.lexicalHandler = (LexicalHandler)value;
86                return;
87            }
88            throw new SAXNotRecognizedException(name);
89        }
90
91        private LexicalHandler lexicalHandler;
92
93        public void parse(InputSource input) throws SAXException {
94            parse();
95        }
96
97        public void parse(String systemId) throws  SAXException {
98            parse();
99        }
100
101        public void parse() throws SAXException {
102            // parses a content object by using the given bridge
103            // SAX events will be sent to the repeater, and the repeater
104            // will further forward it to an appropriate component.
105            try {
106                startDocument();
107                // this method only writes a fragment, so need start/end document
108                bridge.marshal( contentObject, this, null );
109                endDocument();
110            } catch( JAXBException e ) {
111                // wrap it to a SAXException
112                SAXParseException se =
113                    new SAXParseException( e.getMessage(),
114                        null, null, -1, -1, e );
115
116                // if the consumer sets an error handler, it is our responsibility
117                // to notify it.
118                fatalError(se);
119
120                // this is a fatal error. Even if the error handler
121                // returns, we will abort anyway.
122                throw se;
123            }
124        }
125    };
126}
127