Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 699a61905ba508f0237a4660411c12a82066a313 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*******************************************************************************
 * Copyright (c) 2004, 2005 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.ui.internal.intro.impl.html;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * Convenience class for generating HTML elements.
 */
public final class HTMLUtil {
    /**
     * Creates an HTML opening element of the form <elementName
     * elementAttributes>
     * 
     * @param elementName
     *            the name of the element to create
     * @param elementAttributes
     *            a map of attribute names and values to be inserted into the
     *            element start tag
     * @param insertLineBreak
     *            true to insert a line break after the start tag is closed,
     *            false otherwise
     * @return
     */
    public static StringBuffer createHTMLStartTag(String elementName,
            Map elementAttributes, boolean insertLineBreak) {
        StringBuffer element = new StringBuffer();
        if (elementName != null) {
            // open the start tag
            element.append(openHTMLStartTag(elementName));
            // add the attributes, if there are any
            if (elementAttributes != null && !(elementAttributes.isEmpty()))
                element.append(IIntroHTMLConstants.SPACE).append(
                        createAttributeList(elementAttributes));
            // close the start tag
            element.append(closeHTMLTag(insertLineBreak));
        }
        return element;
    }

    /**
     * Creates an HTML start tag of the form <elementName>
     * 
     * @param elementName
     *            the name of the element to create
     * @param insertLineBreak
     *            true to insert a new line after the start tag
     * @return
     */
    public static StringBuffer createHTMLStartTag(String elementName,
            boolean insertLineBreak) {
        return createHTMLStartTag(elementName, null, insertLineBreak);
    }

    /**
     * Creates an HTML start tag of the form <elementName>and inserts a line
     * break after the start tag
     * 
     * @param elementName
     *            the name of the element to create
     * @return
     */
    public static StringBuffer createHTMLStartTag(String elementName) {
        return createHTMLStartTag(elementName, null, true);
    }

    /**
     * Creates an HTML closing element of the form </elementName>
     * 
     * @param elementName
     *            the name of the closing element to create
     * @param addNewLine
     *            true to add a new line at the end
     * @return
     */
    public static StringBuffer createHTMLEndTag(String elementName,
            boolean addNewLine) {
        StringBuffer closingElement = new StringBuffer();
        if (elementName != null)
            closingElement.append(IIntroHTMLConstants.LT).append(
                    IIntroHTMLConstants.FORWARD_SLASH).append(elementName)
                    .append(closeHTMLTag(addNewLine));
        return closingElement;
    }

    /**
     * Given a map of attribute names and values, this method will create a
     * StringBuffer of the attributes in the form: attrName="attrValue". These
     * attributes can appear in the start tag of an HTML element.
     * 
     * @param attributes
     *            the attributes to be converted into a String list
     * @return
     */
    public static String createAttributeList(Map attributes) {
        if (attributes == null)
            return null;
        StringBuffer attributeList = new StringBuffer();
        Set attrNames = attributes.keySet();
        for (Iterator it = attrNames.iterator(); it.hasNext();) {
            Object name = it.next();
            Object value = attributes.get(name);
            if ((name instanceof String) && (value instanceof String)) {
                attributeList.append(createAttribute((String) name,
                        (String) value));
                if (it.hasNext()) {
                    attributeList.append(IIntroHTMLConstants.SPACE);
                }
            }
        }
        return attributeList.toString();
    }

    /**
     * Creates an HTML attribute of the form attrName="attrValue"
     * 
     * @param attrName
     *            the name of the attribute
     * @param attrValue
     *            the value of the attribute
     * @return
     */
    public static StringBuffer createAttribute(String attrName, String attrValue) {
        StringBuffer attribute = new StringBuffer();
        if (attrName != null && attrValue != null) {
            attribute.append(attrName).append(IIntroHTMLConstants.EQUALS)
                    .append(IIntroHTMLConstants.QUOTE).append(attrValue)
                    .append(IIntroHTMLConstants.QUOTE);
        }
        return attribute;
    }

    public static StringBuffer openHTMLStartTag(String elementName) {
        return new StringBuffer().append(IIntroHTMLConstants.LT).append(
                elementName);
    }

    public static StringBuffer closeHTMLTag() {
        return closeHTMLTag(true);
    }

    public static StringBuffer closeHTMLTag(boolean newLine) {
        StringBuffer closing = new StringBuffer()
                .append(IIntroHTMLConstants.GT);
        if (newLine)
            closing.append(IIntroHTMLConstants.NEW_LINE);
        return closing;
    }

    /**
     * Determine if the contents of two character arrays are equal
     * 
     * @param a
     * @param b
     * @return
     */
    public static boolean equalCharArrayContent(char[] a, char[] b) {
        if (a.length != b.length)
            return false;
        for (int i = 0; i < a.length; i++) {
            if (a[i] != b[i])
                return false;
        }
        return true;
    }
}

Back to the top