1 /*-------------------------------------------------------------------------- 2 * Copyright 2007 utgenome.org 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 *--------------------------------------------------------------------------*/ 16 //-------------------------------------- 17 // SCMDProject 18 // 19 // HTMLFilter.java 20 // Since: 2004/08/07 21 // 22 // $URL$ 23 // $LastChangedBy$ 24 //-------------------------------------- 25 26 package org.utgenome.gwt.utgb.server.util.xml; 27 28 import java.util.regex.Matcher; 29 import java.util.regex.Pattern; 30 31 /** 32 * Removes illegal characters that cannot be used as XML text contents into entity references. 33 * 34 * @author leo 35 * 36 */ 37 public class HTMLContentFilter implements TextContentFilter 38 { 39 Pattern cdataPattern = Pattern.compile("<!\\[CDATA\\[([^\\]]*)\\]\\]>"); 40 /** 41 * 42 */ 43 public HTMLContentFilter() 44 { 45 } 46 47 /** 48 * replaces &, <, >, ", ' characters into corresponding entity references 49 * 50 * @param content the conversion target string 51 * 52 * @return 53 */ 54 public String filter(String content) { 55 56 Matcher m = cdataPattern.matcher(content); 57 if(m.matches()) 58 { 59 // returns the content of the CDATA section 60 return m.group(1); 61 } 62 63 StringBuffer substituedStringBuffer = new StringBuffer(content.length()); 64 for (int i = 0; i < content.length(); i++) 65 { 66 char c = content.charAt(i); 67 switch (c) 68 { 69 case '<': 70 substituedStringBuffer.append("<"); 71 break; 72 case '>': 73 substituedStringBuffer.append(">"); 74 break; 75 case '"': 76 substituedStringBuffer.append("""); 77 break; 78 case '\'': 79 substituedStringBuffer.append("'"); 80 break; 81 case '&': 82 substituedStringBuffer.append("&"); 83 break; 84 default: 85 substituedStringBuffer.append(c); 86 } 87 } 88 return substituedStringBuffer.toString(); 89 } 90 91 } 92