KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > go > trove > io > TagReader


1 /* ====================================================================
2  * Trove - Copyright (c) 1997-2000 Walt Disney Internet Group
3  * ====================================================================
4  * The Tea Software License, Version 1.1
5  *
6  * Copyright (c) 2000 Walt Disney Internet Group. All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Walt Disney Internet Group (http://opensource.go.com/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Tea", "TeaServlet", "Kettle", "Trove" and "BeanDoc" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact opensource@dig.com.
31  *
32  * 5. Products derived from this software may not be called "Tea",
33  * "TeaServlet", "Kettle" or "Trove", nor may "Tea", "TeaServlet",
34  * "Kettle", "Trove" or "BeanDoc" appear in their name, without prior
35  * written permission of the Walt Disney Internet Group.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE WALT DISNEY INTERNET GROUP OR ITS
41  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
42  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
43  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
44  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
45  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
46  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
47  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48  * ====================================================================
49  *
50  * For more information about Tea, please see http://opensource.go.com/.
51  */

52
53 package com.go.trove.io;
54
55 import java.io.*;
56
57 /******************************************************************************
58  *
59  * @author Brian S O'Neill
60  * @version
61  * <!--$$Revision:--> 12 <!-- $-->, <!--$$JustDate:--> 12/11/00 <!-- $-->
62  */

63 public class TagReader extends EscapeReader {
64     private int mTagCount;
65     private int[] mTagStarts;
66     private String JavaDoc[] mTagEnds;
67     private int[] mCodes;
68
69     private char[] mMinibuf;
70
71     private static int maxLength(String JavaDoc[] tags) {
72         int max = 0;
73
74         for (int i=0; i<tags.length; i++) {
75             if (tags[i].length() > max) {
76                 max = tags[i].length();
77             }
78         }
79
80         return max;
81     }
82
83     public TagReader(Reader source, String JavaDoc[] tags, int[] codes) {
84         super(source, maxLength(tags));
85
86         mTagCount = tags.length;
87         mTagStarts = new int[mTagCount];
88         mTagEnds = new String JavaDoc[mTagCount];
89         mCodes = new int[mTagCount];
90
91         for (int i=0; i<mTagCount; i++) {
92             mTagStarts[i] = tags[i].charAt(0);
93             mTagEnds[i] = tags[i].substring(1);
94             mCodes[i] = codes[i];
95         }
96         
97         mMinibuf = new char[maxLength(tags)];
98     }
99
100     public int read() throws IOException {
101         int c = mSource.read();
102
103         if (c == -1 || !mEscapesEnabled) {
104             return c;
105         }
106
107         for (int i=0; i<mTagCount; i++) {
108             if (mTagStarts[i] == c) {
109                 int length = mTagEnds[i].length();
110
111                 mMinibuf[0] = (char)c;
112                 int len = mSource.read(mMinibuf, 0, length);
113
114                 if (len == length) {
115                     if (new String JavaDoc(mMinibuf, 0, length).equals(mTagEnds[i])) {
116                         return mCodes[i];
117                     }
118                 }
119      
120                 if (len > 0) {
121                     mSource.unread(len);
122                 }
123             }
124         }
125
126         return c;
127     }
128
129     public static void main(String JavaDoc[] arg) throws Exception JavaDoc {
130         Tester.test(arg);
131     }
132
133     private static class Tester {
134         public static void test(String JavaDoc[] arg) throws Exception JavaDoc {
135             String JavaDoc str = "This <%is a %> % > > % %% >> < % test.\n";
136
137             System.out.println("\nOriginal: " + str);
138
139             System.out.println("\nConverted:\n");
140             
141             Reader reader = new StringReader(str);
142             
143             TagReader tr = new TagReader
144                 (reader, new String JavaDoc[] {"<%", "%>"}, new int[] {-2, -3});
145
146             PositionReader pr = new PositionReader(tr);
147
148             int c;
149             System.out.print(pr.getNextPosition() + "\t");
150             while ( (c = pr.read()) != -1 ) {
151                 System.out.println((char)c + "\t" + c);
152                 System.out.print(pr.getNextPosition() + "\t");
153             }
154         }
155     }
156 }
157
Popular Tags