1 17 18 package org.apache.lenya.cms.ant; 19 20 import java.io.File ; 21 import java.io.FileOutputStream ; 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 import java.net.HttpURLConnection ; 25 import java.net.MalformedURLException ; 26 import java.net.URL ; 27 import java.net.URLConnection ; 28 import java.util.NoSuchElementException ; 29 import java.util.StringTokenizer ; 30 31 import javax.xml.parsers.ParserConfigurationException ; 32 import javax.xml.parsers.SAXParser ; 33 import javax.xml.parsers.SAXParserFactory ; 34 35 import org.apache.tools.ant.BuildException; 36 import org.apache.tools.ant.Task; 37 import org.xml.sax.SAXException ; 38 import org.xml.sax.helpers.DefaultHandler ; 39 40 43 public class DownloadFeeds extends Task { 44 private boolean verbose = false; 45 private boolean ignoreErrors = false; 46 47 String rootDir; 48 String feeds; 49 50 53 public void setFeeds(String str) { 54 feeds = str; 55 } 56 57 60 public void setRootdir(String str) { 61 rootDir = str; 62 } 63 64 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 tok = new StringTokenizer (feeds, ","); 79 80 if (tok.countTokens() == 0) { 81 throw new BuildException("Illegal feeds string"); 82 } 83 84 try { 85 String url; 86 87 while ((url = tok.nextToken()) != null) { 88 String fname = tok.nextToken(); 89 90 URL source = null; 91 try { 92 source = new URL (url); 93 log("Feed: " + url); 94 } catch (MalformedURLException e) { 95 log("bad url"); 96 throw new BuildException(e, getLocation()); 97 } 98 99 File dest = null; 100 try { 101 dest = new File (fname); 103 if (!dest.isAbsolute()) { 104 dest = new File (rootDir, fname); 106 } 107 log("Destination: " + dest); 108 } catch (NullPointerException e) { 109 log(e.toString()); 110 } 111 112 try { 113 URLConnection connection = source.openConnection(); 114 115 connection.connect(); 116 HttpURLConnection httpConnection = (HttpURLConnection ) connection; 117 118 InputStream is = null; 119 for (int i = 0; i < 3; i++) { 120 try { 121 is = connection.getInputStream(); 122 break; 123 } catch (IOException 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 fos = new FileOutputStream (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 if (!finished) { 166 dest.delete(); 167 } 168 } 169 } catch (IOException e) { 170 log("IOException: " + e.toString()); 171 throw new BuildException(e, getLocation()); 172 } 173 174 SAXParserFactory factory = SAXParserFactory.newInstance(); 175 try { 176 SAXParser saxParser = factory.newSAXParser(); 177 saxParser.parse( dest, new DefaultHandler () ); 178 } catch (IOException e) { 179 e.toString(); 180 throw new BuildException(e, getLocation()); 181 } catch (ParserConfigurationException e) { 182 e.toString(); 183 throw new BuildException(e, getLocation()); 184 } catch (IllegalArgumentException e) { 185 e.toString(); 186 throw new BuildException(e, getLocation()); 187 } catch (SAXException 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 e) { 195 return; 196 } 197 } 198 } 199 | Popular Tags |