KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > source > engine > StringSourceRewriter


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.java.source.engine;
21
22 import java.io.IOException JavaDoc;
23 import java.io.PrintWriter JavaDoc;
24 import java.io.StringWriter JavaDoc;
25 import javax.swing.text.BadLocationException JavaDoc;
26
27 /**
28  * Manages writing source file changes to a buffer, which toString() returns.
29  */

30 public class StringSourceRewriter implements SourceRewriter {
31     PrintWriter JavaDoc out;
32     StringWriter JavaDoc sout;
33
34     public StringSourceRewriter() {
35         sout = new StringWriter JavaDoc();
36         out = new PrintWriter JavaDoc(sout);
37     }
38
39     public void writeTo(String JavaDoc s) throws IOException JavaDoc, BadLocationException JavaDoc {
40         out.print(s);
41     }
42
43     public void skipThrough(SourceReader in, int offset) throws IOException JavaDoc, BadLocationException JavaDoc {
44         in.seek(offset);
45     }
46
47     public void copyTo(SourceReader in, int offset) throws IOException JavaDoc {
48         char[] buf = in.getCharsTo(offset);
49         out.write(buf);
50     }
51
52     public void copyRest(SourceReader in) throws IOException JavaDoc {
53         char[] buf = new char[4096];
54         int i;
55         while ((i = in.read(buf)) > 0)
56             out.write(buf, 0, i);
57     }
58
59     public void close(boolean save) throws IOException JavaDoc {
60         out.flush();
61     }
62
63     /**
64      * Returns the output written to this SourceRewriter.
65      */

66     public String JavaDoc toString() {
67         return sout.toString();
68     }
69 }
70
Popular Tags