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

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

author
alanb
date
Tue, 09 Apr 2013 14:51:13 +0100
changeset 368
0989ad8c0860
parent 286
f50545b5e2f1
child 637
9c07ef4934dd
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

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

mercurial