001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 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.pde.internal.ui.util;
011:
012: import java.io.IOException;
013: import java.net.URL;
014:
015: import org.eclipse.core.runtime.FileLocator;
016: import org.eclipse.core.runtime.Platform;
017: import org.eclipse.pde.internal.ui.IPDEUIConstants;
018: import org.eclipse.pde.internal.ui.PDEPlugin;
019: import org.osgi.framework.Bundle;
020:
021: public abstract class TextUtil {
022:
023: private static URL fJavaDocStyleSheet = null;
024:
025: public static String createMultiLine(String text, int limit) {
026: return createMultiLine(text, limit, false);
027: }
028:
029: public static String createMultiLine(String text, int limit,
030: boolean ignoreNewLine) {
031: StringBuffer buffer = new StringBuffer();
032: int counter = 0;
033: boolean preformatted = false;
034:
035: for (int i = 0; i < text.length(); i++) {
036: char c = text.charAt(i);
037: counter++;
038: if (c == '<') {
039: if (isPreStart(text, i)) {
040: preformatted = true;
041: } else if (isPreEnd(text, i)) {
042: preformatted = false;
043: } else if (isParagraph(text, i)) {
044: buffer.append("\n<p>\n"); //$NON-NLS-1$
045: counter = 0;
046: i += 2;
047: continue;
048: }
049: }
050: if (preformatted) {
051: if (c == '\n')
052: counter = 0;
053: buffer.append(c);
054: continue;
055: }
056: if (Character.isWhitespace(c)) {
057: if (counter == 1) {
058: counter = 0;
059: continue; // skip
060: } else if (counter > limit) {
061: buffer.append('\n');
062: counter = 0;
063: i--;
064: continue;
065: }
066: }
067: if (c == '\n') {
068: if (ignoreNewLine)
069: c = ' ';
070: else
071: counter = 0;
072: }
073: buffer.append(c);
074: }
075: return buffer.toString();
076: }
077:
078: private static boolean isParagraph(String text, int loc) {
079: if (text.charAt(loc) != '<')
080: return false;
081: if (loc + 2 >= text.length())
082: return false;
083: if (text.charAt(loc + 1) != 'p')
084: return false;
085: if (text.charAt(loc + 2) != '>')
086: return false;
087: return true;
088: }
089:
090: private static boolean isPreEnd(String text, int loc) {
091: if (text.charAt(loc) != '<')
092: return false;
093: if (loc + 5 >= text.length())
094: return false;
095: if (text.charAt(loc + 1) != '/')
096: return false;
097: if (text.charAt(loc + 2) != 'p')
098: return false;
099: if (text.charAt(loc + 3) != 'r')
100: return false;
101: if (text.charAt(loc + 4) != 'e')
102: return false;
103: if (text.charAt(loc + 5) != '>')
104: return false;
105: return true;
106: }
107:
108: private static boolean isPreStart(String text, int loc) {
109: if (text.charAt(loc) != '<')
110: return false;
111: if (loc + 4 >= text.length())
112: return false;
113: if (text.charAt(loc + 1) != 'p')
114: return false;
115: if (text.charAt(loc + 2) != 'r')
116: return false;
117: if (text.charAt(loc + 3) != 'e')
118: return false;
119: if (text.charAt(loc + 4) != '>')
120: return false;
121: return true;
122: }
123:
124: public static URL getJavaDocStyleSheerURL() {
125: if (fJavaDocStyleSheet == null) {
126: Bundle bundle = Platform
127: .getBundle(IPDEUIConstants.PLUGIN_ID);
128: fJavaDocStyleSheet = bundle
129: .getEntry("/JavadocHoverStyleSheet.css"); //$NON-NLS-1$
130: if (fJavaDocStyleSheet != null) {
131: try {
132: fJavaDocStyleSheet = FileLocator
133: .toFileURL(fJavaDocStyleSheet);
134: } catch (IOException ex) {
135: PDEPlugin.log(ex);
136: }
137: }
138: }
139: return fJavaDocStyleSheet;
140: }
141:
142: public static String trimNonAlphaChars(String value) {
143: value = value.trim();
144: while (value.length() > 0
145: && !Character.isLetter(value.charAt(0)))
146: value = value.substring(1, value.length());
147: int loc = value.indexOf(":"); //$NON-NLS-1$
148: if (loc != -1 && loc > 0)
149: value = value.substring(0, loc);
150: else if (loc == 0)
151: value = ""; //$NON-NLS-1$
152: return value;
153: }
154:
155: }
|