HtmlEncoder.java

1
/*
2
 * $Id: HtmlEncoder.java 3373 2008-05-12 16:21:24Z xlv $
3
 *
4
 * Copyright 1999, 2000, 2001, 2002 by Bruno Lowagie.
5
 *
6
 * The contents of this file are subject to the Mozilla Public License Version 1.1
7
 * (the "License"); you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at http://www.mozilla.org/MPL/
9
 *
10
 * Software distributed under the License is distributed on an "AS IS" basis,
11
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12
 * for the specific language governing rights and limitations under the License.
13
 *
14
 * The Original Code is 'iText, a free JAVA-PDF library'.
15
 *
16
 * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
17
 * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
18
 * All Rights Reserved.
19
 * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
20
 * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
21
 *
22
 * Contributor(s): all the names of the contributors are added in the source code
23
 * where applicable.
24
 *
25
 * Alternatively, the contents of this file may be used under the terms of the
26
 * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
27
 * provisions of LGPL are applicable instead of those above.  If you wish to
28
 * allow use of your version of this file only under the terms of the LGPL
29
 * License and not to allow others to use your version of this file under
30
 * the MPL, indicate your decision by deleting the provisions above and
31
 * replace them with the notice and other provisions required by the LGPL.
32
 * If you do not delete the provisions above, a recipient may use your version
33
 * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
34
 *
35
 * This library is free software; you can redistribute it and/or modify it
36
 * under the terms of the MPL as stated above or under the terms of the GNU
37
 * Library General Public License as published by the Free Software Foundation;
38
 * either version 2 of the License, or any later version.
39
 *
40
 * This library is distributed in the hope that it will be useful, but WITHOUT
41
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
42
 * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
43
 * details.
44
 *
45
 * If you didn't download this code from the following link, you should check if
46
 * you aren't using an obsolete version:
47
 * http://www.lowagie.com/iText/
48
 */
49
50
package com.lowagie.text.html;
51
52
import java.awt.Color;
53
54
import com.lowagie.text.Element;
55
56
/**
57
 * This class converts a <CODE>String</CODE> to the HTML-format of a String.
58
 * <P>
59
 * To convert the <CODE>String</CODE>, each character is examined:
60
 * <UL>
61
 * <LI>ASCII-characters from 000 till 031 are represented as &amp;#xxx;<BR>
62
 *     (with xxx = the value of the character)
63
 * <LI>ASCII-characters from 032 t/m 127 are represented by the character itself, except for:
64
 *     <UL>
65
 *     <LI>'\n'    becomes &lt;BR&gt;\n
66
 *     <LI>&quot; becomes &amp;quot;
67
 *     <LI>&amp; becomes &amp;amp;
68
 *     <LI>&lt; becomes &amp;lt;
69
 *     <LI>&gt; becomes &amp;gt;
70
 *     </UL>
71
 * <LI>ASCII-characters from 128 till 255 are represented as &amp;#xxx;<BR>
72
 *     (with xxx = the value of the character)
73
 * </UL>
74
 * <P>
75
 * Example:
76
 * <P><BLOCKQUOTE><PRE>
77
 *    String htmlPresentation = HtmlEncoder.encode("Marie-Th&#233;r&#232;se S&#248;rensen");
78
 * </PRE></BLOCKQUOTE><P>
79
 * for more info: see O'Reilly; "HTML: The Definitive Guide" (page 164)
80
 *
81
 * @author  mario.maccarini@ugent.be
82
 */
83
84
public final class HtmlEncoder {
85
    
86
    // membervariables
87
    
88
/** List with the HTML translation of all the characters. */
89
    private static final String[] htmlCode = new String[256];
90
    
91
    static {
92
        for (int i = 0; i < 10; i++) {
93
            htmlCode[i] = "&#00" + i + ";";
94
        }
95
        
96
        for (int i = 10; i < 32; i++) {
97
            htmlCode[i] = "&#0" + i + ";";
98
        }
99
        
100
        for (int i = 32; i < 128; i++) {
101
            htmlCode[i] = String.valueOf((char)i);
102
        }
103
        
104
        // Special characters
105
        htmlCode['\t'] = "\t";
106
        htmlCode['\n'] = "<" + HtmlTags.NEWLINE + " />\n";
107
        htmlCode['\"'] = "&quot;"; // double quote
108
        htmlCode['&'] = "&amp;"; // ampersand
109
        htmlCode['<'] = "&lt;"; // lower than
110
        htmlCode['>'] = "&gt;"; // greater than
111
        
112
        for (int i = 128; i < 256; i++) {
113
            htmlCode[i] = "&#" + i + ";";
114
        }
115
    }
116
    
117
    
118
    // constructors
119
    
120
/**
121
 * This class will never be constructed.
122
 * <P>
123
 * HtmlEncoder only contains static methods.
124
 */
125
    
126
    private HtmlEncoder () { }
127
    
128
    // methods
129
    
130
/**
131
 * Converts a <CODE>String</CODE> to the HTML-format of this <CODE>String</CODE>.
132
 *
133
 * @param    string    The <CODE>String</CODE> to convert
134
 * @return    a <CODE>String</CODE>
135
 */
136
    
137
    public static String encode(String string) {
138
        int n = string.length();
139
        char character;
140
        StringBuilder buffer = new StringBuilder();
141
        // loop over all the characters of the String.
142 3 1. encode : changed conditional boundary → NO_COVERAGE
2. encode : Changed increment from 1 to -1 → NO_COVERAGE
3. encode : negated conditional → NO_COVERAGE
        for (int i = 0; i < n; i++) {
143
            character = string.charAt(i);
144
            // the Htmlcode of these characters are added to a StringBuffer one by one
145 2 1. encode : changed conditional boundary → NO_COVERAGE
2. encode : negated conditional → NO_COVERAGE
            if (character < 256) {
146
                buffer.append(htmlCode[character]);
147
            }
148
            else {
149
                // Improvement posted by Joachim Eyrich
150
                buffer.append("&#").append((int)character).append(';');
151
            }
152
        }
153 1 1. encode : mutated return of Object value for com/lowagie/text/html/HtmlEncoder::encode to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
        return buffer.toString();
154
    }
155
    
156
/**
157
 * Converts a <CODE>Color</CODE> into a HTML representation of this <CODE>Color</CODE>.
158
 *
159
 * @param    color    the <CODE>Color</CODE> that has to be converted.
160
 * @return    the HTML representation of this <COLOR>Color</COLOR>
161
 */
162
    
163
    public static String encode(Color color) {
164
        StringBuilder buffer = new StringBuilder("#");
165 2 1. encode : changed conditional boundary → NO_COVERAGE
2. encode : negated conditional → NO_COVERAGE
        if (color.getRed() < 16) {
166
            buffer.append('0');
167
        }
168
        buffer.append(Integer.toString(color.getRed(), 16));
169 2 1. encode : changed conditional boundary → NO_COVERAGE
2. encode : negated conditional → NO_COVERAGE
        if (color.getGreen() < 16) {
170
            buffer.append('0');
171
        }
172
        buffer.append(Integer.toString(color.getGreen(), 16));
173 2 1. encode : changed conditional boundary → NO_COVERAGE
2. encode : negated conditional → NO_COVERAGE
        if (color.getBlue() < 16) {
174
            buffer.append('0');
175
        }
176
        buffer.append(Integer.toString(color.getBlue(), 16));
177 1 1. encode : mutated return of Object value for com/lowagie/text/html/HtmlEncoder::encode to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
        return buffer.toString();
178
    }
179
    
180
/**
181
 * Translates the alignment value.
182
 *
183
 * @param   alignment   the alignment value
184
 * @return  the translated value
185
 */
186
    
187
    public static String getAlignment(int alignment) {
188
        switch(alignment) {
189
            case Element.ALIGN_LEFT:
190 1 1. getAlignment : mutated return of Object value for com/lowagie/text/html/HtmlEncoder::getAlignment to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
                return HtmlTags.ALIGN_LEFT;
191
            case Element.ALIGN_CENTER:
192 1 1. getAlignment : mutated return of Object value for com/lowagie/text/html/HtmlEncoder::getAlignment to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
                return HtmlTags.ALIGN_CENTER;
193
            case Element.ALIGN_RIGHT:
194 1 1. getAlignment : mutated return of Object value for com/lowagie/text/html/HtmlEncoder::getAlignment to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
                return HtmlTags.ALIGN_RIGHT;
195
            case Element.ALIGN_JUSTIFIED:
196
            case Element.ALIGN_JUSTIFIED_ALL:
197 1 1. getAlignment : mutated return of Object value for com/lowagie/text/html/HtmlEncoder::getAlignment to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
                return HtmlTags.ALIGN_JUSTIFIED;
198
            case Element.ALIGN_TOP:
199 1 1. getAlignment : mutated return of Object value for com/lowagie/text/html/HtmlEncoder::getAlignment to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
                return HtmlTags.ALIGN_TOP;
200
            case Element.ALIGN_MIDDLE:
201 1 1. getAlignment : mutated return of Object value for com/lowagie/text/html/HtmlEncoder::getAlignment to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
                return HtmlTags.ALIGN_MIDDLE;
202
            case Element.ALIGN_BOTTOM:
203 1 1. getAlignment : mutated return of Object value for com/lowagie/text/html/HtmlEncoder::getAlignment to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
                return HtmlTags.ALIGN_BOTTOM;
204
            case Element.ALIGN_BASELINE:
205 1 1. getAlignment : mutated return of Object value for com/lowagie/text/html/HtmlEncoder::getAlignment to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
                return HtmlTags.ALIGN_BASELINE;
206
                default:
207 1 1. getAlignment : mutated return of Object value for com/lowagie/text/html/HtmlEncoder::getAlignment to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
                    return "";
208
        }
209
    }
210
}

Mutations

142

1.1
Location : encode
Killed by : none
changed conditional boundary → NO_COVERAGE

2.2
Location : encode
Killed by : none
Changed increment from 1 to -1 → NO_COVERAGE

3.3
Location : encode
Killed by : none
negated conditional → NO_COVERAGE

145

1.1
Location : encode
Killed by : none
changed conditional boundary → NO_COVERAGE

2.2
Location : encode
Killed by : none
negated conditional → NO_COVERAGE

153

1.1
Location : encode
Killed by : none
mutated return of Object value for com/lowagie/text/html/HtmlEncoder::encode to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

165

1.1
Location : encode
Killed by : none
changed conditional boundary → NO_COVERAGE

2.2
Location : encode
Killed by : none
negated conditional → NO_COVERAGE

169

1.1
Location : encode
Killed by : none
changed conditional boundary → NO_COVERAGE

2.2
Location : encode
Killed by : none
negated conditional → NO_COVERAGE

173

1.1
Location : encode
Killed by : none
changed conditional boundary → NO_COVERAGE

2.2
Location : encode
Killed by : none
negated conditional → NO_COVERAGE

177

1.1
Location : encode
Killed by : none
mutated return of Object value for com/lowagie/text/html/HtmlEncoder::encode to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

190

1.1
Location : getAlignment
Killed by : none
mutated return of Object value for com/lowagie/text/html/HtmlEncoder::getAlignment to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

192

1.1
Location : getAlignment
Killed by : none
mutated return of Object value for com/lowagie/text/html/HtmlEncoder::getAlignment to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

194

1.1
Location : getAlignment
Killed by : none
mutated return of Object value for com/lowagie/text/html/HtmlEncoder::getAlignment to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

197

1.1
Location : getAlignment
Killed by : none
mutated return of Object value for com/lowagie/text/html/HtmlEncoder::getAlignment to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

199

1.1
Location : getAlignment
Killed by : none
mutated return of Object value for com/lowagie/text/html/HtmlEncoder::getAlignment to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

201

1.1
Location : getAlignment
Killed by : none
mutated return of Object value for com/lowagie/text/html/HtmlEncoder::getAlignment to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

203

1.1
Location : getAlignment
Killed by : none
mutated return of Object value for com/lowagie/text/html/HtmlEncoder::getAlignment to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

205

1.1
Location : getAlignment
Killed by : none
mutated return of Object value for com/lowagie/text/html/HtmlEncoder::getAlignment to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

207

1.1
Location : getAlignment
Killed by : none
mutated return of Object value for com/lowagie/text/html/HtmlEncoder::getAlignment to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

Active mutators

Tests examined


Report generated by PIT 1.4.2