KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > cvsclient > command > PipedFilesBuilder


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 package org.netbeans.lib.cvsclient.command;
20
21 import java.io.*;
22
23 import org.netbeans.lib.cvsclient.event.*;
24
25 /**
26  * Handles the building of "checkout with -p switch" information object and storing of
27  * the checked out file to the temporary file and the firing of
28  * events when complete objects are built.
29  *
30  * @author Milos Kleint
31  */

32 public class PipedFilesBuilder implements Builder, BinaryBuilder {
33
34     private static final String JavaDoc ERR_START = "======="; //NOI18N
35
private static final String JavaDoc ERR_CHECK = "Checking out "; //NOI18N
36
private static final String JavaDoc ERR_RCS = "RCS: "; //NOI18N
37
private static final String JavaDoc ERR_VERS = "VERS: "; //NOI18N
38
private static final String JavaDoc EXAM_DIR = ": Updating"; //NOI18N
39

40     private static final byte [] lineSeparator = System.getProperty("line.separator").getBytes();
41     
42     /**
43      * The module object that is currently being built.
44      */

45     private PipedFileInformation fileInformation;
46
47     /**
48      * The event manager to use.
49      */

50     private EventManager eventManager;
51
52     /**
53      * The directory in which the file being processed lives.
54      * This is relative to the local directory.
55      */

56     private String JavaDoc fileDirectory;
57
58     private BuildableCommand command;
59
60     private TemporaryFileCreator tempFileCreator;
61
62     /**
63      * Creates a new Builder for the PipeFileResponse.
64      */

65     public PipedFilesBuilder(EventManager eventManager,
66                              BuildableCommand command,
67                              TemporaryFileCreator tempFileCreator) {
68         this.eventManager = eventManager;
69         this.command = command;
70         this.tempFileCreator = tempFileCreator;
71     }
72
73     public void outputDone() {
74         if (fileInformation == null) {
75             return;
76         }
77
78         try {
79             fileInformation.closeTempFile();
80         }
81         catch (IOException exc) {
82             //TODO
83
}
84         eventManager.fireCVSEvent(new FileInfoEvent(this, fileInformation));
85         fileInformation = null;
86     }
87
88     public void parseBytes(byte[] bytes, int len) {
89         if (fileInformation == null) {
90             // HOTFIX there is no header for :local: repositories (thereare two copies in this source)
91
// XXX it might be dangerous because PipedFileInformation stays partialy unitialized
92
try {
93                 fileInformation = new PipedFileInformation(File.createTempFile("checkout", null));
94             } catch (IOException e) {
95                 e.printStackTrace();
96             }
97         }
98
99         try {
100             fileInformation.addToTempFile(bytes, len);
101         }
102         catch (IOException exc) {
103             outputDone();
104         }
105     }
106
107     public void parseLine(String JavaDoc line, boolean isErrorMessage) {
108         if (isErrorMessage) {
109             if (line.indexOf(EXAM_DIR) >= 0) {
110                 fileDirectory = line.substring(line.indexOf(EXAM_DIR) + EXAM_DIR.length()).trim();
111             }
112             else if (line.startsWith(ERR_CHECK)) {
113                 processFile(line);
114             }
115             else if (line.startsWith(ERR_RCS)) {
116                 if (fileInformation != null) {
117                     String JavaDoc repositoryName =
118                             line.substring(ERR_RCS.length()).trim();
119                     fileInformation.setRepositoryFileName(repositoryName);
120                 }
121             }
122             else if (line.startsWith(ERR_VERS)) {
123                 if (fileInformation != null) {
124                     String JavaDoc repositoryRevision =
125                             line.substring(ERR_RCS.length()).trim();
126                     fileInformation.setRepositoryRevision(repositoryRevision);
127                 }
128             }
129             // header stuff..
130
}
131         else {
132             if (fileInformation == null) {
133                 // HOTFIX there is no header for :local: repositories (thereare two copies in this source)
134
// XXX it might be dangerous because PipedFileInformation stays partialy unitialized
135
try {
136                     fileInformation = new PipedFileInformation(File.createTempFile("checkout", null));
137                 } catch (IOException e) {
138                     e.printStackTrace();
139                 }
140             }
141             if (fileInformation != null) {
142                 try {
143                     fileInformation.addToTempFile(line.getBytes("ISO-8859-1")); // see BuildableCommand
144
fileInformation.addToTempFile(lineSeparator);
145                 }
146                 catch (IOException exc) {
147                     outputDone();
148                 }
149             }
150         }
151     }
152
153     private void processFile(String JavaDoc line) {
154         outputDone();
155         String JavaDoc filename = line.substring(ERR_CHECK.length());
156         try {
157             File temporaryFile = tempFileCreator.createTempFile(filename);
158             fileInformation = new PipedFileInformation(temporaryFile);
159         }
160         catch (IOException ex) {
161             fileInformation = null;
162             return;
163         }
164         fileInformation.setFile(createFile(filename));
165     }
166
167     private File createFile(String JavaDoc fileName) {
168         File file = new File(command.getLocalDirectory(), fileName);
169         return file;
170     }
171
172     public void parseEnhancedMessage(String JavaDoc key, Object JavaDoc value) {
173     }
174 }
175
Popular Tags