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.GroupItem;
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 GroupPattern extends Pattern {
042: private ArrayList<Pattern> _patterns = new ArrayList<Pattern>();
043:
044: /**
045: * Creates a new element pattern.
046: */
047: public GroupPattern() {
048: }
049:
050: /**
051: * Returns the Relax schema name.
052: */
053: public String getTagName() {
054: return "group";
055: }
056:
057: /**
058: * Returns the number of children.
059: */
060: public int getSize() {
061: return _patterns.size();
062: }
063:
064: /**
065: * Returns the n-th child.
066: */
067: public Pattern getChild(int i) {
068: return _patterns.get(i);
069: }
070:
071: /**
072: * Adds an element.
073: */
074: public void addChild(Pattern child) throws RelaxException {
075: child.setParent(this );
076: child.setElementName(getElementName());
077:
078: if (child instanceof GroupPattern) {
079: GroupPattern list = (GroupPattern) child;
080:
081: for (int i = 0; i < list.getSize(); i++)
082: addChild(list.getChild(i));
083:
084: return;
085: }
086:
087: _patterns.add(child);
088:
089: boolean hasData = false;
090: boolean hasElement = false;
091: for (int i = 0; i < _patterns.size(); i++) {
092: if (_patterns.get(i).hasData())
093: hasData = true;
094: else if (_patterns.get(i).hasElement())
095: hasElement = true;
096: }
097:
098: if (hasData && hasElement)
099: throw new RelaxException(
100: L
101: .l("<data> may not be in a <group>. Use <text> instead."));
102: }
103:
104: /**
105: * Creates the production item.
106: */
107: public Item createItem(GrammarPattern grammar)
108: throws RelaxException {
109: if (_patterns.size() == 0)
110: return null;
111:
112: Item tail = _patterns.get(_patterns.size() - 1).createItem(
113: grammar);
114:
115: for (int i = _patterns.size() - 2; i >= 0; i--)
116: tail = GroupItem.create(_patterns.get(i)
117: .createItem(grammar), tail);
118:
119: return tail;
120: }
121:
122: /**
123: * Returns a string for the production.
124: */
125: public String toProduction() {
126: if (_patterns.size() == 0)
127: return "notAllowed";
128:
129: CharBuffer cb = new CharBuffer();
130:
131: for (int i = 0; i < _patterns.size(); i++) {
132: if (i != 0)
133: cb.append(", ");
134:
135: Pattern pattern = _patterns.get(i);
136:
137: if (pattern instanceof ChoicePattern
138: && !(((ChoicePattern) pattern).hasEmpty()))
139: cb.append("(" + _patterns.get(i).toProduction() + ")");
140: else
141: cb.append(_patterns.get(i).toProduction());
142: }
143:
144: return cb.toString();
145: }
146:
147: public boolean equals(Object o) {
148: if (this == o)
149: return true;
150:
151: if (!(o instanceof GroupPattern))
152: return false;
153:
154: GroupPattern group = (GroupPattern) o;
155:
156: if (_patterns.size() != group._patterns.size())
157: return false;
158:
159: for (int i = 0; i < _patterns.size(); i++)
160: if (!_patterns.get(i).equals(group._patterns.get(i)))
161: return false;
162:
163: return true;
164: }
165:
166: /**
167: * Debugging.
168: */
169: public String toString() {
170: return "GroupPattern" + _patterns;
171: }
172: }
|