KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > retouche > source > ModificationResult


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.api.retouche.source;
21
22 import java.io.*;
23 import java.util.*;
24 import javax.swing.text.BadLocationException JavaDoc;
25 import javax.swing.text.Document JavaDoc;
26
27 import org.openide.cookies.EditorCookie;
28 import org.openide.filesystems.FileObject;
29 import org.openide.filesystems.FileUtil;
30 import org.openide.loaders.DataObject;
31 import org.openide.text.PositionRef;
32
33 /**
34  * This file is originally from Retouche, the Java Support
35  * infrastructure in NetBeans. I have modified the file as little
36  * as possible to make merging Retouche fixes back as simple as
37  * possible.
38  *
39  * Class that collects changes built during a modification task run.
40  *
41  * @author Dusan Balek
42  */

43 public final class ModificationResult {
44
45     Map<FileObject, List<Difference>> diffs = new HashMap<FileObject, List<Difference>>();
46     
47     /** Creates a new instance of ModificationResult */
48     ModificationResult() {
49     }
50
51     // API of the class --------------------------------------------------------
52

53     public Set<? extends FileObject> getModifiedFileObjects() {
54         return diffs.keySet();
55     }
56     
57     public List<? extends Difference> getDifferences(FileObject fo) {
58         return diffs.get(fo);
59     }
60     
61     /**
62      * Once all of the changes have been collected, this method can be used
63      * to commit the changes to the source files
64      */

65     public void commit() throws IOException {
66         for (Map.Entry<FileObject, List<Difference>> me : diffs.entrySet()) {
67             FileObject fo = me.getKey();
68             DataObject dObj = DataObject.find(fo);
69             EditorCookie ec = dObj != null ? (EditorCookie) dObj.getCookie(EditorCookie.class) : null;
70             if (ec != null) {
71                 Document JavaDoc doc = ec.getDocument();
72                 if (doc != null) {
73                     for (Difference diff : me.getValue()) {
74                         if (diff.isExcluded())
75                             continue;
76                         try {
77                             switch (diff.getKind()) {
78                                 case INSERT:
79                                     doc.insertString(diff.getStartPosition().getOffset(), diff.getNewText(), null);
80                                     break;
81                                 case REMOVE:
82                                     doc.remove(diff.getStartPosition().getOffset(), diff.getEndPosition().getOffset() - diff.getStartPosition().getOffset());
83                                     break;
84                                 case CHANGE:
85                                     doc.remove(diff.getStartPosition().getOffset(), diff.getEndPosition().getOffset() - diff.getStartPosition().getOffset());
86                                     doc.insertString(diff.getStartPosition().getOffset(), diff.getNewText(), null);
87                                     break;
88                             }
89                         } catch (BadLocationException JavaDoc ex) {
90                             IOException ioe = new IOException();
91                             ioe.initCause(ex);
92                             throw ioe;
93                         }
94                     }
95                     continue;
96                 }
97             }
98             InputStream ins = null;
99             ByteArrayOutputStream baos = null;
100             Reader in = null;
101             Writer out = null;
102             try {
103                 ins = fo.getInputStream();
104                 baos = new ByteArrayOutputStream();
105                 FileUtil.copy(ins, baos);
106                 
107                 ins.close();
108                 ins = null;
109                 byte[] arr = baos.toByteArray();
110                 baos.close();
111                 baos = null;
112                 in = new InputStreamReader(new ByteArrayInputStream(arr));
113                 out = new OutputStreamWriter(fo.getOutputStream());
114                 int offset = 0;
115                 for (Difference diff : me.getValue()) {
116                     if (diff.isExcluded())
117                         continue;
118                     int pos = diff.getStartPosition().getOffset();
119                     char[] buff = new char[pos - offset];
120                     int n;
121                     if ((n = in.read(buff)) > 0) {
122                         out.write(buff, 0, n);
123                         offset += n;
124                     }
125                     switch (diff.getKind()) {
126                         case INSERT:
127                             out.write(diff.getNewText());
128                             break;
129                         case REMOVE:
130                             int len = diff.getEndPosition().getOffset() - diff.getStartPosition().getOffset();
131                             in.skip(len);
132                             offset += len;
133                             break;
134                         case CHANGE:
135                             len = diff.getEndPosition().getOffset() - diff.getStartPosition().getOffset();
136                             in.skip(len);
137                             offset += len;
138                             out.write(diff.getNewText());
139                             break;
140                     }
141                 }
142                 char[] buff = new char[1024];
143                 int n;
144                 while ((n = in.read(buff)) > 0)
145                     out.write(buff, 0, n);
146             } finally {
147                 if (ins != null)
148                     ins.close();
149                 if (baos != null)
150                     baos.close();
151                 if (in != null)
152                     in.close();
153                 if (out != null)
154                     out.close();
155             }
156         }
157     }
158     
159     public static final class Difference {
160         Kind kind;
161         PositionRef startPos;
162         PositionRef endPos;
163         String JavaDoc oldText;
164         String JavaDoc newText;
165         private boolean excluded;
166
167         Difference(Kind kind, PositionRef startPos, PositionRef endPos, String JavaDoc oldText, String JavaDoc newText) {
168             this.kind = kind;
169             this.startPos = startPos;
170             this.endPos = endPos;
171             this.oldText = oldText;
172             this.newText = newText;
173             this.excluded = false;
174         }
175         
176         public Kind getKind() {
177             return kind;
178         }
179         
180         public PositionRef getStartPosition() {
181             return startPos;
182         }
183         
184         public PositionRef getEndPosition() {
185             return endPos;
186         }
187         
188         public String JavaDoc getOldText() {
189             return oldText;
190         }
191         
192         public String JavaDoc getNewText() {
193             return newText;
194         }
195         
196         public boolean isExcluded() {
197             return excluded;
198         }
199         
200         public void exclude(boolean b) {
201             excluded = b;
202         }
203
204         public String JavaDoc toString() {
205             return kind + "<" + startPos.getOffset() + ", " + endPos.getOffset() + ">: " + oldText + " -> " + newText;
206         }
207
208         public static enum Kind {
209             INSERT,
210             REMOVE,
211             CHANGE
212         }
213     }
214 }
215
Popular Tags