KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > servlet > HtmlUtil


1 package jodd.servlet;
2
3 import jodd.util.StringUtil;
4
5 /**
6  * Html utils for working with tag's names and attributes.
7  */

8 public class HtmlUtil {
9
10     // ---------------------------------------------------------------- tag name
11

12     /**
13      * Returns tag's name. Given string represents a HTML body of a tag,
14      * therefore it <b>must</b> start with '<'.
15      *
16      * @param tagBody tag's body
17      *
18      * @return tag's name, or <code>null</code> if tag not found
19      */

20     public static String getTagName(String tagBody) {
21         return getTagName(tagBody, 0);
22     }
23
24     /**
25      * Returns tag's name. Given string represents a HTML body and given starting index
26      * <b>must</b> be the index of tag's start (i.e. '<').
27      * <p>
28      *
29      * Names of ending tags will always start with '/' character.
30      *
31      * @param body hmtl body
32      * @param i index of tag's start
33      *
34      * @return tag's name, or <code>null</code> if tag not found
35      */

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; // no tag
42
}
43         int start = i + 1; // skip '<'
44
int len = body.length();
45         boolean isEndTag = false;
46         // skip all non-letters
47
while (start < len) {
48             char c = body.charAt(start);
49             if (c == '>') {
50                 return null; // tag end found => name not found
51
}
52             if (c == '/') { // this is an end tag
53
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; // tag name not found
64
}
65         int end = start;
66         // skip all letters
67
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; // tag end not found
76
}
77
78         String tagName = body.substring(start, end);
79         if (isEndTag == true) {
80             tagName = "/" + tagName;
81         }
82         return tagName;
83     }
84
85
86
87     // ---------------------------------------------------------------- tag attribute
88

89     /**
90      * Returns value of the first founded attribute that matches given name.
91      * It is assumed that given string represents tag's body.
92      * Note: attribute <b>must</b> end with the <code>="</code> or <code>='</code>.
93      * Attribute name is not case sensitive.
94      *
95      * @param tagBody tag body
96      * @param attrName attribute name
97      *
98      * @return attribute value or <code>null</code> if attribute not found
99      */

100     public static String getAttribute(String tagBody, String attrName) {
101         return getAttribute(tagBody, attrName, 0);
102     }
103
104     /**
105      * Returns value of the first founded attribute that matches given name.
106      * Given string may not be just a tag's body, however, start and end
107      * parameters must define tags body.
108      * Note: attribute <b>must</b> end with the <code>="</code> or <code>='</code>.
109      * Attribute name is not case sensitive.
110      *
111      * @param body html body
112      * @param attrName attribute name
113      * @param start index of tag's start
114      *
115      * @return attribute value or <code>null</code> if attribute not found
116      */

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; // tag's end not found
125
}
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; // closed quation not found
142
}
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     // ---------------------------------------------------------------- add attribute & value
155

156
157     /**
158      * Adds attribute and its value to a tag. Attribute is added to the end of
159      * the tag, just before closing '>'. If name is not specified, nothing will
160      * be added. If value is not specified, it will be set to an empty string.
161      *
162      * @param tagBody tag body
163      * @param name attribute name
164      * @param value attribute value
165      *
166      * @return tag string with added attribute and value
167      */

168     public static String addAttribute(String tagBody, String name, String value) {
169         return addAttribute(tagBody, name, value, 0);
170     }
171
172     /**
173      * Adds attribute and its value to a tag. Attribute is added to the end of
174      * the tag, just before closing '>'. If name is not specified, nothing will
175      * be added. If value is not specified, it will be set to an empty string.
176      *
177      * @param body html body
178      * @param name attribute name
179      * @param value attribute value
180      * @param i tag's offset in html body
181      *
182      * @return tag string with added attribute and value
183      */

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(ServletUtil.encodeHtml(value)).append('"');
202
result.append(HtmlEncoder.encode(value)).append('"');
203         result.append(body.substring(end));
204         return result.toString();
205     }
206
207
208     // ---------------------------------------------------------------- add attribute, no value
209

210
211     /**
212      * Adds single attribute without value to a tag. Attribute is added to the
213      * end of the tag, just before closing '>'. If name is not specified, nothing
214      * will be added.
215      *
216      * @param tagBody tag body
217      * @param name attribute name
218      *
219      * @return tag string with added attribute
220      */

221     public static String addAttribute(String tagBody, String name) {
222         return addAttribute(tagBody, name, 0);
223     }
224
225     /**
226      * Adds single attribute without value to a tag. Attribute is added to the
227      * end of the tag, just before closing '>'. If name is not specified, nothing
228      * will be added.
229      *
230      * @param body html body
231      * @param name attribute name
232      * @param i tag's offset in html body
233      *
234      * @return tag string with added attribute
235      */

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