KickJava   Java API By Example, From Geeks To Geeks.

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


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.InterleaveItem;
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 InterleavePattern extends Pattern {
42   private ArrayList JavaDoc<Pattern> _patterns = new ArrayList JavaDoc<Pattern>();
43
44   private Item _item;
45
46   /**
47    * Creates a new interleave pattern.
48    */

49   public InterleavePattern()
50   {
51   }
52
53   /**
54    * Returns the Relax schema name.
55    */

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

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

72   public Pattern getChild(int i)
73   {
74     return _patterns.get(i);
75   }
76
77   /**
78    * Returns true if it contains a data element.
79    */

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

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

106   public void addChild(Pattern child)
107     throws RelaxException
108   {
109     if (child instanceof DataPattern)
110       throw new RelaxException(L.l("<data> or <string> may not be used with interleave. Use <text> instead."));
111
112     if (child instanceof EmptyPattern)
113       throw new RelaxException(L.l("<empty> is not allowed as a child of <interleave>"));
114     
115     child.setParent(this);
116     child.setElementName(getElementName());
117
118     if (child instanceof InterleavePattern) {
119       InterleavePattern interleave = (InterleavePattern) child;
120
121       for (int i = 0; i < interleave.getSize(); i++)
122         addChild(interleave.getChild(i));
123       
124       return;
125     }
126
127     if (_patterns.contains(child))
128       return;
129     
130     _patterns.add(child);
131   }
132
133   /**
134    * Creates the production item.
135    */

136   public Item createItem(GrammarPattern grammar)
137     throws RelaxException
138   {
139     if (_item == null) {
140       InterleaveItem item = new InterleaveItem();
141
142       for (int i = 0; i < _patterns.size(); i++) {
143         item.addItem(_patterns.get(i).createItem(grammar));
144       }
145
146       _item = item.getMin();
147     }
148
149     return _item;
150   }
151
152   /**
153    * Returns a string for the production.
154    */

155   public String JavaDoc toProduction()
156   {
157     CharBuffer cb = new CharBuffer();
158     
159     for (int i = 0; i < _patterns.size(); i++) {
160       if (i != 0)
161         cb.append(" & ");
162       cb.append(_patterns.get(i).toProduction());
163     }
164     
165     return cb.toString();
166   }
167
168   public boolean equals(Object JavaDoc o)
169   {
170     if (this == o)
171       return true;
172
173     if (! (o instanceof InterleavePattern))
174       return false;
175
176     InterleavePattern interleave = (InterleavePattern) o;
177
178     if (_patterns.size() != interleave._patterns.size())
179       return false;
180
181     return isSubset(interleave) && interleave.isSubset(this);
182   }
183
184   private boolean isSubset(InterleavePattern item)
185   {
186     if (_patterns.size() != item._patterns.size())
187       return false;
188
189     for (int i = 0; i < _patterns.size(); i++) {
190       Pattern subPattern = _patterns.get(i);
191
192       if (! item._patterns.contains(subPattern))
193         return false;
194     }
195
196     return true;
197   }
198
199   /**
200    * Debugging.
201    */

202   public String JavaDoc toString()
203   {
204     return "InterleavePattern" + _patterns;
205   }
206 }
207
208
Popular Tags