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

changeset 0
373ffda63c9a
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/bind/v2/runtime/unmarshaller/ValidatingUnmarshaller.java	Wed Apr 27 01:27:09 2016 +0800
     1.3 @@ -0,0 +1,134 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.xml.internal.bind.v2.runtime.unmarshaller;
    1.30 +
    1.31 +import javax.xml.namespace.NamespaceContext;
    1.32 +import javax.xml.validation.Schema;
    1.33 +import javax.xml.validation.ValidatorHandler;
    1.34 +
    1.35 +import com.sun.xml.internal.bind.v2.util.FatalAdapter;
    1.36 +
    1.37 +import org.xml.sax.SAXException;
    1.38 +
    1.39 +/**
    1.40 + * {@link XmlVisitor} decorator that validates the events by using JAXP validation API.
    1.41 + *
    1.42 + * @author Kohsuke Kawaguchi
    1.43 + */
    1.44 +final class ValidatingUnmarshaller implements XmlVisitor, XmlVisitor.TextPredictor {
    1.45 +
    1.46 +    private final XmlVisitor next;
    1.47 +    private final ValidatorHandler validator;
    1.48 +    private NamespaceContext nsContext = null;
    1.49 +
    1.50 +    /**
    1.51 +     * {@link TextPredictor} of the next {@link XmlVisitor}.
    1.52 +     */
    1.53 +    private final TextPredictor predictor;
    1.54 +
    1.55 +    private char[] buf = new char[256];
    1.56 +
    1.57 +    /**
    1.58 +     * Creates a new instance of ValidatingUnmarshaller.
    1.59 +     */
    1.60 +    public ValidatingUnmarshaller( Schema schema, XmlVisitor next ) {
    1.61 +        this.validator = schema.newValidatorHandler();
    1.62 +        this.next = next;
    1.63 +        this.predictor = next.getPredictor();
    1.64 +        // if the user bothers to use a validator, make validation errors fatal
    1.65 +        // so that it will abort unmarshalling.
    1.66 +        validator.setErrorHandler(new FatalAdapter(getContext()));
    1.67 +    }
    1.68 +
    1.69 +    public void startDocument(LocatorEx locator, NamespaceContext nsContext) throws SAXException {
    1.70 +        this.nsContext = nsContext;
    1.71 +        validator.setDocumentLocator(locator);
    1.72 +        validator.startDocument();
    1.73 +        next.startDocument(locator,nsContext);
    1.74 +    }
    1.75 +
    1.76 +    public void endDocument() throws SAXException {
    1.77 +        this.nsContext = null;
    1.78 +        validator.endDocument();
    1.79 +        next.endDocument();
    1.80 +    }
    1.81 +
    1.82 +    public void startElement(TagName tagName) throws SAXException {
    1.83 +        if(nsContext != null) {
    1.84 +            String tagNamePrefix = tagName.getPrefix().intern();
    1.85 +            if(tagNamePrefix != "") {
    1.86 +                validator.startPrefixMapping(tagNamePrefix, nsContext.getNamespaceURI(tagNamePrefix));
    1.87 +            }
    1.88 +        }
    1.89 +        validator.startElement(tagName.uri,tagName.local,tagName.getQname(),tagName.atts);
    1.90 +        next.startElement(tagName);
    1.91 +    }
    1.92 +
    1.93 +    public void endElement(TagName tagName ) throws SAXException {
    1.94 +        validator.endElement(tagName.uri,tagName.local,tagName.getQname());
    1.95 +        next.endElement(tagName);
    1.96 +    }
    1.97 +
    1.98 +    public void startPrefixMapping(String prefix, String nsUri) throws SAXException {
    1.99 +        validator.startPrefixMapping(prefix,nsUri);
   1.100 +        next.startPrefixMapping(prefix,nsUri);
   1.101 +    }
   1.102 +
   1.103 +    public void endPrefixMapping(String prefix) throws SAXException {
   1.104 +        validator.endPrefixMapping(prefix);
   1.105 +        next.endPrefixMapping(prefix);
   1.106 +    }
   1.107 +
   1.108 +    public void text( CharSequence pcdata ) throws SAXException {
   1.109 +        int len = pcdata.length();
   1.110 +        if(buf.length<len) {
   1.111 +            buf = new char[len];
   1.112 +        }
   1.113 +        for( int i=0;i<len; i++ )
   1.114 +            buf[i] = pcdata.charAt(i);  // isn't this kinda slow?
   1.115 +
   1.116 +        validator.characters(buf,0,len);
   1.117 +        if(predictor.expectText())
   1.118 +            next.text(pcdata);
   1.119 +    }
   1.120 +
   1.121 +    public UnmarshallingContext getContext() {
   1.122 +        return next.getContext();
   1.123 +    }
   1.124 +
   1.125 +    public TextPredictor getPredictor() {
   1.126 +        return this;
   1.127 +    }
   1.128 +
   1.129 +    // should be always invoked through TextPredictor
   1.130 +    @Deprecated
   1.131 +    public boolean expectText() {
   1.132 +        // validator needs to make sure that there's no text
   1.133 +        // even when it's not expected. So always have them
   1.134 +        // send text, ignoring optimization hints from the unmarshaller
   1.135 +        return true;
   1.136 +    }
   1.137 +}

mercurial