KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > rdf > arp > Token


1 /* Generated By:JavaCC: Do not edit this line. Token.java Version 3.0 */
2 /*
3  * (c) Copyright 2001,2004 Hewlett-Packard Development Company, LP
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  * derived from this software without specific prior written permission.
16
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  
28  * * $Id: Token.java,v 1.9 2005/04/08 13:29:29 jeremy_carroll Exp $
29  
30  AUTHOR: Jeremy J. Carroll
31  */

32 package com.hp.hpl.jena.rdf.arp;
33
34 import java.util.*;
35
36 /**
37  * This class is not part of the API. It is public for test purposes only.
38  * Token's are passed from the XML parser to the RDF parser.
39  * An important part of the contract is that you must not keep a reference
40  * to any token for too long, since each token has a reference to the next
41  * token, and hence to the rest of the file.
42  * If it is necessary to keep a token, clone it, and set the next field to
43  * null.
44  *
45  */

46
47 public class Token implements RDFParserConstants, Cloneable JavaDoc {
48     /**
49      * For debugging or testing Token garbage problems, set to true.
50      */

51     static public boolean COUNT = false;
52
53     /**
54      * For testing Token garbage problems, set to true, also set {@link #COUNT}
55      * to true. If set to true then no debugging messages are produced.
56      */

57     static public boolean COUNTTEST = false;
58
59     /**
60      * After DBGSIZE tokens a small debugging message, and hightide computations
61      * are done.
62      */

63     static private int DBGSIZE = 3000;
64
65     /**
66      * After BIGSIZE tokens a detailed debugging message is produced.
67      * Must be integer multiple of {@link #DBGSIZE}.
68      **/

69     static private int BIGSIZE = 99000;
70
71     /**
72      * exit when {@link #BIGSIZE} tokens have been processed.
73      */

74     static private boolean GIVEUP = true;
75
76     static private int SLEEPTIME = 0;
77     /**
78      * The maximum number of tokens ever alive. Call
79      * {@link #reinitHighTide} before testing. Only
80      * set when {@link #COUNTTEST} is true.
81      */

82     static volatile public int highTide;
83
84     static volatile private int dead = 0;
85
86     static volatile private int alive = 0;
87     
88     static public void reinitHighTide() {
89         PullingTokenPipe.lastMade = null;
90         Runtime.getRuntime().gc();
91         Runtime.getRuntime().gc();
92         try {
93         Thread.sleep(SLEEPTIME);
94         }
95         catch (Exception JavaDoc e){}
96         highTide = 0;
97         dead=0;
98         alive=0;
99     }
100
101     static private Map countMap = new HashMap();
102
103     static private Map weakMap = new WeakHashMap();
104
105     private boolean isDead = false;
106
107     protected void finalize() {
108         if (COUNT && !isDead) {
109             isDead = true;
110             dead++;
111             Integer JavaDoc oldCnt = (Integer JavaDoc) countMap
112                     .get(getClass());
113             if (oldCnt != null)
114             countMap.put(getClass(), new Integer JavaDoc(oldCnt.intValue() - 1));
115         }
116     }
117
118     final Location location;
119
120     Token(int kind, Location where) {
121         if (COUNT)
122             initCounting();
123         this.kind = kind;
124         this.location = where;
125     }
126
127     Token() {
128         if (COUNT)
129             initCounting();
130         location = null;
131     }
132
133     private void initCounting() {
134         alive++;
135         weakMap.put(this, null);
136         Integer JavaDoc oldCnt = (Integer JavaDoc) countMap.get(getClass());
137         if (oldCnt == null)
138             oldCnt = new Integer JavaDoc(0);
139         countMap.put(getClass(), new Integer JavaDoc(oldCnt.intValue() + 1));
140
141         if (alive % DBGSIZE == 0) {
142             boolean big = alive % BIGSIZE == 0;
143             Runtime.getRuntime().gc();
144             Runtime.getRuntime().gc();
145             try {
146                 Thread.sleep(SLEEPTIME);
147                 }
148             catch (Exception JavaDoc e){}
149             if (COUNTTEST) {
150                 int inUse = alive - dead;
151
152         // System.err.println(dead + "/" + alive + " ("+inUse+")");
153
if (highTide < inUse)
154                     highTide = inUse;
155             } else {
156                 System.err.println("[" + (alive-dead)+"]"+dead + "/" + alive);
157                 if (big) {
158                     System.err.println("Total: " + (alive - dead));
159                     Iterator it = countMap.keySet().iterator();
160                     while (it.hasNext()) {
161                         Object JavaDoc key = it.next();
162                         System.err.println(key.toString() + ": "
163                                 + countMap.get(key).toString());
164                     }
165
166                     if (GIVEUP || (alive - dead) < 10000) {
167                         Map prev = new HashMap();
168                         it = weakMap.keySet().iterator();
169                         while (it.hasNext()) {
170                             Object JavaDoc o = it.next();
171                             if (o != null) {
172                                 Token t = (Token) o;
173                                 Token n = t.next;
174                                 if (n != null) {
175                                     prev.put(n, t);
176                                 }
177                             }
178                         }
179                         it = weakMap.keySet().iterator();
180                         while (it.hasNext()) {
181                             Object JavaDoc o = it.next();
182                             if (o != null) {
183                                 Token t = (Token) o;
184                                 if (!prev.containsKey(t)) {
185                                     System.err.println("No prev: " + t + " "
186                                             + t.location);
187                                 }
188                             }
189                         }
190                         it = PullingTokenPipe.lastMade.pipe.iterator();
191                         while (it.hasNext()) {
192                             Object JavaDoc o = it.next();
193                             if (o != null) {
194                                 Token t = (Token) o;
195                                 System.err.println("Pipe: " + t + " "
196                                         + t.location);
197                             }
198                         }
199                     }
200                     RDFParser rdfp = ((SingleThreadedParser) PullingTokenPipe.lastMade.arp).rdfParser;
201                     Token t = rdfp.startAttr;
202                     if (t != null)
203                         System.err
204                                 .println("startAttr: " + t + " " + t.location);
205                     t = rdfp.token;
206                     if (t != null)
207                         System.err.println("token: " + t + " " + t.location);
208                     t = rdfp.jj_nt;
209                     if (t != null)
210                         System.err.println("jj_nt: " + t + " " + t.location);
211 // change visibility of field.
212
t = rdfp.jj_scanpos;
213                     if (t != null)
214                         System.err.println("jj_scanpos: " + t + " "
215                                 + t.location);
216                     t = rdfp.jj_lastpos;
217                     if (t != null)
218                         System.err.println("jj_lastpos: " + t + " "
219                                 + t.location);
220                     if (GIVEUP)
221                         System.exit(0);
222                 }
223             }
224         }
225     }
226
227     /**
228      * An integer that describes the kind of this token. This numbering system
229      * is determined by JavaCCParser, and a table of these numbers is stored in
230      * the file ...Constants.java.
231      */

232     public int kind;
233
234     /**
235      * A reference to the next regular (non-special) token from the input
236      * stream. If this is the last token from the input stream, or if the token
237      * manager has not read tokens beyond this one, this field is set to null.
238      */

239     public Token next;
240
241     public Object JavaDoc clone() {
242         try {
243             Token rslt = (Token) super.clone();
244             if (COUNT)
245                 rslt.initCounting();
246             return rslt;
247         } catch (CloneNotSupportedException JavaDoc e) {
248             return "Impossible";
249         }
250     }
251
252     /**
253      * Returns the image.
254      */

255     public String JavaDoc toString() {
256         return tokenImage[kind];
257     }
258
259     /**
260      * Returns a new Token object, by default. However, if you want, you can
261      * create and return subclass objects based on the value of ofKind. Simply
262      * add the cases to the switch for all those special cases. For example, if
263      * you have a subclass of Token called IDToken that you want to create if
264      * ofKind is ID, simlpy add something like :
265      *
266      * case MyParserConstants.ID : return new IDToken();
267      *
268      * to the following switch statement. Then you can cast matchedToken
269      * variable to the appropriate type and use it in your lexical actions.
270      * public static final Token newToken(int ofKind) { switch(ofKind) { default :
271      * return new Token(); } }
272      */

273
274 }
Popular Tags