001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.relaxng.pattern;
030:
031: import com.caucho.relaxng.RelaxException;
032: import com.caucho.relaxng.program.InterleaveItem;
033: import com.caucho.relaxng.program.Item;
034: import com.caucho.util.CharBuffer;
035:
036: import java.util.ArrayList;
037:
038: /**
039: * Relax element pattern
040: */
041: public class InterleavePattern extends Pattern {
042: private ArrayList<Pattern> _patterns = new ArrayList<Pattern>();
043:
044: private Item _item;
045:
046: /**
047: * Creates a new interleave pattern.
048: */
049: public InterleavePattern() {
050: }
051:
052: /**
053: * Returns the Relax schema name.
054: */
055: public String getTagName() {
056: return "interleave";
057: }
058:
059: /**
060: * Returns the number of children.
061: */
062: public int getSize() {
063: return _patterns.size();
064: }
065:
066: /**
067: * Returns the n-th child.
068: */
069: public Pattern getChild(int i) {
070: return _patterns.get(i);
071: }
072:
073: /**
074: * Returns true if it contains a data element.
075: */
076: public boolean hasData() {
077: for (int i = 0; i < _patterns.size(); i++) {
078: if (_patterns.get(i).hasData())
079: return true;
080: }
081:
082: return false;
083: }
084:
085: /**
086: * Returns true if it contains a data element.
087: */
088: public boolean hasElement() {
089: for (int i = 0; i < _patterns.size(); i++) {
090: if (_patterns.get(i).hasElement())
091: return true;
092: }
093:
094: return false;
095: }
096:
097: /**
098: * Adds an element.
099: */
100: public void addChild(Pattern child) throws RelaxException {
101: if (child instanceof DataPattern)
102: throw new RelaxException(
103: L
104: .l("<data> or <string> may not be used with interleave. Use <text> instead."));
105:
106: if (child instanceof EmptyPattern)
107: throw new RelaxException(
108: L
109: .l("<empty> is not allowed as a child of <interleave>"));
110:
111: child.setParent(this );
112: child.setElementName(getElementName());
113:
114: if (child instanceof InterleavePattern) {
115: InterleavePattern interleave = (InterleavePattern) child;
116:
117: for (int i = 0; i < interleave.getSize(); i++)
118: addChild(interleave.getChild(i));
119:
120: return;
121: }
122:
123: if (_patterns.contains(child))
124: return;
125:
126: _patterns.add(child);
127: }
128:
129: /**
130: * Creates the production item.
131: */
132: public Item createItem(GrammarPattern grammar)
133: throws RelaxException {
134: if (_item == null) {
135: InterleaveItem item = new InterleaveItem();
136:
137: for (int i = 0; i < _patterns.size(); i++) {
138: item.addItem(_patterns.get(i).createItem(grammar));
139: }
140:
141: _item = item.getMin();
142: }
143:
144: return _item;
145: }
146:
147: /**
148: * Returns a string for the production.
149: */
150: public String toProduction() {
151: CharBuffer cb = new CharBuffer();
152:
153: for (int i = 0; i < _patterns.size(); i++) {
154: if (i != 0)
155: cb.append(" & ");
156: cb.append(_patterns.get(i).toProduction());
157: }
158:
159: return cb.toString();
160: }
161:
162: public boolean equals(Object o) {
163: if (this == o)
164: return true;
165:
166: if (!(o instanceof InterleavePattern))
167: return false;
168:
169: InterleavePattern interleave = (InterleavePattern) o;
170:
171: if (_patterns.size() != interleave._patterns.size())
172: return false;
173:
174: return isSubset(interleave) && interleave.isSubset(this );
175: }
176:
177: private boolean isSubset(InterleavePattern item) {
178: if (_patterns.size() != item._patterns.size())
179: return false;
180:
181: for (int i = 0; i < _patterns.size(); i++) {
182: Pattern subPattern = _patterns.get(i);
183:
184: if (!item._patterns.contains(subPattern))
185: return false;
186: }
187:
188: return true;
189: }
190:
191: /**
192: * Debugging.
193: */
194: public String toString() {
195: return "InterleavePattern" + _patterns;
196: }
197: }
|