aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2012, 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.dtd.bindinfo; aoqi@0: aoqi@0: import java.io.IOException; aoqi@0: import java.util.Collection; aoqi@0: import java.util.HashMap; aoqi@0: import java.util.Map; aoqi@0: aoqi@0: import javax.xml.parsers.ParserConfigurationException; aoqi@0: import javax.xml.parsers.SAXParserFactory; aoqi@0: import javax.xml.validation.ValidatorHandler; aoqi@0: aoqi@0: import com.sun.codemodel.internal.ClassType; aoqi@0: import com.sun.codemodel.internal.JClass; aoqi@0: import com.sun.codemodel.internal.JClassAlreadyExistsException; aoqi@0: import com.sun.codemodel.internal.JCodeModel; aoqi@0: import com.sun.codemodel.internal.JDefinedClass; aoqi@0: import com.sun.codemodel.internal.JPackage; aoqi@0: import com.sun.istack.internal.SAXParseException2; aoqi@0: import com.sun.tools.internal.xjc.AbortException; aoqi@0: import com.sun.tools.internal.xjc.ErrorReceiver; aoqi@0: import com.sun.tools.internal.xjc.SchemaCache; aoqi@0: import com.sun.tools.internal.xjc.model.CCustomizations; aoqi@0: import com.sun.tools.internal.xjc.model.CPluginCustomization; aoqi@0: import com.sun.tools.internal.xjc.model.Model; aoqi@0: import com.sun.tools.internal.xjc.reader.Const; aoqi@0: import com.sun.tools.internal.xjc.util.CodeModelClassFactory; aoqi@0: import com.sun.tools.internal.xjc.util.ErrorReceiverFilter; aoqi@0: import com.sun.tools.internal.xjc.util.ForkContentHandler; aoqi@0: aoqi@0: import com.sun.xml.internal.bind.v2.util.XmlFactory; aoqi@0: import javax.xml.parsers.DocumentBuilderFactory; aoqi@0: import org.w3c.dom.Document; aoqi@0: import org.w3c.dom.Element; aoqi@0: import org.xml.sax.InputSource; aoqi@0: import org.xml.sax.SAXException; aoqi@0: import org.xml.sax.XMLReader; aoqi@0: aoqi@0: /** aoqi@0: * Root of the binding information. aoqi@0: */ aoqi@0: public class BindInfo aoqi@0: { aoqi@0: /** Controller object that can be used to report errors. */ aoqi@0: protected final ErrorReceiver errorReceiver; aoqi@0: aoqi@0: /*package*/ final Model model; aoqi@0: aoqi@0: /** aoqi@0: * The -p option that should control the default Java package that aoqi@0: * will contain the generated code. Null if unspecified. This takes aoqi@0: * precedence over the value specified in the binding file. aoqi@0: */ aoqi@0: private final String defaultPackage; aoqi@0: aoqi@0: public BindInfo(Model model, InputSource source, ErrorReceiver _errorReceiver) throws AbortException { aoqi@0: this( model, parse(model,source,_errorReceiver), _errorReceiver); aoqi@0: } aoqi@0: aoqi@0: public BindInfo(Model model, Document _dom, ErrorReceiver _errorReceiver) { aoqi@0: this.model = model; aoqi@0: this.dom = _dom.getDocumentElement(); aoqi@0: this.codeModel = model.codeModel; aoqi@0: this.errorReceiver = _errorReceiver; aoqi@0: this.classFactory = new CodeModelClassFactory(_errorReceiver); aoqi@0: // TODO: decide name converter from the binding file aoqi@0: aoqi@0: this.defaultPackage = model.options.defaultPackage; aoqi@0: aoqi@0: // copy global customizations to the model aoqi@0: model.getCustomizations().addAll(getGlobalCustomizations()); aoqi@0: aoqi@0: // process element declarations aoqi@0: for( Element ele : DOMUtil.getChildElements(dom,"element")) { aoqi@0: BIElement e = new BIElement(this,ele); aoqi@0: elements.put(e.name(),e); aoqi@0: } aoqi@0: aoqi@0: // add built-in conversions aoqi@0: BIUserConversion.addBuiltinConversions(this,conversions); aoqi@0: aoqi@0: // process conversion declarations aoqi@0: for( Element cnv : DOMUtil.getChildElements(dom,"conversion")) { aoqi@0: BIConversion c = new BIUserConversion(this,cnv); aoqi@0: conversions.put(c.name(),c); aoqi@0: } aoqi@0: for( Element en : DOMUtil.getChildElements(dom,"enumeration")) { aoqi@0: BIConversion c = BIEnumeration.create( en, this ); aoqi@0: conversions.put(c.name(),c); aoqi@0: } aoqi@0: // TODO: check the uniquness of conversion name aoqi@0: aoqi@0: aoqi@0: // process interface definitions aoqi@0: for( Element itf : DOMUtil.getChildElements(dom,"interface")) { aoqi@0: BIInterface c = new BIInterface(itf); aoqi@0: interfaces.put(c.name(),c); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: /** CodeModel object that is used by this binding file. */ aoqi@0: final JCodeModel codeModel; aoqi@0: aoqi@0: /** Wrap the codeModel object and automate error reporting. */ aoqi@0: final CodeModelClassFactory classFactory; aoqi@0: aoqi@0: /** DOM tree that represents binding info. */ aoqi@0: private final Element dom; aoqi@0: aoqi@0: /** Conversion declarations. */ aoqi@0: private final Map conversions = new HashMap(); aoqi@0: aoqi@0: /** Element declarations keyed by names. */ aoqi@0: private final Map elements = new HashMap(); aoqi@0: aoqi@0: /** interface declarations keyed by names. */ aoqi@0: private final Map interfaces = new HashMap(); aoqi@0: aoqi@0: aoqi@0: /** XJC extension namespace. */ aoqi@0: private static final String XJC_NS = Const.XJC_EXTENSION_URI; aoqi@0: aoqi@0: // aoqi@0: // aoqi@0: // Exposed public methods aoqi@0: // aoqi@0: // aoqi@0: /** Gets the serialVersionUID if it's turned on. */ aoqi@0: public Long getSerialVersionUID() { aoqi@0: Element serial = DOMUtil.getElement(dom,XJC_NS,"serializable"); aoqi@0: if(serial==null) return null; aoqi@0: aoqi@0: String v = DOMUtil.getAttribute(serial,"uid"); aoqi@0: if(v==null) v="1"; aoqi@0: return new Long(v); aoqi@0: } aoqi@0: aoqi@0: /** Gets the xjc:superClass customization if it's turned on. */ aoqi@0: public JClass getSuperClass() { aoqi@0: Element sc = DOMUtil.getElement(dom,XJC_NS,"superClass"); aoqi@0: if (sc == null) return null; aoqi@0: aoqi@0: JDefinedClass c; aoqi@0: aoqi@0: try { aoqi@0: String v = DOMUtil.getAttribute(sc,"name"); aoqi@0: if(v==null) return null; aoqi@0: c = codeModel._class(v); aoqi@0: c.hide(); aoqi@0: } catch (JClassAlreadyExistsException e) { aoqi@0: c = e.getExistingClass(); aoqi@0: } aoqi@0: aoqi@0: return c; aoqi@0: } aoqi@0: aoqi@0: /** Gets the xjc:superInterface customization if it's turned on. */ aoqi@0: public JClass getSuperInterface() { aoqi@0: Element sc = DOMUtil.getElement(dom,XJC_NS,"superInterface"); aoqi@0: if (sc == null) return null; aoqi@0: aoqi@0: String name = DOMUtil.getAttribute(sc,"name"); aoqi@0: if (name == null) return null; aoqi@0: aoqi@0: JDefinedClass c; aoqi@0: aoqi@0: try { aoqi@0: c = codeModel._class(name, ClassType.INTERFACE); aoqi@0: c.hide(); aoqi@0: } catch (JClassAlreadyExistsException e) { aoqi@0: c = e.getExistingClass(); aoqi@0: } aoqi@0: aoqi@0: return c; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Gets the specified package name (options/@package). aoqi@0: */ aoqi@0: public JPackage getTargetPackage() { aoqi@0: if(model.options.defaultPackage!=null) aoqi@0: // "-p" takes precedence over everything else aoqi@0: return codeModel._package(model.options.defaultPackage); aoqi@0: aoqi@0: String p; aoqi@0: if( defaultPackage!=null ) aoqi@0: p = defaultPackage; aoqi@0: else aoqi@0: p = getOption("package", ""); aoqi@0: return codeModel._package(p); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Gets the conversion declaration from the binding info. aoqi@0: * aoqi@0: * @return aoqi@0: * A non-null valid BIConversion object. aoqi@0: */ aoqi@0: public BIConversion conversion(String name) { aoqi@0: BIConversion r = conversions.get(name); aoqi@0: if (r == null) aoqi@0: throw new AssertionError("undefined conversion name: this should be checked by the validator before we read it"); aoqi@0: return r; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Gets the element declaration from the binding info. aoqi@0: * aoqi@0: * @return aoqi@0: * If there is no declaration with a given name, aoqi@0: * this method returns null. aoqi@0: */ aoqi@0: public BIElement element( String name ) { aoqi@0: return elements.get(name); aoqi@0: } aoqi@0: /** Iterates all {@link BIElement}s in a read-only set. */ aoqi@0: public Collection elements() { aoqi@0: return elements.values(); aoqi@0: } aoqi@0: aoqi@0: /** Returns all {@link BIInterface}s in a read-only set. */ aoqi@0: public Collection interfaces() { aoqi@0: return interfaces.values(); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Gets the list of top-level {@link CPluginCustomization}s. aoqi@0: */ aoqi@0: private CCustomizations getGlobalCustomizations() { aoqi@0: CCustomizations r=null; aoqi@0: for( Element e : DOMUtil.getChildElements(dom) ) { aoqi@0: if(!model.options.pluginURIs.contains(e.getNamespaceURI())) aoqi@0: continue; // this isn't a plugin customization aoqi@0: if(r==null) aoqi@0: r = new CCustomizations(); aoqi@0: r.add(new CPluginCustomization(e, DOMLocator.getLocationInfo(e))); aoqi@0: } aoqi@0: aoqi@0: if(r==null) r = CCustomizations.EMPTY; aoqi@0: return new CCustomizations(r); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: aoqi@0: aoqi@0: // aoqi@0: // aoqi@0: // Internal utility methods aoqi@0: // aoqi@0: // aoqi@0: aoqi@0: aoqi@0: /** Gets the value from the option element. */ aoqi@0: private String getOption(String attName, String defaultValue) { aoqi@0: Element opt = DOMUtil.getElement(dom,"options"); aoqi@0: if (opt != null) { aoqi@0: String s = DOMUtil.getAttribute(opt,attName); aoqi@0: if (s != null) aoqi@0: return s; aoqi@0: } aoqi@0: return defaultValue; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Lazily parsed schema for the binding file. aoqi@0: */ aoqi@0: private static SchemaCache bindingFileSchema = new SchemaCache(BindInfo.class.getResource("bindingfile.xsd")); aoqi@0: aoqi@0: /** aoqi@0: * Parses an InputSource into dom4j Document. aoqi@0: * Returns null in case of an exception. aoqi@0: */ aoqi@0: private static Document parse( Model model, InputSource is, ErrorReceiver receiver ) throws AbortException { aoqi@0: try { aoqi@0: ValidatorHandler validator = bindingFileSchema.newValidator(); aoqi@0: aoqi@0: // set up the pipe line as : aoqi@0: // /-> extensionChecker -> validator aoqi@0: // parser-> -< aoqi@0: // \-> DOM builder aoqi@0: SAXParserFactory pf = XmlFactory.createParserFactory(model.options.disableXmlSecurity); aoqi@0: DocumentBuilderFactory domFactory = XmlFactory.createDocumentBuilderFactory(model.options.disableXmlSecurity); aoqi@0: DOMBuilder builder = new DOMBuilder(domFactory); aoqi@0: aoqi@0: ErrorReceiverFilter controller = new ErrorReceiverFilter(receiver); aoqi@0: validator.setErrorHandler(controller); aoqi@0: XMLReader reader = pf.newSAXParser().getXMLReader(); aoqi@0: reader.setErrorHandler(controller); aoqi@0: aoqi@0: DTDExtensionBindingChecker checker = new DTDExtensionBindingChecker("", model.options, controller); aoqi@0: checker.setContentHandler(validator); aoqi@0: aoqi@0: reader.setContentHandler(new ForkContentHandler(checker,builder)); aoqi@0: aoqi@0: reader.parse(is); aoqi@0: aoqi@0: if(controller.hadError()) throw new AbortException(); aoqi@0: return (Document)builder.getDOM(); aoqi@0: } catch( IOException e ) { aoqi@0: receiver.error( new SAXParseException2(e.getMessage(),null,e) ); aoqi@0: } catch( SAXException e ) { aoqi@0: receiver.error( new SAXParseException2(e.getMessage(),null,e) ); aoqi@0: } catch( ParserConfigurationException e ) { aoqi@0: receiver.error( new SAXParseException2(e.getMessage(),null,e) ); aoqi@0: } aoqi@0: aoqi@0: throw new AbortException(); aoqi@0: } aoqi@0: }