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.xml.bind.v2.schemagen;
038:
039: import java.util.ArrayList;
040: import java.util.Arrays;
041: import java.util.List;
042:
043: import com.sun.xml.bind.v2.schemagen.xmlschema.ContentModelContainer;
044: import com.sun.xml.bind.v2.schemagen.xmlschema.Particle;
045: import com.sun.xml.bind.v2.schemagen.xmlschema.TypeDefParticle;
046: import com.sun.xml.bind.v2.schemagen.xmlschema.Occurs;
047:
048: /**
049: * Normalized representation of the content model.
050: *
051: * <p>
052: * This is built from bottom up so that we can eliminate redundant constructs,
053: * and produce the most concise content model definition in XML.
054: *
055: * @author Kohsuke Kawaguchi
056: */
057: abstract class Tree {
058:
059: /**
060: * Returns "T?" from "T".
061: *
062: * @param really
063: * if false this method becomes no-op. This is so that we can write
064: * the caller fluently.
065: */
066: Tree makeOptional(boolean really) {
067: return really ? new Optional(this ) : this ;
068: }
069:
070: /**
071: * Returns "T+" from "T".
072: *
073: * @param really
074: * if false this method becomes no-op. This is so that we can write
075: * the caller fluently.
076: */
077: Tree makeRepeated(boolean really) {
078: return really ? new Repeated(this ) : this ;
079: }
080:
081: /**
082: * Returns a group tree.
083: */
084: static Tree makeGroup(GroupKind kind, List<Tree> children) {
085: // pointless binary operator.
086: if (children.size() == 1)
087: return children.get(0);
088:
089: // we neither have epsilon or emptySet, so can't handle children.length==0 nicely
090:
091: // eliminated nesting groups of the same kind.
092: // this is where binary tree would have shined.
093: List<Tree> normalizedChildren = new ArrayList<Tree>(children
094: .size());
095: for (Tree t : children) {
096: if (t instanceof Group) {
097: Group g = (Group) t;
098: if (g.kind == kind) {
099: normalizedChildren
100: .addAll(Arrays.asList(g.children));
101: continue;
102: }
103: }
104: normalizedChildren.add(t);
105: }
106:
107: return new Group(kind, normalizedChildren
108: .toArray(new Tree[normalizedChildren.size()]));
109: }
110:
111: /**
112: * Returns true if this tree accepts empty sequence.
113: */
114: abstract boolean isNullable();
115:
116: /**
117: * Returns true if the top node of this tree can
118: * appear as a valid top-level content model in XML Schema.
119: *
120: * <p>
121: * Model groups and occurrences that have model group in it can.
122: */
123: boolean canBeTopLevel() {
124: return false;
125: }
126:
127: /**
128: * Writes out the content model.
129: *
130: * Normall this runs recursively until we write out the whole content model.
131: */
132: protected abstract void write(ContentModelContainer parent,
133: boolean isOptional, boolean repeated);
134:
135: /**
136: * Writes inside the given complex type.
137: */
138: protected void write(TypeDefParticle ct) {
139: if (canBeTopLevel())
140: write(ct._cast(ContentModelContainer.class), false, false);
141: else
142: // need a dummy wrapper
143: new Group(GroupKind.SEQUENCE, this ).write(ct);
144: }
145:
146: /**
147: * Convenience method to write occurrence constraints.
148: */
149: protected final void writeOccurs(Occurs o, boolean isOptional,
150: boolean repeated) {
151: if (isOptional)
152: o.minOccurs(0);
153: if (repeated)
154: o.maxOccurs("unbounded");
155: }
156:
157: /**
158: * Represents a terminal tree node, such as element, wildcard, etc.
159: */
160: abstract static class Term extends Tree {
161: boolean isNullable() {
162: return false;
163: }
164: }
165:
166: /**
167: * "T?"
168: */
169: private static final class Optional extends Tree {
170: private final Tree body;
171:
172: private Optional(Tree body) {
173: this .body = body;
174: }
175:
176: @Override
177: boolean isNullable() {
178: return true;
179: }
180:
181: @Override
182: Tree makeOptional(boolean really) {
183: return this ;
184: }
185:
186: @Override
187: protected void write(ContentModelContainer parent,
188: boolean isOptional, boolean repeated) {
189: body.write(parent, true, repeated);
190: }
191: }
192:
193: /**
194: * "T+"
195: */
196: private static final class Repeated extends Tree {
197: private final Tree body;
198:
199: private Repeated(Tree body) {
200: this .body = body;
201: }
202:
203: @Override
204: boolean isNullable() {
205: return body.isNullable();
206: }
207:
208: @Override
209: Tree makeRepeated(boolean really) {
210: return this ;
211: }
212:
213: @Override
214: protected void write(ContentModelContainer parent,
215: boolean isOptional, boolean repeated) {
216: body.write(parent, isOptional, true);
217: }
218: }
219:
220: /**
221: * "S|T", "S,T", and "S&T".
222: */
223: private static final class Group extends Tree {
224: private final GroupKind kind;
225: private final Tree[] children;
226:
227: private Group(GroupKind kind, Tree... children) {
228: this .kind = kind;
229: this .children = children;
230: }
231:
232: @Override
233: boolean canBeTopLevel() {
234: return true;
235: }
236:
237: @Override
238: boolean isNullable() {
239: if (kind == GroupKind.CHOICE) {
240: for (Tree t : children) {
241: if (t.isNullable())
242: return true;
243: }
244: return false;
245: } else {
246: for (Tree t : children) {
247: if (!t.isNullable())
248: return false;
249: }
250: return true;
251: }
252: }
253:
254: @Override
255: protected void write(ContentModelContainer parent,
256: boolean isOptional, boolean repeated) {
257: Particle c = kind.write(parent);
258: writeOccurs(c, isOptional, repeated);
259:
260: for (Tree child : children) {
261: child.write(c, false, false);
262: }
263: }
264: }
265: }
|