KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > views > bookmarkexplorer > MarkerUtil


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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
12 package org.eclipse.ui.views.bookmarkexplorer;
13
14 import com.ibm.icu.text.DateFormat;
15 import java.util.Date JavaDoc;
16
17 import org.eclipse.core.resources.IMarker;
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.IPath;
20
21 /**
22  * Utility class for accessing marker attributes.
23  */

24 class MarkerUtil {
25
26     /**
27      * Don't allow instantiation.
28      */

29     private MarkerUtil() {
30     }
31
32     /**
33      * Returns the ending character offset of the given marker.
34      */

35     static int getCharEnd(IMarker marker) {
36         return marker.getAttribute(IMarker.CHAR_END, -1);
37     }
38
39     /**
40      * Returns the starting character offset of the given marker.
41      */

42     static int getCharStart(IMarker marker) {
43         return marker.getAttribute(IMarker.CHAR_START, -1);
44     }
45
46     /**
47      * Returns the container name if it is defined, or empty string if not.
48      */

49     static String JavaDoc getContainerName(IMarker marker) {
50         IPath path = marker.getResource().getFullPath();
51         int n = path.segmentCount() - 1; // n is the number of segments in container, not path
52
if (n <= 0) {
53             return ""; //$NON-NLS-1$
54
}
55         int len = 0;
56         for (int i = 0; i < n; ++i) {
57             len += path.segment(i).length();
58         }
59         // account for /'s
60
if (n > 1) {
61             len += n - 1;
62         }
63         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(len);
64         for (int i = 0; i < n; ++i) {
65             if (i != 0) {
66                 sb.append('/');
67             }
68             sb.append(path.segment(i));
69         }
70         return sb.toString();
71     }
72
73     /**
74      * Returns the line number of the given marker.
75      */

76     static int getLineNumber(IMarker marker) {
77         return marker.getAttribute(IMarker.LINE_NUMBER, -1);
78     }
79
80     /**
81      * Returns the text for the location field.
82      */

83     static String JavaDoc getLocation(IMarker marker) {
84         return marker.getAttribute(IMarker.LOCATION, "");//$NON-NLS-1$
85
}
86
87     /**
88      * Returns the message attribute of the given marker,
89      * or the empty string if the message attribute is not defined.
90      */

91     static String JavaDoc getMessage(IMarker marker) {
92         return marker.getAttribute(IMarker.MESSAGE, "");//$NON-NLS-1$
93
}
94
95     /**
96      * Returns the numeric value of the given string, which is assumed to represent a numeric value.
97      *
98      * @return <code>true</code> if numeric, <code>false</code> if not
99      */

100     static int getNumericValue(String JavaDoc value) {
101         boolean negative = false;
102         int i = 0;
103         int len = value.length();
104
105         // skip any leading '#'
106
// workaround for 1GCE69U: ITPJCORE:ALL - Java problems should not have '#' in location.
107
if (i < len && value.charAt(i) == '#') {
108             ++i;
109         }
110
111         if (i < len && value.charAt(i) == '-') {
112             negative = true;
113             ++i;
114         }
115
116         int result = 0;
117         while (i < len) {
118             int digit = Character.digit(value.charAt(i++), 10);
119             if (digit < 0) {
120                 return result;
121             }
122             result = result * 10 + digit;
123         }
124         if (negative) {
125             result = -result;
126         }
127         return result;
128     }
129
130     /**
131      * Implements IProvider interface by supporting a number of
132      * properties required for visual representation of markers
133      * in the tasklist.
134      */

135
136     /**
137      * Returns name if it is defined, or
138      * blank string if not.
139      */

140     static String JavaDoc getResourceName(IMarker marker) {
141         return marker.getResource().getName();
142     }
143
144     /**
145      * Returns the creation time of the marker as a string.
146      */

147     static String JavaDoc getCreationTime(IMarker marker) {
148         try {
149             return DateFormat.getDateTimeInstance(DateFormat.LONG,
150                     DateFormat.MEDIUM).format(
151                     new Date JavaDoc(marker.getCreationTime()));
152         } catch (CoreException e) {
153             return null;
154         }
155     }
156 }
157
Popular Tags