src/share/jaxws_classes/com/sun/tools/internal/xjc/reader/xmlschema/ExpressionBuilder.java

Tue, 09 Apr 2013 14:51:13 +0100

author
alanb
date
Tue, 09 Apr 2013 14:51:13 +0100
changeset 368
0989ad8c0860
parent 0
373ffda63c9a
permissions
-rw-r--r--

8010393: Update JAX-WS RI to 2.2.9-b12941
Reviewed-by: alanb, erikj
Contributed-by: miroslav.kos@oracle.com, martin.grebac@oracle.com

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 package com.sun.tools.internal.xjc.reader.xmlschema;
aoqi@0 27
aoqi@0 28 import java.util.HashMap;
aoqi@0 29 import java.util.Map;
aoqi@0 30
aoqi@0 31 import javax.xml.bind.annotation.XmlAnyElement;
aoqi@0 32 import javax.xml.namespace.QName;
aoqi@0 33
aoqi@0 34 import com.sun.tools.internal.xjc.reader.gbind.Choice;
aoqi@0 35 import com.sun.tools.internal.xjc.reader.gbind.Element;
aoqi@0 36 import com.sun.tools.internal.xjc.reader.gbind.Expression;
aoqi@0 37 import com.sun.tools.internal.xjc.reader.gbind.OneOrMore;
aoqi@0 38 import com.sun.tools.internal.xjc.reader.gbind.Sequence;
aoqi@0 39 import com.sun.xml.internal.xsom.XSElementDecl;
aoqi@0 40 import com.sun.xml.internal.xsom.XSModelGroup;
aoqi@0 41 import com.sun.xml.internal.xsom.XSModelGroupDecl;
aoqi@0 42 import com.sun.xml.internal.xsom.XSParticle;
aoqi@0 43 import com.sun.xml.internal.xsom.XSWildcard;
aoqi@0 44 import com.sun.xml.internal.xsom.visitor.XSTermFunction;
aoqi@0 45 import java.math.BigInteger;
aoqi@0 46
aoqi@0 47 /**
aoqi@0 48 * Visits {@link XSParticle} and creates a corresponding {@link Expression} tree.
aoqi@0 49 * @author Kohsuke Kawaguchi
aoqi@0 50 */
aoqi@0 51 public final class ExpressionBuilder implements XSTermFunction<Expression> {
aoqi@0 52
aoqi@0 53 public static Expression createTree(XSParticle p) {
aoqi@0 54 return new ExpressionBuilder().particle(p);
aoqi@0 55 }
aoqi@0 56
aoqi@0 57 private ExpressionBuilder() {}
aoqi@0 58
aoqi@0 59 /**
aoqi@0 60 * Wildcard instance needs to be consolidated to one,
aoqi@0 61 * and this is such instance (if any.)
aoqi@0 62 */
aoqi@0 63 private GWildcardElement wildcard = null;
aoqi@0 64
aoqi@0 65 private final Map<QName,GElementImpl> decls = new HashMap<QName,GElementImpl>();
aoqi@0 66
aoqi@0 67 private XSParticle current;
aoqi@0 68
aoqi@0 69 /**
aoqi@0 70 * We can only have one {@link XmlAnyElement} property,
aoqi@0 71 * so all the wildcards need to be treated as one node.
aoqi@0 72 */
aoqi@0 73 public Expression wildcard(XSWildcard wc) {
aoqi@0 74 if(wildcard==null)
aoqi@0 75 wildcard = new GWildcardElement();
aoqi@0 76 wildcard.merge(wc);
aoqi@0 77 wildcard.particles.add(current);
aoqi@0 78 return wildcard;
aoqi@0 79 }
aoqi@0 80
aoqi@0 81 public Expression modelGroupDecl(XSModelGroupDecl decl) {
aoqi@0 82 return modelGroup(decl.getModelGroup());
aoqi@0 83 }
aoqi@0 84
aoqi@0 85 public Expression modelGroup(XSModelGroup group) {
aoqi@0 86 XSModelGroup.Compositor comp = group.getCompositor();
aoqi@0 87 if(comp==XSModelGroup.CHOICE) {
aoqi@0 88 // empty choice is not epsilon, but empty set,
aoqi@0 89 // so this initial value is incorrect. But this
aoqi@0 90 // kinda works.
aoqi@0 91 // properly handling empty set requires more work.
aoqi@0 92 Expression e = Expression.EPSILON;
aoqi@0 93 for (XSParticle p : group.getChildren()) {
aoqi@0 94 if(e==null) e = particle(p);
aoqi@0 95 else e = new Choice(e,particle(p));
aoqi@0 96 }
aoqi@0 97 return e;
aoqi@0 98 } else {
aoqi@0 99 Expression e = Expression.EPSILON;
aoqi@0 100 for (XSParticle p : group.getChildren()) {
aoqi@0 101 if(e==null) e = particle(p);
aoqi@0 102 else e = new Sequence(e,particle(p));
aoqi@0 103 }
aoqi@0 104 return e;
aoqi@0 105 }
aoqi@0 106 }
aoqi@0 107
aoqi@0 108 public Element elementDecl(XSElementDecl decl) {
aoqi@0 109 QName n = BGMBuilder.getName(decl);
aoqi@0 110
aoqi@0 111 GElementImpl e = decls.get(n);
aoqi@0 112 if(e==null)
aoqi@0 113 decls.put(n,e=new GElementImpl(n,decl));
aoqi@0 114
aoqi@0 115 e.particles.add(current);
aoqi@0 116 assert current.getTerm()==decl;
aoqi@0 117
aoqi@0 118 return e;
aoqi@0 119 }
aoqi@0 120
aoqi@0 121 public Expression particle(XSParticle p) {
aoqi@0 122 current = p;
aoqi@0 123 Expression e = p.getTerm().apply(this);
aoqi@0 124
aoqi@0 125 if(p.isRepeated())
aoqi@0 126 e = new OneOrMore(e);
aoqi@0 127
aoqi@0 128 if (BigInteger.ZERO.equals(p.getMinOccurs()))
aoqi@0 129 e = new Choice(e,Expression.EPSILON);
aoqi@0 130
aoqi@0 131 return e;
aoqi@0 132 }
aoqi@0 133
aoqi@0 134 }

mercurial