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: package com.sun.tools.xjc.reader.dtd.bindinfo;
037:
038: import java.util.ArrayList;
039:
040: import com.sun.codemodel.JClass;
041: import com.sun.tools.xjc.Options;
042: import com.sun.tools.xjc.generator.bean.field.FieldRenderer;
043:
044: import org.w3c.dom.Element;
045:
046: /**
047: * Particles in the <content> declaration in the binding file.
048: *
049: */
050: public class BIContent {
051: /**
052: * Wraps a given particle.
053: *
054: * <p>
055: * This object should be created through
056: * the {@link #create(Element, BIElement)} method.
057: */
058: private BIContent(Element e, BIElement _parent) {
059: this .element = e;
060: this .parent = _parent;
061: this .opts = parent.parent.model.options;
062: }
063:
064: /** The particle element which this object is wrapping. */
065: protected final Element element;
066:
067: /** The parent object.*/
068: protected final BIElement parent;
069:
070: private final Options opts;
071:
072: /**
073: * Gets the realization of this particle, if any.
074: *
075: * @return
076: * null if the "collection" attribute was not specified.
077: */
078: public final FieldRenderer getRealization() {
079: String v = DOMUtil.getAttribute(element, "collection");
080: if (v == null)
081: return null;
082:
083: v = v.trim();
084: if (v.equals("array"))
085: return opts.getFieldRendererFactory().getArray();
086: if (v.equals("list"))
087: return opts.getFieldRendererFactory().getList(
088: parent.parent.codeModel.ref(ArrayList.class));
089:
090: // the correctness of the attribute value must be
091: // checked by the validator.
092: throw new InternalError("unexpected collection value: " + v);
093: }
094:
095: /**
096: * Gets the property name of this particle.
097: *
098: * @return
099: * always a non-null, valid string.
100: */
101: public final String getPropertyName() {
102: String r = DOMUtil.getAttribute(element, "property");
103:
104: // in case of <element-ref>, @property is optional and
105: // defaults to @name.
106: // in all other cases, @property is mandatory.
107: if (r != null)
108: return r;
109: return DOMUtil.getAttribute(element, "name");
110: }
111:
112: /**
113: * Gets the type of this property, if any.
114: * <p>
115: * <element-ref> particle doesn't have the type.
116: *
117: * @return
118: * null if none is specified.
119: */
120: public final JClass getType() {
121: try {
122: String type = DOMUtil.getAttribute(element, "supertype");
123: if (type == null)
124: return null;
125:
126: // TODO: does this attribute defaults to the current package?
127: int idx = type.lastIndexOf('.');
128: if (idx < 0)
129: return parent.parent.codeModel.ref(type);
130: else
131: return parent.parent.getTargetPackage().ref(type);
132: } catch (ClassNotFoundException e) {
133: // TODO: better error handling
134: throw new NoClassDefFoundError(e.getMessage());
135: }
136: }
137:
138: /**
139: * Creates an appropriate subclass of BIContent
140: * by sniffing the tag name.
141: * <p>
142: * This method should be only called by the BIElement class.
143: */
144: static BIContent create(Element e, BIElement _parent) {
145: return new BIContent(e, _parent);
146: }
147:
148: }
|