aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2011, 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.model; aoqi@0: aoqi@0: import java.util.Collection; aoqi@0: import java.util.Collections; aoqi@0: import java.util.HashSet; aoqi@0: import java.util.List; aoqi@0: import java.util.Set; aoqi@0: aoqi@0: import javax.xml.bind.JAXBElement; aoqi@0: import javax.xml.bind.annotation.XmlElement; aoqi@0: import javax.xml.namespace.QName; aoqi@0: aoqi@0: import com.sun.codemodel.internal.JPackage; aoqi@0: import com.sun.codemodel.internal.JType; aoqi@0: import com.sun.istack.internal.Nullable; aoqi@0: import static com.sun.tools.internal.xjc.model.CElementPropertyInfo.CollectionMode.NOT_REPEATED; aoqi@0: import static com.sun.tools.internal.xjc.model.CElementPropertyInfo.CollectionMode.REPEATED_VALUE; aoqi@0: import com.sun.tools.internal.xjc.model.nav.NClass; aoqi@0: import com.sun.tools.internal.xjc.model.nav.NType; aoqi@0: import com.sun.tools.internal.xjc.model.nav.NavigatorImpl; aoqi@0: import com.sun.tools.internal.xjc.outline.Aspect; aoqi@0: import com.sun.tools.internal.xjc.outline.Outline; aoqi@0: import com.sun.tools.internal.xjc.reader.xmlschema.bindinfo.BIInlineBinaryData; aoqi@0: import com.sun.tools.internal.xjc.reader.xmlschema.bindinfo.BIFactoryMethod; aoqi@0: import com.sun.tools.internal.xjc.reader.xmlschema.BGMBuilder; aoqi@0: import com.sun.tools.internal.xjc.reader.Ring; aoqi@0: import com.sun.xml.internal.bind.v2.model.core.ElementInfo; aoqi@0: import com.sun.xml.internal.xsom.XSElementDecl; aoqi@0: import com.sun.xml.internal.xsom.XmlString; aoqi@0: aoqi@0: import org.xml.sax.Locator; aoqi@0: aoqi@0: /** aoqi@0: * {@link ElementInfo} implementation for the compile-time model. aoqi@0: * aoqi@0: *

aoqi@0: * As an NType, it represents the Java representation of this element aoqi@0: * (either JAXBElement<T> or Foo). aoqi@0: * aoqi@0: * @author Kohsuke Kawaguchi aoqi@0: */ aoqi@0: public final class CElementInfo extends AbstractCElement aoqi@0: implements ElementInfo, NType, CClassInfoParent { aoqi@0: aoqi@0: private final QName tagName; aoqi@0: aoqi@0: /** aoqi@0: * Represents {@code JAXBElement<ContentType>}. aoqi@0: */ aoqi@0: private NType type; aoqi@0: aoqi@0: /** aoqi@0: * If this element produces its own class, the short name of that class. aoqi@0: * Otherwise null. aoqi@0: */ aoqi@0: private String className; aoqi@0: aoqi@0: /** aoqi@0: * If this element is global, the element info is considered to be aoqi@0: * package-level, and this points to the package in which this element aoqi@0: * lives in. aoqi@0: * aoqi@0: *

aoqi@0: * For local elements, this points to the parent {@link CClassInfo}. aoqi@0: */ aoqi@0: public final CClassInfoParent parent; aoqi@0: aoqi@0: private CElementInfo substitutionHead; aoqi@0: aoqi@0: /** aoqi@0: * Lazily computed. aoqi@0: */ aoqi@0: private Set substitutionMembers; aoqi@0: aoqi@0: /** aoqi@0: * {@link Model} that owns this object. aoqi@0: */ aoqi@0: private final Model model; aoqi@0: aoqi@0: private CElementPropertyInfo property; aoqi@0: aoqi@0: /** aoqi@0: * Custom {@link #getSqueezedName() squeezed name}, if any. aoqi@0: */ aoqi@0: private /*almost final*/ @Nullable String squeezedName; aoqi@0: aoqi@0: /** aoqi@0: * Creates an element in the given parent. aoqi@0: * aoqi@0: *

aoqi@0: * When using this construction, {@link #initContentType(TypeUse, XSElementDecl, XmlString)} aoqi@0: * must not be invoked. aoqi@0: */ aoqi@0: public CElementInfo(Model model,QName tagName, CClassInfoParent parent, TypeUse contentType, XmlString defaultValue, XSElementDecl source, CCustomizations customizations, Locator location ) { aoqi@0: super(model,source,location,customizations); aoqi@0: this.tagName = tagName; aoqi@0: this.model = model; aoqi@0: this.parent = parent; aoqi@0: if(contentType!=null) aoqi@0: initContentType(contentType, source, defaultValue); aoqi@0: aoqi@0: model.add(this); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Creates an element with a class in the given parent. aoqi@0: * aoqi@0: *

aoqi@0: * When using this construction, the caller must use aoqi@0: * {@link #initContentType(TypeUse, XSElementDecl, XmlString)} to fill in the content type aoqi@0: * later. aoqi@0: * aoqi@0: * This is to avoid a circular model construction dependency between buidling a type aoqi@0: * inside an element and element itself. To build a content type, you need to have aoqi@0: * {@link CElementInfo} for a parent, so we can't take it as a constructor parameter. aoqi@0: */ aoqi@0: public CElementInfo(Model model,QName tagName, CClassInfoParent parent, String className, CCustomizations customizations, Locator location ) { aoqi@0: this(model,tagName,parent,null,null,null,customizations,location); aoqi@0: this.className = className; aoqi@0: } aoqi@0: aoqi@0: public void initContentType(TypeUse contentType, @Nullable XSElementDecl source, XmlString defaultValue) { aoqi@0: assert this.property==null; // must not be called twice aoqi@0: aoqi@0: this.property = new CElementPropertyInfo("Value", aoqi@0: contentType.isCollection()?REPEATED_VALUE:NOT_REPEATED, aoqi@0: contentType.idUse(), aoqi@0: contentType.getExpectedMimeType(), aoqi@0: source,null,getLocator(),true); aoqi@0: this.property.setAdapter(contentType.getAdapterUse()); aoqi@0: BIInlineBinaryData.handle(source,property); aoqi@0: property.getTypes().add(new CTypeRef(contentType.getInfo(),tagName,CTypeRef.getSimpleTypeName(source), true,defaultValue)); aoqi@0: this.type = NavigatorImpl.createParameterizedType( aoqi@0: NavigatorImpl.theInstance.ref(JAXBElement.class), aoqi@0: getContentInMemoryType() ); aoqi@0: aoqi@0: BIFactoryMethod factoryMethod = Ring.get(BGMBuilder.class).getBindInfo(source).get(BIFactoryMethod.class); aoqi@0: if(factoryMethod!=null) { aoqi@0: factoryMethod.markAsAcknowledged(); aoqi@0: this.squeezedName = factoryMethod.name; aoqi@0: } aoqi@0: aoqi@0: } aoqi@0: aoqi@0: public final String getDefaultValue() { aoqi@0: return getProperty().getTypes().get(0).getDefaultValue(); aoqi@0: } aoqi@0: aoqi@0: public final JPackage _package() { aoqi@0: return parent.getOwnerPackage(); aoqi@0: } aoqi@0: aoqi@0: public CNonElement getContentType() { aoqi@0: return getProperty().ref().get(0); aoqi@0: } aoqi@0: aoqi@0: public NType getContentInMemoryType() { aoqi@0: if(getProperty().getAdapter()==null) { aoqi@0: NType itemType = getContentType().getType(); aoqi@0: if(!property.isCollection()) aoqi@0: return itemType; aoqi@0: aoqi@0: return NavigatorImpl.createParameterizedType(List.class,itemType); aoqi@0: } else { aoqi@0: return getProperty().getAdapter().customType; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public CElementPropertyInfo getProperty() { aoqi@0: return property; aoqi@0: } aoqi@0: aoqi@0: public CClassInfo getScope() { aoqi@0: if(parent instanceof CClassInfo) aoqi@0: return (CClassInfo)parent; aoqi@0: return null; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * @deprecated why are you calling a method that returns this? aoqi@0: */ aoqi@0: public NType getType() { aoqi@0: return this; aoqi@0: } aoqi@0: aoqi@0: public QName getElementName() { aoqi@0: return tagName; aoqi@0: } aoqi@0: aoqi@0: public JType toType(Outline o, Aspect aspect) { aoqi@0: if(className==null) aoqi@0: return type.toType(o,aspect); aoqi@0: else aoqi@0: return o.getElement(this).implClass; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Returns the "squeezed name" of this element. aoqi@0: * aoqi@0: * @see CClassInfo#getSqueezedName() aoqi@0: */ aoqi@0: @XmlElement aoqi@0: public String getSqueezedName() { aoqi@0: if(squeezedName!=null) return squeezedName; aoqi@0: aoqi@0: StringBuilder b = new StringBuilder(); aoqi@0: CClassInfo s = getScope(); aoqi@0: if(s!=null) aoqi@0: b.append(s.getSqueezedName()); aoqi@0: if(className!=null) aoqi@0: b.append(className); aoqi@0: else aoqi@0: b.append( model.getNameConverter().toClassName(tagName.getLocalPart())); aoqi@0: return b.toString(); aoqi@0: } aoqi@0: aoqi@0: public CElementInfo getSubstitutionHead() { aoqi@0: return substitutionHead; aoqi@0: } aoqi@0: aoqi@0: public Collection getSubstitutionMembers() { aoqi@0: if(substitutionMembers==null) aoqi@0: return Collections.emptyList(); aoqi@0: else aoqi@0: return substitutionMembers; aoqi@0: } aoqi@0: aoqi@0: public void setSubstitutionHead(CElementInfo substitutionHead) { aoqi@0: // don't set it twice aoqi@0: assert this.substitutionHead==null; aoqi@0: assert substitutionHead!=null; aoqi@0: this.substitutionHead = substitutionHead; aoqi@0: aoqi@0: if(substitutionHead.substitutionMembers==null) aoqi@0: substitutionHead.substitutionMembers = new HashSet(); aoqi@0: substitutionHead.substitutionMembers.add(this); aoqi@0: } aoqi@0: aoqi@0: public boolean isBoxedType() { aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: public String fullName() { aoqi@0: if(className==null) aoqi@0: return type.fullName(); aoqi@0: else { aoqi@0: String r = parent.fullName(); aoqi@0: if(r.length()==0) return className; aoqi@0: else return r+'.'+className; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public T accept(Visitor visitor) { aoqi@0: return visitor.onElement(this); aoqi@0: } aoqi@0: aoqi@0: public JPackage getOwnerPackage() { aoqi@0: return parent.getOwnerPackage(); aoqi@0: } aoqi@0: aoqi@0: public String shortName() { aoqi@0: return className; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * True if this element has its own class aoqi@0: * (as opposed to be represented as an instance of {@link JAXBElement}. aoqi@0: */ aoqi@0: public boolean hasClass() { aoqi@0: return className!=null; aoqi@0: } aoqi@0: }