aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.tools.internal.ws.wsdl.parser; aoqi@0: aoqi@0: import com.sun.tools.internal.ws.resources.WsdlMessages; aoqi@0: import com.sun.tools.internal.ws.wsdl.document.jaxws.JAXWSBindingsConstants; aoqi@0: import org.xml.sax.*; aoqi@0: import org.xml.sax.helpers.LocatorImpl; aoqi@0: import org.xml.sax.helpers.XMLFilterImpl; aoqi@0: aoqi@0: import java.util.Arrays; aoqi@0: import java.util.HashSet; aoqi@0: import java.util.Set; aoqi@0: aoqi@0: /** aoqi@0: * Checks the jaxb:version attribute on a XML Schema document. aoqi@0: * aoqi@0: * jaxws:version is optional, if absent its value is assumed to be "2.0" and if present its value must be aoqi@0: * "2.0" or more. aoqi@0: * aoqi@0: * @author aoqi@0: * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com) aoqi@0: * Vivek Pandey aoqi@0: */ aoqi@0: public class VersionChecker extends XMLFilterImpl { aoqi@0: aoqi@0: /** aoqi@0: * We store the value of the version attribute in this variable aoqi@0: * when we hit the root element. aoqi@0: */ aoqi@0: private String version = null ; aoqi@0: aoqi@0: /** Will be set to true once we hit the root element. */ aoqi@0: private boolean seenRoot = false; aoqi@0: aoqi@0: /** Will be set to true once we hit a binding declaration. */ aoqi@0: private boolean seenBindings = false; aoqi@0: aoqi@0: private Locator locator; aoqi@0: aoqi@0: /** aoqi@0: * Stores the location of the start tag of the root tag. aoqi@0: */ aoqi@0: private Locator rootTagStart; aoqi@0: aoqi@0: public VersionChecker( XMLReader parent ) { aoqi@0: setParent(parent); aoqi@0: } aoqi@0: aoqi@0: public VersionChecker( ContentHandler handler, ErrorHandler eh, EntityResolver er ) { aoqi@0: setContentHandler(handler); aoqi@0: if(eh!=null) setErrorHandler(eh); aoqi@0: if(er!=null) setEntityResolver(er); aoqi@0: } aoqi@0: aoqi@0: public void startElement(String namespaceURI, String localName, String qName, Attributes atts) aoqi@0: throws SAXException { aoqi@0: aoqi@0: super.startElement(namespaceURI, localName, qName, atts); aoqi@0: aoqi@0: if(!seenRoot) { aoqi@0: // if this is the root element aoqi@0: seenRoot = true; aoqi@0: rootTagStart = new LocatorImpl(locator); aoqi@0: aoqi@0: version = atts.getValue(JAXWSBindingsConstants.NS_JAXWS_BINDINGS,"version"); aoqi@0: if( namespaceURI.equals(JAXWSBindingsConstants.NS_JAXWS_BINDINGS) ) { aoqi@0: String version2 = atts.getValue("","version"); aoqi@0: if( version!=null && version2!=null ) { aoqi@0: // we have both @version and @jaxb:version. error. aoqi@0: SAXParseException e = new SAXParseException( aoqi@0: WsdlMessages.INTERNALIZER_TWO_VERSION_ATTRIBUTES(), locator); aoqi@0: getErrorHandler().error(e); aoqi@0: } aoqi@0: //According to JAXWS 2.0 spec, if version attribute is missing its assumed to be "2.0" aoqi@0: if( version==null) aoqi@0: version = (version2!=null)?version2:"2.0"; aoqi@0: } aoqi@0: aoqi@0: } aoqi@0: aoqi@0: if( JAXWSBindingsConstants.NS_JAXWS_BINDINGS.equals(namespaceURI)){ aoqi@0: seenBindings = true; aoqi@0: if(version == null) aoqi@0: version = "2.0"; aoqi@0: } aoqi@0: aoqi@0: } aoqi@0: aoqi@0: public void endDocument() throws SAXException { aoqi@0: super.endDocument(); aoqi@0: aoqi@0: if( seenBindings && version==null ) { aoqi@0: // if we see a binding declaration but not version attribute aoqi@0: SAXParseException e = new SAXParseException(WsdlMessages.INTERNALIZER_VERSION_NOT_PRESENT(), rootTagStart); aoqi@0: getErrorHandler().error(e); aoqi@0: } aoqi@0: aoqi@0: // if present, the value must be >= 2.0 aoqi@0: if( version!=null && !VERSIONS.contains(version) ) { aoqi@0: SAXParseException e = new SAXParseException(WsdlMessages.INTERNALIZER_INCORRECT_VERSION(), rootTagStart); aoqi@0: getErrorHandler().error(e); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public void setDocumentLocator(Locator locator) { aoqi@0: super.setDocumentLocator(locator); aoqi@0: this.locator = locator; aoqi@0: } aoqi@0: aoqi@0: private static final Set VERSIONS = new HashSet(Arrays.asList("2.0","2.1")); aoqi@0: aoqi@0: }