KickJava   Java API By Example, From Geeks To Geeks.

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


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.log.Log;
32 import com.caucho.relaxng.RelaxException;
33 import com.caucho.util.L10N;
34 import com.caucho.util.LruCache;
35 import com.caucho.xml.QName;
36
37 import java.util.HashSet JavaDoc;
38 import java.util.Iterator JavaDoc;
39 import java.util.logging.Logger JavaDoc;
40
41 /**
42  * Generates programs from patterns.
43  */

44 public class MemoItem extends Item {
45   protected final static L10N L = new L10N(MemoItem.class);
46   protected final static Logger JavaDoc log = Log.open(MemoItem.class);
47
48   private LruCache<Object JavaDoc,Item> _memoMap;
49   private Item _item;
50
51   public MemoItem(Item item, LruCache<Object JavaDoc,Item> memoMap)
52   {
53     _item = item;
54     _memoMap = memoMap;
55   }
56
57   public MemoItem(Item item)
58   {
59     _item = item;
60     _memoMap = new LruCache<Object JavaDoc,Item>(256);
61   }
62   
63   /**
64    * Adds to the first set, the set of element names possible.
65    */

66   public void firstSet(HashSet JavaDoc<QName> set)
67   {
68     _item.firstSet(set);
69   }
70   
71   /**
72    * Adds to the first set, the set of element names possible.
73    */

74   public void requiredFirstSet(HashSet JavaDoc<QName> set)
75   {
76     _item.requiredFirstSet(set);
77   }
78
79   /**
80    * Returns true if the item can match empty.
81    */

82   public boolean allowEmpty()
83   {
84     return _item.allowEmpty();
85   }
86   
87   /**
88    * Return all possible child items or null
89    */

90   public Iterator JavaDoc<Item> getItemsIterator()
91   {
92     return itemIterator( _item );
93   }
94
95
96   /**
97    * Returns the next item when an element of the given name is returned
98    *
99    * @param name the name of the element
100    *
101    * @return the program for handling the element
102    */

103   public Item startElement(QName name)
104     throws RelaxException
105   {
106     Item item = _item.startElement(name);
107
108     if (item == null)
109       return null;
110     else if (item == this)
111       return this;
112     else
113       return new MemoItem(item, _memoMap);
114   }
115
116   /**
117    * Adds to the first set, the set of attribute names possible.
118    */

119   public void attributeSet(HashSet JavaDoc<QName> set)
120   {
121     _item.attributeSet(set);
122   }
123   
124   /**
125    * Sets an attribute.
126    *
127    * @param name the name of the attribute
128    * @param value the value of the attribute
129    *
130    * @return the program for handling the element
131    */

132   public boolean allowAttribute(QName name, String JavaDoc value)
133     throws RelaxException
134   {
135     return _item.allowAttribute(name, value);
136   }
137   
138   /**
139    * Sets an attribute.
140    *
141    * @param name the name of the attribute
142    * @param value the value of the attribute
143    *
144    * @return the program for handling the element
145    */

146   public Item setAttribute(QName name, String JavaDoc value)
147     throws RelaxException
148   {
149     Item item = _item.setAttribute(name, value);
150
151     if (item == null)
152       return null;
153     else if (item == this)
154       return this;
155     else
156       return new MemoItem(item, _memoMap);
157   }
158
159   /**
160    * Returns true if the item can match empty.
161    */

162   public Item attributeEnd()
163   {
164     Item item = _item.attributeEnd();
165     
166     if (item == null)
167       return null;
168     else if (item == this)
169       return this;
170     else
171       return new MemoItem(item, _memoMap);
172   }
173
174   /**
175    * Adds text.
176    */

177   public Item text(String JavaDoc text)
178     throws RelaxException
179   {
180     Item item = _item.text(text);
181     
182     if (item == null)
183       return null;
184     else if (item == this)
185       return this;
186     else
187       return new MemoItem(item, _memoMap);
188   }
189   
190   /**
191    * Returns the next item when the element closes
192    */

193   public Item endElement()
194     throws RelaxException
195   {
196     Item item = _item.endElement();
197     
198     if (item == null)
199       return null;
200     else if (item == this)
201       return this;
202     else
203       return new MemoItem(item, _memoMap);
204   }
205 }
206
207
Popular Tags