blob: 6d387579d2b1c3722f717df3c4209cc85a0286ca [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2000, 2003 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.team.internal.ccvs.core.syncinfo;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.ParseException;
import java.util.Date;
import org.eclipse.core.resources.IContainer;
import org.eclipse.team.internal.ccvs.core.CVSException;
import org.eclipse.team.internal.ccvs.core.ICVSFolder;
import org.eclipse.team.internal.ccvs.core.Policy;
import org.eclipse.team.internal.ccvs.core.resources.CVSWorkspaceRoot;
import org.eclipse.team.internal.ccvs.core.util.CVSDateFormatter;
import org.eclipse.team.internal.ccvs.core.util.Util;
/**
* This class contains the information required by the server for edit/unedit.
*/
public class NotifyInfo {
// constants for the notifiation type and watches
public static final char EDIT = 'E';
public static final char UNEDIT = 'U';
public static final char COMMIT = 'C';
public static final char[] ALL = new char[] {EDIT, UNEDIT, COMMIT};
protected static final String TAB_SEPARATOR = "\t"; //$NON-NLS-1$
private String filename;
private char notificationType;
private Date timeStamp;
private char[] watches;
/**
* Constructor for setting all variables
*/
public NotifyInfo(String filename, char notificationType, Date timeStamp, char[] watches) {
this.filename = filename;
this.notificationType = notificationType;
this.timeStamp = timeStamp;
this.watches = watches;
}
/**
* Constructor for a line from the CVS/Notify file
* @param line
*/
public NotifyInfo(IContainer parent, String line) throws CVSException {
ICVSFolder cvsFolder = CVSWorkspaceRoot.getCVSFolderFor(parent);
String[] strings = Util.parseIntoSubstrings(line, ResourceSyncInfo.SEPARATOR);
if(strings.length != 4) {
throw new CVSException(Policy.bind("NotifyInfo.MalformedLine", line)); //$NON-NLS-1$
}
this.filename = strings[0];
String type = strings[1];
if (type.length() != 1) {
throw new CVSException(Policy.bind("NotifyInfo.MalformedNotificationType", line)); //$NON-NLS-1$
}
this.notificationType = type.charAt(0);
String date = strings[2];
try {
this.timeStamp = CVSDateFormatter.entryLineToDate(date);
} catch(ParseException e) {
throw new CVSException(Policy.bind("NotifyInfo.MalformedNotifyDate", line)); //$NON-NLS-1$
}
String watchesString = strings[3];
if (watchesString.length() > 0) {
this.watches = new char[watchesString.length()];
for (int i = 0; i < watchesString.length(); i++) {
watches[i] = watchesString.charAt(i);
}
} else {
this.watches = null;
}
}
/**
* Answer a Sting formatted to be written to the CVS/Notify file.
*
* XXX NOTE: This is a guess at the local format. Need to obtain proper format
*
* @return String
*/
public String getNotifyLine() {
StringBuffer buffer = new StringBuffer();
buffer.append(getName());
buffer.append(ResourceSyncInfo.SEPARATOR);
buffer.append(notificationType);
buffer.append(ResourceSyncInfo.SEPARATOR);
buffer.append(CVSDateFormatter.dateToEntryLine(timeStamp));
buffer.append(ResourceSyncInfo.SEPARATOR);
if (watches != null) {
for (int i = 0; i < watches.length; i++) {
char c = watches[i];
buffer.append(c);
}
}
return buffer.toString();
}
/**
* Answer a Sting formatted to be sent to the server.
*
* @return String
*/
public String getServerLine(ICVSFolder parent) throws CVSException {
StringBuffer buffer = new StringBuffer();
buffer.append(notificationType);
buffer.append(TAB_SEPARATOR);
buffer.append(getServerTimestamp());
buffer.append(TAB_SEPARATOR);
buffer.append(getHost());
buffer.append(TAB_SEPARATOR);
buffer.append(getWorkingDirectory(parent));
buffer.append(TAB_SEPARATOR);
if (watches != null) {
for (int i = 0; i < watches.length; i++) {
char c = watches[i];
buffer.append(c);
}
}
return buffer.toString();
}
/**
* Answer the timestamp in GMT format.
* @return String
*/
private String getServerTimestamp() {
return CVSDateFormatter.dateToNotifyServer(timeStamp);
}
/**
* Answer the working directory for the receiver's file. The format
* is NOT device dependant (i.e. /'s are used as the path separator).
*
* @return String
*/
private String getWorkingDirectory(ICVSFolder parent) throws CVSException {
return parent.getIResource().getLocation().toString();
}
/**
* Answer the host name of the client machine.
* @return String
*/
private String getHost() throws CVSException {
try {
return InetAddress.getLocalHost().getHostName();
} catch (UnknownHostException e) {
throw CVSException.wrapException(e);
}
}
/**
* Answer the name of the file associated with the notification
* @return String
*/
public String getName() {
return filename;
}
/**
* Answer the notification type associated with the notification
* @return char
*/
public char getNotificationType() {
return notificationType;
}
}