KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > relaxng > pattern > GroupPattern


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.relaxng.pattern;
30
31 import com.caucho.relaxng.RelaxException;
32 import com.caucho.relaxng.program.GroupItem;
33 import com.caucho.relaxng.program.Item;
34 import com.caucho.util.CharBuffer;
35
36 import java.util.ArrayList JavaDoc;
37
38 /**
39  * Relax element pattern
40  */

41 public class GroupPattern extends Pattern {
42   private ArrayList JavaDoc<Pattern> _patterns = new ArrayList JavaDoc<Pattern>();
43
44   /**
45    * Creates a new element pattern.
46    */

47   public GroupPattern()
48   {
49   }
50
51   /**
52    * Returns the Relax schema name.
53    */

54   public String JavaDoc getTagName()
55   {
56     return "group";
57   }
58
59   /**
60    * Returns the number of children.
61    */

62   public int getSize()
63   {
64     return _patterns.size();
65   }
66
67   /**
68    * Returns the n-th child.
69    */

70   public Pattern getChild(int i)
71   {
72     return _patterns.get(i);
73   }
74
75   /**
76    * Adds an element.
77    */

78   public void addChild(Pattern child)
79     throws RelaxException
80   {
81     child.setParent(this);
82     child.setElementName(getElementName());
83
84     if (child instanceof GroupPattern) {
85       GroupPattern list = (GroupPattern) child;
86
87       for (int i = 0; i < list.getSize(); i++)
88         addChild(list.getChild(i));
89       
90       return;
91     }
92
93     _patterns.add(child);
94
95     boolean hasData = false;
96     boolean hasElement = false;
97     for (int i = 0; i < _patterns.size(); i++) {
98       if (_patterns.get(i).hasData())
99         hasData = true;
100       else if (_patterns.get(i).hasElement())
101         hasElement = true;
102     }
103
104     if (hasData && hasElement)
105       throw new RelaxException(L.l("<data> may not be in a <group>. Use <text> instead."));
106   }
107
108   /**
109    * Creates the production item.
110    */

111   public Item createItem(GrammarPattern grammar)
112     throws RelaxException
113   {
114     if (_patterns.size() == 0)
115       return null;
116
117     Item tail = _patterns.get(_patterns.size() - 1).createItem(grammar);
118
119     for (int i = _patterns.size() - 2; i >= 0; i--)
120       tail = GroupItem.create(_patterns.get(i).createItem(grammar), tail);
121     
122     return tail;
123   }
124
125   /**
126    * Returns a string for the production.
127    */

128   public String JavaDoc toProduction()
129   {
130     if (_patterns.size() == 0)
131       return "notAllowed";
132
133     CharBuffer cb = new CharBuffer();
134     
135     for (int i = 0; i < _patterns.size(); i++) {
136       if (i != 0)
137         cb.append(", ");
138
139       Pattern pattern = _patterns.get(i);
140
141       if (pattern instanceof ChoicePattern &&
142           ! (((ChoicePattern) pattern).hasEmpty()))
143         cb.append("(" + _patterns.get(i).toProduction() + ")");
144       else
145         cb.append(_patterns.get(i).toProduction());
146     }
147     
148     return cb.toString();
149   }
150
151   public boolean equals(Object JavaDoc o)
152   {
153     if (this == o)
154       return true;
155
156     if (! (o instanceof GroupPattern))
157       return false;
158
159     GroupPattern group = (GroupPattern) o;
160
161     if (_patterns.size() != group._patterns.size())
162       return false;
163
164     for (int i = 0; i < _patterns.size(); i++)
165       if (! _patterns.get(i).equals(group._patterns.get(i)))
166         return false;
167
168     return true;
169   }
170
171   /**
172    * Debugging.
173    */

174   public String JavaDoc toString()
175   {
176     return "GroupPattern" + _patterns;
177   }
178 }
179
180
Popular Tags