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