KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > cvsclient > command > editors > EditorsBuilder


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.editors;
20
21 import java.io.*;
22 import java.text.*;
23 import java.util.*;
24
25 import org.netbeans.lib.cvsclient.command.*;
26 import org.netbeans.lib.cvsclient.event.*;
27
28 /**
29  * @author Thomas Singer
30  * @version Nov 11, 2001
31  */

32 public class EditorsBuilder
33         implements Builder {
34     // Constants ==============================================================
35

36     private static final DateFormat DATE_FORMAT = new SimpleDateFormat("MMM dd hh:mm:ss yyyy");
37 // private static final DateFormat DATE_FORMAT = new SimpleDateFormat("EEE MMM dd hh:mm:ss yyyy zzz");
38

39     // Fields =================================================================
40

41     private final EventManager eventManager;
42
43     private String JavaDoc editorsFileName;
44
45     // Setup ==================================================================
46

47     EditorsBuilder(EventManager eventManager) {
48     this.editorsFileName=null;
49         this.eventManager = eventManager;
50     }
51
52     // Implemented ============================================================
53

54     public void parseLine(String JavaDoc line, boolean isErrorMessage) {
55         if (!isErrorMessage) {
56             parseLine(line);
57         }
58     }
59
60     public void parseEnhancedMessage(String JavaDoc key, Object JavaDoc value) {
61     }
62
63     public void outputDone() {
64     }
65
66     // Utils ==================================================================
67

68     private boolean parseLine(String JavaDoc line) {
69         StringTokenizer tokenizer = new StringTokenizer(line, "\t");
70         if (!tokenizer.hasMoreTokens()) {
71             return false;
72         }
73
74     //check whether line is the first editors line for this file.
75
//persist for later lines.
76
if(!line.startsWith("\t")) {
77         editorsFileName = tokenizer.nextToken();
78         if (!tokenizer.hasMoreTokens()) {
79             return false;
80         }
81     }
82     //must have a filename associated with the line,
83
// either from this line or a previous one
84
else if(editorsFileName==null) {
85         return false;
86     }
87
88         final String JavaDoc user = tokenizer.nextToken();
89         if (!tokenizer.hasMoreTokens()) {
90             return false;
91         }
92
93         final String JavaDoc dateString = tokenizer.nextToken();
94         if (!tokenizer.hasMoreTokens()) {
95             return false;
96         }
97
98         final String JavaDoc clientName = tokenizer.nextToken();
99         if (!tokenizer.hasMoreTokens()) {
100             return false;
101         }
102
103         final String JavaDoc localDirectory = tokenizer.nextToken();
104
105         try {
106             FileInfoContainer fileInfoContainer = parseEntries(localDirectory,
107                                                                editorsFileName,
108                                                                user,
109                                                                dateString,
110                                                                clientName);
111             final CVSEvent event = new FileInfoEvent(this, fileInfoContainer);
112             eventManager.fireCVSEvent(event);
113             return true;
114         }
115         catch (ParseException ex) {
116             return false;
117         }
118     }
119
120     private EditorsFileInfoContainer parseEntries(String JavaDoc localDirectory,
121                                                   String JavaDoc fileName,
122                                                   String JavaDoc user,
123                                                   String JavaDoc dateString,
124                                                   String JavaDoc clientName) throws ParseException {
125         int lastSlashIndex = fileName.lastIndexOf('/');
126         if (lastSlashIndex >= 0) {
127             fileName = fileName.substring(lastSlashIndex + 1);
128         }
129
130         final Date date = parseDate(dateString);
131         final File file = new File(localDirectory, fileName);
132         return new EditorsFileInfoContainer(file,
133                                             user,
134                                             date,
135                                             clientName);
136     }
137
138     private Date parseDate(String JavaDoc dateString) throws ParseException {
139         int firstSpaceIndex = Math.max(dateString.indexOf(' '), 0);
140         int lastSpaceIndex = Math.min(dateString.lastIndexOf(' '), dateString.length());
141
142 // dateString = dateString.substring(0, lastSpaceIndex).trim();
143
dateString = dateString.substring(firstSpaceIndex, lastSpaceIndex).trim();
144
145         return DATE_FORMAT.parse(dateString);
146     }
147 }
148
Popular Tags