All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sun.tools.xjc.reader.ExtensionBindingChecker Maven / Gradle / Ivy

Go to download

Old JAXB Binding Compiler. Contains source code needed for binding customization files into java sources. In other words: the *tool* to generate java classes for the given xml representation.

There is a newer version: 4.0.5
Show newest version
/*
 * The contents of this file are subject to the terms
 * of the Common Development and Distribution License
 * (the "License").  You may not use this file except
 * in compliance with the License.
 * 
 * You can obtain a copy of the license at
 * https://jwsdp.dev.java.net/CDDLv1.0.html
 * See the License for the specific language governing
 * permissions and limitations under the License.
 * 
 * When distributing Covered Code, include this CDDL
 * HEADER in each file and include the License file at
 * https://jwsdp.dev.java.net/CDDLv1.0.html  If applicable,
 * add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your
 * own identifying information: Portions Copyright [yyyy]
 * [name of copyright owner]
 */
package com.sun.tools.xjc.reader;

import java.util.HashSet;
import java.util.Set;
import java.util.StringTokenizer;

import com.sun.tools.xjc.Options;
import com.sun.tools.xjc.Plugin;

import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.ErrorHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.NamespaceSupport;
import org.xml.sax.helpers.XMLFilterImpl;

/**
 * This filter checks jaxb:extensionBindingPrefix and
 * pass/filter extension bindings.
 * 
 * 

* This filter also remembers enabled extension namespaces * and filters out any extension namespaces that doesn't belong * to those. The net effect is that disabled customizations * will never pass through this filter. * *

* Note that we can't just filter out all foreign namespaces, * as we need to use user-defined tags in documentations to generate javadoc. * *

* The class needs to know the list of extension binding namespaces * that the RI recognizes. * To add new URI, modify the isSupportedExtension method. * * @author * Kohsuke Kawaguchi ([email protected]) */ public final class ExtensionBindingChecker extends XMLFilterImpl { /** Remembers in-scope namespace bindings. */ private final NamespaceSupport nsSupport = new NamespaceSupport(); /** * Number of the elements encountered. Used to detect the root element. */ private int count=0; /** * Set of namespace URIs that designates enabled extensions. */ private final Set enabledExtensions = new HashSet(); private final Set recognizableExtensions = new HashSet(); private Locator locator; /** * When we are pruning a sub tree, this field holds the depth of * elements that are being cut. Used to resume event forwarding. * * As long as this value is 0, we will pass through data. */ private int cutDepth=0; /** * This object will receive SAX events while a sub tree is being * pruned. */ private static final ContentHandler stub = new DefaultHandler(); /** * This field remembers the user-specified ContentHandler. * So that we can restore it once the sub tree is completely pruned. */ private ContentHandler next; /** * Namespace URI of the target schema language. Elements in this * namespace are always allowed. */ private final String schemaLanguage; /** * If false, any use of extensions is reported as an error. */ private final boolean allowExtensions; private final Options options; /** * @param handler * This error handler will receive detected errors. */ public ExtensionBindingChecker( String schemaLanguage, Options options, ErrorHandler handler ) { this.schemaLanguage = schemaLanguage; this.allowExtensions = options.compatibilityMode!=Options.STRICT; this.options = options; setErrorHandler(handler); for (Plugin plugin : options.getAllPlugins()) recognizableExtensions.addAll(plugin.getCustomizationURIs()); recognizableExtensions.add(Const.XJC_EXTENSION_URI); } /** * Checks if the given namespace URI is supported as the extension * bindings. */ private boolean isSupportedExtension( String namespaceUri ) { if(namespaceUri.equals(Const.XJC_EXTENSION_URI)) return true; return options.pluginURIs.contains(namespaceUri); } /** * Checks if the given namespace URI can be potentially recognized * by this XJC. */ private boolean isRecognizableExtension( String namespaceUri ) { return recognizableExtensions.contains(namespaceUri); } /** * Returns true if the elements with the given namespace URI * should be blocked by this filter. */ private boolean needsToBePruned( String uri ) { if( uri.equals(schemaLanguage) ) return false; if( uri.equals(Const.JAXB_NSURI) ) return false; if( enabledExtensions.contains(uri) ) return false; // we don't need to prune something unless // the rest of the processor recognizes it as something special. // this allows us to send the documentation and other harmless // foreign XML fragments, which may be picked up as documents. return isRecognizableExtension(uri); } public void startDocument() throws SAXException { super.startDocument(); count=0; cutDepth=0; nsSupport.reset(); enabledExtensions.clear(); } public void startPrefixMapping(String prefix, String uri) throws SAXException { super.startPrefixMapping(prefix, uri); nsSupport.pushContext(); nsSupport.declarePrefix(prefix,uri); } public void endPrefixMapping(String prefix) throws SAXException { super.endPrefixMapping(prefix); nsSupport.popContext(); } public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { if( cutDepth==0 ) { String v = atts.getValue(Const.JAXB_NSURI,"extensionBindingPrefixes"); if(v!=null) { if(count!=0) // the binding attribute is allowed only at the root level. error( Messages.ERR_UNEXPECTED_EXTENSION_BINDING_PREFIXES.format() ); if(!allowExtensions) error( Messages.ERR_VENDOR_EXTENSION_DISALLOWED_IN_STRICT_MODE.format() ); // then remember the associated namespace URIs. StringTokenizer tokens = new StringTokenizer(v); while(tokens.hasMoreTokens()) { String prefix = tokens.nextToken(); String uri = nsSupport.getURI(prefix); if( uri==null ) { // undeclared prefix error( Messages.ERR_UNDECLARED_PREFIX.format(prefix) ); } else { if( !isRecognizableExtension(uri) ) // not the namespace URI we know of error( Messages.ERR_UNSUPPORTED_EXTENSION.format(prefix) ); else if( !isSupportedExtension(uri) ) { // recognizable but not not supported, meaning // the plug-in isn't enabled // look for plug-in that handles this URI Plugin owner = null; for( Plugin p : options.getAllPlugins() ) { if(p.getCustomizationURIs().contains(uri)) { owner = p; break; } } if(owner!=null) // we know the plug-in that supports this namespace, but it's not enabled error( Messages.ERR_PLUGIN_NOT_ENABLED.format(owner.getOptionName(),uri)); else { // this shouldn't happen, but be defensive... error( Messages.ERR_UNSUPPORTED_EXTENSION.format(prefix) ); } } // as an error recovery enable this namespace URI anyway. enabledExtensions.add(uri); } } } if( needsToBePruned(namespaceURI) ) { // start pruning the tree. Call the super class method directly. if( isRecognizableExtension(namespaceURI) ) { // but this is a supported customization. // isn't the user forgetting @jaxb:extensionBindingPrefixes? warning( Messages.ERR_SUPPORTED_EXTENSION_IGNORED.format(namespaceURI) ); } super.setContentHandler(stub); cutDepth=1; } else if(options.pluginURIs.contains(namespaceURI)) { // make sure that this is a valid tag name boolean correct = false; for( Plugin p : options.activePlugins ) { if(p.isCustomizationTagName(namespaceURI,localName)) { correct = true; break; } } if(!correct) { error( Messages.ERR_ILLEGAL_CUSTOMIZATION_TAGNAME.format(qName) ); super.setContentHandler(stub); cutDepth=1; } } } else cutDepth++; count++; super.startElement(namespaceURI, localName, qName, atts); } public void endElement(String namespaceURI, String localName, String qName) throws SAXException { super.endElement(namespaceURI, localName, qName); if( cutDepth!=0 ) { cutDepth--; if( cutDepth == 0 ) // pruning completed. restore the user handler super.setContentHandler(next); } } public void setDocumentLocator(Locator locator) { super.setDocumentLocator(locator); this.locator = locator; } public void setContentHandler(ContentHandler handler) { next = handler; // changes take effect immediately unless the sub-tree is being pruned if(getContentHandler()!=stub) super.setContentHandler(handler); } /** * Reports an error and returns the created SAXParseException */ private SAXParseException error( String msg ) throws SAXException { SAXParseException spe = new SAXParseException( msg, locator ); getErrorHandler().error(spe); return spe; } /** * Reports a warning. */ private void warning( String msg ) throws SAXException { SAXParseException spe = new SAXParseException( msg, locator ); getErrorHandler().warning(spe); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy