src/share/jaxws_classes/com/sun/xml/internal/bind/v2/runtime/unmarshaller/ValidatingUnmarshaller.java

Thu, 12 Oct 2017 19:44:07 +0800

author
aoqi
date
Thu, 12 Oct 2017 19:44:07 +0800
changeset 760
e530533619ec
parent 0
373ffda63c9a
permissions
-rw-r--r--

merge

     1 /*
     2  * Copyright (c) 1997, 2011, 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.bind.v2.runtime.unmarshaller;
    28 import javax.xml.namespace.NamespaceContext;
    29 import javax.xml.validation.Schema;
    30 import javax.xml.validation.ValidatorHandler;
    32 import com.sun.xml.internal.bind.v2.util.FatalAdapter;
    34 import org.xml.sax.SAXException;
    36 /**
    37  * {@link XmlVisitor} decorator that validates the events by using JAXP validation API.
    38  *
    39  * @author Kohsuke Kawaguchi
    40  */
    41 final class ValidatingUnmarshaller implements XmlVisitor, XmlVisitor.TextPredictor {
    43     private final XmlVisitor next;
    44     private final ValidatorHandler validator;
    45     private NamespaceContext nsContext = null;
    47     /**
    48      * {@link TextPredictor} of the next {@link XmlVisitor}.
    49      */
    50     private final TextPredictor predictor;
    52     private char[] buf = new char[256];
    54     /**
    55      * Creates a new instance of ValidatingUnmarshaller.
    56      */
    57     public ValidatingUnmarshaller( Schema schema, XmlVisitor next ) {
    58         this.validator = schema.newValidatorHandler();
    59         this.next = next;
    60         this.predictor = next.getPredictor();
    61         // if the user bothers to use a validator, make validation errors fatal
    62         // so that it will abort unmarshalling.
    63         validator.setErrorHandler(new FatalAdapter(getContext()));
    64     }
    66     public void startDocument(LocatorEx locator, NamespaceContext nsContext) throws SAXException {
    67         this.nsContext = nsContext;
    68         validator.setDocumentLocator(locator);
    69         validator.startDocument();
    70         next.startDocument(locator,nsContext);
    71     }
    73     public void endDocument() throws SAXException {
    74         this.nsContext = null;
    75         validator.endDocument();
    76         next.endDocument();
    77     }
    79     public void startElement(TagName tagName) throws SAXException {
    80         if(nsContext != null) {
    81             String tagNamePrefix = tagName.getPrefix().intern();
    82             if(tagNamePrefix != "") {
    83                 validator.startPrefixMapping(tagNamePrefix, nsContext.getNamespaceURI(tagNamePrefix));
    84             }
    85         }
    86         validator.startElement(tagName.uri,tagName.local,tagName.getQname(),tagName.atts);
    87         next.startElement(tagName);
    88     }
    90     public void endElement(TagName tagName ) throws SAXException {
    91         validator.endElement(tagName.uri,tagName.local,tagName.getQname());
    92         next.endElement(tagName);
    93     }
    95     public void startPrefixMapping(String prefix, String nsUri) throws SAXException {
    96         validator.startPrefixMapping(prefix,nsUri);
    97         next.startPrefixMapping(prefix,nsUri);
    98     }
   100     public void endPrefixMapping(String prefix) throws SAXException {
   101         validator.endPrefixMapping(prefix);
   102         next.endPrefixMapping(prefix);
   103     }
   105     public void text( CharSequence pcdata ) throws SAXException {
   106         int len = pcdata.length();
   107         if(buf.length<len) {
   108             buf = new char[len];
   109         }
   110         for( int i=0;i<len; i++ )
   111             buf[i] = pcdata.charAt(i);  // isn't this kinda slow?
   113         validator.characters(buf,0,len);
   114         if(predictor.expectText())
   115             next.text(pcdata);
   116     }
   118     public UnmarshallingContext getContext() {
   119         return next.getContext();
   120     }
   122     public TextPredictor getPredictor() {
   123         return this;
   124     }
   126     // should be always invoked through TextPredictor
   127     @Deprecated
   128     public boolean expectText() {
   129         // validator needs to make sure that there's no text
   130         // even when it's not expected. So always have them
   131         // send text, ignoring optimization hints from the unmarshaller
   132         return true;
   133     }
   134 }

mercurial