1 /*
2 * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26 package com.sun.tools.internal.xjc.reader.xmlschema.parser;
27
28 import com.sun.tools.internal.xjc.reader.Const;
29
30 import org.xml.sax.Attributes;
31 import org.xml.sax.ErrorHandler;
32 import org.xml.sax.Locator;
33 import org.xml.sax.SAXException;
34 import org.xml.sax.SAXParseException;
35 import org.xml.sax.helpers.XMLFilterImpl;
36
37 /**
38 * This filter detects the use of incorrect JAXB namespace URI.
39 *
40 * When the binding compiler looks at a schema file, it always look
41 * for the namespace URI of the elements (which is correct, BTW.)
42 *
43 * <p>
44 * However, one unfortunate downside of this philosophically correct
45 * behavior is that there is no provision or safety check when an user
46 * misspelled JAXB binding customization namespace.
47 *
48 * <p>
49 * This checker inspects the input document and look for the use of the
50 * prefix "jaxb". If the document doesn't associate any prefix to the
51 * JAXB customization URI and if it does associate the jaxb prefix,
52 * this checker will issue a warning.
53 *
54 * <p>
55 * This warning can happen to completely correct schema (because
56 * nothing prevents you from using the prefix "jaxb" for other purpose
57 * while using a JAXB compiler on the same schema) but in practice
58 * this would be quite unlikely.
59 *
60 * <p>
61 * This justifies the use of this filter.
62 *
63 * @author
64 * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
65 */
66 public class IncorrectNamespaceURIChecker extends XMLFilterImpl {
67
68 public IncorrectNamespaceURIChecker( ErrorHandler handler ) {
69 this.errorHandler = handler;
70 }
71
72 private ErrorHandler errorHandler;
73
74 private Locator locator = null;
75
76 /** Sets to true once we see the jaxb prefix in use. */
77 private boolean isJAXBPrefixUsed = false;
78 /** Sets to true once we see the JAXB customization namespace URI. */
79 private boolean isCustomizationUsed = false;
80
81 public void endDocument() throws SAXException {
82 if( isJAXBPrefixUsed && !isCustomizationUsed ) {
83 SAXParseException e = new SAXParseException(
84 Messages.format(Messages.WARN_INCORRECT_URI, Const.JAXB_NSURI),
85 locator );
86 errorHandler.warning(e);
87 }
88
89 super.endDocument();
90 }
91
92 public void startPrefixMapping(String prefix, String uri) throws SAXException {
93 if( prefix.equals("jaxb") )
94 isJAXBPrefixUsed = true;
95 if( uri.equals(Const.JAXB_NSURI) )
96 isCustomizationUsed = true;
97
98 super.startPrefixMapping(prefix, uri);
99 }
100
101 public void startElement(String namespaceURI, String localName, String qName, Attributes atts)
102 throws SAXException {
103 super.startElement(namespaceURI, localName, qName, atts);
104
105 // I'm not sure if this is necessary (SAX might report the change of the default prefix
106 // through the startPrefixMapping method, and I think it does indeed.)
107 //
108 // but better safe than sorry.
109
110 if( namespaceURI.equals(Const.JAXB_NSURI) )
111 isCustomizationUsed = true;
112 }
113
114 public void setDocumentLocator( Locator locator ) {
115 super.setDocumentLocator( locator );
116 this.locator = locator;
117 }
118 }