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: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.relaxng.pattern;
031:
032: import com.caucho.relaxng.RelaxException;
033: import com.caucho.relaxng.program.ChoiceNameItem;
034: import com.caucho.relaxng.program.NameClassItem;
035: import com.caucho.util.CharBuffer;
036:
037: import java.util.ArrayList;
038:
039: /**
040: * Relax element pattern
041: */
042: public class ChoiceNamePattern extends NameClassPattern {
043: private ArrayList<NameClassPattern> _patterns = new ArrayList<NameClassPattern>();
044:
045: private NameClassItem _item;
046:
047: /**
048: * Creates a new choice pattern.
049: */
050: public ChoiceNamePattern() {
051: }
052:
053: /**
054: * Returns the number of children.
055: */
056: public int getSize() {
057: return _patterns.size();
058: }
059:
060: /**
061: * Returns the n-th child.
062: */
063: public NameClassPattern getChild(int i) {
064: return _patterns.get(i);
065: }
066:
067: /**
068: * Adds an element.
069: */
070: public void addNameChild(NameClassPattern child)
071: throws RelaxException {
072: if (child instanceof ChoiceNamePattern) {
073: ChoiceNamePattern list = (ChoiceNamePattern) child;
074:
075: for (int i = 0; i < list.getSize(); i++)
076: addChild(list.getChild(i));
077:
078: return;
079: }
080:
081: if (!_patterns.contains(child))
082: _patterns.add(child);
083: }
084:
085: /**
086: * Returns the Relax schema name.
087: */
088: public String getTagName() {
089: return "choice";
090: }
091:
092: /**
093: * Creates the production item.
094: */
095: public NameClassItem createNameItem() throws RelaxException {
096: if (_item == null) {
097: ChoiceNameItem item = new ChoiceNameItem();
098:
099: for (int i = 0; i < _patterns.size(); i++) {
100: item.addItem(_patterns.get(i).createNameItem());
101: }
102:
103: _item = item.getMin();
104: }
105:
106: return _item;
107: }
108:
109: /**
110: * Returns a string for the production.
111: */
112: public String toProduction() {
113: CharBuffer cb = new CharBuffer();
114:
115: for (int i = 0; i < _patterns.size(); i++) {
116: if (i != 0)
117: cb.append(" | ");
118: cb.append(_patterns.get(i).toProduction());
119: }
120:
121: return cb.toString();
122: }
123:
124: public boolean equals(Object o) {
125: if (this == o)
126: return true;
127:
128: if (!(o instanceof ChoiceNamePattern))
129: return false;
130:
131: ChoiceNamePattern choice = (ChoiceNamePattern) o;
132:
133: if (_patterns.size() != choice._patterns.size())
134: return false;
135:
136: return isSubset(choice) && choice.isSubset(this );
137: }
138:
139: private boolean isSubset(ChoiceNamePattern item) {
140: if (_patterns.size() != item._patterns.size())
141: return false;
142:
143: for (int i = 0; i < _patterns.size(); i++) {
144: Pattern subPattern = _patterns.get(i);
145:
146: if (!item._patterns.contains(subPattern))
147: return false;
148: }
149:
150: return true;
151: }
152:
153: /**
154: * Debugging.
155: */
156: public String toString() {
157: return "ChoiceNamePattern" + _patterns;
158: }
159: }
|