1/* 2 * Copyright (c) 1997, 2013, 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 */ 25 26package com.sun.tools.internal.ws.wsdl.parser; 27 28import com.sun.tools.internal.ws.resources.WsdlMessages; 29import com.sun.tools.internal.ws.wsdl.document.jaxws.JAXWSBindingsConstants; 30import org.xml.sax.*; 31import org.xml.sax.helpers.LocatorImpl; 32import org.xml.sax.helpers.XMLFilterImpl; 33 34import java.util.Arrays; 35import java.util.HashSet; 36import java.util.Set; 37 38/** 39 * Checks the jaxb:version attribute on a XML Schema document. 40 * 41 * jaxws:version is optional, if absent its value is assumed to be "2.0" and if present its value must be 42 * "2.0" or more. 43 * 44 * @author 45 * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com) 46 * Vivek Pandey 47 */ 48public class VersionChecker extends XMLFilterImpl { 49 50 /** 51 * We store the value of the version attribute in this variable 52 * when we hit the root element. 53 */ 54 private String version = null ; 55 56 /** Will be set to true once we hit the root element. */ 57 private boolean seenRoot = false; 58 59 /** Will be set to true once we hit a binding declaration. */ 60 private boolean seenBindings = false; 61 62 private Locator locator; 63 64 /** 65 * Stores the location of the start tag of the root tag. 66 */ 67 private Locator rootTagStart; 68 69 public VersionChecker( XMLReader parent ) { 70 setParent(parent); 71 } 72 73 public VersionChecker( ContentHandler handler, ErrorHandler eh, EntityResolver er ) { 74 setContentHandler(handler); 75 if(eh!=null) setErrorHandler(eh); 76 if(er!=null) setEntityResolver(er); 77 } 78 79 public void startElement(String namespaceURI, String localName, String qName, Attributes atts) 80 throws SAXException { 81 82 super.startElement(namespaceURI, localName, qName, atts); 83 84 if(!seenRoot) { 85 // if this is the root element 86 seenRoot = true; 87 rootTagStart = new LocatorImpl(locator); 88 89 version = atts.getValue(JAXWSBindingsConstants.NS_JAXWS_BINDINGS,"version"); 90 if( namespaceURI.equals(JAXWSBindingsConstants.NS_JAXWS_BINDINGS) ) { 91 String version2 = atts.getValue("","version"); 92 if( version!=null && version2!=null ) { 93 // we have both @version and @jaxb:version. error. 94 SAXParseException e = new SAXParseException( 95 WsdlMessages.INTERNALIZER_TWO_VERSION_ATTRIBUTES(), locator); 96 getErrorHandler().error(e); 97 } 98 //According to JAXWS 2.0 spec, if version attribute is missing its assumed to be "2.0" 99 if( version==null) 100 version = (version2!=null)?version2:"2.0"; 101 } 102 103 } 104 105 if( JAXWSBindingsConstants.NS_JAXWS_BINDINGS.equals(namespaceURI)){ 106 seenBindings = true; 107 if(version == null) 108 version = "2.0"; 109 } 110 111 } 112 113 public void endDocument() throws SAXException { 114 super.endDocument(); 115 116 if( seenBindings && version==null ) { 117 // if we see a binding declaration but not version attribute 118 SAXParseException e = new SAXParseException(WsdlMessages.INTERNALIZER_VERSION_NOT_PRESENT(), rootTagStart); 119 getErrorHandler().error(e); 120 } 121 122 // if present, the value must be >= 2.0 123 if( version!=null && !VERSIONS.contains(version) ) { 124 SAXParseException e = new SAXParseException(WsdlMessages.INTERNALIZER_INCORRECT_VERSION(), rootTagStart); 125 getErrorHandler().error(e); 126 } 127 } 128 129 public void setDocumentLocator(Locator locator) { 130 super.setDocumentLocator(locator); 131 this.locator = locator; 132 } 133 134 private static final Set<String> VERSIONS = new HashSet<String>(Arrays.asList("2.0","2.1")); 135 136} 137