src/share/jaxws_classes/com/sun/tools/internal/xjc/reader/AbstractExtensionBindingChecker.java

Tue, 09 Apr 2013 14:51:13 +0100

author
alanb
date
Tue, 09 Apr 2013 14:51:13 +0100
changeset 368
0989ad8c0860
parent 0
373ffda63c9a
permissions
-rw-r--r--

8010393: Update JAX-WS RI to 2.2.9-b12941
Reviewed-by: alanb, erikj
Contributed-by: miroslav.kos@oracle.com, martin.grebac@oracle.com

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

mercurial