Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 4c08bcdb42ec988326a58be862776a11a38f894b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*******************************************************************************
 * Copyright (c) 2000, 2007 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.team.internal.ccvs.core.resources;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.team.internal.ccvs.core.*;

public class CVSEntryLineTag extends CVSTag {
	
    /*
     * This is the format of a date as it appears in the entry line. The date in an entry
     * line is always in GMT.
     */
	private static final String ENTRY_LINE_DATE_TAG_FORMAT = "yyyy.MM.dd.HH.mm.ss"; //$NON-NLS-1$
	
	/*
	 * This is a formatter that will translate dates to and from text as it appears in the entry line 
	 */
	private static SimpleDateFormat entryLineDateTagFormatter = new SimpleDateFormat(ENTRY_LINE_DATE_TAG_FORMAT, Locale.US);
	
	/*
	 * Convert the tag name as it appears as an argument to a command
	 * into the format that appears in the entry line of a folder or file
	 */
	private static String getNameInInternalFormat(CVSTag tag) {
		if(tag.getType() == DATE){
			String s = ensureEntryLineFormat(tag.getName());
			if(s != null){
				return s;
			}
		}
		return tag.getName();
	}
	
	/*
	 * Helper for converting the tag name as it appears as an argument to a command
	 * into the format that appears in the entry line of a folder or file
	 */
	private static synchronized String ensureEntryLineFormat(String text){
		if(text.length() == ENTRY_LINE_DATE_TAG_FORMAT.length()) return text;
		Date date = tagNameToDate(text);
		if (date == null) return text;
		entryLineDateTagFormatter.setTimeZone(TimeZone.getTimeZone("GMT")); //$NON-NLS-1$
		return entryLineDateTagFormatter.format(date);
	}
	
	static synchronized public Date entryLineToDate(String text){
		try {
		    entryLineDateTagFormatter.setTimeZone(TimeZone.getTimeZone("GMT")); //$NON-NLS-1$
			return entryLineDateTagFormatter.parse(text);
		} catch (ParseException e) {
			CVSProviderPlugin.log(new CVSStatus(IStatus.ERROR, CVSStatus.ERROR, "Tag name " + text + " is not of the expected format " + ENTRY_LINE_DATE_TAG_FORMAT, e)); //$NON-NLS-1$ //$NON-NLS-2$
			return null;
		}		
	}
	
	/*
	 * The parameter tag must not be null.
	 */
	public CVSEntryLineTag(CVSTag tag) {
		super(getNameInInternalFormat(tag), tag.getType());
	}
	
	public CVSEntryLineTag(String entryLineTag) {
		switch (entryLineTag.charAt(0)) {
			case 'T' : type = BRANCH; break;
			case 'N' : type = VERSION; break;
			case 'D' : type = DATE; break;
			default: type = HEAD;
		}
		name = entryLineTag.substring(1);
	}
	/*
	 * Returns the tag name
	 */
	public String getName() {
		if (getType() == DATE) {
			// Use same format as CVSTag when the name is requested
			Date date = asDate();
			if(date != null){
				return dateToTagName(date);
			}
		}
		return name;
	}
	/*
	 * Returns the tag type
	 */
	public int getType() {
		return type;
	}
	
	public String toEntryLineFormat(boolean useSamePrefixForBranchAndTag) {
		if (type == BRANCH || (type == VERSION && useSamePrefixForBranchAndTag))
			return "T" + name;//$NON-NLS-1$
		else if (type == VERSION)
			return "N" + name;//$NON-NLS-1$
		else if (type == DATE)
			return "D" + name;//$NON-NLS-1$
		return "";//$NON-NLS-1$
	}

	/*
	 * For debugging purposes.
	 */
	public String toString() {
		return toEntryLineFormat(false);
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.team.internal.ccvs.core.CVSTag#asDate()
	 */
	public Date asDate() {
		return entryLineToDate(name);
	}
}

Back to the top