1 /* GenericScannerState.java
2  Copyright (C) 2005 Free Software Foundation, Inc.
3 
4  This file is part of GNU Classpath.
5 
6  GNU Classpath is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2, or (at your option)
9  any later version.
10 
11  GNU Classpath is distributed in the hope that it will be useful, but
12  WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with GNU Classpath; see the file COPYING.  If not, write to the
18  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  02110-1301 USA.
20 
21  Linking this library statically or dynamically with other modules is
22  making a combined work based on this library.  Thus, the terms and
23  conditions of the GNU General Public License cover the whole
24  combination.
25 
26  As a special exception, the copyright holders of this library give you
27  permission to link this library with independent modules to produce an
28  executable, regardless of the license terms of these independent
29  modules, and to copy and distribute the resulting executable under
30  terms of your choice, provided that you also meet, for each linked
31  independent module, the terms and conditions of the license of that
32  module.  An independent module is a module which is not derived from
33  or based on this library.  If you modify this library, you may extend
34  this exception to your version of the library, but you are not
35  obligated to do so.  If you do not wish to do so, delete this
36  exception statement from your version. */
37 
38 
39 package gnu.java.beans.encoder;
40 
41 import java.util.HashMap;
42 
43 import gnu.java.beans.encoder.elements.ArrayInstantiation;
44 import gnu.java.beans.encoder.elements.Array_Get;
45 import gnu.java.beans.encoder.elements.Array_Set;
46 import gnu.java.beans.encoder.elements.ClassResolution;
47 import gnu.java.beans.encoder.elements.Element;
48 import gnu.java.beans.encoder.elements.List_Get;
49 import gnu.java.beans.encoder.elements.List_Set;
50 import gnu.java.beans.encoder.elements.MethodInvocation;
51 import gnu.java.beans.encoder.elements.NullObject;
52 import gnu.java.beans.encoder.elements.ObjectInstantiation;
53 import gnu.java.beans.encoder.elements.ObjectReference;
54 import gnu.java.beans.encoder.elements.PrimitiveInstantiation;
55 import gnu.java.beans.encoder.elements.StaticFieldAccess;
56 import gnu.java.beans.encoder.elements.StaticMethodInvocation;
57 import gnu.java.beans.encoder.elements.StringReference;
58 
59 /**
60  * This class is a {@link ScannerState} implementation that creates
61  * suitable {@link gnu.java.beans.encoder.elements.Element} instances
62  * for each transition variant.
63  *
64  * <p>Furthermore it can optionally skip a certain number of child
65  * elements. The algorithm can cope with the fact that one
66  * <code>GenericScannerState</code> instance may be called at
67  * different levels of recursions.</p>
68  *
69  * @author Robert Schuster (robertschuster@fsfe.org)
70  */
71 class GenericScannerState extends ScannerState
72 {
73   private int skipElements, initialSkipElements;
74 
75   final Root root;
76 
77   HashMap skipValues;
78 
GenericScannerState(Root newRoot)79   GenericScannerState(Root newRoot)
80   {
81     root = newRoot;
82   }
83 
GenericScannerState(Root root, int skipElements)84   GenericScannerState(Root root, int skipElements)
85   {
86     this(root);
87     this.skipElements = initialSkipElements = skipElements;
88 
89     if (skipElements > 0)
90       skipValues = new HashMap();
91   }
92 
enterImpl(Context ctx)93   protected void enterImpl(Context ctx)
94   {
95     if (skipValues != null)
96       {
97         Integer skip = (Integer) skipValues.get(ctx);
98 
99         if (skip == null)
100           {
101             skip = Integer.valueOf(initialSkipElements);
102             skipValues.put(ctx, skip);
103           }
104 
105         skipElements = skip.intValue();
106       }
107   }
108 
methodInvocation(String methodName)109   void methodInvocation(String methodName)
110   {
111     if (skipValues != null && skipElements > 0)
112       return;
113 
114     root.addChild(new MethodInvocation(methodName));
115   }
116 
staticMethodInvocation(String className, String methodName)117   void staticMethodInvocation(String className, String methodName)
118   {
119     if (skipValues != null && skipElements > 0)
120       return;
121 
122     root.addChild(new StaticMethodInvocation(className, methodName));
123   }
124 
staticFieldAccess(String className, String fieldName)125   void staticFieldAccess(String className, String fieldName)
126   {
127     if (skipValues != null && skipElements > 0)
128       return;
129 
130     root.addChild(new StaticFieldAccess(className, fieldName));
131   }
132 
classResolution(String className)133   void classResolution(String className)
134   {
135     if (skipValues != null && skipElements > 0)
136       return;
137 
138     root.addChild(new ClassResolution(className));
139   }
140 
objectInstantiation(String className, ObjectId objectId)141   void objectInstantiation(String className, ObjectId objectId)
142   {
143     if (skipValues != null && skipElements > 0)
144       return;
145 
146     Element elem = new ObjectInstantiation(className);
147     elem.initId(objectId);
148 
149     root.addChild(elem);
150   }
151 
primitiveInstantiation(String primitiveName, String valueAsString)152   void primitiveInstantiation(String primitiveName, String valueAsString)
153   {
154     if (skipValues != null && skipElements > 0)
155       return;
156 
157     root.addChild(new PrimitiveInstantiation(primitiveName, valueAsString));
158   }
159 
objectArrayInstantiation(String arrayClassName, String lengthAsString, ObjectId objectId)160   void objectArrayInstantiation(String arrayClassName, String lengthAsString,
161                           ObjectId objectId)
162   {
163     if (skipValues != null && skipElements > 0)
164       return;
165 
166     Element elem = new ArrayInstantiation(arrayClassName, lengthAsString);
167     elem.initId(objectId);
168 
169     root.addChild(elem);
170   }
171 
primitiveArrayInstantiation(String arrayClassName, String lengthAsString, ObjectId objectId)172   void primitiveArrayInstantiation(String arrayClassName, String lengthAsString,
173                                 ObjectId objectId)
174   {
175     objectArrayInstantiation(arrayClassName, lengthAsString, objectId);
176   }
177 
arraySet(String indexAsString)178   void arraySet(String indexAsString)
179   {
180     if (skipValues != null && skipElements > 0)
181       return;
182 
183     root.addChild(new Array_Set(indexAsString));
184   }
185 
arrayGet(String indexAsString)186   void arrayGet(String indexAsString)
187   {
188     if (skipValues != null && skipElements > 0)
189       return;
190 
191     root.addChild(new Array_Get(indexAsString));
192   }
193 
listGet()194   void listGet()
195   {
196     if (skipValues != null && skipElements > 0)
197       return;
198 
199     root.addChild(new List_Get());
200   }
201 
listSet()202   void listSet()
203   {
204     if (skipValues != null && skipElements > 0)
205       return;
206 
207     root.addChild(new List_Set());
208   }
209 
nullObject()210   void nullObject()
211   {
212     if (skipValues != null && skipElements > 0)
213       return;
214 
215     root.addChild(new NullObject());
216   }
217 
stringReference(String string)218   void stringReference(String string)
219   {
220     if (skipValues != null && skipElements > 0)
221       return;
222 
223     root.addChild(new StringReference(string));
224   }
225 
objectReference(ObjectId id)226   void objectReference(ObjectId id)
227   {
228     if (skipValues != null && skipElements > 0)
229       return;
230 
231     root.addChild(new ObjectReference(id));
232   }
233 
end()234   void end()
235   {
236     if (skipValues != null)
237       {
238         if (skipElements > 0)
239           skipElements--;
240         else
241           {
242             // Finishes the Element we are constructing.
243             root.end();
244           }
245         skipValues.put(context(), Integer.valueOf(skipElements));
246       }
247     else
248       root.end();
249 
250   }
251 
enter()252   void enter()
253   {
254 
255   }
256 
257 }
258