001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.tools.ws.wsdl.parser;
038:
039: import com.sun.tools.ws.resources.WsdlMessages;
040: import com.sun.tools.ws.wsdl.document.jaxws.JAXWSBindingsConstants;
041: import org.xml.sax.*;
042: import org.xml.sax.helpers.LocatorImpl;
043: import org.xml.sax.helpers.XMLFilterImpl;
044:
045: import java.util.Arrays;
046: import java.util.HashSet;
047: import java.util.Set;
048:
049: /**
050: * Checks the jaxb:version attribute on a XML Schema document.
051: *
052: * jaxws:version is optional, if absent its value is assumed to be "2.0" and if present its value must be
053: * "2.0" or more.
054: *
055: * @author
056: * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
057: * Vivek Pandey
058: */
059: public class VersionChecker extends XMLFilterImpl {
060:
061: /**
062: * We store the value of the version attribute in this variable
063: * when we hit the root element.
064: */
065: private String version = null;
066:
067: /** Will be set to true once we hit the root element. */
068: private boolean seenRoot = false;
069:
070: /** Will be set to true once we hit a binding declaration. */
071: private boolean seenBindings = false;
072:
073: private Locator locator;
074:
075: /**
076: * Stores the location of the start tag of the root tag.
077: */
078: private Locator rootTagStart;
079:
080: public VersionChecker(XMLReader parent) {
081: setParent(parent);
082: }
083:
084: public VersionChecker(ContentHandler handler, ErrorHandler eh,
085: EntityResolver er) {
086: setContentHandler(handler);
087: if (eh != null)
088: setErrorHandler(eh);
089: if (er != null)
090: setEntityResolver(er);
091: }
092:
093: public void startElement(String namespaceURI, String localName,
094: String qName, Attributes atts) throws SAXException {
095:
096: super .startElement(namespaceURI, localName, qName, atts);
097:
098: if (!seenRoot) {
099: // if this is the root element
100: seenRoot = true;
101: rootTagStart = new LocatorImpl(locator);
102:
103: version = atts
104: .getValue(JAXWSBindingsConstants.NS_JAXWS_BINDINGS,
105: "version");
106: if (namespaceURI
107: .equals(JAXWSBindingsConstants.NS_JAXWS_BINDINGS)) {
108: String version2 = atts.getValue("", "version");
109: if (version != null && version2 != null) {
110: // we have both @version and @jaxb:version. error.
111: SAXParseException e = new SAXParseException(
112: WsdlMessages
113: .INTERNALIZER_TWO_VERSION_ATTRIBUTES(),
114: locator);
115: getErrorHandler().error(e);
116: }
117: //According to JAXWS 2.0 spec, if version attribute is missing its assumed to be "2.0"
118: if (version == null)
119: version = (version2 != null) ? version2 : "2.0";
120: }
121:
122: }
123:
124: if (JAXWSBindingsConstants.NS_JAXWS_BINDINGS
125: .equals(namespaceURI)) {
126: seenBindings = true;
127: if (version == null)
128: version = "2.0";
129: }
130:
131: }
132:
133: public void endDocument() throws SAXException {
134: super .endDocument();
135:
136: if (seenBindings && version == null) {
137: // if we see a binding declaration but not version attribute
138: SAXParseException e = new SAXParseException(WsdlMessages
139: .INTERNALIZER_VERSION_NOT_PRESENT(), rootTagStart);
140: getErrorHandler().error(e);
141: }
142:
143: // if present, the value must be >= 2.0
144: if (version != null && !VERSIONS.contains(version)) {
145: SAXParseException e = new SAXParseException(WsdlMessages
146: .INTERNALIZER_INCORRECT_VERSION(), rootTagStart);
147: getErrorHandler().error(e);
148: }
149: }
150:
151: public void setDocumentLocator(Locator locator) {
152: super .setDocumentLocator(locator);
153: this .locator = locator;
154: }
155:
156: private static final Set<String> VERSIONS = new HashSet<String>(
157: Arrays.asList("2.0", "2.1"));
158:
159: }
|