001: /*
002: * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.tools.internal.xjc.reader.internalizer;
027:
028: import com.sun.tools.internal.xjc.reader.Const;
029:
030: import org.xml.sax.Attributes;
031: import org.xml.sax.ContentHandler;
032: import org.xml.sax.EntityResolver;
033: import org.xml.sax.ErrorHandler;
034: import org.xml.sax.Locator;
035: import org.xml.sax.SAXException;
036: import org.xml.sax.SAXParseException;
037: import org.xml.sax.XMLReader;
038: import org.xml.sax.helpers.LocatorImpl;
039: import org.xml.sax.helpers.XMLFilterImpl;
040:
041: /**
042: * Checks the jaxb:version attribute on a XML Schema document.
043: *
044: * jaxb:version is optional if no binding customization is used,
045: * but if present, its value must be "1.0".
046: *
047: * @author
048: * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
049: */
050: public class VersionChecker extends XMLFilterImpl {
051:
052: /**
053: * We store the value of the version attribute in this variable
054: * when we hit the root element.
055: */
056: private String version = null;
057:
058: /** Will be set to true once we hit the root element. */
059: private boolean seenRoot = false;
060:
061: /** Will be set to true once we hit a binding declaration. */
062: private boolean seenBindings = false;
063:
064: private Locator locator;
065:
066: /**
067: * Stores the location of the start tag of the root tag.
068: */
069: private Locator rootTagStart;
070:
071: public VersionChecker(XMLReader parent) {
072: setParent(parent);
073: }
074:
075: public VersionChecker(ContentHandler handler, ErrorHandler eh,
076: EntityResolver er) {
077: setContentHandler(handler);
078: if (eh != null)
079: setErrorHandler(eh);
080: if (er != null)
081: setEntityResolver(er);
082: }
083:
084: public void startElement(String namespaceURI, String localName,
085: String qName, Attributes atts) throws SAXException {
086:
087: super .startElement(namespaceURI, localName, qName, atts);
088:
089: if (!seenRoot) {
090: // if this is the root element
091: seenRoot = true;
092: rootTagStart = new LocatorImpl(locator);
093:
094: version = atts.getValue(Const.JAXB_NSURI, "version");
095: if (namespaceURI.equals(Const.JAXB_NSURI)) {
096: String version2 = atts.getValue("", "version");
097: if (version != null && version2 != null) {
098: // we have both @version and @jaxb:version. error.
099: SAXParseException e = new SAXParseException(
100: Messages
101: .format(Messages.TWO_VERSION_ATTRIBUTES),
102: locator);
103: getErrorHandler().error(e);
104: }
105: if (version == null)
106: version = version2;
107: }
108:
109: }
110:
111: if (Const.JAXB_NSURI.equals(namespaceURI))
112: seenBindings = true;
113: }
114:
115: public void endDocument() throws SAXException {
116: super .endDocument();
117:
118: if (seenBindings && version == null) {
119: // if we see a binding declaration but not version attribute
120: SAXParseException e = new SAXParseException(Messages
121: .format(Messages.ERR_VERSION_NOT_FOUND),
122: rootTagStart);
123: getErrorHandler().error(e);
124: }
125:
126: // if present, the value must be either 1.0 or 2.0
127: if (version != null && !version.equals("1.0")
128: && !version.equals("2.0")) {
129: SAXParseException e = new SAXParseException(Messages
130: .format(Messages.ERR_INCORRECT_VERSION),
131: rootTagStart);
132: getErrorHandler().error(e);
133: }
134: }
135:
136: public void setDocumentLocator(Locator locator) {
137: super.setDocumentLocator(locator);
138: this.locator = locator;
139: }
140:
141: }
|