1 37 38 package org.htmlcleaner; 39 40 import org.apache.tools.ant.BuildException; 41 42 import java.net.URL ; 43 import java.io.File ; 44 import java.io.IOException ; 45 46 52 public class HtmlCleanerForAnt extends org.apache.tools.ant.Task { 53 54 private String text; 55 private String src; 56 private String dest; 57 private String incharset = HtmlCleaner.DEFAULT_CHARSET; 58 private String outcharset = HtmlCleaner.DEFAULT_CHARSET; 59 private String outputtype = "simple"; 60 private boolean advancedxmlescape = true; 61 private boolean usecdata = true; 62 private boolean specialentities = true; 63 private boolean unicodechars = true; 64 private boolean omitunknowntags = false; 65 private boolean omitdeprtags = false; 66 private boolean omitcomments = false; 67 private boolean omitxmldecl = false; 68 private boolean omitdoctypedecl = true; 69 private boolean omitxmlnsatt = false; 70 private String hyphenreplacement = "="; 71 72 public void setText(String text) { 73 this.text = text; 74 } 75 76 public void setSrc(String src) { 77 this.src = src; 78 } 79 80 public void setDest(String dest) { 81 this.dest = dest; 82 } 83 84 public void setIncharset(String incharset) { 85 this.incharset = incharset; 86 } 87 88 public void setOutcharset(String outcharset) { 89 this.outcharset = outcharset; 90 } 91 92 public void setOutputtype(String outputtype) { 93 this.outputtype = outputtype; 94 } 95 96 public void setAdvancedxmlescape(boolean advancedxmlescape) { 97 this.advancedxmlescape = advancedxmlescape; 98 } 99 100 public void setUsecdata(boolean usecdata) { 101 this.usecdata = usecdata; 102 } 103 104 public void setSpecialentities(boolean specialentities) { 105 this.specialentities = specialentities; 106 } 107 108 public void setUnicodechars(boolean unicodechars) { 109 this.unicodechars = unicodechars; 110 } 111 112 public void setOmitunknowntags(boolean omitunknowntags) { 113 this.omitunknowntags = omitunknowntags; 114 } 115 116 public void setOmitdeprtags(boolean omitdeprtags) { 117 this.omitdeprtags = omitdeprtags; 118 } 119 120 public void setOmitcomments(boolean omitcomments) { 121 this.omitcomments = omitcomments; 122 } 123 124 public void setOmitxmldecl(boolean omitxmldecl) { 125 this.omitxmldecl = omitxmldecl; 126 } 127 128 public void setOmitdoctypedecl(boolean omitdoctypedecl) { 129 this.omitdoctypedecl = omitdoctypedecl; 130 } 131 132 public void setOmitxmlnsatt(boolean omitxmlnsatt) { 133 this.omitxmlnsatt = omitxmlnsatt; 134 } 135 136 public void setHyphenreplacement(String hyphenreplacement) { 137 this.hyphenreplacement = hyphenreplacement; 138 } 139 140 public void addText(String text) { 141 this.text = text; 142 } 143 144 148 public void execute() throws BuildException { 149 HtmlCleaner cleaner = null; 150 151 if (text == null && src == null) { 152 throw new BuildException("Eather attribute 'src' or text body containing HTML must be specified!"); 153 } 154 155 try { 156 if ( src != null && (src.startsWith("http://") || src.startsWith("https://")) ) { 157 cleaner = new HtmlCleaner(new URL (src), incharset); 158 } else if (src != null) { 159 cleaner = new HtmlCleaner(new File (src), incharset); 160 } else { 161 cleaner = new HtmlCleaner(text); 162 } 163 } catch (IOException e) { 164 throw new BuildException(e); 165 } 166 167 cleaner.setAdvancedXmlEscape(this.advancedxmlescape); 168 cleaner.setUseCdataForScriptAndStyle(this.usecdata); 169 cleaner.setTranslateSpecialEntities(this.specialentities); 170 cleaner.setRecognizeUnicodeChars(this.unicodechars); 171 cleaner.setOmitUnknownTags(this.omitunknowntags); 172 cleaner.setOmitDeprecatedTags(this.omitdeprtags); 173 cleaner.setOmitComments(this.omitcomments); 174 cleaner.setOmitXmlDeclaration(this.omitxmldecl); 175 cleaner.setOmitDoctypeDeclaration(this.omitdoctypedecl); 176 cleaner.setOmitXmlnsAttributes(this.omitxmlnsatt); 177 cleaner.setHyphenReplacementInComment(this.hyphenreplacement); 178 179 try { 180 cleaner.clean(); 181 182 if ( dest == null || "".equals(dest.trim()) ) { 183 if ( "compact".equals(outputtype) ) { 184 cleaner.writeCompactXmlToStream(System.out, outcharset); 185 } else if ( "pretty".equals(outputtype) ) { 186 cleaner.writePrettyXmlToStream(System.out, outcharset); 187 } else { 188 cleaner.writeXmlToStream(System.out, outcharset); 189 } 190 } else { 191 if ( "compact".equals(outputtype) ) { 192 cleaner.writeCompactXmlToFile(dest, outcharset); 193 } else if ( "pretty".equals(outputtype) ) { 194 cleaner.writePrettyXmlToFile(dest, outcharset); 195 } else { 196 cleaner.writeXmlToFile(dest, outcharset); 197 } 198 } 199 } catch (IOException e) { 200 throw new BuildException(e); 201 } 202 } 203 204 } | Popular Tags |