View Javadoc

1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.mirae.j2me.xml;
18  
19  import java.io.IOException;
20  import java.io.Reader;
21  
22  import org.apache.mirae.j2me.xml.sax.LocatorImpl;
23  
24  /***
25   * This is designed to work for CLDC just like PushbackReader of CDC and J2SE
26   * @author Ias (iasandcb@tmax.co.kr)
27   */
28  public class PushBackReader {
29  
30      /*** default buffer size */
31  	public static final int DEFAULT_BUFFER_SIZE = 1024;
32  	private Reader reader;
33  	private char[] buffer;
34  	private int pointer;
35  	private int length;
36  	private boolean eof;
37  	private LocatorImpl locator;
38  	
39      /*** 
40       * Constructor with reader and locator
41       * @param reader
42       * @param locator
43       */
44  	public PushBackReader(Reader reader, LocatorImpl locator) {
45  		this.reader = reader;
46  		this.locator = locator;
47  		buffer = new char[DEFAULT_BUFFER_SIZE];
48  	}
49  	
50      /***
51       * Read a single character.
52       */
53  	public char read() throws IOException {
54  		char result;
55  		if (pointer == length) {
56  			length = reader.read(buffer);
57  			if (length == -1) {
58  				eof = true;
59  				return (char) length;
60  			}
61  			pointer = 0;			
62  		}
63  		result = buffer[pointer];
64  		pointer++;
65  		switch (result) {
66  			case '\t':
67  				locator.addColumnNumber(8);
68  				break;
69  			case '\n':
70  				locator.incrementLineNumber();
71  				break;
72  			default:
73  				locator.incrementColumnNumber();
74  		}
75  		return result;
76  	}
77  	
78      /***
79       * Push a String to this reader
80       * @param insertString
81       */
82  	public void addString(String insertString) {
83  		int insertStringLength = insertString.length();
84  		int bufferLength = buffer.length;
85  		char[] enlargedBuffer = new char[bufferLength + insertStringLength];
86  		System.arraycopy(buffer, 0, enlargedBuffer, 0, pointer);
87  		System.arraycopy(insertString.toCharArray(), 0, enlargedBuffer, pointer, insertStringLength);
88  		System.arraycopy(buffer, pointer, enlargedBuffer, pointer + insertStringLength, bufferLength - pointer - insertStringLength);
89  		buffer = enlargedBuffer;
90  		length += insertStringLength;
91  	}
92  	
93      /***
94       * Push back a single character.
95       *
96       */
97  	public void unread() {
98  		pointer--;
99  		switch (buffer[pointer]) {
100 			case '\t':
101 				locator.addColumnNumber(-8);
102 				break;
103 			case '\n':
104 				locator.decrementLineNumber();
105 				break;
106 			default:
107 				locator.addColumnNumber(-1);
108 		}
109 	}
110 
111     /***
112      * check out whether this reader reaches End Of File
113      * @return a boolean value indicating the result
114      */
115 	public boolean isEof() {
116 		return eof;
117 	}
118 }