1 package jodd.servlet; 2 3 import jodd.util.StringUtil; 4 5 8 public class HtmlUtil { 9 10 12 20 public static String getTagName(String tagBody) { 21 return getTagName(tagBody, 0); 22 } 23 24 36 public static String getTagName(String body, int i) { 37 if (body == null) { 38 return null; 39 } 40 if (body.charAt(i) != '<') { 41 return null; } 43 int start = i + 1; int len = body.length(); 45 boolean isEndTag = false; 46 while (start < len) { 48 char c = body.charAt(start); 49 if (c == '>') { 50 return null; } 52 if (c == '/') { start++; 54 isEndTag = true; 55 continue; 56 } 57 if (Character.isWhitespace(c) == false) { 58 break; 59 } 60 start++; 61 } 62 if (start == len) { 63 return null; } 65 int end = start; 66 while (end < len) { 68 char c = body.charAt(end); 69 if ((Character.isWhitespace(c) == true) || (c == '>')) { 70 break; 71 } 72 end++; 73 } 74 if (end == len) { 75 return null; } 77 78 String tagName = body.substring(start, end); 79 if (isEndTag == true) { 80 tagName = "/" + tagName; 81 } 82 return tagName; 83 } 84 85 86 87 89 100 public static String getAttribute(String tagBody, String attrName) { 101 return getAttribute(tagBody, attrName, 0); 102 } 103 104 117 public static String getAttribute(String body, String attrName, int start) { 118 if (body == null) { 119 return null; 120 } 121 char quote = '\"'; 122 int end = body.indexOf('>'); 123 if (end == -1) { 124 return null; } 126 int i = StringUtil.indexOfIgnoreCase(body, attrName + "=\"", start); 127 if ((i == -1) || (i > end)) { 128 i = StringUtil.indexOfIgnoreCase(body, attrName + "='", start); 129 if ((i == -1) || (i > end)) { 130 return null; 131 } 132 quote = '\''; 133 } 134 String value = null; 135 i += attrName.length() + 2; 136 int s = i; 137 int j = -1; 138 while (true) { 139 j = body.indexOf(quote, s); 140 if (j == -1) { 141 break; } 143 if (body.charAt(j - 1) == '\\') { 144 s = j + 1; 145 continue; 146 } else { 147 value = body.substring(i, j); 148 break; 149 } 150 } 151 return value; 152 } 153 154 156 157 168 public static String addAttribute(String tagBody, String name, String value) { 169 return addAttribute(tagBody, name, value, 0); 170 } 171 172 184 public static String addAttribute(String body, String name, String value, int i) { 185 if (body == null) { 186 return null; 187 } 188 if (name == null) { 189 return body; 190 } 191 if (value == null) { 192 value = ""; 193 } 194 int end = body.indexOf('>', i); 195 if (end == -1) { 196 return body; 197 } 198 StringBuffer result = new StringBuffer(body.length()); 199 result.append(body.substring(i, end)).append(' '); 200 result.append(name).append('=').append('"'); 201 result.append(HtmlEncoder.encode(value)).append('"'); 203 result.append(body.substring(end)); 204 return result.toString(); 205 } 206 207 208 210 211 221 public static String addAttribute(String tagBody, String name) { 222 return addAttribute(tagBody, name, 0); 223 } 224 225 236 public static String addAttribute(String body, String name, int i) { 237 if (body == null) { 238 return null; 239 } 240 if (name == null) { 241 return body; 242 } 243 int end = body.indexOf('>', i); 244 if (end == -1) { 245 return body; 246 } 247 StringBuffer result = new StringBuffer(body.length()); 248 result.append(body.substring(i, end)).append(' '); 249 result.append(name).append(body.substring(end)); 250 return result.toString(); 251 } 252 253 } 254 | Popular Tags |