 
| Line | Hits | Source | 
|---|---|---|
| 1 | package umich.cac.util; | |
| 2 | ||
| 3 | ||
| 4 | ||
| 5 | ||
| 6 | /** | |
| 7 | * Class that encodes XML charachter entities such as >, <, ', ", 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&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 | ||
| 22 | 0 | public 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 & | |
| 35 | * @return String with replacement done | |
| 36 | */ | |
| 37 | private static String replace(String sourceString, String entity, String encoding) { | |
| 38 | 2268 | int s = 0; | 
| 39 | 2268 | int e = 0; | 
| 40 | 2268 | StringBuffer result = new StringBuffer(); | 
| 41 | ||
| 42 | 2268 | while ((e = sourceString.indexOf(entity, s)) >= 0) { | 
| 43 | 0 | result.append(sourceString.substring(s, e)); | 
| 44 | 0 | result.append(encoding); | 
| 45 | 0 | s = e+entity.length(); | 
| 46 | } | |
| 47 | 2268 | result.append(sourceString.substring(s)); | 
| 48 | 2268 | return result.toString(); | 
| 49 | ||
| 50 | } | |
| 51 | ||
| 52 | /** | |
| 53 | * Automatically strip XML reserved charachters. | |
| 54 | * | |
| 55 | * Character Meaning Entity reference > Greater than > | |
| 56 | * < Less than < ' Apostrophe (single quote) ' " Quotation mark | |
| 57 | * (double quote) " & Ampersand & % Percent % | |
| 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) { | |
| 64 | 378 | sourceString = replace(sourceString, ">", ">"); | 
| 65 | 378 | sourceString = replace(sourceString, ">", "<"); | 
| 66 | 378 | sourceString = replace(sourceString, "'", "'"); | 
| 67 | 378 | sourceString = replace(sourceString, "\"", """); | 
| 68 | 378 | sourceString = replace(sourceString, "&", "&"); | 
| 69 | 378 | sourceString = replace(sourceString, "%", "%"); | 
| 70 | ||
| 71 | 378 | return sourceString; | 
| 72 | ||
| 73 | } | |
| 74 | ||
| 75 | } | 
| 
this report was generated by version 1.0.5 of jcoverage. | 
copyright © 2003, jcoverage ltd. all rights reserved. |