KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > cvsclient > command > annotate > AnnotateInformation


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.lib.cvsclient.command.annotate;
21
22 import java.io.*;
23 import java.util.*;
24
25 import org.netbeans.lib.cvsclient.command.*;
26
27 /**
28  * Describes annotate information for a file. This is the result of doing a
29  * cvs annotate command. The fields in instances of this object are populated
30  * by response handlers.
31  * @author Milos Kleint
32  */

33 public class AnnotateInformation extends FileInfoContainer {
34     /**
35      * The file, associated with thiz.
36      */

37     private File file;
38
39     /**
40      * List of lines stored here.
41      */

42     private List linesList;
43
44     private Iterator iterator;
45
46     private File tempFile;
47     
48     private File tempDir;
49
50     private BufferedOutputStream tempOutStream;
51
52     public AnnotateInformation() {
53         this.tempDir = null;
54     }
55
56     public AnnotateInformation(File tempDir) {
57         this.tempDir = tempDir;
58     }
59
60     /**
61      * Getter for property file.
62      * @return Value of property file.
63      */

64     public File getFile() {
65         return file;
66     }
67
68     /**
69      * Setter for property file.
70      * @param file New value of property file.
71      */

72     public void setFile(File file) {
73         this.file = file;
74     }
75
76     /**
77      * Return a string representation of this object. Useful for debugging.
78      */

79     public String JavaDoc toString() {
80         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(30);
81         buf.append("\nFile: " + ((file != null)?file.getAbsolutePath():"null")); //NOI18N
82
return buf.toString();
83     }
84
85     public AnnotateLine createAnnotateLine() {
86         return new AnnotateLine();
87     }
88
89     public void addLine(AnnotateLine line) {
90         linesList.add(line);
91     }
92
93     public AnnotateLine getFirstLine() {
94         if (linesList == null) {
95             linesList = createLinesList();
96         }
97         iterator = linesList.iterator();
98         return getNextLine();
99     }
100
101     public AnnotateLine getNextLine() {
102         if (iterator == null) {
103             return null;
104         }
105         if (!iterator.hasNext()) {
106             return null;
107         }
108         return (AnnotateLine)iterator.next();
109     }
110
111     /**
112      * Adds the specified line to the temporary file.
113      */

114     protected void addToTempFile(String JavaDoc line) throws IOException {
115         if (tempOutStream == null) {
116             try {
117                 tempFile = File.createTempFile("ann", ".cvs", tempDir); //NOI18N
118
tempFile.deleteOnExit();
119                 tempOutStream = new BufferedOutputStream(
120                         new FileOutputStream(tempFile));
121             }
122             catch (IOException ex) {
123                 // TODO
124
}
125         }
126         tempOutStream.write(line.getBytes());
127         tempOutStream.write('\n');
128     }
129
130     protected void closeTempFile() throws IOException {
131         if (tempOutStream == null) {
132             return;
133         }
134         try {
135             tempOutStream.flush();
136         } finally {
137             tempOutStream.close();
138         }
139     }
140
141     public File getTempFile() {
142         return tempFile;
143     }
144
145     private List createLinesList() {
146         List toReturn = new LinkedList();
147         BufferedReader reader = null;
148         if (tempFile == null) {
149             return toReturn;
150         }
151         try {
152             reader = new BufferedReader(new FileReader(tempFile));
153             String JavaDoc line = reader.readLine();
154             int lineNum = 1;
155             while (line != null) {
156                 AnnotateLine annLine = AnnotateBuilder.processLine(line);
157                 if (annLine != null) {
158                     annLine.setLineNum(lineNum);
159                     toReturn.add(annLine);
160                     lineNum++;
161                 }
162                 line = reader.readLine();
163             }
164         }
165         catch (IOException exc) {
166         }
167         finally {
168             try {
169                 if (reader != null) {
170                     reader.close();
171                 }
172             }
173             catch (IOException ex2) {
174             }
175         }
176         return toReturn;
177     }
178
179 }
180
Popular Tags