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.xmlschema;
037:
038: import java.text.ParseException;
039: import java.util.Collection;
040: import java.util.Collections;
041:
042: import com.sun.codemodel.JJavaName;
043: import com.sun.tools.xjc.model.CClassInfo;
044: import com.sun.tools.xjc.model.CPropertyInfo;
045: import com.sun.tools.xjc.reader.Ring;
046: import com.sun.tools.xjc.reader.xmlschema.bindinfo.BIDeclaration;
047: import com.sun.tools.xjc.reader.xmlschema.bindinfo.BIProperty;
048: import com.sun.xml.xsom.XSElementDecl;
049: import com.sun.xml.xsom.XSModelGroup;
050: import com.sun.xml.xsom.XSModelGroupDecl;
051: import com.sun.xml.xsom.XSParticle;
052: import com.sun.xml.xsom.XSTerm;
053: import com.sun.xml.xsom.XSWildcard;
054: import com.sun.xml.xsom.visitor.XSTermVisitor;
055:
056: /**
057: * Binds the content models of {@link XSParticle} as properties of the class that's being built.
058: *
059: * @author
060: * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
061: */
062: public abstract class ParticleBinder {
063:
064: protected final BGMBuilder builder = Ring.get(BGMBuilder.class);
065:
066: protected ParticleBinder() {
067: // make sure that this object is available as ParticleBinder, not as their actual implementation classes
068: Ring.add(ParticleBinder.class, this );
069: }
070:
071: /**
072: * Builds the {@link CPropertyInfo}s from the given particle
073: * (and its descendants), and set them to the class returned by
074: * {@link ClassSelector#getCurrentBean()}.
075: */
076: public final void build(XSParticle p) {
077: build(p, Collections.<XSParticle> emptySet());
078: }
079:
080: /**
081: * The version of the build method that forces a specified set of particles
082: * to become a property.
083: */
084: public abstract void build(XSParticle p,
085: Collection<XSParticle> forcedProps);
086:
087: /**
088: * Similar to the build method but this method only checks if
089: * the BGM that will be built by the build method will
090: * do the fallback (map all the properties into one list) or not.
091: *
092: * @return
093: * false if the fallback will not happen.
094: */
095: public abstract boolean checkFallback(XSParticle p);
096:
097: //
098: //
099: // convenient utility methods
100: //
101: //
102:
103: protected final CClassInfo getCurrentBean() {
104: return getClassSelector().getCurrentBean();
105: }
106:
107: /**
108: * Gets the BIProperty object that applies to the given particle.
109: */
110: protected final BIProperty getLocalPropCustomization(XSParticle p) {
111: return getLocalCustomization(p, BIProperty.class);
112: }
113:
114: protected final <T extends BIDeclaration> T getLocalCustomization(
115: XSParticle p, Class<T> type) {
116: // check the property customization of this component first
117: T cust = builder.getBindInfo(p).get(type);
118: if (cust != null)
119: return cust;
120:
121: // if not, the term might have one.
122: cust = builder.getBindInfo(p.getTerm()).get(type);
123: if (cust != null)
124: return cust;
125:
126: return null;
127: }
128:
129: /**
130: * Computes the label of a given particle.
131: * Usually, the getLabel method should be used instead.
132: */
133: protected final String computeLabel(XSParticle p) {
134: // if the particle carries a customization, use that value.
135: // since we are binding content models, it's always non-constant properties.
136: BIProperty cust = getLocalPropCustomization(p);
137: if (cust != null && cust.getPropertyName(false) != null)
138: return cust.getPropertyName(false);
139:
140: // no explicit property name is given. Compute one.
141:
142: XSTerm t = p.getTerm();
143:
144: // // first, check if a term is going to be a class, if so, use that name.
145: // ClassItem ci = owner.selector.select(t);
146: // if(ci!=null) {
147: // return makeJavaName(ci.getTypeAsDefined().name());
148: // }
149:
150: // if it fails, compute the default name according to the spec.
151: if (t.isElementDecl())
152: // for element, take the element name.
153: return makeJavaName(p, t.asElementDecl().getName());
154: if (t.isModelGroupDecl())
155: // for named model groups, take that name
156: return makeJavaName(p, t.asModelGroupDecl().getName());
157: if (t.isWildcard())
158: // the spec says it will map to "any" by default.
159: return makeJavaName(p, "Any");
160: if (t.isModelGroup()) {
161: try {
162: return getSpecDefaultName(t.asModelGroup(), p
163: .isRepeated());
164: } catch (ParseException e) {
165: // unable to generate a name.
166: getErrorReporter()
167: .error(
168: t.getLocator(),
169: Messages.ERR_UNABLE_TO_GENERATE_NAME_FROM_MODELGROUP);
170: return "undefined"; // recover from error by assuming something
171: }
172: }
173:
174: // there are only four types of XSTerm.
175: throw new AssertionError();
176: }
177:
178: /** Converts an XML name to the corresponding Java name. */
179: protected final String makeJavaName(boolean isRepeated,
180: String xmlName) {
181: String name = builder.getNameConverter()
182: .toPropertyName(xmlName);
183: if (builder.getGlobalBinding().isSimpleMode() && isRepeated)
184: name = JJavaName.getPluralForm(name);
185: return name;
186: }
187:
188: protected final String makeJavaName(XSParticle p, String xmlName) {
189: return makeJavaName(p.isRepeated(), xmlName);
190: }
191:
192: /**
193: * Computes a name from unnamed model group by following the spec.
194: *
195: * Taking first three elements and combine them.
196: *
197: * @param repeated
198: * if the said model group is repeated more than once
199: *
200: * @exception ParseException
201: * If the method cannot generate a name. For example, when
202: * a model group doesn't contain any element reference/declaration
203: * at all.
204: */
205: protected final String getSpecDefaultName(XSModelGroup mg,
206: final boolean repeated) throws ParseException {
207:
208: final StringBuilder name = new StringBuilder();
209:
210: mg.visit(new XSTermVisitor() {
211: /**
212: * Count the number of tokens we combined.
213: * We will concat up to 3.
214: */
215: private int count = 0;
216:
217: /**
218: * Is the current particple/term repeated?
219: */
220: private boolean rep = repeated;
221:
222: public void wildcard(XSWildcard wc) {
223: append("any");
224: }
225:
226: public void modelGroupDecl(XSModelGroupDecl mgd) {
227: modelGroup(mgd.getModelGroup());
228: }
229:
230: public void modelGroup(XSModelGroup mg) {
231: String operator;
232: if (mg.getCompositor() == XSModelGroup.CHOICE)
233: operator = "Or";
234: else
235: operator = "And";
236:
237: int size = mg.getSize();
238: for (int i = 0; i < size; i++) {
239: XSParticle p = mg.getChild(i);
240: boolean oldRep = rep;
241: rep |= p.isRepeated();
242: p.getTerm().visit(this );
243: rep = oldRep;
244:
245: if (count == 3)
246: return; // we have enough
247: if (i != size - 1)
248: name.append(operator);
249: }
250: }
251:
252: public void elementDecl(XSElementDecl ed) {
253: append(ed.getName());
254: }
255:
256: private void append(String token) {
257: if (count < 3) {
258: name.append(makeJavaName(rep, token));
259: count++;
260: }
261: }
262: });
263:
264: if (name.length() == 0)
265: throw new ParseException("no element", -1);
266:
267: return name.toString();
268: }
269:
270: protected final ErrorReporter getErrorReporter() {
271: return Ring.get(ErrorReporter.class);
272: }
273:
274: protected final ClassSelector getClassSelector() {
275: return Ring.get(ClassSelector.class);
276: }
277: }
|