KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > lucene > parser > SwingHTMLParser


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 /* $Id: SwingHTMLParser.java 42598 2004-03-01 16:18:28Z gregor $ */
19
20 package org.apache.lenya.lucene.parser;
21
22 import java.io.File JavaDoc;
23 import java.io.FileReader JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.InputStreamReader JavaDoc;
26 import java.io.Reader JavaDoc;
27 import java.net.MalformedURLException JavaDoc;
28 import java.net.URI JavaDoc;
29 import java.net.URLConnection JavaDoc;
30
31 import javax.swing.text.html.parser.ParserDelegator JavaDoc;
32
33 public class SwingHTMLParser extends AbstractHTMLParser {
34     /** Creates a new instance of SwingHTMLParser */
35     public SwingHTMLParser() {
36     }
37
38     /**
39      * Parses a URI.
40      */

41     public void parse(URI JavaDoc uri) throws ParseException {
42         try {
43             ParserDelegator JavaDoc delagator = new ParserDelegator JavaDoc();
44             handler = new SwingHTMLHandler();
45
46             Reader JavaDoc reader = new PreParser().parse(getReader(uri));
47             delagator.parse(reader, handler, true);
48         } catch (IOException JavaDoc e) {
49             throw new ParseException(e);
50         }
51     }
52
53     private SwingHTMLHandler handler;
54
55     protected SwingHTMLHandler getHandler() {
56         return handler;
57     }
58
59     /**
60      * Get title
61      *
62      * @return DOCUMENT ME!
63      */

64     public String JavaDoc getTitle() {
65         return getHandler().getTitle();
66     }
67
68     /**
69      * Get keywords
70      *
71      * @return DOCUMENT ME!
72      */

73     public String JavaDoc getKeywords() {
74         return getHandler().getKeywords();
75     }
76
77     private Reader JavaDoc reader;
78
79     /**
80      * DOCUMENT ME!
81      *
82      * @return DOCUMENT ME!
83      */

84     public Reader JavaDoc getReader() {
85         return getHandler().getReader();
86     }
87
88     protected Reader JavaDoc getReader(URI JavaDoc uri) throws IOException JavaDoc, MalformedURLException JavaDoc {
89         if (uri.toString().startsWith("http:")) {
90             // uri is url
91
URLConnection JavaDoc connection = uri.toURL().openConnection();
92
93             return new InputStreamReader JavaDoc(connection.getInputStream());
94         } else {
95             // uri is file
96
return new FileReader JavaDoc(new File JavaDoc(uri));
97         }
98     }
99 }
100
Popular Tags