author | mkos |
Sun, 30 Dec 2012 00:00:00 +0100 | |
changeset 22678 | ac1ea46be942 |
parent 12009 | 4abb694f273a |
permissions | -rw-r--r-- |
12009 | 1 |
/* |
22678
ac1ea46be942
8029237: Update copyright year to match last edit in jaxws repository for 2012
mkos
parents:
12009
diff
changeset
|
2 |
* Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. |
12009 | 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. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
26 |
package com.sun.tools.internal.xjc.reader; |
|
27 |
||
28 |
import java.util.Set; |
|
29 |
import java.util.HashSet; |
|
30 |
||
31 |
import com.sun.tools.internal.xjc.util.SubtreeCutter; |
|
32 |
import com.sun.tools.internal.xjc.Options; |
|
33 |
import com.sun.tools.internal.xjc.Plugin; |
|
34 |
import com.sun.xml.internal.bind.v2.util.EditDistance; |
|
35 |
||
36 |
import org.xml.sax.helpers.NamespaceSupport; |
|
37 |
import org.xml.sax.Locator; |
|
38 |
import org.xml.sax.ErrorHandler; |
|
39 |
import org.xml.sax.SAXParseException; |
|
40 |
import org.xml.sax.SAXException; |
|
41 |
||
42 |
import com.sun.xml.internal.bind.v2.WellKnownNamespace; |
|
43 |
||
44 |
/** |
|
45 |
* Common code between {@code DTDExtensionBindingChecker} and {@link ExtensionBindingChecker}. |
|
46 |
* |
|
47 |
* @author Kohsuke Kawaguchi |
|
48 |
*/ |
|
49 |
public abstract class AbstractExtensionBindingChecker extends SubtreeCutter { |
|
50 |
/** Remembers in-scope namespace bindings. */ |
|
51 |
protected final NamespaceSupport nsSupport = new NamespaceSupport(); |
|
52 |
||
53 |
/** |
|
54 |
* Set of namespace URIs that designates enabled extensions. |
|
55 |
*/ |
|
56 |
protected final Set<String> enabledExtensions = new HashSet<String>(); |
|
57 |
||
58 |
private final Set<String> recognizableExtensions = new HashSet<String>(); |
|
59 |
||
60 |
private Locator locator; |
|
61 |
||
62 |
/** |
|
63 |
* Namespace URI of the target schema language. Elements in this |
|
64 |
* namespace are always allowed. |
|
65 |
*/ |
|
66 |
protected final String schemaLanguage; |
|
67 |
||
68 |
/** |
|
69 |
* If false, any use of extensions is reported as an error. |
|
70 |
*/ |
|
71 |
protected final boolean allowExtensions; |
|
72 |
||
73 |
private final Options options; |
|
74 |
||
75 |
/** |
|
76 |
* @param handler |
|
77 |
* This error handler will receive detected errors. |
|
78 |
*/ |
|
79 |
public AbstractExtensionBindingChecker( String schemaLanguage, Options options, ErrorHandler handler ) { |
|
80 |
this.schemaLanguage = schemaLanguage; |
|
81 |
this.allowExtensions = options.compatibilityMode!=Options.STRICT; |
|
82 |
this.options = options; |
|
83 |
setErrorHandler(handler); |
|
84 |
||
85 |
for (Plugin plugin : options.getAllPlugins()) |
|
86 |
recognizableExtensions.addAll(plugin.getCustomizationURIs()); |
|
87 |
recognizableExtensions.add(Const.XJC_EXTENSION_URI); |
|
88 |
} |
|
89 |
||
90 |
/** |
|
91 |
* Verify that the given URI is indeed a valid extension namespace URI, |
|
92 |
* and if so enable it. |
|
93 |
* <p> |
|
94 |
* This method does all the error handling. |
|
95 |
*/ |
|
96 |
protected final void checkAndEnable(String uri) throws SAXException { |
|
97 |
if( !isRecognizableExtension(uri) ) { |
|
98 |
String nearest = EditDistance.findNearest(uri, recognizableExtensions); |
|
99 |
// not the namespace URI we know of |
|
100 |
error( Messages.ERR_UNSUPPORTED_EXTENSION.format(uri,nearest) ); |
|
101 |
} else |
|
102 |
if( !isSupportedExtension(uri) ) { |
|
103 |
// recognizable but not not supported, meaning |
|
104 |
// the plug-in isn't enabled |
|
105 |
||
106 |
// look for plug-in that handles this URI |
|
107 |
Plugin owner = null; |
|
108 |
for( Plugin p : options.getAllPlugins() ) { |
|
109 |
if(p.getCustomizationURIs().contains(uri)) { |
|
110 |
owner = p; |
|
111 |
break; |
|
112 |
} |
|
113 |
} |
|
114 |
if(owner!=null) |
|
115 |
// we know the plug-in that supports this namespace, but it's not enabled |
|
116 |
error( Messages.ERR_PLUGIN_NOT_ENABLED.format(owner.getOptionName(),uri)); |
|
117 |
else { |
|
118 |
// this shouldn't happen, but be defensive... |
|
119 |
error( Messages.ERR_UNSUPPORTED_EXTENSION.format(uri) ); |
|
120 |
} |
|
121 |
} |
|
122 |
||
123 |
// as an error recovery enable this namespace URI anyway. |
|
124 |
enabledExtensions.add(uri); |
|
125 |
} |
|
126 |
||
127 |
/** |
|
128 |
* If the tag name belongs to a plugin namespace-wise, check its local name |
|
129 |
* to make sure it's correct. |
|
130 |
*/ |
|
131 |
protected final void verifyTagName(String namespaceURI, String localName, String qName) throws SAXException { |
|
132 |
if(options.pluginURIs.contains(namespaceURI)) { |
|
133 |
// make sure that this is a valid tag name |
|
134 |
boolean correct = false; |
|
135 |
for( Plugin p : options.activePlugins ) { |
|
136 |
if(p.isCustomizationTagName(namespaceURI,localName)) { |
|
137 |
correct = true; |
|
138 |
break; |
|
139 |
} |
|
140 |
} |
|
141 |
if(!correct) { |
|
142 |
error( Messages.ERR_ILLEGAL_CUSTOMIZATION_TAGNAME.format(qName) ); |
|
143 |
startCutting(); |
|
144 |
} |
|
145 |
} |
|
146 |
} |
|
147 |
||
148 |
/** |
|
149 |
* Checks if the given namespace URI is supported as the extension |
|
150 |
* bindings. |
|
151 |
*/ |
|
152 |
protected final boolean isSupportedExtension( String namespaceUri ) { |
|
153 |
return namespaceUri.equals(Const.XJC_EXTENSION_URI) || options.pluginURIs.contains(namespaceUri); |
|
154 |
} |
|
155 |
||
156 |
/** |
|
157 |
* Checks if the given namespace URI can be potentially recognized |
|
158 |
* by this XJC. |
|
159 |
*/ |
|
160 |
protected final boolean isRecognizableExtension( String namespaceUri ) { |
|
161 |
return recognizableExtensions.contains(namespaceUri); |
|
162 |
} |
|
163 |
||
164 |
@Override |
|
165 |
public void setDocumentLocator(Locator locator) { |
|
166 |
super.setDocumentLocator(locator); |
|
167 |
this.locator = locator; |
|
168 |
} |
|
169 |
||
170 |
@Override |
|
171 |
public void startDocument() throws SAXException { |
|
172 |
super.startDocument(); |
|
173 |
||
174 |
nsSupport.reset(); |
|
175 |
enabledExtensions.clear(); |
|
176 |
} |
|
177 |
||
178 |
@Override |
|
179 |
public void startPrefixMapping(String prefix, String uri) throws SAXException { |
|
180 |
if (WellKnownNamespace.XML_NAMESPACE_URI.equals(uri)) return; |
|
181 |
super.startPrefixMapping(prefix, uri); //xml prefix shall not be declared based on jdk api javado |
|
182 |
nsSupport.pushContext(); |
|
183 |
nsSupport.declarePrefix(prefix,uri); |
|
184 |
} |
|
185 |
||
186 |
@Override |
|
187 |
public void endPrefixMapping(String prefix) throws SAXException { |
|
188 |
if ("xml".equals(prefix)) return; //xml prefix shall not be declared based on jdk api javadoc |
|
189 |
super.endPrefixMapping(prefix); |
|
190 |
nsSupport.popContext(); |
|
191 |
} |
|
192 |
||
193 |
||
194 |
/** |
|
195 |
* Reports an error and returns the created SAXParseException |
|
196 |
*/ |
|
197 |
protected final SAXParseException error( String msg ) throws SAXException { |
|
198 |
SAXParseException spe = new SAXParseException( msg, locator ); |
|
199 |
getErrorHandler().error(spe); |
|
200 |
return spe; |
|
201 |
} |
|
202 |
||
203 |
/** |
|
204 |
* Reports a warning. |
|
205 |
*/ |
|
206 |
protected final void warning( String msg ) throws SAXException { |
|
207 |
SAXParseException spe = new SAXParseException( msg, locator ); |
|
208 |
getErrorHandler().warning(spe); |
|
209 |
} |
|
210 |
} |