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.dtd;
038:
039: import java.util.ArrayList;
040: import java.util.List;
041:
042: import com.sun.xml.dtdparser.DTDEventListener;
043:
044: /**
045: * @author Kohsuke Kawaguchi
046: */
047: final class ModelGroup extends Term {
048: enum Kind {
049: CHOICE, SEQUENCE
050: }
051:
052: Kind kind;
053:
054: private final List<Term> terms = new ArrayList<Term>();
055:
056: void normalize(List<Block> r, boolean optional) {
057: switch (kind) {
058: case SEQUENCE:
059: for (Term t : terms)
060: t.normalize(r, optional);
061: return;
062: case CHOICE:
063: Block b = new Block(isOptional() || optional, isRepeated());
064: addAllElements(b);
065: r.add(b);
066: return;
067: }
068: }
069:
070: void addAllElements(Block b) {
071: for (Term t : terms)
072: t.addAllElements(b);
073: }
074:
075: boolean isOptional() {
076: switch (kind) {
077: case SEQUENCE:
078: for (Term t : terms)
079: if (!t.isOptional())
080: return false;
081: return true;
082: case CHOICE:
083: for (Term t : terms)
084: if (t.isOptional())
085: return true;
086: return false;
087: default:
088: throw new IllegalArgumentException();
089: }
090: }
091:
092: boolean isRepeated() {
093: switch (kind) {
094: case SEQUENCE:
095: return true;
096: case CHOICE:
097: for (Term t : terms)
098: if (t.isRepeated())
099: return true;
100: return false;
101: default:
102: throw new IllegalArgumentException();
103: }
104: }
105:
106: void setKind(short connectorType) {
107: Kind k;
108: switch (connectorType) {
109: case DTDEventListener.SEQUENCE:
110: k = Kind.SEQUENCE;
111: break;
112: case DTDEventListener.CHOICE:
113: k = Kind.CHOICE;
114: break;
115: default:
116: throw new IllegalArgumentException();
117: }
118:
119: assert kind == null || k == kind;
120: kind = k;
121: }
122:
123: void addTerm(Term t) {
124: if (t instanceof ModelGroup) {
125: ModelGroup mg = (ModelGroup) t;
126: if (mg.kind == this .kind) {
127: terms.addAll(mg.terms);
128: return;
129: }
130: }
131: terms.add(t);
132: }
133:
134: Term wrapUp() {
135: switch (terms.size()) {
136: case 0:
137: return EMPTY;
138: case 1:
139: assert kind == null;
140: return terms.get(0);
141: default:
142: assert kind != null;
143: return this;
144: }
145: }
146:
147: }
|