KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > relaxng > program > ChoiceNameItem


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.program;
30
31 import com.caucho.util.CharBuffer;
32 import com.caucho.util.L10N;
33 import com.caucho.xml.QName;
34
35 import java.util.ArrayList JavaDoc;
36 import java.util.HashSet JavaDoc;
37
38 /**
39  * Generates programs from patterns.
40  */

41 public class ChoiceNameItem extends NameClassItem {
42   protected final static L10N L = new L10N(ChoiceNameItem.class);
43
44   private ArrayList JavaDoc<NameClassItem> _items = new ArrayList JavaDoc<NameClassItem>();
45
46   public ChoiceNameItem()
47   {
48   }
49
50   public static NameClassItem create(NameClassItem left, NameClassItem right)
51   {
52     ChoiceNameItem choice = new ChoiceNameItem();
53     choice.addItem(left);
54     choice.addItem(right);
55
56     return choice.getMin();
57   }
58
59   public void addItem(NameClassItem item)
60   {
61     if (item == null)
62       return;
63     else if (item instanceof ChoiceNameItem) {
64       ChoiceNameItem choice = (ChoiceNameItem) item;
65
66       for (int i = 0; i < choice._items.size(); i++)
67         addItem(choice._items.get(i));
68
69       return;
70     }
71
72     for (int i = 0; i < _items.size(); i++) {
73       NameClassItem subItem = _items.get(i);
74
75       if (item.equals(subItem))
76         return;
77     }
78
79     _items.add(item);
80   }
81
82   public NameClassItem getMin()
83   {
84     if (_items.size() == 0)
85       return null;
86     else if (_items.size() == 1)
87       return _items.get(0);
88     else
89       return this;
90   }
91
92   /**
93    * Returns the first set, the set of element names possible.
94    */

95   public void firstSet(HashSet JavaDoc<QName> set)
96   {
97     for (int i = 0; i < _items.size(); i++)
98       _items.get(i).firstSet(set);
99   }
100   
101   /**
102    * Allows empty if both allow empty.
103    */

104   public boolean matches(QName name)
105   {
106     for (int i = 0; i < _items.size(); i++)
107       if (_items.get(i).matches(name))
108         return true;
109
110     return false;
111   }
112
113   /**
114    * Returns the pretty printed syntax.
115    */

116   public String JavaDoc toSyntaxDescription(String JavaDoc prefix)
117   {
118     CharBuffer cb = new CharBuffer();
119
120     cb.append("(");
121     
122     for (int i = 0; i < _items.size(); i++) {
123       if (i != 0)
124     cb.append(" | ");
125       
126       cb.append(_items.get(i).toSyntaxDescription(prefix));
127     }
128
129     cb.append(")");
130
131     return cb.toString();
132   }
133
134   /**
135    * Returns the hash code for the empty item.
136    */

137   public int hashCode()
138   {
139     int hash = 37;
140
141     for (int i = 0; i < _items.size(); i++)
142       hash += _items.get(i).hashCode();
143
144     return hash;
145   }
146
147   /**
148    * Returns true if the object is an empty item.
149    */

150   public boolean equals(Object JavaDoc o)
151   {
152     if (this == o)
153       return true;
154     
155     if (! (o instanceof ChoiceNameItem))
156       return false;
157
158     ChoiceNameItem choice = (ChoiceNameItem) o;
159
160     return isSubset(choice) && choice.isSubset(this);
161   }
162
163   private boolean isSubset(ChoiceNameItem item)
164   {
165     if (_items.size() != item._items.size())
166       return false;
167
168     for (int i = 0; i < _items.size(); i++) {
169       NameClassItem subItem = _items.get(i);
170
171       if (! item._items.contains(subItem))
172         return false;
173     }
174
175     return true;
176   }
177
178   public String JavaDoc toString()
179   {
180     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
181
182     sb.append("ChoiceNameItem[");
183     for (int i = 0; i < _items.size(); i++) {
184       if (i != 0)
185         sb.append(", ");
186       sb.append(_items.get(i));
187     }
188
189     sb.append("]");
190
191     return sb.toString();
192   }
193 }
194
195
Popular Tags