KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > htmlcleaner > HtmlCleanerForAnt


1 /* Copyright (c) 2006-2007, Vladimir Nikic
2     All rights reserved.
3
4     Redistribution and use of this software in source and binary forms,
5     with or without modification, are permitted provided that the following
6     conditions are met:
7
8     * Redistributions of source code must retain the above
9       copyright notice, this list of conditions and the
10       following disclaimer.
11
12     * Redistributions in binary form must reproduce the above
13       copyright notice, this list of conditions and the
14       following disclaimer in the documentation and/or other
15       materials provided with the distribution.
16
17     * The name of HtmlCleaner may not be used to endorse or promote
18       products derived from this software without specific prior
19       written permission.
20
21     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22     AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23     IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24     ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25     LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26     CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27     SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28     INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30     ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31     POSSIBILITY OF SUCH DAMAGE.
32
33     You can contact Vladimir Nikic by sending e-mail to
34     nikic_vladimir@yahoo.com. Please include the word "HtmlCleaner" in the
35     subject line.
36 */

37
38 package org.htmlcleaner;
39
40 import org.apache.tools.ant.BuildException;
41
42 import java.net.URL JavaDoc;
43 import java.io.File JavaDoc;
44 import java.io.IOException JavaDoc;
45
46 /**
47  * <p>Support for ANT.</p>
48  *
49  * Created by: Vladimir Nikic <br/>
50  * Date: December, 2006.
51  */

52 public class HtmlCleanerForAnt extends org.apache.tools.ant.Task {
53
54     private String JavaDoc text;
55     private String JavaDoc src;
56     private String JavaDoc dest;
57     private String JavaDoc incharset = HtmlCleaner.DEFAULT_CHARSET;
58     private String JavaDoc outcharset = HtmlCleaner.DEFAULT_CHARSET;
59     private String JavaDoc 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 JavaDoc hyphenreplacement = "=";
71
72     public void setText(String JavaDoc text) {
73         this.text = text;
74     }
75
76     public void setSrc(String JavaDoc src) {
77         this.src = src;
78     }
79
80     public void setDest(String JavaDoc dest) {
81         this.dest = dest;
82     }
83
84     public void setIncharset(String JavaDoc incharset) {
85         this.incharset = incharset;
86     }
87
88     public void setOutcharset(String JavaDoc outcharset) {
89         this.outcharset = outcharset;
90     }
91
92     public void setOutputtype(String JavaDoc 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 JavaDoc hyphenreplacement) {
137         this.hyphenreplacement = hyphenreplacement;
138     }
139
140     public void addText(String JavaDoc text) {
141         this.text = text;
142     }
143
144     /**
145      * Implementation of Ant task execution.
146      * @throws BuildException
147      */

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 JavaDoc(src), incharset);
158             } else if (src != null) {
159                 cleaner = new HtmlCleaner(new File JavaDoc(src), incharset);
160             } else {
161                 cleaner = new HtmlCleaner(text);
162             }
163         } catch (IOException JavaDoc 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 JavaDoc e) {
200              throw new BuildException(e);
201         }
202     }
203
204 }
Popular Tags