Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
XMLTokenTableBase |
|
| 2.4285714285714284;2.429 |
1 | /* $Id: XMLTokenTableBase.java 17832 2010-01-12 19:02:29Z linus $ | |
2 | ***************************************************************************** | |
3 | * Copyright (c) 2009 Contributors - see below | |
4 | * All rights reserved. This program and the accompanying materials | |
5 | * are made available under the terms of the Eclipse Public License v1.0 | |
6 | * which accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
9 | * Contributors: | |
10 | * bobtarling | |
11 | ***************************************************************************** | |
12 | * | |
13 | * Some portions of this file was previously release using the BSD License: | |
14 | */ | |
15 | ||
16 | // Copyright (c) 1996-2006 The Regents of the University of California. All | |
17 | // Rights Reserved. Permission to use, copy, modify, and distribute this | |
18 | // software and its documentation without fee, and without a written | |
19 | // agreement is hereby granted, provided that the above copyright notice | |
20 | // and this paragraph appear in all copies. This software program and | |
21 | // documentation are copyrighted by The Regents of the University of | |
22 | // California. The software program and documentation are supplied "AS | |
23 | // IS", without any accompanying services from The Regents. The Regents | |
24 | // does not warrant that the operation of the program will be | |
25 | // uninterrupted or error-free. The end-user understands that the program | |
26 | // was developed for research purposes and is advised not to rely | |
27 | // exclusively on the program for any reason. IN NO EVENT SHALL THE | |
28 | // UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, | |
29 | // SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, | |
30 | // ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF | |
31 | // THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF | |
32 | // SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY | |
33 | // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | |
34 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE | |
35 | // PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF | |
36 | // CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT, | |
37 | // UPDATES, ENHANCEMENTS, OR MODIFICATIONS. | |
38 | ||
39 | package org.argouml.persistence; | |
40 | ||
41 | import java.util.Hashtable; | |
42 | ||
43 | import org.apache.log4j.Logger; | |
44 | ||
45 | /** | |
46 | * @author Jim Holy | |
47 | */ | |
48 | ||
49 | abstract class XMLTokenTableBase { | |
50 | 0 | private static final Logger LOG = Logger.getLogger(XMLTokenTableBase.class); |
51 | ||
52 | //////////////////////////////////////////////////////////////// | |
53 | // instance variables | |
54 | ||
55 | 0 | private Hashtable tokens = null; |
56 | 0 | private boolean dbg = false; |
57 | 0 | private String[] openTags = new String[100]; |
58 | 0 | private int[] openTokens = new int[100]; |
59 | 0 | private int numOpen = 0; |
60 | ||
61 | ||
62 | //////////////////////////////////////////////////////////////// | |
63 | // constructors | |
64 | ||
65 | /** | |
66 | * The constructor. | |
67 | * | |
68 | * @param tableSize the size of the table | |
69 | */ | |
70 | 0 | public XMLTokenTableBase(int tableSize) { |
71 | 0 | tokens = new Hashtable(tableSize); |
72 | 0 | setupTokens(); |
73 | 0 | } |
74 | ||
75 | //////////////////////////////////////////////////////////////// | |
76 | // accessors | |
77 | ||
78 | /** | |
79 | * @param s the string | |
80 | * @param push true if the token is to be pushed | |
81 | * @return the token | |
82 | */ | |
83 | public final int toToken(String s, boolean push) { | |
84 | 0 | if (push) openTags[++numOpen] = s; |
85 | 0 | else if (s.equals(openTags[numOpen])) { |
86 | 0 | LOG.debug("matched: " + s); |
87 | 0 | return openTokens[numOpen--]; |
88 | } | |
89 | 0 | Integer i = (Integer) tokens.get(s); |
90 | 0 | if (i != null) { |
91 | 0 | openTokens[numOpen] = i.intValue(); |
92 | 0 | return openTokens[numOpen]; |
93 | } | |
94 | 0 | else return -1; |
95 | } | |
96 | ||
97 | /** | |
98 | * @param d true if debugging | |
99 | */ | |
100 | 0 | public void setDbg(boolean d) { dbg = d; } |
101 | ||
102 | /** | |
103 | * @return true if debugging is turned on | |
104 | */ | |
105 | 0 | public boolean getDbg() { return dbg; } |
106 | ||
107 | //////////////////////////////////////////////////////////////// | |
108 | // class methods | |
109 | ||
110 | /** | |
111 | * @param s the string represented by the token number | |
112 | * @param i the token number | |
113 | */ | |
114 | protected void addToken(String s, Integer i) { | |
115 | 0 | boolean error = false; |
116 | 0 | if (dbg) { |
117 | 0 | if (tokens.contains(i) || tokens.containsKey(s)) { |
118 | 0 | LOG.error("ERROR: token table already contains " + s); |
119 | 0 | error = true; |
120 | } | |
121 | } | |
122 | 0 | tokens.put(s, i); |
123 | 0 | if (dbg && !error) { |
124 | 0 | LOG.debug("NOTE: added '" + s + "' to token table"); |
125 | } | |
126 | 0 | } |
127 | ||
128 | /** | |
129 | * @param token the given token | |
130 | * @return true if the token is present | |
131 | */ | |
132 | public boolean contains(String token) { | |
133 | 0 | return tokens.containsKey(token); |
134 | } | |
135 | ||
136 | /** | |
137 | * This function shall set up all the tokens with the addToken function. | |
138 | */ | |
139 | protected abstract void setupTokens(); | |
140 | ||
141 | } /* end class XMLTokenTableBase */ |