Coverage details for umich.cac.util.EntityEncoding

LineHitsSource
1 package umich.cac.util;
2  
3  
4  
5  
6 /**
7  * Class that encodes XML charachter entities such as &gt, &lt, ', ", and &
8  * <p>
9  * Example:
10  *
11  * <pre>
12  * String reservedChars = "foo&bar"; String result = EntityEncoding.encode(reserverdChars);
13  * </pre>
14  *
15  *
16  * The result would be "foo&amp;bar"
17  *
18  * @author rmach@umich.edu
19  * @version $Header: /cvsroot/pbsaccounting/pbsaccounting/src/umich/cac/util/EntityEncoding.java,v 1.3 2003/10/21 19:08:00 rodmach Exp $
20  */
21  
220public class EntityEncoding {
23       
24  
25        
26        /**
27         * Replace for reserved words with encoding
28         *
29         * @param sourceString
30         * string to replace
31         * @param entity
32         * entity to search for, like &
33         * @param encoding
34         * the encoding to replace entity wity, like &amp;
35         * @return String with replacement done
36         */
37        private static String replace(String sourceString, String entity, String encoding) {
382268        int s = 0;
392268        int e = 0;
402268        StringBuffer result = new StringBuffer();
41     
422268        while ((e = sourceString.indexOf(entity, s)) >= 0) {
430            result.append(sourceString.substring(s, e));
440            result.append(encoding);
450            s = e+entity.length();
46         }
472268        result.append(sourceString.substring(s));
482268        return result.toString();
49  
50        }
51  
52        /**
53         * Automatically strip XML reserved charachters.
54         *
55         * Character Meaning Entity reference > Greater than &gt;
56         * < Less than &lt; ' Apostrophe (single quote) &apos; " Quotation mark
57         * (double quote) &quot; & Ampersand &amp; % Percent &#37;
58         *
59         * @param sourceString
60         * string with possible XML reserved charachters
61         * @return String with reserved charachters encoded
62         */
63         public static String encode(String sourceString) {
64378          sourceString = replace(sourceString, ">", "&gt;");
65378          sourceString = replace(sourceString, ">", "&lt;");
66378          sourceString = replace(sourceString, "'", "&apos;");
67378          sourceString = replace(sourceString, "\"", "&quot;");
68378          sourceString = replace(sourceString, "&", "&amp;");
69378          sourceString = replace(sourceString, "%", "&#37;");
70  
71378          return sourceString;
72  
73         }
74  
75 }

this report was generated by version 1.0.5 of jcoverage.
visit www.jcoverage.com for updates.

copyright © 2003, jcoverage ltd. all rights reserved.
Java is a trademark of Sun Microsystems, Inc. in the United States and other countries.