src/share/jaxws_classes/com/sun/tools/internal/xjc/reader/dtd/bindinfo/BindInfo.java

Thu, 31 Aug 2017 15:18:52 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:18:52 +0800
changeset 637
9c07ef4934dd
parent 368
0989ad8c0860
parent 0
373ffda63c9a
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1997, 2012, 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.dtd.bindinfo;
aoqi@0 27
aoqi@0 28 import java.io.IOException;
aoqi@0 29 import java.util.Collection;
aoqi@0 30 import java.util.HashMap;
aoqi@0 31 import java.util.Map;
aoqi@0 32
aoqi@0 33 import javax.xml.parsers.ParserConfigurationException;
aoqi@0 34 import javax.xml.parsers.SAXParserFactory;
aoqi@0 35 import javax.xml.validation.ValidatorHandler;
aoqi@0 36
aoqi@0 37 import com.sun.codemodel.internal.ClassType;
aoqi@0 38 import com.sun.codemodel.internal.JClass;
aoqi@0 39 import com.sun.codemodel.internal.JClassAlreadyExistsException;
aoqi@0 40 import com.sun.codemodel.internal.JCodeModel;
aoqi@0 41 import com.sun.codemodel.internal.JDefinedClass;
aoqi@0 42 import com.sun.codemodel.internal.JPackage;
aoqi@0 43 import com.sun.istack.internal.SAXParseException2;
aoqi@0 44 import com.sun.tools.internal.xjc.AbortException;
aoqi@0 45 import com.sun.tools.internal.xjc.ErrorReceiver;
aoqi@0 46 import com.sun.tools.internal.xjc.SchemaCache;
aoqi@0 47 import com.sun.tools.internal.xjc.model.CCustomizations;
aoqi@0 48 import com.sun.tools.internal.xjc.model.CPluginCustomization;
aoqi@0 49 import com.sun.tools.internal.xjc.model.Model;
aoqi@0 50 import com.sun.tools.internal.xjc.reader.Const;
aoqi@0 51 import com.sun.tools.internal.xjc.util.CodeModelClassFactory;
aoqi@0 52 import com.sun.tools.internal.xjc.util.ErrorReceiverFilter;
aoqi@0 53 import com.sun.tools.internal.xjc.util.ForkContentHandler;
aoqi@0 54
aoqi@0 55 import com.sun.xml.internal.bind.v2.util.XmlFactory;
aoqi@0 56 import javax.xml.parsers.DocumentBuilderFactory;
aoqi@0 57 import org.w3c.dom.Document;
aoqi@0 58 import org.w3c.dom.Element;
aoqi@0 59 import org.xml.sax.InputSource;
aoqi@0 60 import org.xml.sax.SAXException;
aoqi@0 61 import org.xml.sax.XMLReader;
aoqi@0 62
aoqi@0 63 /**
aoqi@0 64 * Root of the binding information.
aoqi@0 65 */
aoqi@0 66 public class BindInfo
aoqi@0 67 {
aoqi@0 68 /** Controller object that can be used to report errors. */
aoqi@0 69 protected final ErrorReceiver errorReceiver;
aoqi@0 70
aoqi@0 71 /*package*/ final Model model;
aoqi@0 72
aoqi@0 73 /**
aoqi@0 74 * The -p option that should control the default Java package that
aoqi@0 75 * will contain the generated code. Null if unspecified. This takes
aoqi@0 76 * precedence over the value specified in the binding file.
aoqi@0 77 */
aoqi@0 78 private final String defaultPackage;
aoqi@0 79
aoqi@0 80 public BindInfo(Model model, InputSource source, ErrorReceiver _errorReceiver) throws AbortException {
aoqi@0 81 this( model, parse(model,source,_errorReceiver), _errorReceiver);
aoqi@0 82 }
aoqi@0 83
aoqi@0 84 public BindInfo(Model model, Document _dom, ErrorReceiver _errorReceiver) {
aoqi@0 85 this.model = model;
aoqi@0 86 this.dom = _dom.getDocumentElement();
aoqi@0 87 this.codeModel = model.codeModel;
aoqi@0 88 this.errorReceiver = _errorReceiver;
aoqi@0 89 this.classFactory = new CodeModelClassFactory(_errorReceiver);
aoqi@0 90 // TODO: decide name converter from the binding file
aoqi@0 91
aoqi@0 92 this.defaultPackage = model.options.defaultPackage;
aoqi@0 93
aoqi@0 94 // copy global customizations to the model
aoqi@0 95 model.getCustomizations().addAll(getGlobalCustomizations());
aoqi@0 96
aoqi@0 97 // process element declarations
aoqi@0 98 for( Element ele : DOMUtil.getChildElements(dom,"element")) {
aoqi@0 99 BIElement e = new BIElement(this,ele);
aoqi@0 100 elements.put(e.name(),e);
aoqi@0 101 }
aoqi@0 102
aoqi@0 103 // add built-in conversions
aoqi@0 104 BIUserConversion.addBuiltinConversions(this,conversions);
aoqi@0 105
aoqi@0 106 // process conversion declarations
aoqi@0 107 for( Element cnv : DOMUtil.getChildElements(dom,"conversion")) {
aoqi@0 108 BIConversion c = new BIUserConversion(this,cnv);
aoqi@0 109 conversions.put(c.name(),c);
aoqi@0 110 }
aoqi@0 111 for( Element en : DOMUtil.getChildElements(dom,"enumeration")) {
aoqi@0 112 BIConversion c = BIEnumeration.create( en, this );
aoqi@0 113 conversions.put(c.name(),c);
aoqi@0 114 }
aoqi@0 115 // TODO: check the uniquness of conversion name
aoqi@0 116
aoqi@0 117
aoqi@0 118 // process interface definitions
aoqi@0 119 for( Element itf : DOMUtil.getChildElements(dom,"interface")) {
aoqi@0 120 BIInterface c = new BIInterface(itf);
aoqi@0 121 interfaces.put(c.name(),c);
aoqi@0 122 }
aoqi@0 123 }
aoqi@0 124
aoqi@0 125
aoqi@0 126 /** CodeModel object that is used by this binding file. */
aoqi@0 127 final JCodeModel codeModel;
aoqi@0 128
aoqi@0 129 /** Wrap the codeModel object and automate error reporting. */
aoqi@0 130 final CodeModelClassFactory classFactory;
aoqi@0 131
aoqi@0 132 /** DOM tree that represents binding info. */
aoqi@0 133 private final Element dom;
aoqi@0 134
aoqi@0 135 /** Conversion declarations. */
aoqi@0 136 private final Map<String,BIConversion> conversions = new HashMap<String,BIConversion>();
aoqi@0 137
aoqi@0 138 /** Element declarations keyed by names. */
aoqi@0 139 private final Map<String,BIElement> elements = new HashMap<String,BIElement>();
aoqi@0 140
aoqi@0 141 /** interface declarations keyed by names. */
aoqi@0 142 private final Map<String,BIInterface> interfaces = new HashMap<String,BIInterface>();
aoqi@0 143
aoqi@0 144
aoqi@0 145 /** XJC extension namespace. */
aoqi@0 146 private static final String XJC_NS = Const.XJC_EXTENSION_URI;
aoqi@0 147
aoqi@0 148 //
aoqi@0 149 //
aoqi@0 150 // Exposed public methods
aoqi@0 151 //
aoqi@0 152 //
aoqi@0 153 /** Gets the serialVersionUID if it's turned on. */
aoqi@0 154 public Long getSerialVersionUID() {
aoqi@0 155 Element serial = DOMUtil.getElement(dom,XJC_NS,"serializable");
aoqi@0 156 if(serial==null) return null;
aoqi@0 157
aoqi@0 158 String v = DOMUtil.getAttribute(serial,"uid");
aoqi@0 159 if(v==null) v="1";
aoqi@0 160 return new Long(v);
aoqi@0 161 }
aoqi@0 162
aoqi@0 163 /** Gets the xjc:superClass customization if it's turned on. */
aoqi@0 164 public JClass getSuperClass() {
aoqi@0 165 Element sc = DOMUtil.getElement(dom,XJC_NS,"superClass");
aoqi@0 166 if (sc == null) return null;
aoqi@0 167
aoqi@0 168 JDefinedClass c;
aoqi@0 169
aoqi@0 170 try {
aoqi@0 171 String v = DOMUtil.getAttribute(sc,"name");
aoqi@0 172 if(v==null) return null;
aoqi@0 173 c = codeModel._class(v);
aoqi@0 174 c.hide();
aoqi@0 175 } catch (JClassAlreadyExistsException e) {
aoqi@0 176 c = e.getExistingClass();
aoqi@0 177 }
aoqi@0 178
aoqi@0 179 return c;
aoqi@0 180 }
aoqi@0 181
aoqi@0 182 /** Gets the xjc:superInterface customization if it's turned on. */
aoqi@0 183 public JClass getSuperInterface() {
aoqi@0 184 Element sc = DOMUtil.getElement(dom,XJC_NS,"superInterface");
aoqi@0 185 if (sc == null) return null;
aoqi@0 186
aoqi@0 187 String name = DOMUtil.getAttribute(sc,"name");
aoqi@0 188 if (name == null) return null;
aoqi@0 189
aoqi@0 190 JDefinedClass c;
aoqi@0 191
aoqi@0 192 try {
aoqi@0 193 c = codeModel._class(name, ClassType.INTERFACE);
aoqi@0 194 c.hide();
aoqi@0 195 } catch (JClassAlreadyExistsException e) {
aoqi@0 196 c = e.getExistingClass();
aoqi@0 197 }
aoqi@0 198
aoqi@0 199 return c;
aoqi@0 200 }
aoqi@0 201
aoqi@0 202 /**
aoqi@0 203 * Gets the specified package name (options/@package).
aoqi@0 204 */
aoqi@0 205 public JPackage getTargetPackage() {
aoqi@0 206 if(model.options.defaultPackage!=null)
aoqi@0 207 // "-p" takes precedence over everything else
aoqi@0 208 return codeModel._package(model.options.defaultPackage);
aoqi@0 209
aoqi@0 210 String p;
aoqi@0 211 if( defaultPackage!=null )
aoqi@0 212 p = defaultPackage;
aoqi@0 213 else
aoqi@0 214 p = getOption("package", "");
aoqi@0 215 return codeModel._package(p);
aoqi@0 216 }
aoqi@0 217
aoqi@0 218 /**
aoqi@0 219 * Gets the conversion declaration from the binding info.
aoqi@0 220 *
aoqi@0 221 * @return
aoqi@0 222 * A non-null valid BIConversion object.
aoqi@0 223 */
aoqi@0 224 public BIConversion conversion(String name) {
aoqi@0 225 BIConversion r = conversions.get(name);
aoqi@0 226 if (r == null)
aoqi@0 227 throw new AssertionError("undefined conversion name: this should be checked by the validator before we read it");
aoqi@0 228 return r;
aoqi@0 229 }
aoqi@0 230
aoqi@0 231 /**
aoqi@0 232 * Gets the element declaration from the binding info.
aoqi@0 233 *
aoqi@0 234 * @return
aoqi@0 235 * If there is no declaration with a given name,
aoqi@0 236 * this method returns null.
aoqi@0 237 */
aoqi@0 238 public BIElement element( String name ) {
aoqi@0 239 return elements.get(name);
aoqi@0 240 }
aoqi@0 241 /** Iterates all {@link BIElement}s in a read-only set. */
aoqi@0 242 public Collection<BIElement> elements() {
aoqi@0 243 return elements.values();
aoqi@0 244 }
aoqi@0 245
aoqi@0 246 /** Returns all {@link BIInterface}s in a read-only set. */
aoqi@0 247 public Collection<BIInterface> interfaces() {
aoqi@0 248 return interfaces.values();
aoqi@0 249 }
aoqi@0 250
aoqi@0 251 /**
aoqi@0 252 * Gets the list of top-level {@link CPluginCustomization}s.
aoqi@0 253 */
aoqi@0 254 private CCustomizations getGlobalCustomizations() {
aoqi@0 255 CCustomizations r=null;
aoqi@0 256 for( Element e : DOMUtil.getChildElements(dom) ) {
aoqi@0 257 if(!model.options.pluginURIs.contains(e.getNamespaceURI()))
aoqi@0 258 continue; // this isn't a plugin customization
aoqi@0 259 if(r==null)
aoqi@0 260 r = new CCustomizations();
aoqi@0 261 r.add(new CPluginCustomization(e, DOMLocator.getLocationInfo(e)));
aoqi@0 262 }
aoqi@0 263
aoqi@0 264 if(r==null) r = CCustomizations.EMPTY;
aoqi@0 265 return new CCustomizations(r);
aoqi@0 266 }
aoqi@0 267
aoqi@0 268
aoqi@0 269
aoqi@0 270
aoqi@0 271 //
aoqi@0 272 //
aoqi@0 273 // Internal utility methods
aoqi@0 274 //
aoqi@0 275 //
aoqi@0 276
aoqi@0 277
aoqi@0 278 /** Gets the value from the option element. */
aoqi@0 279 private String getOption(String attName, String defaultValue) {
aoqi@0 280 Element opt = DOMUtil.getElement(dom,"options");
aoqi@0 281 if (opt != null) {
aoqi@0 282 String s = DOMUtil.getAttribute(opt,attName);
aoqi@0 283 if (s != null)
aoqi@0 284 return s;
aoqi@0 285 }
aoqi@0 286 return defaultValue;
aoqi@0 287 }
aoqi@0 288
aoqi@0 289 /**
aoqi@0 290 * Lazily parsed schema for the binding file.
aoqi@0 291 */
aoqi@0 292 private static SchemaCache bindingFileSchema = new SchemaCache(BindInfo.class.getResource("bindingfile.xsd"));
aoqi@0 293
aoqi@0 294 /**
aoqi@0 295 * Parses an InputSource into dom4j Document.
aoqi@0 296 * Returns null in case of an exception.
aoqi@0 297 */
aoqi@0 298 private static Document parse( Model model, InputSource is, ErrorReceiver receiver ) throws AbortException {
aoqi@0 299 try {
aoqi@0 300 ValidatorHandler validator = bindingFileSchema.newValidator();
aoqi@0 301
aoqi@0 302 // set up the pipe line as :
aoqi@0 303 // /-> extensionChecker -> validator
aoqi@0 304 // parser-> -<
aoqi@0 305 // \-> DOM builder
aoqi@0 306 SAXParserFactory pf = XmlFactory.createParserFactory(model.options.disableXmlSecurity);
aoqi@0 307 DocumentBuilderFactory domFactory = XmlFactory.createDocumentBuilderFactory(model.options.disableXmlSecurity);
aoqi@0 308 DOMBuilder builder = new DOMBuilder(domFactory);
aoqi@0 309
aoqi@0 310 ErrorReceiverFilter controller = new ErrorReceiverFilter(receiver);
aoqi@0 311 validator.setErrorHandler(controller);
aoqi@0 312 XMLReader reader = pf.newSAXParser().getXMLReader();
aoqi@0 313 reader.setErrorHandler(controller);
aoqi@0 314
aoqi@0 315 DTDExtensionBindingChecker checker = new DTDExtensionBindingChecker("", model.options, controller);
aoqi@0 316 checker.setContentHandler(validator);
aoqi@0 317
aoqi@0 318 reader.setContentHandler(new ForkContentHandler(checker,builder));
aoqi@0 319
aoqi@0 320 reader.parse(is);
aoqi@0 321
aoqi@0 322 if(controller.hadError()) throw new AbortException();
aoqi@0 323 return (Document)builder.getDOM();
aoqi@0 324 } catch( IOException e ) {
aoqi@0 325 receiver.error( new SAXParseException2(e.getMessage(),null,e) );
aoqi@0 326 } catch( SAXException e ) {
aoqi@0 327 receiver.error( new SAXParseException2(e.getMessage(),null,e) );
aoqi@0 328 } catch( ParserConfigurationException e ) {
aoqi@0 329 receiver.error( new SAXParseException2(e.getMessage(),null,e) );
aoqi@0 330 }
aoqi@0 331
aoqi@0 332 throw new AbortException();
aoqi@0 333 }
aoqi@0 334 }

mercurial