KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > guards > GuardedReader


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.editor.guards;
21
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.io.InputStreamReader JavaDoc;
25 import java.io.Reader JavaDoc;
26 import java.io.UnsupportedEncodingException JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import org.netbeans.spi.editor.guards.support.AbstractGuardedSectionsProvider;
29
30 /** This stream is able to filter special guarded comments.
31  * Holding this information is optional and depends on the construction
32  * of this stream - the reason of this feature is that
33  * GuardedReader is used also for parser (and it doesn't require
34  * the storing the guarded block information - just filter the comments).
35  */

36 final class GuardedReader extends Reader JavaDoc {
37
38     
39     /** Encapsulated reader */
40     Reader JavaDoc reader;
41     
42     private NewLineInputStream newLineStream;
43
44     /** Character buffer */
45     char[] charBuff;
46     int howmany;
47
48     /** The flag determining if this stream should store the guarded
49      * block information (list of the SectionDesc).
50      */

51     boolean justFilter;
52
53     /** The position at the current line. */
54     int position;
55
56 // /** The list of the SectionsDesc. */
57
// LinkedList<GuardedSection> list;
58

59 // /** The count of types new line delimiters used in the file */
60
// final int[] newLineTypes;
61

62     
63     private final GuardedSectionsImpl callback;
64
65     private final AbstractGuardedSectionsProvider gr;
66
67     private boolean isClosed = false;
68
69     private AbstractGuardedSectionsProvider.Result result;
70
71     /** Creates new stream.
72      * @param is encapsulated input stream.
73      * @param justFilter The flag determining if this stream should
74      * store the guarded block information. True means just filter,
75      * false means store the information.
76      */

77 // GuardedReader(InputStream is, boolean justFilter) throws UnsupportedEncodingException {
78
// this(is, justFilter, null);
79
// }
80
//
81
GuardedReader(AbstractGuardedSectionsProvider gr, InputStream JavaDoc is, boolean justFilter, String JavaDoc encoding, GuardedSectionsImpl guards) throws UnsupportedEncodingException JavaDoc {
82         newLineStream = new NewLineInputStream(is);
83         if (encoding == null)
84             reader = new InputStreamReader JavaDoc(newLineStream);
85         else
86             reader = new InputStreamReader JavaDoc(newLineStream, encoding);
87         this.justFilter = justFilter;
88 // list = new LinkedList<SectionDescriptor>();
89
// newLineTypes = new int[] { 0, 0, 0 };
90
this.callback = guards;
91         this.gr = gr;
92     }
93
94     /** Read the array of chars */
95     public int read(char[] cbuf, int off, int len) throws IOException JavaDoc {
96
97         if (charBuff == null) {
98             char[] readBuff = readCharBuff();
99             this.result = this.gr.readSections(readBuff);
100             charBuff = this.result.getContent();
101             howmany = charBuff.length;
102         }
103
104         if (howmany <= 0) {
105             return -1;
106         } else {
107             int min = Math.min(len, howmany);
108             System.arraycopy(charBuff, position, cbuf, off, min);
109             howmany -= min;
110             position += min;
111             return min;
112         }
113     }
114
115     /** Reads readBuff */
116     final char[] readCharBuff() throws IOException JavaDoc {
117
118         char[] readBuff;
119         char[] tmp = new char[2048];
120         int read;
121         ArrayList JavaDoc<char[]> buffs = new ArrayList JavaDoc<char[]>(20);
122
123         for (; ; ) {
124             read = readFully(tmp);
125             buffs.add(tmp);
126             if (read < 2048) {
127                 break;
128             } else {
129                 tmp = new char[2048];
130             }
131         }
132
133         int listsize = buffs.size() - 1;
134         int size = listsize * 2048 + read;
135         readBuff = new char[size];
136         charBuff = new char[size];
137         int copy = 0;
138
139         for (int i = 0; i < listsize; i++) {
140             char[] tmp2 = (char[]) buffs.get(i);
141             System.arraycopy(tmp2, 0, readBuff, copy, 2048);
142             copy += 2048;
143         }
144         System.arraycopy(tmp, 0, readBuff, copy, read);
145         return readBuff;
146     }
147
148     /** reads fully given buffer */
149     final int readFully(final char[] buff) throws IOException JavaDoc {
150         int read = 0;
151         int sum = 0;
152
153         do {
154             read = reader.read(buff, sum, buff.length - sum);
155             sum += read;
156         } while ((sum < buff.length) && (read > 0));
157
158         return sum + 1;
159     }
160
161     /** Close underlying writer. */
162     public void close() throws IOException JavaDoc {
163         if (!isClosed) {
164             isClosed = true;
165             reader.close();
166             callback.fillSections(this.result.getGuardedSections(), newLineStream.getNewLineType());
167         }
168     }
169     
170     private final class NewLineInputStream extends InputStream JavaDoc {
171
172         private final InputStream JavaDoc stream;
173         
174         private int b;
175         
176         private int lookahead;
177         private boolean isLookahead = false;
178     
179         /** The count of types new line delimiters used in the file */
180         final int[] newLineTypes;
181         
182         public NewLineInputStream(InputStream JavaDoc source) {
183             this.stream = source;
184             this.newLineTypes = new int[] { 0, 0, 0 };
185         }
186         
187         /** @return most frequent type of new line delimiter */
188         public NewLine getNewLineType() {
189             // special case: an empty file (all newline types equal)
190
if (newLineTypes[NewLine.N.ordinal()] == newLineTypes[NewLine.R.ordinal()] &&
191                     newLineTypes[NewLine.R.ordinal()] == newLineTypes[NewLine.RN.ordinal()]) {
192                 
193                 String JavaDoc s = System.getProperty("line.separator"); // NOI18N
194
return NewLine.resolve(s);
195             }
196             if (newLineTypes[NewLine.N.ordinal()] > newLineTypes[NewLine.R.ordinal()]) {
197                 return (newLineTypes[NewLine.N.ordinal()] > newLineTypes[NewLine.RN.ordinal()]) ? NewLine.N : NewLine.RN;
198             } else {
199                 return (newLineTypes[NewLine.R.ordinal()] > newLineTypes[NewLine.RN.ordinal()]) ? NewLine.R : NewLine.RN;
200             }
201         }
202         
203         public int read() throws IOException JavaDoc {
204             
205             b = isLookahead? lookahead: this.stream.read();
206             isLookahead = false;
207             
208             switch (b) {
209                 case (int) '\n':
210                     newLineTypes[NewLine.N.ordinal()/*NEW_LINE_N*/]++;
211                     return b;
212                 case (int) '\r':
213                     lookahead = this.stream.read();
214                     if (lookahead != (int) '\n') {
215                         newLineTypes[NewLine.R.ordinal()/*NEW_LINE_R*/]++;
216                         isLookahead = true;
217                     } else {
218                         newLineTypes[NewLine.RN.ordinal()/*NEW_LINE_RN*/]++;
219                     }
220                     return (int) '\n';
221                 default:
222                     return b;
223             }
224             
225         }
226
227         public void close() throws IOException JavaDoc {
228             super.close();
229             this.stream.close();
230         }
231         
232     }
233
234 }
235
Popular Tags