KickJava   Java API By Example, From Geeks To Geeks.

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


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  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

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

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

51   public ChoiceNamePattern()
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 NameClassPattern getChild(int i)
67   {
68     return _patterns.get(i);
69   }
70   
71   /**
72    * Adds an element.
73    */

74   public void addNameChild(NameClassPattern child)
75     throws RelaxException
76   {
77     if (child instanceof ChoiceNamePattern) {
78       ChoiceNamePattern list = (ChoiceNamePattern) child;
79
80       for (int i = 0; i < list.getSize(); i++)
81         addChild(list.getChild(i));
82       
83       return;
84     }
85
86     if (! _patterns.contains(child))
87       _patterns.add(child);
88   }
89
90   /**
91    * Returns the Relax schema name.
92    */

93   public String JavaDoc getTagName()
94   {
95     return "choice";
96   }
97
98   /**
99    * Creates the production item.
100    */

101   public NameClassItem createNameItem()
102     throws RelaxException
103   {
104     if (_item == null) {
105       ChoiceNameItem item = new ChoiceNameItem();
106
107       for (int i = 0; i < _patterns.size(); i++) {
108     item.addItem(_patterns.get(i).createNameItem());
109       }
110
111       _item = item.getMin();
112     }
113
114     return _item;
115   }
116
117   /**
118    * Returns a string for the production.
119    */

120   public String JavaDoc toProduction()
121   {
122     CharBuffer cb = new CharBuffer();
123
124     for (int i = 0; i < _patterns.size(); i++) {
125       if (i != 0)
126         cb.append(" | ");
127       cb.append(_patterns.get(i).toProduction());
128     }
129     
130     return cb.toString();
131   }
132
133   public boolean equals(Object JavaDoc o)
134   {
135     if (this == o)
136       return true;
137
138     if (! (o instanceof ChoiceNamePattern))
139       return false;
140
141     ChoiceNamePattern choice = (ChoiceNamePattern) o;
142
143     if (_patterns.size() != choice._patterns.size())
144       return false;
145
146     return isSubset(choice) && choice.isSubset(this);
147   }
148
149   private boolean isSubset(ChoiceNamePattern item)
150   {
151     if (_patterns.size() != item._patterns.size())
152       return false;
153
154     for (int i = 0; i < _patterns.size(); i++) {
155       Pattern subPattern = _patterns.get(i);
156
157       if (! item._patterns.contains(subPattern))
158         return false;
159     }
160
161     return true;
162   }
163
164   /**
165    * Debugging.
166    */

167   public String JavaDoc toString()
168   {
169     return "ChoiceNamePattern" + _patterns;
170   }
171 }
172
173
Popular Tags