1 37 38 package org.htmlcleaner; 39 40 import java.io.IOException ; 41 42 import org.htmlcleaner.BaseToken; 43 44 50 public class DoctypeToken implements BaseToken { 51 52 private String part1; 53 private String part2; 54 private String part3; 55 private String part4; 56 57 public DoctypeToken(String part1, String part2, String part3, String part4) { 58 this.part1 = part1 != null ? part1.toUpperCase() : part1; 59 this.part2 = part2 != null ? part2.toUpperCase() : part2; 60 this.part3 = clean(part3); 61 this.part4 = clean(part4); 62 } 63 64 private String clean(String s) { 65 if (s != null) { 66 s = s.replace('>', ' '); 67 s = s.replace('<', ' '); 68 s = s.replace('&', ' '); 69 s = s.replace('\'', ' '); 70 s = s.replace('\"', ' '); 71 } 72 73 return s; 74 } 75 76 public boolean isValid() { 77 if ( part1 == null || "".equals(part1) ) { 78 return false; 79 } 80 81 if ( !"public".equalsIgnoreCase(part2) && !"system".equalsIgnoreCase(part2) ) { 82 return false; 83 } 84 85 if ( "system".equalsIgnoreCase(part2) && part4 != null && !"".equals(part4) ) { 86 return false; 87 } 88 89 if ( "public".equalsIgnoreCase(part2) && (part4 == null || "".equals(part4)) ) { 90 return false; 91 } 92 93 return true; 94 } 95 96 public String getContent() { 97 String result = "<!DOCTYPE " + part1 + " "; 98 result += part2 + " \"" + part3 + "\""; 99 if ( part4 != null && !"".equals(part4) ) { 100 result += " \"" + part4 + "\""; 101 } 102 103 result += ">"; 104 105 return result; 106 } 107 108 public String toString() { 109 return getContent(); 110 } 111 112 public String getName() { 113 return ""; 114 } 115 116 public void serialize(XmlSerializer xmlSerializer) throws IOException { 117 xmlSerializer.getWriter().write(getContent() + "\n"); 118 } 119 120 } | Popular Tags |