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.reader.xmlschema;
038:
039: import java.util.Collection;
040:
041: import com.sun.tools.xjc.model.CPropertyInfo;
042: import com.sun.tools.xjc.model.Multiplicity;
043: import com.sun.tools.xjc.reader.RawTypeSet;
044: import com.sun.tools.xjc.reader.gbind.ConnectedComponent;
045: import com.sun.tools.xjc.reader.gbind.Element;
046: import com.sun.tools.xjc.reader.gbind.Expression;
047: import com.sun.tools.xjc.reader.gbind.Graph;
048: import com.sun.tools.xjc.reader.xmlschema.bindinfo.BIProperty;
049: import com.sun.xml.bind.v2.model.core.WildcardMode;
050: import com.sun.xml.xsom.XSParticle;
051:
052: /**
053: * {@link ParticleBinder} that uses {@link ExpressionBuilder} et al
054: * for better, more intuitive (but non spec-conforming) binding.
055: *
056: * @author Kohsuke Kawaguchi
057: */
058: final class ExpressionParticleBinder extends ParticleBinder {
059: public void build(XSParticle p, Collection<XSParticle> forcedProps) {
060: // this class isn't about spec conformance, but
061: // for the ease of use.
062: // so we don't give a damn about 'forcedProps'.
063: // although, for a future note, it's conceivable to expand
064: // the binding algorithm to cover this notion.
065:
066: Expression tree = ExpressionBuilder.createTree(p);
067: Graph g = new Graph(tree);
068: for (ConnectedComponent cc : g) {
069: buildProperty(cc);
070: }
071: }
072:
073: /**
074: * Builds a property ouf ot a connected component.
075: */
076: private void buildProperty(ConnectedComponent cc) {
077: StringBuilder propName = new StringBuilder(); // property name
078: int nameTokenCount = 0; // combine only up to 3
079:
080: RawTypeSetBuilder rtsb = new RawTypeSetBuilder();
081: for (Element e : cc) {
082: GElement ge = (GElement) e;
083:
084: if (nameTokenCount < 3) {
085: if (nameTokenCount != 0)
086: propName.append("And");
087: propName.append(makeJavaName(cc.isCollection(), ge
088: .getPropertyNameSeed()));
089: nameTokenCount++;
090: }
091:
092: if (e instanceof GElementImpl) {
093: GElementImpl ei = (GElementImpl) e;
094: rtsb.elementDecl(ei.decl);
095: continue;
096: }
097: if (e instanceof GWildcardElement) {
098: GWildcardElement w = (GWildcardElement) e;
099: rtsb.getRefs().add(
100: new RawTypeSetBuilder.WildcardRef(
101: w.isStrict() ? WildcardMode.STRICT
102: : WildcardMode.SKIP));
103: continue;
104: }
105: assert false : e; // no other kind should be here
106: }
107:
108: Multiplicity m = Multiplicity.ONE;
109: if (cc.isCollection())
110: m = m.makeRepeated();
111: if (!cc.isRequired())
112: m = m.makeOptional();
113:
114: RawTypeSet rts = new RawTypeSet(rtsb.getRefs(), m);
115:
116: XSParticle p = findSourceParticle(cc);
117:
118: BIProperty cust = BIProperty.getCustomization(p);
119: CPropertyInfo prop = cust.createElementOrReferenceProperty(
120: propName.toString(), false, p, rts);
121: getCurrentBean().addProperty(prop);
122: }
123:
124: /**
125: * Finds a {@link XSParticle} that can serve as the representative property of
126: * the given {@link ConnectedComponent}.
127: *
128: * The representative property is used for reporting an error location and
129: * taking {@link BIProperty} customization. Right now, the algorithm is just pick
130: * the first one with {@link BIProperty}, but one can think of a better algorithm,
131: * such as taking a choice of (A|B) if CC={A,B}.
132: */
133: private XSParticle findSourceParticle(ConnectedComponent cc) {
134: XSParticle first = null;
135:
136: for (Element e : cc) {
137: GElement ge = (GElement) e;
138: for (XSParticle p : ge.particles) {
139: if (first == null)
140: first = p;
141: if (getLocalPropCustomization(p) != null)
142: return p;
143: }
144: // if there are multiple property customizations,
145: // all but one will be left unused, which will be detected as an error
146: // later, so no need to check that now.
147: }
148:
149: // if no customization was found, just use the first one.
150: return first;
151: }
152:
153: public boolean checkFallback(XSParticle p) {
154: // this algorithm never falls back to 'getContent'.
155: return false;
156: }
157: }
|