1 23 24 package org.cofax.util; 25 26 import java.net.*; 27 import java.io.*; 28 import java.util.*; 29 import java.text.*; 30 import org.apache.oro.text.perl.Perl5Util; 31 import org.cofax.WysiwygTemplate; 32 import org.cofax.XMLConfig; 33 34 class WebToXML { 35 36 45 46 static String urlToGet; 47 48 static String fileToWrite; 49 50 static String templateFilename; 51 52 static String articleFilename; 53 54 static String section; 55 56 static String pubName; 57 58 static String noVersioning; 59 60 static String disableIndex; 61 62 public static void main(String [] args) { 63 64 72 73 if (args.length != 1) { 74 System.err.println("Usage: WebToXML configFilename"); 75 } else { 76 String xmlConfigFilename = args[0]; 77 readConfigFile(xmlConfigFilename); 78 String results = ""; 79 try { 80 results = getURL(urlToGet); 81 String xmlToWrite = encodeAsXML(results, templateFilename); 82 writeToFile(xmlToWrite, fileToWrite); 83 System.out.println("ok, wrote to: " + fileToWrite); 84 } catch (HTTPNotOKException ex) { 85 System.err.println("Foreign server didn't respond correctly"); 86 System.err.println("exiting..."); 87 } 88 89 } 90 } 91 92 public static String getURL(String URLToGet) throws HTTPNotOKException { 93 102 String pageContents = ""; 103 URL url = null; 104 HttpURLConnection connection = null; 105 int responseCode = 0; 106 try { 107 url = new URL(URLToGet); 108 connection = (HttpURLConnection) url.openConnection(); 109 responseCode = connection.getResponseCode(); 110 } catch (MalformedURLException ex) { 111 System.err.println("Error: Malformed URL"); 112 } catch (IOException ex) { 113 System.err.println("Error: IO Exception"); 114 } 115 116 if (responseCode != 200) { 118 throw new HTTPNotOKException(responseCode); 119 } else { 120 try { 122 BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); 123 String inputLine; 124 while ((inputLine = in.readLine()) != null) { 125 pageContents += inputLine; 126 } 127 } catch (IOException ex) { 128 System.err.println("Error: IO Exception"); 129 } 130 } 131 return pageContents; 132 133 } 134 135 public static String encodeAsXML(String stringToEncode, String xmlTemplateFilename) { 136 137 146 147 String escapedStringToEncode = convertSpecialChars(stringToEncode); 148 149 File inputFile = new File(xmlTemplateFilename); 150 BufferedReader in = null; 151 try { 152 in = new BufferedReader(new InputStreamReader(new FileInputStream(inputFile))); 153 } catch (FileNotFoundException ex) { 154 System.out.println("Error: Template file not found"); 155 } 156 157 String inputLine; 158 String fileContents = ""; try { 160 while ((inputLine = in.readLine()) != null) { 161 fileContents += inputLine + "\n"; 162 } 163 } catch (IOException ex) { 164 System.err.println("Error: IOException found trying to read template file"); 165 } 166 StringBuffer fileContentsStringBuffer = new StringBuffer (fileContents); 167 168 HashMap glossary = new HashMap(); 169 glossary.put("date", returnDate()); 170 glossary.put("pubName", pubName); 171 glossary.put("section", section); 172 glossary.put("filename", articleFilename); 173 glossary.put("body", escapedStringToEncode); 174 glossary.put("noVersioning", noVersioning); 175 glossary.put("disableIndex", disableIndex); 176 177 WysiwygTemplate template = new WysiwygTemplate(); 178 String completeXML = template.applyTemplate(fileContentsStringBuffer, glossary); 179 return completeXML; 180 } 181 182 public static void writeToFile(String stringToWrite, String fileName) { 183 184 191 192 FileOutputStream out; PrintStream p; try { 195 out = new FileOutputStream(fileName); 198 p = new PrintStream(out); 200 p.println(stringToWrite); 201 p.close(); 202 } catch (Exception e) { 203 System.err.println("Error writing to file"); 204 } 205 } 206 207 public static String convertSpecialChars(String linesToParse) { 208 209 216 217 Perl5Util util = new Perl5Util(); 218 219 linesToParse = util.substitute("s/\n+/\n/g", linesToParse); 220 linesToParse = util.substitute("s/\n/<br><br>\n/g", linesToParse); 221 linesToParse = util.substitute("s/\021//g", linesToParse); 222 linesToParse = util.substitute("s/\252 *//g", linesToParse); 223 linesToParse = util.substitute("s/\317/-/g", linesToParse); 224 linesToParse = util.substitute("s/\376//g", linesToParse); 225 linesToParse = util.substitute("s/\004/<li>/g", linesToParse); 226 227 linesToParse = util.substitute("s/</</g", linesToParse); 229 linesToParse = util.substitute("s/>/>/g", linesToParse); 230 231 linesToParse = util.substitute("s/\\^C\\^D/ /g", linesToParse); 233 linesToParse = util.substitute("s/\\^G//g", linesToParse); 234 linesToParse = util.substitute("s/\\\\//g", linesToParse); 235 236 244 linesToParse = util.substitute("s/\\'\\'/\"/g", linesToParse); 245 linesToParse = util.substitute("s/\\`/\\'/g", linesToParse); 246 linesToParse = util.substitute("s/\"/"/g", linesToParse); 247 248 linesToParse = util.substitute("s/&/&/g", linesToParse); 250 linesToParse = util.substitute("s/&([a-zA-Z0-9]*;)/&$1/g", linesToParse); 252 linesToParse = util.substitute("s/ /&nbsp;/g", linesToParse); 254 linesToParse = util.substitute("s/ñ/ñ/g", linesToParse); 256 257 return linesToParse; 258 259 } 260 261 public static String returnDate() { 262 263 270 271 Date now = new Date(); 272 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); 273 String formattedDate = dateFormat.format(now); 274 return formattedDate; 275 } 276 277 public static void readConfigFile(String configFilename) { 278 279 284 285 boolean didload = false; 286 try { 287 XMLConfig configFile = new XMLConfig(); 288 configFile.setXMLFileName(configFilename); 289 290 didload = configFile.load(); 291 if (!didload) { 293 System.err.println("Configuration file didn't load"); 294 System.err.println(configFile.getLastError()); 295 } 296 298 303 urlToGet = configFile.getString("urlToGet"); 304 fileToWrite = configFile.getString("fileToWrite"); 305 templateFilename = configFile.getString("templateFilename"); 306 articleFilename = configFile.getString("articleFilename"); 307 section = configFile.getString("section"); 308 pubName = configFile.getString("pubName"); 309 noVersioning = configFile.getString("noVersioning"); 310 disableIndex = configFile.getString("disableIndex"); 311 312 } catch (Exception e) { 313 314 System.err.println("Error reading configuration:"); 315 e.printStackTrace(System.err); 316 317 } 318 } 319 } 320 321 class HTTPNotOKException extends Exception { 322 HTTPNotOKException(int responseCode) { 323 super("HTTP 200 not returned: server responded with " + responseCode); 324 } 325 } 326 | Popular Tags |