1 11 package org.eclipse.pde.internal.ui.util; 12 13 import java.io.IOException ; 14 import java.net.URL ; 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 fJavaDocStyleSheet = null; 25 26 public static String createMultiLine(String text, int limit) { 27 return createMultiLine(text, limit, false); 28 } 29 30 public static String createMultiLine(String text, int limit, boolean ignoreNewLine) { 31 StringBuffer buffer = new StringBuffer (); 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"); 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; } 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 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 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 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.getBundle(IPDEUIConstants.PLUGIN_ID); 127 fJavaDocStyleSheet= bundle.getEntry("/JavadocHoverStyleSheet.css"); if (fJavaDocStyleSheet != null) { 129 try { 130 fJavaDocStyleSheet= FileLocator.toFileURL(fJavaDocStyleSheet); 131 } catch (IOException ex) { 132 PDEPlugin.log(ex); 133 } 134 } 135 } 136 return fJavaDocStyleSheet; 137 } 138 139 public static String trimNonAlphaChars(String 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(":"); if (loc != -1 && loc > 0) 145 value = value.substring(0, loc); 146 else if (loc == 0) 147 value = ""; return value; 149 } 150 151 } 152 | Popular Tags |