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.xmlschema.parser;
037:
038: import com.sun.tools.xjc.reader.Const;
039:
040: import org.xml.sax.Attributes;
041: import org.xml.sax.ErrorHandler;
042: import org.xml.sax.Locator;
043: import org.xml.sax.SAXException;
044: import org.xml.sax.SAXParseException;
045: import org.xml.sax.helpers.XMLFilterImpl;
046:
047: /**
048: * This filter detects the use of incorrect JAXB namespace URI.
049: *
050: * When the binding compiler looks at a schema file, it always look
051: * for the namespace URI of the elements (which is correct, BTW.)
052: *
053: * <p>
054: * However, one unfortunate downside of this philosophically correct
055: * behavior is that there is no provision or safety check when an user
056: * misspelled JAXB binding customization namespace.
057: *
058: * <p>
059: * This checker inspects the input document and look for the use of the
060: * prefix "jaxb". If the document doesn't associate any prefix to the
061: * JAXB customization URI and if it does associate the jaxb prefix,
062: * this checker will issue a warning.
063: *
064: * <p>
065: * This warning can happen to completely correct schema (because
066: * nothing prevents you from using the prefix "jaxb" for other purpose
067: * while using a JAXB compiler on the same schema) but in practice
068: * this would be quite unlikely.
069: *
070: * <p>
071: * This justifies the use of this filter.
072: *
073: * @author
074: * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
075: */
076: public class IncorrectNamespaceURIChecker extends XMLFilterImpl {
077:
078: public IncorrectNamespaceURIChecker(ErrorHandler handler) {
079: this .errorHandler = handler;
080: }
081:
082: private ErrorHandler errorHandler;
083:
084: private Locator locator = null;
085:
086: /** Sets to true once we see the jaxb prefix in use. */
087: private boolean isJAXBPrefixUsed = false;
088: /** Sets to true once we see the JAXB customization namespace URI. */
089: private boolean isCustomizationUsed = false;
090:
091: public void endDocument() throws SAXException {
092: if (isJAXBPrefixUsed && !isCustomizationUsed) {
093: SAXParseException e = new SAXParseException(Messages
094: .format(Messages.WARN_INCORRECT_URI,
095: Const.JAXB_NSURI), locator);
096: errorHandler.warning(e);
097: }
098:
099: super .endDocument();
100: }
101:
102: public void startPrefixMapping(String prefix, String uri)
103: throws SAXException {
104: if (prefix.equals("jaxb"))
105: isJAXBPrefixUsed = true;
106: if (uri.equals(Const.JAXB_NSURI))
107: isCustomizationUsed = true;
108:
109: super .startPrefixMapping(prefix, uri);
110: }
111:
112: public void startElement(String namespaceURI, String localName,
113: String qName, Attributes atts) throws SAXException {
114: super .startElement(namespaceURI, localName, qName, atts);
115:
116: // I'm not sure if this is necessary (SAX might report the change of the default prefix
117: // through the startPrefixMapping method, and I think it does indeed.)
118: //
119: // but better safe than sorry.
120:
121: if (namespaceURI.equals(Const.JAXB_NSURI))
122: isCustomizationUsed = true;
123: }
124:
125: public void setDocumentLocator(Locator locator) {
126: super.setDocumentLocator(locator);
127: this.locator = locator;
128: }
129: }
|