KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > htmlparser > parserHelper > ParserHelper


1 // $Header: /home/cvs/jakarta-jmeter/src/htmlparser/org/htmlparser/parserHelper/ParserHelper.java,v 1.2 2004/02/10 13:41:08 woolfel Exp $
2
/*
3  * ====================================================================
4  * Copyright 2002-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */

19
20 // The developers of JMeter and Apache are greatful to the developers
21
// of HTMLParser for giving Apache Software Foundation a non-exclusive
22
// license. The performance benefits of HTMLParser are clear and the
23
// users of JMeter will benefit from the hard work the HTMLParser
24
// team. For detailed information about HTMLParser, the project is
25
// hosted on sourceforge at http://htmlparser.sourceforge.net/.
26
//
27
// HTMLParser was originally created by Somik Raha in 2000. Since then
28
// a healthy community of users has formed and helped refine the
29
// design so that it is able to tackle the difficult task of parsing
30
// dirty HTML. Derrick Oswald is the current lead developer and was kind
31
// enough to assist JMeter.
32

33 package org.htmlparser.parserHelper;
34
35 import java.io.File JavaDoc;
36 import java.io.IOException JavaDoc;
37 import java.io.Serializable JavaDoc;
38 import java.net.MalformedURLException JavaDoc;
39 import java.net.URL JavaDoc;
40 import java.net.URLConnection JavaDoc;
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 JavaDoc
47 {
48
49     public ParserHelper()
50     {
51         super();
52     }
53
54     /**
55      * Opens a connection using the given url.
56      * @param url The url to open.
57      * @param feedback The ibject to use for messages or <code>null</code>.
58      * @exception ParserException if an i/o exception occurs accessing the url.
59      */

60     public static URLConnection JavaDoc openConnection(
61         URL JavaDoc url,
62         ParserFeedback feedback)
63         throws ParserException
64     {
65         URLConnection JavaDoc ret;
66
67         try
68         {
69             ret = url.openConnection();
70         }
71         catch (IOException JavaDoc ioe)
72         {
73             String JavaDoc 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     /**
86      * Opens a connection based on a given string.
87      * The string is either a file, in which case <code>file://localhost</code>
88      * is prepended to a canonical path derived from the string, or a url that
89      * begins with one of the known protocol strings, i.e. <code>http://</code>.
90      * Embedded spaces are silently converted to %20 sequences.
91      * @param string The name of a file or a url.
92      * @param feedback The object to use for messages or <code>null</code> for no feedback.
93      * @exception ParserException if the string is not a valid url or file.
94      */

95     public static URLConnection JavaDoc openConnection(
96         String JavaDoc string,
97         ParserFeedback feedback)
98         throws ParserException
99     {
100         final String JavaDoc prefix = "file://localhost";
101         String JavaDoc resource;
102         URL JavaDoc url;
103         StringBuffer JavaDoc buffer;
104         URLConnection JavaDoc ret;
105
106         try
107         {
108             url = new URL JavaDoc(LinkProcessor.fixSpaces(string));
109             ret = ParserHelper.openConnection(url, feedback);
110         }
111         catch (MalformedURLException JavaDoc murle)
112         { // try it as a file
113
try
114             {
115                 File JavaDoc file = new File JavaDoc(string);
116                 resource = file.getCanonicalPath();
117                 buffer = new StringBuffer JavaDoc(prefix.length() + resource.length());
118                 buffer.append(prefix);
119                 buffer.append(resource);
120                 url = new URL JavaDoc(LinkProcessor.fixSpaces(buffer.toString()));
121                 ret = ParserHelper.openConnection(url, feedback);
122                 if (null != feedback)
123                     feedback.info(url.toExternalForm());
124             }
125             catch (MalformedURLException JavaDoc murle2)
126             {
127                 String JavaDoc 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 JavaDoc ioe)
136             {
137                 String JavaDoc 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     /**
151      * Lookup a character set name.
152      * <em>Vacuous for JVM's without <code>java.nio.charset</code>.</em>
153      * This uses reflection so the code will still run under prior JDK's but
154      * in that case the default is always returned.
155      * @param name The name to look up. One of the aliases for a character set.
156      * @param _default The name to return if the lookup fails.
157      */

158     public static String JavaDoc findCharset(String JavaDoc name, String JavaDoc _default)
159     {
160         String JavaDoc ret;
161
162         try
163         {
164             Class JavaDoc cls;
165             java.lang.reflect.Method JavaDoc method;
166             Object JavaDoc object;
167
168             cls = Class.forName("java.nio.charset.Charset");
169             method = cls.getMethod("forName", new Class JavaDoc[] { String JavaDoc.class });
170             object = method.invoke(null, new Object JavaDoc[] { name });
171             method = cls.getMethod("name", new Class JavaDoc[] {
172             });
173             object = method.invoke(object, new Object JavaDoc[] {
174             });
175             ret = (String JavaDoc) object;
176         }
177         catch (ClassNotFoundException JavaDoc cnfe)
178         {
179             // for reflection exceptions, assume the name is correct
180
ret = name;
181         }
182         catch (NoSuchMethodException JavaDoc nsme)
183         {
184             // for reflection exceptions, assume the name is correct
185
ret = name;
186         }
187         catch (IllegalAccessException JavaDoc ia)
188         {
189             // for reflection exceptions, assume the name is correct
190
ret = name;
191         }
192         catch (java.lang.reflect.InvocationTargetException JavaDoc ita)
193         {
194             // java.nio.charset.IllegalCharsetNameException
195
// and java.nio.charset.UnsupportedCharsetException
196
// return the default
197
ret = _default;
198         }
199
200         return (ret);
201     }
202
203 }
204
Popular Tags