KickJava   Java API By Example, From Geeks To Geeks.

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


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.ChoiceItem;
33 import com.caucho.relaxng.program.EmptyItem;
34 import com.caucho.relaxng.program.Item;
35 import com.caucho.util.CharBuffer;
36
37 import java.util.ArrayList JavaDoc;
38
39 /**
40  * Relax element pattern
41  */

42 public class ChoicePattern extends Pattern {
43   private ArrayList JavaDoc<Pattern> _patterns = new ArrayList JavaDoc<Pattern>();
44   private boolean _hasEmpty;
45
46   private Item _item;
47   
48   /**
49    * Creates a new choice pattern.
50    */

51   public ChoicePattern()
52   {
53   }
54
55   /**
56    * Returns the number of children.
57    */

58   public int getSize()
59   {
60     return _patterns.size();
61   }
62
63   /**
64    * Returns the n-th child.
65    */

66   public Pattern getChild(int i)
67   {
68     return _patterns.get(i);
69   }
70
71   public boolean hasEmpty()
72   {
73     return _hasEmpty;
74   }
75
76   /**
77    * Returns true if it contains a data element.
78    */

79   public boolean hasData()
80   {
81     for (int i = 0; i < _patterns.size(); i++) {
82       if (_patterns.get(i).hasData())
83         return true;
84     }
85
86     return false;
87   }
88
89   /**
90    * Returns true if it contains a data element.
91    */

92   public boolean hasElement()
93   {
94     for (int i = 0; i < _patterns.size(); i++) {
95       if (_patterns.get(i).hasElement())
96         return true;
97     }
98
99     return false;
100   }
101   
102   /**
103    * Adds an element.
104    */

105   public void addChild(Pattern child)
106     throws RelaxException
107   {
108     child.setElementName(getElementName());
109
110     if (child instanceof ChoicePattern) {
111       ChoicePattern list = (ChoicePattern) child;
112
113       if (list._hasEmpty)
114         _hasEmpty = true;
115
116       for (int i = 0; i < list.getSize(); i++)
117         addChild(list.getChild(i));
118       
119       return;
120     }
121
122     if (child instanceof EmptyPattern) {
123       _hasEmpty = true;
124       return;
125     }
126
127     if (! _patterns.contains(child))
128       _patterns.add(child);
129   }
130
131   /**
132    * Returns the Relax schema name.
133    */

134   public String JavaDoc getTagName()
135   {
136     return "choice";
137   }
138
139   /**
140    * Creates the production item.
141    */

142   public Item createItem(GrammarPattern grammar)
143     throws RelaxException
144   {
145     if (_item == null) {
146       ChoiceItem item = new ChoiceItem();
147
148       for (int i = 0; i < _patterns.size(); i++) {
149         item.addItem(_patterns.get(i).createItem(grammar));
150       }
151
152       if (_hasEmpty)
153         item.addItem(EmptyItem.create());
154
155       _item = item.getMin();
156     }
157
158     return _item;
159   }
160
161   /**
162    * Returns a string for the production.
163    */

164   public String JavaDoc toProduction()
165   {
166     if (_hasEmpty && _patterns.size() == 1)
167       return "(" + _patterns.get(0).toProduction() + ")?";
168         
169     CharBuffer cb = new CharBuffer();
170
171     if (_hasEmpty)
172       cb.append("(");
173     
174     for (int i = 0; i < _patterns.size(); i++) {
175       if (i != 0)
176         cb.append(" | ");
177       cb.append(_patterns.get(i).toProduction());
178     }
179
180     if (_hasEmpty)
181       cb.append(")?");
182     
183     return cb.toString();
184   }
185
186   public boolean equals(Object JavaDoc o)
187   {
188     if (this == o)
189       return true;
190
191     if (! (o instanceof ChoicePattern))
192       return false;
193
194     ChoicePattern choice = (ChoicePattern) o;
195
196     if (_hasEmpty != choice._hasEmpty)
197       return false;
198     
199     if (_patterns.size() != choice._patterns.size())
200       return false;
201
202     return isSubset(choice) && choice.isSubset(this);
203   }
204
205   private boolean isSubset(ChoicePattern item)
206   {
207     if (_patterns.size() != item._patterns.size())
208       return false;
209
210     for (int i = 0; i < _patterns.size(); i++) {
211       Pattern subPattern = _patterns.get(i);
212
213       if (! item._patterns.contains(subPattern))
214         return false;
215     }
216
217     return true;
218   }
219
220   /**
221    * Debugging.
222    */

223   public String JavaDoc toString()
224   {
225     return "ChoicePattern" + _patterns;
226   }
227 }
228
229
Popular Tags