KickJava   Java API By Example, From Geeks To Geeks.

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


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.BufferedWriter JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.FileNotFoundException JavaDoc;
25 import java.io.FileOutputStream JavaDoc;
26 import java.io.FileWriter JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.OutputStreamWriter JavaDoc;
29 import java.io.PrintWriter JavaDoc;
30 import java.io.Writer JavaDoc;
31 import javax.swing.text.BadLocationException JavaDoc;
32 import javax.tools.JavaFileObject;
33 import org.openide.filesystems.FileObject;
34 import org.openide.filesystems.FileUtil;
35
36 /**
37  * Manages writing source file changes to a copy of the original source file.
38  */

39 public class FileSourceRewriter implements SourceRewriter {
40     JavaFileObject sourcefile;
41     PrintWriter JavaDoc out;
42     File JavaDoc outFile;
43
44     public FileSourceRewriter(JavaFileObject sourcefile) throws IOException JavaDoc {
45         this(sourcefile, null);
46     }
47
48     public FileSourceRewriter(JavaFileObject sourcefile, String JavaDoc encoding) throws IOException JavaDoc {
49         this.sourcefile = sourcefile;
50         String JavaDoc srcFile = sourcefile.toString();
51         File JavaDoc f = new File JavaDoc(srcFile);
52         if (!f.exists())
53             throw new FileNotFoundException JavaDoc(srcFile);
54         if (!f.canWrite())
55             throw new IOException JavaDoc("cannot write to " + srcFile);
56
57         outFile = new File JavaDoc(sourcefile.toUri().getPath() + ".tmp");
58         Writer JavaDoc fileWriter = (encoding != null && encoding.length() > 0) ?
59             new OutputStreamWriter JavaDoc(new FileOutputStream JavaDoc(outFile), encoding) :
60             new FileWriter JavaDoc(outFile);
61         out = new PrintWriter JavaDoc(new BufferedWriter JavaDoc(fileWriter));
62     }
63     
64     public void writeTo(String JavaDoc s) throws IOException JavaDoc, BadLocationException JavaDoc {
65         out.print(s);
66     }
67
68     public void skipThrough(SourceReader in, int offset) throws IOException JavaDoc, BadLocationException JavaDoc {
69         in.seek(offset);
70     }
71
72     public void copyTo(SourceReader in, int offset) throws IOException JavaDoc {
73         char[] buf = in.getCharsTo(offset);
74         out.write(buf);
75     }
76
77     public void copyRest(SourceReader in) throws IOException JavaDoc {
78         char[] buf = new char[4096];
79         int i;
80         while ((i = in.read(buf)) > 0)
81             out.write(buf, 0, i);
82     }
83
84     public void close(boolean save) throws IOException JavaDoc {
85         out.close();
86         out = null;
87         if (save) {
88             String JavaDoc path = sourcefile.toUri().getPath();
89             File JavaDoc f = new File JavaDoc(path);
90             File JavaDoc old = new File JavaDoc(path + '~');
91             if (old.exists())
92                 if (!old.delete())
93                     throw new IOException JavaDoc("failed deleting backup file: " + old);
94             if (!f.renameTo(old))
95                 throw new IOException JavaDoc("failed renaming (" + path +
96                                       ") to backup (" + old + ")");
97             f = new File JavaDoc(path);
98             if (!outFile.renameTo(f))
99                 throw new IOException JavaDoc("failed renaming new output file (" + outFile +
100                                       ") to path (" + path + ")");
101             outFile = f;
102
103             FileObject fo = FileUtil.toFileObject(outFile);
104             if (fo != null) {
105                 fo.refresh(true);
106             }
107         }
108     }
109 }
110
Popular Tags