KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > core > syncinfo > NotifyInfo


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.team.internal.ccvs.core.syncinfo;
12
13 import java.net.InetAddress JavaDoc;
14 import java.net.UnknownHostException JavaDoc;
15 import java.text.ParseException JavaDoc;
16 import java.util.Date JavaDoc;
17
18 import org.eclipse.core.resources.IContainer;
19 import org.eclipse.core.runtime.IStatus;
20 import org.eclipse.osgi.util.NLS;
21 import org.eclipse.team.internal.ccvs.core.*;
22 import org.eclipse.team.internal.ccvs.core.util.CVSDateFormatter;
23 import org.eclipse.team.internal.ccvs.core.util.Util;
24
25 /**
26  * This class contains the information required by the server for edit/unedit.
27  */

28 public class NotifyInfo {
29     
30     // constants for the notifiation type and watches
31
public static final char EDIT = 'E';
32     public static final char UNEDIT = 'U';
33     public static final char COMMIT = 'C';
34     public static final char[] ALL = new char[] {EDIT, UNEDIT, COMMIT};
35     
36     protected static final String JavaDoc TAB_SEPARATOR = "\t"; //$NON-NLS-1$
37

38     private String JavaDoc filename;
39     private char notificationType;
40     private Date JavaDoc timeStamp;
41     private char[] watches;
42     
43     /**
44      * Constructor for setting all variables
45      */

46     public NotifyInfo(String JavaDoc filename, char notificationType, Date JavaDoc timeStamp, char[] watches) {
47             
48         this.filename = filename;
49         this.notificationType = notificationType;
50         this.timeStamp = timeStamp;
51         this.watches = watches;
52     }
53
54     /**
55      * Constructor for a line from the CVS/Notify file
56      * @param line
57      */

58     public NotifyInfo(IContainer parent, String JavaDoc line) throws CVSException {
59         String JavaDoc[] strings = Util.parseIntoSubstrings(line, ResourceSyncInfo.SEPARATOR);
60         if(strings.length != 4) {
61             IStatus status = new CVSStatus(IStatus.ERROR, CVSStatus.ERROR_LINE, NLS.bind(CVSMessages.NotifyInfo_MalformedLine, new String JavaDoc[] { line }), parent);
62             throw new CVSException(status);
63         }
64         this.filename = strings[0];
65         
66         String JavaDoc type = strings[1];
67         if (type.length() != 1) {
68             IStatus status = new CVSStatus(IStatus.ERROR, CVSStatus.ERROR_LINE, NLS.bind(CVSMessages.NotifyInfo_MalformedNotificationType, new String JavaDoc[] { line }), parent);
69             throw new CVSException(status);
70         }
71         this.notificationType = type.charAt(0);
72         
73         String JavaDoc date = strings[2];
74         try {
75             this.timeStamp = CVSDateFormatter.entryLineToDate(date);
76         } catch(ParseException JavaDoc e) {
77             IStatus status = new CVSStatus(IStatus.ERROR, CVSStatus.ERROR_LINE, NLS.bind(CVSMessages.NotifyInfo_MalformedNotifyDate, new String JavaDoc[] { line }), parent);
78             throw new CVSException(status);
79         }
80         
81         String JavaDoc watchesString = strings[3];
82         if (watchesString.length() > 0) {
83             this.watches = new char[watchesString.length()];
84             for (int i = 0; i < watchesString.length(); i++) {
85                 watches[i] = watchesString.charAt(i);
86             }
87         } else {
88             this.watches = null;
89         }
90     }
91     
92     /**
93      * Answer a Sting formatted to be written to the CVS/Notify file.
94      *
95      * XXX NOTE: This is a guess at the local format. Need to obtain proper format
96      *
97      * @return String
98      */

99     public String JavaDoc getNotifyLine() {
100         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
101         buffer.append(getName());
102         buffer.append(ResourceSyncInfo.SEPARATOR);
103         buffer.append(notificationType);
104         buffer.append(ResourceSyncInfo.SEPARATOR);
105         buffer.append(CVSDateFormatter.dateToEntryLine(timeStamp));
106         buffer.append(ResourceSyncInfo.SEPARATOR);
107         if (watches != null) {
108             for (int i = 0; i < watches.length; i++) {
109                 char c = watches[i];
110                 buffer.append(c);
111             }
112         }
113         return buffer.toString();
114     }
115
116     /**
117      * Answer a Sting formatted to be sent to the server.
118      *
119      * @return String
120      */

121     public String JavaDoc getServerLine(ICVSFolder parent) throws CVSException {
122         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
123         buffer.append(notificationType);
124         buffer.append(TAB_SEPARATOR);
125         buffer.append(getServerTimestamp());
126         buffer.append(TAB_SEPARATOR);
127         buffer.append(getHost());
128         buffer.append(TAB_SEPARATOR);
129         buffer.append(getWorkingDirectory(parent));
130         buffer.append(TAB_SEPARATOR);
131         if (watches != null) {
132             for (int i = 0; i < watches.length; i++) {
133                 char c = watches[i];
134                 buffer.append(c);
135             }
136         }
137         return buffer.toString();
138     }
139
140     /**
141      * Answer the timestamp in GMT format.
142      * @return String
143      */

144     private String JavaDoc getServerTimestamp() {
145         return CVSDateFormatter.dateToNotifyServer(timeStamp);
146     }
147
148     /**
149      * Answer the working directory for the receiver's file. The format
150      * is NOT device dependant (i.e. /'s are used as the path separator).
151      *
152      * @return String
153      */

154     private String JavaDoc getWorkingDirectory(ICVSFolder parent) throws CVSException {
155         return parent.getIResource().getLocation().toString();
156     }
157
158     /**
159      * Answer the host name of the client machine.
160      * @return String
161      */

162     private String JavaDoc getHost() throws CVSException {
163         try {
164             return InetAddress.getLocalHost().getHostName();
165         } catch (UnknownHostException JavaDoc e) {
166             throw CVSException.wrapException(e);
167         }
168     }
169
170     /**
171      * Answer the name of the file associated with the notification
172      * @return String
173      */

174     public String JavaDoc getName() {
175         return filename;
176     }
177
178     /**
179      * Answer the notification type associated with the notification
180      * @return char
181      */

182     public char getNotificationType() {
183         return notificationType;
184     }
185
186 }
187
Popular Tags