KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > internal > search > ParsedDocument


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.help.internal.search;
12
13 import java.io.*;
14
15 /**
16  * Parsed Document. It can be used to obtain multiple readers for the same
17  * document.
18  */

19 public class ParsedDocument {
20     // Limit on how many characters will be indexed
21
// from a large document
22
private static final int charsLimit = 1000000;
23     Reader reader;
24     boolean read;
25     char[] docChars;
26
27     /**
28      * Constructor for ParsedDocument.
29      *
30      * @param reader
31      * reader obtained from the parser
32      */

33     public ParsedDocument(Reader reader) {
34         this.reader = reader;
35         this.read = false;
36     }
37     public Reader newContentReader() {
38         if (!read) {
39             read = true;
40             readDocument();
41         }
42         return new CharArrayReader(docChars);
43     }
44     private void readDocument() {
45         CharArrayWriter writer = new CharArrayWriter();
46         char[] buf = new char[4096];
47         int n;
48         int charsWritten = 0;
49         try {
50             while (0 <= (n = reader.read(buf))) {
51                 if (charsWritten < charsLimit) {
52                     if (n > charsLimit - charsWritten) {
53                         // do not exceed the specified limit of characters
54
writer.write(buf, 0, charsLimit - charsWritten);
55                         charsWritten = charsLimit;
56                     } else {
57                         writer.write(buf, 0, n);
58                         charsWritten += n;
59                     }
60                 } else {
61                     // do not break out of the loop
62
// keep reading to avoid breaking pipes
63
}
64             }
65         } catch (IOException ioe) {
66             // do not do anything, will use characters read so far
67
} finally {
68             try {
69                 reader.close();
70             } catch (IOException ioe2) {
71             }
72         }
73         docChars = writer.toCharArray();
74     }
75 }
76
Popular Tags