src/share/jaxws_classes/com/sun/xml/internal/ws/message/jaxb/JAXBBridgeSource.java

Thu, 31 Aug 2017 15:18:52 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:18:52 +0800
changeset 637
9c07ef4934dd
parent 368
0989ad8c0860
parent 0
373ffda63c9a
permissions
-rw-r--r--

merge

     1 /*
     2  * Copyright (c) 1997, 2012, 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  */
    26 package com.sun.xml.internal.ws.message.jaxb;
    28 import com.sun.xml.internal.ws.spi.db.XMLBridge;
    30 import org.xml.sax.*;
    31 import org.xml.sax.ext.LexicalHandler;
    32 import org.xml.sax.helpers.XMLFilterImpl;
    34 import javax.xml.bind.JAXBException;
    35 import javax.xml.transform.Source;
    36 import javax.xml.transform.sax.SAXSource;
    38 /**
    39  * Wraps a bridge and JAXB object into a pseudo-{@link Source}.
    40  * @author Kohsuke Kawaguchi
    41  */
    42 final class JAXBBridgeSource extends SAXSource {
    44     public JAXBBridgeSource( XMLBridge bridge, Object contentObject ) {
    45         this.bridge = bridge;
    46         this.contentObject = contentObject;
    48         super.setXMLReader(pseudoParser);
    49         // pass a dummy InputSource. We don't care
    50         super.setInputSource(new InputSource());
    51     }
    53     private final XMLBridge bridge;
    54     private final Object contentObject;
    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         }
    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         }
    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         }
    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         }
    91         private LexicalHandler lexicalHandler;
    93         public void parse(InputSource input) throws SAXException {
    94             parse();
    95         }
    97         public void parse(String systemId) throws  SAXException {
    98             parse();
    99         }
   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 );
   116                 // if the consumer sets an error handler, it is our responsibility
   117                 // to notify it.
   118                 fatalError(se);
   120                 // this is a fatal error. Even if the error handler
   121                 // returns, we will abort anyway.
   122                 throw se;
   123             }
   124         }
   125     };
   126 }

mercurial