1 19 20 33 package org.htmlparser.parserHelper; 34 35 import java.io.File ; 36 import java.io.IOException ; 37 import java.io.Serializable ; 38 import java.net.MalformedURLException ; 39 import java.net.URL ; 40 import java.net.URLConnection ; 41 42 import org.htmlparser.util.LinkProcessor; 43 import org.htmlparser.util.ParserException; 44 import org.htmlparser.util.ParserFeedback; 45 46 public class ParserHelper implements Serializable 47 { 48 49 public ParserHelper() 50 { 51 super(); 52 } 53 54 60 public static URLConnection openConnection( 61 URL url, 62 ParserFeedback feedback) 63 throws ParserException 64 { 65 URLConnection ret; 66 67 try 68 { 69 ret = url.openConnection(); 70 } 71 catch (IOException ioe) 72 { 73 String msg = 74 "HTMLParser.openConnection() : Error in opening a connection to " 75 + url.toExternalForm(); 76 ParserException ex = new ParserException(msg, ioe); 77 if (null != feedback) 78 feedback.error(msg, ex); 79 throw ex; 80 } 81 82 return (ret); 83 } 84 85 95 public static URLConnection openConnection( 96 String string, 97 ParserFeedback feedback) 98 throws ParserException 99 { 100 final String prefix = "file://localhost"; 101 String resource; 102 URL url; 103 StringBuffer buffer; 104 URLConnection ret; 105 106 try 107 { 108 url = new URL (LinkProcessor.fixSpaces(string)); 109 ret = ParserHelper.openConnection(url, feedback); 110 } 111 catch (MalformedURLException murle) 112 { try 114 { 115 File file = new File (string); 116 resource = file.getCanonicalPath(); 117 buffer = new StringBuffer (prefix.length() + resource.length()); 118 buffer.append(prefix); 119 buffer.append(resource); 120 url = new URL (LinkProcessor.fixSpaces(buffer.toString())); 121 ret = ParserHelper.openConnection(url, feedback); 122 if (null != feedback) 123 feedback.info(url.toExternalForm()); 124 } 125 catch (MalformedURLException murle2) 126 { 127 String msg = 128 "HTMLParser.openConnection() : Error in opening a connection to " 129 + string; 130 ParserException ex = new ParserException(msg, murle2); 131 if (null != feedback) 132 feedback.error(msg, ex); 133 throw ex; 134 } 135 catch (IOException ioe) 136 { 137 String msg = 138 "HTMLParser.openConnection() : Error in opening a connection to " 139 + string; 140 ParserException ex = new ParserException(msg, ioe); 141 if (null != feedback) 142 feedback.error(msg, ex); 143 throw ex; 144 } 145 } 146 147 return (ret); 148 } 149 150 158 public static String findCharset(String name, String _default) 159 { 160 String ret; 161 162 try 163 { 164 Class cls; 165 java.lang.reflect.Method method; 166 Object object; 167 168 cls = Class.forName("java.nio.charset.Charset"); 169 method = cls.getMethod("forName", new Class [] { String .class }); 170 object = method.invoke(null, new Object [] { name }); 171 method = cls.getMethod("name", new Class [] { 172 }); 173 object = method.invoke(object, new Object [] { 174 }); 175 ret = (String ) object; 176 } 177 catch (ClassNotFoundException cnfe) 178 { 179 ret = name; 181 } 182 catch (NoSuchMethodException nsme) 183 { 184 ret = name; 186 } 187 catch (IllegalAccessException ia) 188 { 189 ret = name; 191 } 192 catch (java.lang.reflect.InvocationTargetException ita) 193 { 194 ret = _default; 198 } 199 200 return (ret); 201 } 202 203 } 204 | Popular Tags |