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.HashMap;
040: import java.util.Map;
041:
042: import javax.xml.bind.annotation.XmlAnyElement;
043: import javax.xml.namespace.QName;
044:
045: import com.sun.tools.xjc.reader.gbind.Choice;
046: import com.sun.tools.xjc.reader.gbind.Element;
047: import com.sun.tools.xjc.reader.gbind.Expression;
048: import com.sun.tools.xjc.reader.gbind.OneOrMore;
049: import com.sun.tools.xjc.reader.gbind.Sequence;
050: import com.sun.xml.xsom.XSElementDecl;
051: import com.sun.xml.xsom.XSModelGroup;
052: import com.sun.xml.xsom.XSModelGroupDecl;
053: import com.sun.xml.xsom.XSParticle;
054: import com.sun.xml.xsom.XSWildcard;
055: import com.sun.xml.xsom.visitor.XSTermFunction;
056:
057: /**
058: * Visits {@link XSParticle} and creates a corresponding {@link Expression} tree.
059: * @author Kohsuke Kawaguchi
060: */
061: public final class ExpressionBuilder implements
062: XSTermFunction<Expression> {
063:
064: public static Expression createTree(XSParticle p) {
065: return new ExpressionBuilder().particle(p);
066: }
067:
068: private ExpressionBuilder() {
069: }
070:
071: /**
072: * Wildcard instance needs to be consolidated to one,
073: * and this is such instance (if any.)
074: */
075: private GWildcardElement wildcard = null;
076:
077: private final Map<QName, GElementImpl> decls = new HashMap<QName, GElementImpl>();
078:
079: private XSParticle current;
080:
081: /**
082: * We can only have one {@link XmlAnyElement} property,
083: * so all the wildcards need to be treated as one node.
084: */
085: public Expression wildcard(XSWildcard wc) {
086: if (wildcard == null)
087: wildcard = new GWildcardElement();
088: wildcard.merge(wc);
089: wildcard.particles.add(current);
090: return wildcard;
091: }
092:
093: public Expression modelGroupDecl(XSModelGroupDecl decl) {
094: return modelGroup(decl.getModelGroup());
095: }
096:
097: public Expression modelGroup(XSModelGroup group) {
098: XSModelGroup.Compositor comp = group.getCompositor();
099: if (comp == XSModelGroup.CHOICE) {
100: // empty choice is not epsilon, but empty set,
101: // so this initial value is incorrect. But this
102: // kinda works.
103: // properly handling empty set requires more work.
104: Expression e = Expression.EPSILON;
105: for (XSParticle p : group.getChildren()) {
106: if (e == null)
107: e = particle(p);
108: else
109: e = new Choice(e, particle(p));
110: }
111: return e;
112: } else {
113: Expression e = Expression.EPSILON;
114: for (XSParticle p : group.getChildren()) {
115: if (e == null)
116: e = particle(p);
117: else
118: e = new Sequence(e, particle(p));
119: }
120: return e;
121: }
122: }
123:
124: public Element elementDecl(XSElementDecl decl) {
125: QName n = BGMBuilder.getName(decl);
126:
127: GElementImpl e = decls.get(n);
128: if (e == null)
129: decls.put(n, e = new GElementImpl(n, decl));
130:
131: e.particles.add(current);
132: assert current.getTerm() == decl;
133:
134: return e;
135: }
136:
137: public Expression particle(XSParticle p) {
138: current = p;
139: Expression e = p.getTerm().apply(this );
140:
141: if (p.isRepeated())
142: e = new OneOrMore(e);
143:
144: if (p.getMinOccurs() == 0)
145: e = new Choice(e, Expression.EPSILON);
146:
147: return e;
148: }
149:
150: }
|