src/share/jaxws_classes/com/sun/tools/internal/ws/wsdl/parser/VersionChecker.java

changeset 286
f50545b5e2f1
child 368
0989ad8c0860
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/com/sun/tools/internal/ws/wsdl/parser/VersionChecker.java	Tue Mar 06 16:09:35 2012 -0800
     1.3 @@ -0,0 +1,136 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2010, 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.tools.internal.ws.wsdl.parser;
    1.30 +
    1.31 +import com.sun.tools.internal.ws.resources.WsdlMessages;
    1.32 +import com.sun.tools.internal.ws.wsdl.document.jaxws.JAXWSBindingsConstants;
    1.33 +import org.xml.sax.*;
    1.34 +import org.xml.sax.helpers.LocatorImpl;
    1.35 +import org.xml.sax.helpers.XMLFilterImpl;
    1.36 +
    1.37 +import java.util.Arrays;
    1.38 +import java.util.HashSet;
    1.39 +import java.util.Set;
    1.40 +
    1.41 +/**
    1.42 + * Checks the jaxb:version attribute on a XML Schema document.
    1.43 + *
    1.44 + * jaxws:version is optional, if absent its value is assumed to be "2.0" and if present its value must be
    1.45 + * "2.0" or more.
    1.46 + *
    1.47 + * @author
    1.48 + *     Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
    1.49 + *     Vivek Pandey
    1.50 + */
    1.51 +public class VersionChecker extends XMLFilterImpl {
    1.52 +
    1.53 +    /**
    1.54 +     * We store the value of the version attribute in this variable
    1.55 +     * when we hit the root element.
    1.56 +     */
    1.57 +    private String version = null ;
    1.58 +
    1.59 +    /** Will be set to true once we hit the root element. */
    1.60 +    private boolean seenRoot = false;
    1.61 +
    1.62 +    /** Will be set to true once we hit a binding declaration. */
    1.63 +    private boolean seenBindings = false;
    1.64 +
    1.65 +    private Locator locator;
    1.66 +
    1.67 +    /**
    1.68 +     * Stores the location of the start tag of the root tag.
    1.69 +     */
    1.70 +    private Locator rootTagStart;
    1.71 +
    1.72 +    public VersionChecker( XMLReader parent ) {
    1.73 +        setParent(parent);
    1.74 +    }
    1.75 +
    1.76 +    public VersionChecker( ContentHandler handler, ErrorHandler eh, EntityResolver er ) {
    1.77 +        setContentHandler(handler);
    1.78 +        if(eh!=null)    setErrorHandler(eh);
    1.79 +        if(er!=null)    setEntityResolver(er);
    1.80 +    }
    1.81 +
    1.82 +    public void startElement(String namespaceURI, String localName, String qName, Attributes atts)
    1.83 +        throws SAXException {
    1.84 +
    1.85 +        super.startElement(namespaceURI, localName, qName, atts);
    1.86 +
    1.87 +        if(!seenRoot) {
    1.88 +            // if this is the root element
    1.89 +            seenRoot = true;
    1.90 +            rootTagStart = new LocatorImpl(locator);
    1.91 +
    1.92 +            version = atts.getValue(JAXWSBindingsConstants.NS_JAXWS_BINDINGS,"version");
    1.93 +            if( namespaceURI.equals(JAXWSBindingsConstants.NS_JAXWS_BINDINGS) ) {
    1.94 +                String version2 = atts.getValue("","version");
    1.95 +                if( version!=null && version2!=null ) {
    1.96 +                    // we have both @version and @jaxb:version. error.
    1.97 +                    SAXParseException e = new SAXParseException(
    1.98 +                        WsdlMessages.INTERNALIZER_TWO_VERSION_ATTRIBUTES(), locator);
    1.99 +                    getErrorHandler().error(e);
   1.100 +                }
   1.101 +                //According to JAXWS 2.0 spec, if version attribute is missing its assumed to be "2.0"
   1.102 +                if( version==null)
   1.103 +                    version = (version2!=null)?version2:"2.0";
   1.104 +            }
   1.105 +
   1.106 +        }
   1.107 +
   1.108 +        if( JAXWSBindingsConstants.NS_JAXWS_BINDINGS.equals(namespaceURI)){
   1.109 +            seenBindings = true;
   1.110 +            if(version == null)
   1.111 +                version = "2.0";
   1.112 +        }
   1.113 +
   1.114 +    }
   1.115 +
   1.116 +    public void endDocument() throws SAXException {
   1.117 +        super.endDocument();
   1.118 +
   1.119 +        if( seenBindings && version==null ) {
   1.120 +            // if we see a binding declaration but not version attribute
   1.121 +            SAXParseException e = new SAXParseException(WsdlMessages.INTERNALIZER_VERSION_NOT_PRESENT(), rootTagStart);
   1.122 +            getErrorHandler().error(e);
   1.123 +        }
   1.124 +
   1.125 +        // if present, the value must be >= 2.0
   1.126 +        if( version!=null && !VERSIONS.contains(version) ) {
   1.127 +            SAXParseException e = new SAXParseException(WsdlMessages.INTERNALIZER_INCORRECT_VERSION(), rootTagStart);
   1.128 +            getErrorHandler().error(e);
   1.129 +        }
   1.130 +    }
   1.131 +
   1.132 +    public void setDocumentLocator(Locator locator) {
   1.133 +        super.setDocumentLocator(locator);
   1.134 +        this.locator = locator;
   1.135 +    }
   1.136 +
   1.137 +    private static final Set<String> VERSIONS = new HashSet<String>(Arrays.asList("2.0","2.1"));
   1.138 +
   1.139 +}

mercurial