001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.views.bookmarkexplorer;
011:
012: import com.ibm.icu.text.DateFormat;
013: import java.util.Date;
014:
015: import org.eclipse.core.resources.IMarker;
016: import org.eclipse.core.runtime.CoreException;
017: import org.eclipse.core.runtime.IPath;
018:
019: /**
020: * Utility class for accessing marker attributes.
021: */
022: class MarkerUtil {
023:
024: /**
025: * Don't allow instantiation.
026: */
027: private MarkerUtil() {
028: }
029:
030: /**
031: * Returns the ending character offset of the given marker.
032: */
033: static int getCharEnd(IMarker marker) {
034: return marker.getAttribute(IMarker.CHAR_END, -1);
035: }
036:
037: /**
038: * Returns the starting character offset of the given marker.
039: */
040: static int getCharStart(IMarker marker) {
041: return marker.getAttribute(IMarker.CHAR_START, -1);
042: }
043:
044: /**
045: * Returns the container name if it is defined, or empty string if not.
046: */
047: static String getContainerName(IMarker marker) {
048: IPath path = marker.getResource().getFullPath();
049: int n = path.segmentCount() - 1; // n is the number of segments in container, not path
050: if (n <= 0) {
051: return ""; //$NON-NLS-1$
052: }
053: int len = 0;
054: for (int i = 0; i < n; ++i) {
055: len += path.segment(i).length();
056: }
057: // account for /'s
058: if (n > 1) {
059: len += n - 1;
060: }
061: StringBuffer sb = new StringBuffer(len);
062: for (int i = 0; i < n; ++i) {
063: if (i != 0) {
064: sb.append('/');
065: }
066: sb.append(path.segment(i));
067: }
068: return sb.toString();
069: }
070:
071: /**
072: * Returns the line number of the given marker.
073: */
074: static int getLineNumber(IMarker marker) {
075: return marker.getAttribute(IMarker.LINE_NUMBER, -1);
076: }
077:
078: /**
079: * Returns the text for the location field.
080: */
081: static String getLocation(IMarker marker) {
082: return marker.getAttribute(IMarker.LOCATION, "");//$NON-NLS-1$
083: }
084:
085: /**
086: * Returns the message attribute of the given marker,
087: * or the empty string if the message attribute is not defined.
088: */
089: static String getMessage(IMarker marker) {
090: return marker.getAttribute(IMarker.MESSAGE, "");//$NON-NLS-1$
091: }
092:
093: /**
094: * Returns the numeric value of the given string, which is assumed to represent a numeric value.
095: *
096: * @return <code>true</code> if numeric, <code>false</code> if not
097: */
098: static int getNumericValue(String value) {
099: boolean negative = false;
100: int i = 0;
101: int len = value.length();
102:
103: // skip any leading '#'
104: // workaround for 1GCE69U: ITPJCORE:ALL - Java problems should not have '#' in location.
105: if (i < len && value.charAt(i) == '#') {
106: ++i;
107: }
108:
109: if (i < len && value.charAt(i) == '-') {
110: negative = true;
111: ++i;
112: }
113:
114: int result = 0;
115: while (i < len) {
116: int digit = Character.digit(value.charAt(i++), 10);
117: if (digit < 0) {
118: return result;
119: }
120: result = result * 10 + digit;
121: }
122: if (negative) {
123: result = -result;
124: }
125: return result;
126: }
127:
128: /**
129: * Implements IProvider interface by supporting a number of
130: * properties required for visual representation of markers
131: * in the tasklist.
132: */
133:
134: /**
135: * Returns name if it is defined, or
136: * blank string if not.
137: */
138: static String getResourceName(IMarker marker) {
139: return marker.getResource().getName();
140: }
141:
142: /**
143: * Returns the creation time of the marker as a string.
144: */
145: static String getCreationTime(IMarker marker) {
146: try {
147: return DateFormat.getDateTimeInstance(DateFormat.LONG,
148: DateFormat.MEDIUM).format(
149: new Date(marker.getCreationTime()));
150: } catch (CoreException e) {
151: return null;
152: }
153: }
154: }
|