001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.tools.xjc.model;
038:
039: import java.util.Collection;
040: import java.util.Collections;
041: import java.util.HashSet;
042: import java.util.List;
043: import java.util.Set;
044:
045: import javax.xml.bind.JAXBElement;
046: import javax.xml.bind.annotation.XmlElement;
047: import javax.xml.namespace.QName;
048:
049: import com.sun.codemodel.JPackage;
050: import com.sun.codemodel.JType;
051: import com.sun.istack.Nullable;
052: import static com.sun.tools.xjc.model.CElementPropertyInfo.CollectionMode.NOT_REPEATED;
053: import static com.sun.tools.xjc.model.CElementPropertyInfo.CollectionMode.REPEATED_VALUE;
054: import com.sun.tools.xjc.model.nav.NClass;
055: import com.sun.tools.xjc.model.nav.NType;
056: import com.sun.tools.xjc.model.nav.NavigatorImpl;
057: import com.sun.tools.xjc.outline.Aspect;
058: import com.sun.tools.xjc.outline.Outline;
059: import com.sun.tools.xjc.reader.xmlschema.bindinfo.BIInlineBinaryData;
060: import com.sun.tools.xjc.reader.xmlschema.bindinfo.BIFactoryMethod;
061: import com.sun.tools.xjc.reader.xmlschema.BGMBuilder;
062: import com.sun.tools.xjc.reader.Ring;
063: import com.sun.xml.bind.v2.model.core.ElementInfo;
064: import com.sun.xml.xsom.XSElementDecl;
065: import com.sun.xml.xsom.XmlString;
066:
067: import org.xml.sax.Locator;
068:
069: /**
070: * {@link ElementInfo} implementation for the compile-time model.
071: *
072: * <p>
073: * As an NType, it represents the Java representation of this element
074: * (either JAXBElement<T> or Foo).
075: *
076: * @author Kohsuke Kawaguchi
077: */
078: public final class CElementInfo extends AbstractCElement implements
079: ElementInfo<NType, NClass>, NType, CClassInfoParent {
080:
081: private final QName tagName;
082:
083: /**
084: * Represents {@code JAXBElement<ContentType>}.
085: */
086: private NType type;
087:
088: /**
089: * If this element produces its own class, the short name of that class.
090: * Otherwise null.
091: */
092: private String className;
093:
094: /**
095: * If this element is global, the element info is considered to be
096: * package-level, and this points to the package in which this element
097: * lives in.
098: *
099: * <p>
100: * For local elements, this points to the parent {@link CClassInfo}.
101: */
102: public final CClassInfoParent parent;
103:
104: private CElementInfo substitutionHead;
105:
106: /**
107: * Lazily computed.
108: */
109: private Set<CElementInfo> substitutionMembers;
110:
111: /**
112: * {@link Model} that owns this object.
113: */
114: private final Model model;
115:
116: private CElementPropertyInfo property;
117:
118: /**
119: * Custom {@link #getSqueezedName() squeezed name}, if any.
120: */
121: private/*almost final*/@Nullable
122: String squeezedName;
123:
124: /**
125: * Creates an element in the given parent.
126: *
127: * <p>
128: * When using this construction, {@link #initContentType(TypeUse, XSElementDecl, XmlString)}
129: * must not be invoked.
130: */
131: public CElementInfo(Model model, QName tagName,
132: CClassInfoParent parent, TypeUse contentType,
133: XmlString defaultValue, XSElementDecl source,
134: CCustomizations customizations, Locator location) {
135: super (model, source, location, customizations);
136: this .tagName = tagName;
137: this .model = model;
138: this .parent = parent;
139: if (contentType != null)
140: initContentType(contentType, source, defaultValue);
141:
142: model.add(this );
143: }
144:
145: /**
146: * Creates an element with a class in the given parent.
147: *
148: * <p>
149: * When using this construction, the caller must use
150: * {@link #initContentType(TypeUse, XSElementDecl, XmlString)} to fill in the content type
151: * later.
152: *
153: * This is to avoid a circular model construction dependency between buidling a type
154: * inside an element and element itself. To build a content type, you need to have
155: * {@link CElementInfo} for a parent, so we can't take it as a constructor parameter.
156: */
157: public CElementInfo(Model model, QName tagName,
158: CClassInfoParent parent, String className,
159: CCustomizations customizations, Locator location) {
160: this (model, tagName, parent, null, null, null, customizations,
161: location);
162: this .className = className;
163: }
164:
165: public void initContentType(TypeUse contentType, @Nullable
166: XSElementDecl source, XmlString defaultValue) {
167: assert this .property == null; // must not be called twice
168:
169: this .property = new CElementPropertyInfo("Value", contentType
170: .isCollection() ? REPEATED_VALUE : NOT_REPEATED,
171: contentType.idUse(), contentType.getExpectedMimeType(),
172: source, null, getLocator(), true);
173: this .property.setAdapter(contentType.getAdapterUse());
174: BIInlineBinaryData.handle(source, property);
175: property.getTypes()
176: .add(
177: new CTypeRef(contentType.getInfo(), tagName,
178: CTypeRef.getSimpleTypeName(source),
179: true, defaultValue));
180: this .type = NavigatorImpl.createParameterizedType(
181: NavigatorImpl.theInstance.ref(JAXBElement.class),
182: getContentInMemoryType());
183:
184: BIFactoryMethod factoryMethod = Ring.get(BGMBuilder.class)
185: .getBindInfo(source).get(BIFactoryMethod.class);
186: if (factoryMethod != null) {
187: factoryMethod.markAsAcknowledged();
188: this .squeezedName = factoryMethod.name;
189: }
190:
191: }
192:
193: public final String getDefaultValue() {
194: return getProperty().getTypes().get(0).getDefaultValue();
195: }
196:
197: public final JPackage _package() {
198: return parent.getOwnerPackage();
199: }
200:
201: public CNonElement getContentType() {
202: return getProperty().ref().get(0);
203: }
204:
205: public NType getContentInMemoryType() {
206: if (getProperty().getAdapter() == null) {
207: NType itemType = getContentType().getType();
208: if (!property.isCollection())
209: return itemType;
210:
211: return NavigatorImpl.createParameterizedType(List.class,
212: itemType);
213: } else {
214: return getProperty().getAdapter().customType;
215: }
216: }
217:
218: public CElementPropertyInfo getProperty() {
219: return property;
220: }
221:
222: public CClassInfo getScope() {
223: if (parent instanceof CClassInfo)
224: return (CClassInfo) parent;
225: return null;
226: }
227:
228: /**
229: * @deprecated why are you calling a method that returns this?
230: */
231: public NType getType() {
232: return this ;
233: }
234:
235: public QName getElementName() {
236: return tagName;
237: }
238:
239: public JType toType(Outline o, Aspect aspect) {
240: if (className == null)
241: return type.toType(o, aspect);
242: else
243: return o.getElement(this ).implClass;
244: }
245:
246: /**
247: * Returns the "squeezed name" of this element.
248: *
249: * @see CClassInfo#getSqueezedName()
250: */
251: @XmlElement
252: public String getSqueezedName() {
253: if (squeezedName != null)
254: return squeezedName;
255:
256: StringBuilder b = new StringBuilder();
257: CClassInfo s = getScope();
258: if (s != null)
259: b.append(s.getSqueezedName());
260: if (className != null)
261: b.append(className);
262: else
263: b.append(model.getNameConverter().toClassName(
264: tagName.getLocalPart()));
265: return b.toString();
266: }
267:
268: public CElementInfo getSubstitutionHead() {
269: return substitutionHead;
270: }
271:
272: public Collection<CElementInfo> getSubstitutionMembers() {
273: if (substitutionMembers == null)
274: return Collections.emptyList();
275: else
276: return substitutionMembers;
277: }
278:
279: public void setSubstitutionHead(CElementInfo substitutionHead) {
280: // don't set it twice
281: assert this .substitutionHead == null;
282: assert substitutionHead != null;
283: this .substitutionHead = substitutionHead;
284:
285: if (substitutionHead.substitutionMembers == null)
286: substitutionHead.substitutionMembers = new HashSet<CElementInfo>();
287: substitutionHead.substitutionMembers.add(this );
288: }
289:
290: public boolean isBoxedType() {
291: return false;
292: }
293:
294: public String fullName() {
295: if (className == null)
296: return type.fullName();
297: else {
298: String r = parent.fullName();
299: if (r.length() == 0)
300: return className;
301: else
302: return r + '.' + className;
303: }
304: }
305:
306: public <T> T accept(Visitor<T> visitor) {
307: return visitor.onElement(this );
308: }
309:
310: public JPackage getOwnerPackage() {
311: return parent.getOwnerPackage();
312: }
313:
314: public String shortName() {
315: return className;
316: }
317:
318: /**
319: * True if this element has its own class
320: * (as opposed to be represented as an instance of {@link JAXBElement}.
321: */
322: public boolean hasClass() {
323: return className != null;
324: }
325: }
|