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;
325N/A
325N/Aimport java.util.ArrayList;
325N/Aimport java.util.HashSet;
325N/Aimport java.util.List;
325N/Aimport java.util.Set;
325N/A
325N/Aimport javax.xml.namespace.QName;
325N/A
325N/Aimport com.sun.tools.internal.xjc.model.CBuiltinLeafInfo;
325N/Aimport com.sun.tools.internal.xjc.model.CClassInfo;
325N/Aimport com.sun.tools.internal.xjc.model.CElementPropertyInfo;
325N/Aimport static com.sun.tools.internal.xjc.model.CElementPropertyInfo.CollectionMode.*;
325N/Aimport com.sun.tools.internal.xjc.model.CPropertyInfo;
325N/Aimport com.sun.tools.internal.xjc.model.CReferencePropertyInfo;
325N/Aimport com.sun.tools.internal.xjc.model.CTypeRef;
325N/Aimport com.sun.tools.internal.xjc.model.CValuePropertyInfo;
325N/Aimport com.sun.tools.internal.xjc.model.TypeUse;
325N/Aimport com.sun.tools.internal.xjc.reader.dtd.bindinfo.BIConversion;
325N/Aimport com.sun.tools.internal.xjc.reader.dtd.bindinfo.BIElement;
325N/Aimport com.sun.xml.internal.bind.v2.model.core.ID;
325N/Aimport com.sun.xml.internal.bind.v2.model.core.WildcardMode;
325N/Aimport com.sun.xml.internal.dtdparser.DTDEventListener;
325N/A
325N/Aimport org.xml.sax.Locator;
325N/A
325N/A/**
325N/A * DTD Element.
325N/A *
325N/A * <p>
325N/A * This class extends {@link Term} to participate in the content model tree.
325N/A *
325N/A * <p>
325N/A * This class is repsonsible for binding the element.
325N/A *
325N/A * @author Kohsuke Kawaguchi
325N/A */
325N/Afinal class Element extends Term implements Comparable<Element> {
325N/A
325N/A /**
325N/A * Name of the element.
325N/A */
325N/A final String name;
325N/A
325N/A private final TDTDReader owner;
325N/A
325N/A /**
325N/A * @see DTDEventListener#endContentModel(String, short)
325N/A */
325N/A private short contentModelType;
325N/A
325N/A private Term contentModel;
325N/A
325N/A /**
325N/A * True if this element is referenced from another element.
325N/A */
325N/A boolean isReferenced;
325N/A
325N/A /**
325N/A * If this element maps to a class, that class representation.
325N/A * Otherwise null.
325N/A */
325N/A private CClassInfo classInfo;
325N/A
325N/A /**
325N/A * True if {@link #classInfo} field is computed.
325N/A */
325N/A private boolean classInfoComputed;
325N/A
325N/A /**
325N/A * List of attribute properties on this element
325N/A */
325N/A final List<CPropertyInfo> attributes = new ArrayList<CPropertyInfo>();
325N/A
325N/A /**
325N/A * Normalized blocks of the content model.
325N/A */
325N/A private final List<Block> normalizedBlocks = new ArrayList<Block>();
325N/A
325N/A /**
325N/A * True if this element needs to be a class.
325N/A *
325N/A * Currently, if an element is referenced from a construct like (A|B|C),
325N/A * we require those A,B, and C to be a class.
325N/A */
325N/A private boolean mustBeClass;
325N/A
325N/A /**
325N/A * The source location where this element is defined.
325N/A */
325N/A private Locator locator;
325N/A
325N/A public Element(TDTDReader owner,String name) {
325N/A this.owner = owner;
325N/A this.name = name;
325N/A }
325N/A
325N/A void normalize(List<Block> r, boolean optional) {
325N/A Block o = new Block(optional,false);
325N/A o.elements.add(this);
325N/A r.add(o);
325N/A }
325N/A
325N/A void addAllElements(Block b) {
325N/A b.elements.add(this);
325N/A }
325N/A
325N/A boolean isOptional() {
325N/A return false;
325N/A }
325N/A
325N/A boolean isRepeated() {
325N/A return false;
325N/A }
325N/A
325N/A
325N/A /**
325N/A * Define its content model.
325N/A */
325N/A void define(short contentModelType, Term contentModel, Locator locator) {
325N/A assert this.contentModel==null; // may not be called twice
325N/A this.contentModelType = contentModelType;
325N/A this.contentModel = contentModel;
325N/A this.locator = locator;
325N/A contentModel.normalize(normalizedBlocks,false);
325N/A
325N/A for( Block b : normalizedBlocks ) {
325N/A if(b.isRepeated || b.elements.size()>1) {
325N/A for( Element e : b.elements ) {
325N/A owner.getOrCreateElement(e.name).mustBeClass = true;
325N/A }
325N/A }
325N/A }
325N/A }
325N/A
325N/A /**
325N/A * When this element is an PCDATA-only content model,
325N/A * returns the conversion for it. Otherwise the behavior is undefined.
325N/A */
325N/A private TypeUse getConversion() {
325N/A assert contentModel == Term.EMPTY; // this is PCDATA-only element
325N/A
325N/A BIElement e = owner.bindInfo.element(name);
325N/A if(e!=null) {
325N/A BIConversion conv = e.getConversion();
325N/A if(conv!=null)
325N/A return conv.getTransducer();
325N/A }
325N/A return CBuiltinLeafInfo.STRING;
325N/A }
325N/A
325N/A /**
325N/A * Return null if this class is not bound to a class.
325N/A */
325N/A CClassInfo getClassInfo() {
325N/A if(!classInfoComputed) {
325N/A classInfoComputed = true;
325N/A classInfo = calcClass();
325N/A }
325N/A return classInfo;
325N/A }
325N/A
325N/A private CClassInfo calcClass() {
325N/A BIElement e = owner.bindInfo.element(name);
325N/A if(e==null) {
325N/A if(contentModelType!=DTDEventListener.CONTENT_MODEL_MIXED
325N/A || !attributes.isEmpty()
325N/A || mustBeClass)
325N/A return createDefaultClass();
325N/A if(contentModel!=Term.EMPTY) {
325N/A throw new UnsupportedOperationException("mixed content model not supported");
325N/A } else {
325N/A // just #PCDATA
325N/A if(isReferenced)
325N/A return null;
325N/A else
325N/A // if no one else is referencing, assumed to be the root.
325N/A return createDefaultClass();
325N/A }
325N/A } else {
325N/A return e.clazz;
325N/A }
325N/A }
325N/A
325N/A private CClassInfo createDefaultClass() {
325N/A String className = owner.model.getNameConverter().toClassName(name);
325N/A QName tagName = new QName("",name);
325N/A
325N/A return new CClassInfo(owner.model,owner.getTargetPackage(),className,locator,null,tagName,null,null/*TODO*/);
325N/A }
325N/A
325N/A void bind() {
325N/A CClassInfo ci = getClassInfo();
325N/A assert ci!=null || attributes.isEmpty();
325N/A for( CPropertyInfo p : attributes )
325N/A ci.addProperty(p);
325N/A
325N/A switch(contentModelType) {
325N/A case DTDEventListener.CONTENT_MODEL_ANY:
325N/A CReferencePropertyInfo rp = new CReferencePropertyInfo("Content",true,false,true,null,null/*TODO*/,locator, false, false, false);
325N/A rp.setWildcard(WildcardMode.SKIP);
325N/A ci.addProperty(rp);
325N/A return;
325N/A case DTDEventListener.CONTENT_MODEL_CHILDREN:
325N/A break; // handling follows
325N/A case DTDEventListener.CONTENT_MODEL_MIXED:
325N/A if(contentModel!=Term.EMPTY)
325N/A throw new UnsupportedOperationException("mixed content model unsupported yet");
325N/A
325N/A if(ci!=null) {
325N/A // if this element is mapped to a class, just put one property
325N/A CValuePropertyInfo p = new CValuePropertyInfo("value", null,null/*TODO*/,locator,getConversion(),null);
325N/A ci.addProperty(p);
325N/A }
325N/A return;
325N/A case DTDEventListener.CONTENT_MODEL_EMPTY:
325N/A // no content model
325N/A assert ci!=null;
325N/A return;
325N/A }
325N/A
325N/A // normalize
325N/A List<Block> n = new ArrayList<Block>();
325N/A contentModel.normalize(n,false);
325N/A
325N/A {// check collision among Blocks
325N/A Set<String> names = new HashSet<String>();
325N/A boolean collision = false;
325N/A
325N/A OUTER:
325N/A for( Block b : n )
325N/A for( Element e : b.elements )
325N/A if(!names.add(e.name)) {
325N/A collision = true;
325N/A break OUTER;
325N/A }
325N/A
325N/A if(collision) {
325N/A // collapse all blocks into one
325N/A Block all = new Block(true,true);
325N/A for( Block b : n )
325N/A all.elements.addAll(b.elements);
325N/A n.clear();
325N/A n.add(all);
325N/A }
325N/A }
325N/A
325N/A for( Block b : n ) {
325N/A CElementPropertyInfo p;
325N/A if(b.isRepeated || b.elements.size()>1) {
325N/A // collection
325N/A StringBuilder name = new StringBuilder();
325N/A for( Element e : b.elements ) {
325N/A if(name.length()>0)
325N/A name.append("Or");
325N/A name.append(owner.model.getNameConverter().toPropertyName(e.name));
325N/A }
325N/A p = new CElementPropertyInfo(name.toString(), REPEATED_ELEMENT, ID.NONE, null, null,null/*TODO*/, locator, !b.isOptional );
325N/A for( Element e : b.elements ) {
325N/A CClassInfo child = owner.getOrCreateElement(e.name).getClassInfo();
325N/A assert child!=null; // we are requiring them to be classes.
325N/A p.getTypes().add(new CTypeRef(child,new QName("",e.name),null,false,null));
325N/A }
325N/A } else {
325N/A // single property
325N/A String name = b.elements.iterator().next().name;
325N/A String propName = owner.model.getNameConverter().toPropertyName(name);
325N/A
325N/A TypeUse refType;
325N/A Element ref = owner.getOrCreateElement(name);
325N/A if(ref.getClassInfo()!=null)
325N/A refType = ref.getClassInfo();
325N/A else {
325N/A refType = ref.getConversion().getInfo();
325N/A }
325N/A
325N/A p = new CElementPropertyInfo(propName,
325N/A refType.isCollection()?REPEATED_VALUE:NOT_REPEATED, ID.NONE, null, null,null/*TODO*/, locator, !b.isOptional );
325N/A
325N/A p.getTypes().add(new CTypeRef(refType.getInfo(),new QName("",name),null,false,null));
325N/A }
325N/A ci.addProperty(p);
325N/A }
325N/A }
325N/A
325N/A public int compareTo(Element that) {
325N/A return this.name.compareTo(that.name);
325N/A }
325N/A}