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: package com.sun.tools.xjc.reader.internalizer;
037:
038: import java.util.Set;
039: import java.util.HashSet;
040: import java.util.Arrays;
041:
042: import com.sun.tools.xjc.reader.Const;
043:
044: import org.xml.sax.Attributes;
045: import org.xml.sax.ContentHandler;
046: import org.xml.sax.EntityResolver;
047: import org.xml.sax.ErrorHandler;
048: import org.xml.sax.Locator;
049: import org.xml.sax.SAXException;
050: import org.xml.sax.SAXParseException;
051: import org.xml.sax.XMLReader;
052: import org.xml.sax.helpers.LocatorImpl;
053: import org.xml.sax.helpers.XMLFilterImpl;
054:
055: /**
056: * Checks the jaxb:version attribute on a XML Schema document.
057: *
058: * jaxb:version is optional if no binding customization is used,
059: * but if present, its value must be "1.0".
060: *
061: * @author
062: * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
063: */
064: public class VersionChecker extends XMLFilterImpl {
065:
066: /**
067: * We store the value of the version attribute in this variable
068: * when we hit the root element.
069: */
070: private String version = null;
071:
072: /** Will be set to true once we hit the root element. */
073: private boolean seenRoot = false;
074:
075: /** Will be set to true once we hit a binding declaration. */
076: private boolean seenBindings = false;
077:
078: private Locator locator;
079:
080: /**
081: * Stores the location of the start tag of the root tag.
082: */
083: private Locator rootTagStart;
084:
085: public VersionChecker(XMLReader parent) {
086: setParent(parent);
087: }
088:
089: public VersionChecker(ContentHandler handler, ErrorHandler eh,
090: EntityResolver er) {
091: setContentHandler(handler);
092: if (eh != null)
093: setErrorHandler(eh);
094: if (er != null)
095: setEntityResolver(er);
096: }
097:
098: public void startElement(String namespaceURI, String localName,
099: String qName, Attributes atts) throws SAXException {
100:
101: super .startElement(namespaceURI, localName, qName, atts);
102:
103: if (!seenRoot) {
104: // if this is the root element
105: seenRoot = true;
106: rootTagStart = new LocatorImpl(locator);
107:
108: version = atts.getValue(Const.JAXB_NSURI, "version");
109: if (namespaceURI.equals(Const.JAXB_NSURI)) {
110: String version2 = atts.getValue("", "version");
111: if (version != null && version2 != null) {
112: // we have both @version and @jaxb:version. error.
113: SAXParseException e = new SAXParseException(
114: Messages
115: .format(Messages.TWO_VERSION_ATTRIBUTES),
116: locator);
117: getErrorHandler().error(e);
118: }
119: if (version == null)
120: version = version2;
121: }
122:
123: }
124:
125: if (Const.JAXB_NSURI.equals(namespaceURI))
126: seenBindings = true;
127: }
128:
129: public void endDocument() throws SAXException {
130: super .endDocument();
131:
132: if (seenBindings && version == null) {
133: // if we see a binding declaration but not version attribute
134: SAXParseException e = new SAXParseException(Messages
135: .format(Messages.ERR_VERSION_NOT_FOUND),
136: rootTagStart);
137: getErrorHandler().error(e);
138: }
139:
140: // if present, the value must be either 1.0 or 2.0
141: if (version != null && !VERSIONS.contains(version)) {
142: SAXParseException e = new SAXParseException(Messages
143: .format(Messages.ERR_INCORRECT_VERSION),
144: rootTagStart);
145: getErrorHandler().error(e);
146: }
147: }
148:
149: public void setDocumentLocator(Locator locator) {
150: super .setDocumentLocator(locator);
151: this .locator = locator;
152: }
153:
154: private static final Set<String> VERSIONS = new HashSet<String>(
155: Arrays.asList("1.0", "2.0", "2.1"));
156:
157: }
|