KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > intro > impl > html > HTMLUtil


1 /*******************************************************************************
2  * Copyright (c) 2004, 2005 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.ui.internal.intro.impl.html;
12
13 import java.util.Iterator JavaDoc;
14 import java.util.Map JavaDoc;
15 import java.util.Set JavaDoc;
16
17 /**
18  * Convenience class for generating HTML elements.
19  */

20 public final class HTMLUtil {
21     /**
22      * Creates an HTML opening element of the form <elementName
23      * elementAttributes>
24      *
25      * @param elementName
26      * the name of the element to create
27      * @param elementAttributes
28      * a map of attribute names and values to be inserted into the
29      * element start tag
30      * @param insertLineBreak
31      * true to insert a line break after the start tag is closed,
32      * false otherwise
33      * @return
34      */

35     public static StringBuffer JavaDoc createHTMLStartTag(String JavaDoc elementName,
36             Map JavaDoc elementAttributes, boolean insertLineBreak) {
37         StringBuffer JavaDoc element = new StringBuffer JavaDoc();
38         if (elementName != null) {
39             // open the start tag
40
element.append(openHTMLStartTag(elementName));
41             // add the attributes, if there are any
42
if (elementAttributes != null && !(elementAttributes.isEmpty()))
43                 element.append(IIntroHTMLConstants.SPACE).append(
44                         createAttributeList(elementAttributes));
45             // close the start tag
46
element.append(closeHTMLTag(insertLineBreak));
47         }
48         return element;
49     }
50
51     /**
52      * Creates an HTML start tag of the form <elementName>
53      *
54      * @param elementName
55      * the name of the element to create
56      * @param insertLineBreak
57      * true to insert a new line after the start tag
58      * @return
59      */

60     public static StringBuffer JavaDoc createHTMLStartTag(String JavaDoc elementName,
61             boolean insertLineBreak) {
62         return createHTMLStartTag(elementName, null, insertLineBreak);
63     }
64
65     /**
66      * Creates an HTML start tag of the form <elementName>and inserts a line
67      * break after the start tag
68      *
69      * @param elementName
70      * the name of the element to create
71      * @return
72      */

73     public static StringBuffer JavaDoc createHTMLStartTag(String JavaDoc elementName) {
74         return createHTMLStartTag(elementName, null, true);
75     }
76
77     /**
78      * Creates an HTML closing element of the form </elementName>
79      *
80      * @param elementName
81      * the name of the closing element to create
82      * @param addNewLine
83      * true to add a new line at the end
84      * @return
85      */

86     public static StringBuffer JavaDoc createHTMLEndTag(String JavaDoc elementName,
87             boolean addNewLine) {
88         StringBuffer JavaDoc closingElement = new StringBuffer JavaDoc();
89         if (elementName != null)
90             closingElement.append(IIntroHTMLConstants.LT).append(
91                     IIntroHTMLConstants.FORWARD_SLASH).append(elementName)
92                     .append(closeHTMLTag(addNewLine));
93         return closingElement;
94     }
95
96     /**
97      * Given a map of attribute names and values, this method will create a
98      * StringBuffer of the attributes in the form: attrName="attrValue". These
99      * attributes can appear in the start tag of an HTML element.
100      *
101      * @param attributes
102      * the attributes to be converted into a String list
103      * @return
104      */

105     public static String JavaDoc createAttributeList(Map JavaDoc attributes) {
106         if (attributes == null)
107             return null;
108         StringBuffer JavaDoc attributeList = new StringBuffer JavaDoc();
109         Set JavaDoc attrNames = attributes.keySet();
110         for (Iterator JavaDoc it = attrNames.iterator(); it.hasNext();) {
111             Object JavaDoc name = it.next();
112             Object JavaDoc value = attributes.get(name);
113             if ((name instanceof String JavaDoc) && (value instanceof String JavaDoc)) {
114                 attributeList.append(createAttribute((String JavaDoc) name,
115                         (String JavaDoc) value));
116                 if (it.hasNext()) {
117                     attributeList.append(IIntroHTMLConstants.SPACE);
118                 }
119             }
120         }
121         return attributeList.toString();
122     }
123
124     /**
125      * Creates an HTML attribute of the form attrName="attrValue"
126      *
127      * @param attrName
128      * the name of the attribute
129      * @param attrValue
130      * the value of the attribute
131      * @return
132      */

133     public static StringBuffer JavaDoc createAttribute(String JavaDoc attrName, String JavaDoc attrValue) {
134         StringBuffer JavaDoc attribute = new StringBuffer JavaDoc();
135         if (attrName != null && attrValue != null) {
136             attribute.append(attrName).append(IIntroHTMLConstants.EQUALS)
137                     .append(IIntroHTMLConstants.QUOTE).append(attrValue)
138                     .append(IIntroHTMLConstants.QUOTE);
139         }
140         return attribute;
141     }
142
143     public static StringBuffer JavaDoc openHTMLStartTag(String JavaDoc elementName) {
144         return new StringBuffer JavaDoc().append(IIntroHTMLConstants.LT).append(
145                 elementName);
146     }
147
148     public static StringBuffer JavaDoc closeHTMLTag() {
149         return closeHTMLTag(true);
150     }
151
152     public static StringBuffer JavaDoc closeHTMLTag(boolean newLine) {
153         StringBuffer JavaDoc closing = new StringBuffer JavaDoc()
154                 .append(IIntroHTMLConstants.GT);
155         if (newLine)
156             closing.append(IIntroHTMLConstants.NEW_LINE);
157         return closing;
158     }
159
160     /**
161      * Determine if the contents of two character arrays are equal
162      *
163      * @param a
164      * @param b
165      * @return
166      */

167     public static boolean equalCharArrayContent(char[] a, char[] b) {
168         if (a.length != b.length)
169             return false;
170         for (int i = 0; i < a.length; i++) {
171             if (a[i] != b[i])
172                 return false;
173         }
174         return true;
175     }
176 }
177
Popular Tags