KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > util > TextUtil


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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 package org.eclipse.pde.internal.ui.util;
12
13 import java.io.IOException JavaDoc;
14 import java.net.URL JavaDoc;
15
16 import org.eclipse.core.runtime.FileLocator;
17 import org.eclipse.core.runtime.Platform;
18 import org.eclipse.pde.internal.ui.IPDEUIConstants;
19 import org.eclipse.pde.internal.ui.PDEPlugin;
20 import org.osgi.framework.Bundle;
21
22 public abstract class TextUtil {
23
24     private static URL JavaDoc fJavaDocStyleSheet = null;
25     
26     public static String JavaDoc createMultiLine(String JavaDoc text, int limit) {
27         return createMultiLine(text, limit, false);
28     }
29
30     public static String JavaDoc createMultiLine(String JavaDoc text, int limit, boolean ignoreNewLine) {
31         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
32         int counter = 0;
33         boolean preformatted = false;
34
35         for (int i = 0; i < text.length(); i++) {
36             char c = text.charAt(i);
37             counter++;
38             if (c == '<') {
39                 if (isPreStart(text, i)) {
40                     preformatted = true;
41                 } else if (isPreEnd(text, i)) {
42                     preformatted = false;
43                 } else if (isParagraph(text, i)) {
44                     buffer.append("\n<p>\n"); //$NON-NLS-1$
45
counter = 0;
46                     i += 2;
47                     continue;
48                 }
49             }
50             if (preformatted) {
51                 if (c == '\n')
52                     counter = 0;
53                 buffer.append(c);
54                 continue;
55             }
56             if (Character.isWhitespace(c)) {
57                 if (counter == 1) {
58                     counter = 0;
59                     continue; // skip
60
} else if (counter > limit) {
61                     buffer.append('\n');
62                     counter = 0;
63                     i--;
64                     continue;
65                 }
66             }
67             if (c == '\n') {
68                 if (ignoreNewLine)
69                     c = ' ';
70                 else
71                     counter = 0;
72             }
73             buffer.append(c);
74         }
75         return buffer.toString();
76     }
77
78     private static boolean isParagraph(String JavaDoc text, int loc) {
79         if (text.charAt(loc) != '<')
80             return false;
81         if (loc + 2 >= text.length())
82             return false;
83         if (text.charAt(loc + 1) != 'p')
84             return false;
85         if (text.charAt(loc + 2) != '>')
86             return false;
87         return true;
88     }
89
90     private static boolean isPreEnd(String JavaDoc text, int loc) {
91         if (text.charAt(loc) != '<')
92             return false;
93         if (loc + 5 >= text.length())
94             return false;
95         if (text.charAt(loc + 1) != '/')
96             return false;
97         if (text.charAt(loc + 2) != 'p')
98             return false;
99         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 JavaDoc 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 JavaDoc getJavaDocStyleSheerURL() {
125         if (fJavaDocStyleSheet == null) {
126             Bundle bundle= Platform.getBundle(IPDEUIConstants.PLUGIN_ID);
127             fJavaDocStyleSheet= bundle.getEntry("/JavadocHoverStyleSheet.css"); //$NON-NLS-1$
128
if (fJavaDocStyleSheet != null) {
129                 try {
130                     fJavaDocStyleSheet= FileLocator.toFileURL(fJavaDocStyleSheet);
131                 } catch (IOException JavaDoc ex) {
132                     PDEPlugin.log(ex);
133                 }
134             }
135         }
136         return fJavaDocStyleSheet;
137     }
138     
139     public static String JavaDoc trimNonAlphaChars(String JavaDoc value) {
140         value = value.trim();
141         while (value.length() > 0 && !Character.isLetter(value.charAt(0)))
142             value = value.substring(1, value.length());
143         int loc = value.indexOf(":"); //$NON-NLS-1$
144
if (loc != -1 && loc > 0)
145             value = value.substring(0, loc);
146         else if (loc == 0)
147             value = ""; //$NON-NLS-1$
148
return value;
149     }
150     
151 }
152
Popular Tags