325N/A/*
325N/A * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
325N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
325N/A *
325N/A * This code is free software; you can redistribute it and/or modify it
325N/A * under the terms of the GNU General Public License version 2 only, as
325N/A * published by the Free Software Foundation. Oracle designates this
325N/A * particular file as subject to the "Classpath" exception as provided
325N/A * by Oracle in the LICENSE file that accompanied this code.
325N/A *
325N/A * This code is distributed in the hope that it will be useful, but WITHOUT
325N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
325N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
325N/A * version 2 for more details (a copy is included in the LICENSE file that
325N/A * accompanied this code).
325N/A *
325N/A * You should have received a copy of the GNU General Public License version
325N/A * 2 along with this work; if not, write to the Free Software Foundation,
325N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
325N/A *
325N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
325N/A * or visit www.oracle.com if you need additional information or have any
325N/A * questions.
325N/A */
325N/A
325N/Apackage com.sun.tools.internal.xjc.reader.dtd.bindinfo;
325N/A
325N/Aimport java.util.ArrayList;
325N/Aimport java.util.HashMap;
325N/Aimport java.util.List;
325N/Aimport java.util.Map;
325N/A
325N/Aimport javax.xml.namespace.QName;
325N/A
325N/Aimport com.sun.tools.internal.xjc.model.CClassInfo;
325N/Aimport com.sun.xml.internal.bind.api.impl.NameConverter;
325N/A
325N/Aimport org.w3c.dom.Element;
325N/Aimport org.xml.sax.Locator;
325N/A
325N/A
325N/A/**
325N/A * <element> declaration in the binding file.
325N/A */
325N/Apublic final class BIElement
325N/A{
325N/A /**
325N/A * Wraps a given <element> element in the binding file.
325N/A *
325N/A * <p>
325N/A * Should be created only from {@link BindInfo}.
325N/A */
325N/A BIElement( BindInfo bi, Element _e ) {
325N/A this.parent = bi;
325N/A this.e = _e;
325N/A
325N/A {
325N/A Element c = DOMUtil.getElement(e,"content");
325N/A if(c!=null) {
325N/A if(DOMUtil.getAttribute(c,"property")!=null) {
325N/A // if @property is there, this is a general declaration
325N/A this.rest = BIContent.create(c,this);
325N/A } else {
325N/A // this must be a model-based declaration
325N/A for( Element p : DOMUtil.getChildElements(c) ) {
325N/A if(p.getLocalName().equals("rest"))
325N/A this.rest = BIContent.create(p,this);
325N/A else
325N/A this.contents.add(BIContent.create(p,this));
325N/A }
325N/A }
325N/A }
325N/A }
325N/A
325N/A // parse <attribute>s
325N/A for( Element atr : DOMUtil.getChildElements(e,"attribute") ) {
325N/A BIAttribute a = new BIAttribute( this, atr );
325N/A attributes.put(a.name(),a);
325N/A }
325N/A
325N/A if(isClass()) {
325N/A // if this is a class-declaration, create JClass object now
325N/A String className = DOMUtil.getAttribute(e,"class");
325N/A if(className==null)
325N/A // none was specified. infer the name.
325N/A className = NameConverter.standard.toClassName(name());
325N/A this.className = className;
325N/A } else {
325N/A // this is not an element-class declaration
325N/A className = null;
325N/A }
325N/A
325N/A // process conversion declarations
325N/A for( Element conv : DOMUtil.getChildElements(e,"conversion") ) {
325N/A BIConversion c = new BIUserConversion(bi,conv);
325N/A conversions.put(c.name(),c);
325N/A }
325N/A for( Element en : DOMUtil.getChildElements(e,"enumeration") ) {
325N/A BIConversion c = BIEnumeration.create(en,this);
325N/A conversions.put(c.name(),c);
325N/A }
325N/A
325N/A // parse <constructor>s
325N/A for( Element c : DOMUtil.getChildElements(e,"constructor") ) {
325N/A constructors.add( new BIConstructor(c) );
325N/A }
325N/A
325N/A String name = name();
325N/A QName tagName = new QName("",name);
325N/A
325N/A this.clazz = new CClassInfo(parent.model,parent.getTargetPackage(),className,getLocation(),null,tagName,null,null/*TODO*/);
325N/A }
325N/A
325N/A /**
325N/A * Gets the source location where this element is declared.
325N/A */
325N/A public Locator getLocation() {
325N/A return DOMLocator.getLocationInfo(e);
325N/A }
325N/A
325N/A
325N/A /** The parent {@link BindInfo} object to which this object belongs. */
325N/A final BindInfo parent;
325N/A
325N/A /** &lt;element> element which this object is wrapping. */
325N/A private final Element e;
325N/A
325N/A /**
325N/A * The bean representation for this element.
325N/A */
325N/A public final CClassInfo clazz;
325N/A
325N/A /**
325N/A * Content-property declarations.
325N/A * <p>
325N/A * This vector will be empty if no content-property declaration is made.
325N/A */
325N/A private final List<BIContent> contents = new ArrayList<BIContent>();
325N/A
325N/A /** Conversion declarations. */
325N/A private final Map<String,BIConversion> conversions = new HashMap<String,BIConversion>();
325N/A
325N/A /**
325N/A * The "rest" content-property declaration.
325N/A * <p>
325N/A * This field is null when there was no "rest" declaration.
325N/A */
325N/A private BIContent rest;
325N/A
325N/A /** Attribute-property declarations. */
325N/A private final Map<String,BIAttribute> attributes = new HashMap<String,BIAttribute>();
325N/A
325N/A /** Constructor declarations. */
325N/A private final List<BIConstructor> constructors = new ArrayList<BIConstructor>();
325N/A
325N/A /**
325N/A * the class which is generated by this declaration.
325N/A * This field will be null if this declaration is an element-property
325N/A * declaration.
325N/A */
325N/A private final String className;
325N/A
325N/A
325N/A
325N/A /** Gets the element name. */
325N/A public String name() { return DOMUtil.getAttribute(e,"name"); }
325N/A
325N/A /**
325N/A * Checks if the element type is "class".
325N/A * If false, that means this element will be a value.
325N/A */
325N/A public boolean isClass() {
325N/A return "class".equals(e.getAttribute("type"));
325N/A }
325N/A
325N/A /**
325N/A * Checks if this element is designated as a root element.
325N/A */
325N/A public boolean isRoot() {
325N/A return "true".equals(e.getAttribute("root"));
325N/A }
325N/A
325N/A /**
325N/A * Gets the JClass object that represents this declaration.
325N/A *
325N/A * <p>
325N/A * This method returns null if this declaration
325N/A * is an element-property declaration.
325N/A */
325N/A public String getClassName() {
325N/A return className;
325N/A }
325N/A
325N/A /**
325N/A * Creates constructor declarations for this element.
325N/A *
325N/A * <p>
325N/A * This method should only be called by DTDReader <b>after</b>
325N/A * the normalization has completed.
325N/A *
325N/A * @param src
325N/A * The ClassItem object that corresponds to this declaration
325N/A */
325N/A public void declareConstructors( CClassInfo src ) {
325N/A for( BIConstructor c : constructors )
325N/A c.createDeclaration(src);
325N/A }
325N/A
325N/A /**
325N/A * Gets the conversion method for this element.
325N/A *
325N/A * <p>
325N/A * This method can be called only when this element
325N/A * declaration is designated as element-value.
325N/A *
325N/A * @return
325N/A * If the convert attribute is not specified, this
325N/A * method returns null.
325N/A */
325N/A public BIConversion getConversion() {
325N/A String cnv = DOMUtil.getAttribute(e,"convert");
325N/A if(cnv==null) return null;
325N/A
325N/A return conversion(cnv);
325N/A }
325N/A
325N/A /**
325N/A * Resolves the conversion name to the conversion declaration.
325N/A *
325N/A * <p>
325N/A * Element-local declarations are checked first.
325N/A *
325N/A * @return
325N/A * A non-null valid BIConversion object.
325N/A */
325N/A public BIConversion conversion( String name ) {
325N/A BIConversion r = conversions.get(name);
325N/A if(r!=null) return r;
325N/A
325N/A // check the global conversion declarations
325N/A return parent.conversion(name);
325N/A }
325N/A
325N/A
325N/A /**
325N/A * Iterates all content-property declarations (except 'rest').
325N/A */
325N/A public List<BIContent> getContents() {
325N/A return contents;
325N/A }
325N/A
325N/A /**
325N/A * Gets the attribute-property declaration, if any.
325N/A *
325N/A * @return
325N/A * null if attribute declaration was not given by that name.
325N/A */
325N/A public BIAttribute attribute( String name ) {
325N/A return attributes.get(name);
325N/A }
325N/A
325N/A /**
325N/A * Gets the 'rest' content-property declaration, if any.
325N/A * @return
325N/A * if there is no 'rest' declaration, return null.
325N/A */
325N/A public BIContent getRest() { return this.rest; }
325N/A
325N/A /** Gets the location where this declaration is declared. */
325N/A public Locator getSourceLocation() {
325N/A return DOMLocator.getLocationInfo(e);
325N/A }
325N/A}