01: package com.rimfaxe.xml.xmlreader;
02:
03: /**
04: * Circular character buffer used to store parsing history for debug
05: * purposes.
06:
07: <blockquote><small> Copyright (C) 2002 Hewlett-Packard Company.
08: This file is part of Sparta, an XML Parser, DOM, and XPath library.
09: This library is free software; you can redistribute it and/or
10: modify it under the terms of the GNU Lesser General Public License
11: as published by the Free Software Foundation; either version 2.1 of
12: the License, or (at your option) any later version. This library
13: is distributed in the hope that it will be useful, but WITHOUT ANY
14: WARRANTY; without even the implied warranty of MERCHANTABILITY or
15: FITNESS FOR A PARTICULAR PURPOSE.</small></blockquote>
16: @see <a "href="doc-files/LGPL.txt">GNU Lesser General Public License</a>
17: @version $Date: 2002/08/19 05:04:02 $ $Revision: 1.1.1.1 $
18: @author Sergio Marti
19:
20: */
21:
22: class CharCircBuffer {
23:
24: CharCircBuffer(int n) {
25: buf_ = new int[n];
26: }
27:
28: void enable() {
29: enabled_ = true;
30: }
31:
32: void disable() {
33: enabled_ = false;
34: }
35:
36: void addInt(int i) {
37: addRaw(i + 0x10000);
38: }
39:
40: void addChar(char ch) {
41: addRaw(ch);
42: }
43:
44: private void addRaw(int v) {
45: if (enabled_) {
46: buf_[next_] = v;
47: next_ = (next_ + 1) % buf_.length;
48: ++total_;
49: }
50: }
51:
52: void addString(String s) {
53: char[] chars = s.toCharArray();
54: int slen = chars.length;
55: for (int i = 0; i < slen; ++i)
56: addChar(chars[i]);
57: }
58:
59: public String toString() {
60: StringBuffer result = new StringBuffer(
61: (int) (buf_.length * 1.1));
62: int first_ = total_ < buf_.length ? buf_.length - total_ : 0;
63: for (int i = first_; i < buf_.length; ++i) {
64: int ii = (i + next_) % buf_.length;
65: int v = buf_[ii];
66: if (v < 0x10000)
67: result.append((char) v);
68: else
69: result.append(Integer.toString(v - 0x10000));
70: }
71: return result.toString();
72: }
73:
74: private final int[] buf_; //Stores either the chars or the integers+0x10000
75: private int next_ = 0;
76: private int total_ = 0;
77: private boolean enabled_ = true;
78: }
79:
80: // $Log: CharCircBuffer.java,v $
81: // Revision 1.1.1.1 2002/08/19 05:04:02 eobrain
82: // import from HP Labs internal CVS
83: //
84: // Revision 1.2 2002/08/18 04:31:45 eob
85: // Add copyright and other formatting and commenting in preparation for
86: // release to SourceForge.
87: //
88: // Revision 1.1 2002/08/01 23:29:17 sermarti
89: // Much faster Sparta parsing.
90: // Has debug features enabled by default. Currently toggled
91: // in ParseCharStream.java and recompiled.
|