KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > cms > ant > DownloadFeeds


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
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
18 package org.apache.lenya.cms.ant;
19
20 import java.io.File JavaDoc;
21 import java.io.FileOutputStream JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.net.HttpURLConnection JavaDoc;
25 import java.net.MalformedURLException JavaDoc;
26 import java.net.URL JavaDoc;
27 import java.net.URLConnection JavaDoc;
28 import java.util.NoSuchElementException JavaDoc;
29 import java.util.StringTokenizer JavaDoc;
30
31 import javax.xml.parsers.ParserConfigurationException JavaDoc;
32 import javax.xml.parsers.SAXParser JavaDoc;
33 import javax.xml.parsers.SAXParserFactory JavaDoc;
34
35 import org.apache.tools.ant.BuildException;
36 import org.apache.tools.ant.Task;
37 import org.xml.sax.SAXException JavaDoc;
38 import org.xml.sax.helpers.DefaultHandler JavaDoc;
39
40 /**
41  * Download (via HTTP) feed (RSS, Atom, ...) and verify well-formedness of XML
42  */

43 public class DownloadFeeds extends Task {
44     private boolean verbose = false;
45     private boolean ignoreErrors = false;
46     
47     String JavaDoc rootDir;
48     String JavaDoc feeds;
49
50     /**
51      * Get feeds from build.xml
52      */

53     public void setFeeds(String JavaDoc str) {
54         feeds = str;
55     }
56
57     /**
58      * Get root directory from build.xml
59      */

60     public void setRootdir(String JavaDoc str) {
61         rootDir = str;
62     }
63     
64     /**
65      *
66      */

67     public void execute() {
68         if (rootDir == null) {
69             throw new BuildException("No rootdir specified");
70         } else {
71             log("Root directory: " + rootDir);
72         }
73
74         if (feeds == null) {
75             throw new BuildException("No feeds specified");
76         }
77         
78         StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc(feeds, ",");
79         
80         if (tok.countTokens() == 0) {
81             throw new BuildException("Illegal feeds string");
82         }
83         
84         try {
85             String JavaDoc url;
86             
87             while ((url = tok.nextToken()) != null) {
88                 String JavaDoc fname = tok.nextToken();
89                 
90                 URL JavaDoc source = null;
91                 try {
92                     source = new URL JavaDoc(url);
93                     log("Feed: " + url);
94                 } catch (MalformedURLException JavaDoc e) {
95                     log("bad url");
96                     throw new BuildException(e, getLocation());
97                 }
98                 
99                 File JavaDoc dest = null;
100                 try {
101                     //log("file: " + fname);
102
dest = new File JavaDoc(fname);
103                     if (!dest.isAbsolute()) {
104                         //log("Is NOT absolute: " + dest);
105
dest = new File JavaDoc(rootDir, fname);
106                     }
107                     log("Destination: " + dest);
108                 } catch (NullPointerException JavaDoc e) {
109                     log(e.toString());
110                 }
111
112                 try {
113                     URLConnection JavaDoc connection = source.openConnection();
114                     
115                     connection.connect();
116                     HttpURLConnection JavaDoc httpConnection = (HttpURLConnection JavaDoc) connection;
117                     
118                     InputStream JavaDoc is = null;
119                     for (int i = 0; i < 3; i++) {
120                         try {
121                             is = connection.getInputStream();
122                             break;
123                         } catch (IOException JavaDoc ex) {
124                             log("Error opening connection " + ex);
125                         }
126                     }
127                     if (is == null) {
128                         log("Can't get " + source + " to " + dest);
129                         if (ignoreErrors) {
130                             return;
131                         }
132                         throw new BuildException("Can't get " + source + " to " + dest,
133                                                  getLocation());
134                     }
135
136                     FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(dest);
137                     boolean finished = false;
138                     try {
139                         byte[] buffer = new byte[100 * 1024];
140                         int length;
141                         int dots = 0;
142
143                         while ((length = is.read(buffer)) >= 0) {
144                             fos.write(buffer, 0, length);
145                             if (verbose) {
146                                 System.out.print(".");
147                                 if (dots++ > 50) {
148                                     System.out.flush();
149                                     dots = 0;
150                                 }
151                             }
152                         }
153                         if (verbose) {
154                             System.out.println();
155                         }
156                         finished = true;
157                     } finally {
158                         if (fos != null) {
159                             fos.close();
160                         }
161                         is.close();
162                         // we have started to (over)write dest, but failed.
163
// Try to delete the garbage we'd otherwise leave
164
// behind.
165
if (!finished) {
166                             dest.delete();
167                         }
168                     }
169                 } catch (IOException JavaDoc e) {
170                     log("IOException: " + e.toString());
171                     throw new BuildException(e, getLocation());
172                 }
173                 
174                 SAXParserFactory JavaDoc factory = SAXParserFactory.newInstance();
175                 try {
176                     SAXParser JavaDoc saxParser = factory.newSAXParser();
177                     saxParser.parse( dest, new DefaultHandler JavaDoc() );
178                 } catch (IOException JavaDoc e) {
179                     e.toString();
180                     throw new BuildException(e, getLocation());
181                 } catch (ParserConfigurationException JavaDoc e) {
182                     e.toString();
183                     throw new BuildException(e, getLocation());
184                 } catch (IllegalArgumentException JavaDoc e) {
185                     e.toString();
186                     throw new BuildException(e, getLocation());
187                 } catch (SAXException JavaDoc e) {
188                     log("XML: " + dest + " is NOT well-formed!!!");
189                     throw new BuildException(e, getLocation());
190                 }
191
192                 log("XML file: " + dest + " seems to be well-formed :-)");
193             }
194         } catch (NoSuchElementException JavaDoc e) {
195             return;
196         }
197     }
198 }
199
Popular Tags