aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.tools.internal.xjc.reader.xmlschema.parser; aoqi@0: aoqi@0: import com.sun.tools.internal.xjc.reader.Const; aoqi@0: import com.sun.xml.internal.bind.v2.WellKnownNamespace; aoqi@0: import org.xml.sax.Attributes; aoqi@0: import org.xml.sax.ErrorHandler; aoqi@0: import org.xml.sax.Locator; aoqi@0: import org.xml.sax.SAXException; aoqi@0: import org.xml.sax.SAXParseException; aoqi@0: import org.xml.sax.helpers.XMLFilterImpl; aoqi@0: aoqi@0: /** aoqi@0: * This filter detects the use of incorrect JAXB namespace URI. aoqi@0: * aoqi@0: * When the binding compiler looks at a schema file, it always look aoqi@0: * for the namespace URI of the elements (which is correct, BTW.) aoqi@0: * aoqi@0: *

aoqi@0: * However, one unfortunate downside of this philosophically correct aoqi@0: * behavior is that there is no provision or safety check when an user aoqi@0: * misspelled JAXB binding customization namespace. aoqi@0: * aoqi@0: *

aoqi@0: * This checker inspects the input document and look for the use of the aoqi@0: * prefix "jaxb". If the document doesn't associate any prefix to the aoqi@0: * JAXB customization URI and if it does associate the jaxb prefix, aoqi@0: * this checker will issue a warning. aoqi@0: * aoqi@0: *

aoqi@0: * This warning can happen to completely correct schema (because aoqi@0: * nothing prevents you from using the prefix "jaxb" for other purpose aoqi@0: * while using a JAXB compiler on the same schema) but in practice aoqi@0: * this would be quite unlikely. aoqi@0: * aoqi@0: *

aoqi@0: * This justifies the use of this filter. aoqi@0: * aoqi@0: * @author aoqi@0: * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com) aoqi@0: */ aoqi@0: public class IncorrectNamespaceURIChecker extends XMLFilterImpl { aoqi@0: aoqi@0: public IncorrectNamespaceURIChecker( ErrorHandler handler ) { aoqi@0: this.errorHandler = handler; aoqi@0: } aoqi@0: aoqi@0: private ErrorHandler errorHandler; aoqi@0: aoqi@0: private Locator locator = null; aoqi@0: aoqi@0: /** Sets to true once we see the jaxb prefix in use. */ aoqi@0: private boolean isJAXBPrefixUsed = false; aoqi@0: /** Sets to true once we see the JAXB customization namespace URI. */ aoqi@0: private boolean isCustomizationUsed = false; aoqi@0: aoqi@0: @Override aoqi@0: public void endDocument() throws SAXException { aoqi@0: if( isJAXBPrefixUsed && !isCustomizationUsed ) { aoqi@0: SAXParseException e = new SAXParseException( aoqi@0: Messages.format(Messages.WARN_INCORRECT_URI, Const.JAXB_NSURI), aoqi@0: locator ); aoqi@0: errorHandler.warning(e); aoqi@0: } aoqi@0: aoqi@0: super.endDocument(); aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public void startPrefixMapping(String prefix, String uri) throws SAXException { aoqi@0: if (WellKnownNamespace.XML_NAMESPACE_URI.equals(uri)) return; //xml prefix shall not be declared based on jdk api javadoc aoqi@0: if( prefix.equals("jaxb") ) aoqi@0: isJAXBPrefixUsed = true; aoqi@0: if( uri.equals(Const.JAXB_NSURI) ) aoqi@0: isCustomizationUsed = true; aoqi@0: aoqi@0: super.startPrefixMapping(prefix, uri); aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public void endPrefixMapping(String prefix) throws SAXException { aoqi@0: if ("xml".equals(prefix)) return; //xml prefix shall not be declared based on jdk api javadoc aoqi@0: super.endPrefixMapping(prefix); aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public void startElement(String namespaceURI, String localName, String qName, Attributes atts) aoqi@0: throws SAXException { aoqi@0: super.startElement(namespaceURI, localName, qName, atts); aoqi@0: aoqi@0: // I'm not sure if this is necessary (SAX might report the change of the default prefix aoqi@0: // through the startPrefixMapping method, and I think it does indeed.) aoqi@0: // aoqi@0: // but better safe than sorry. aoqi@0: aoqi@0: if( namespaceURI.equals(Const.JAXB_NSURI) ) aoqi@0: isCustomizationUsed = true; aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public void setDocumentLocator( Locator locator ) { aoqi@0: super.setDocumentLocator( locator ); aoqi@0: this.locator = locator; aoqi@0: } aoqi@0: }