KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > linkextraction > DaisyHtmlLinkExtractor


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
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 package org.outerj.daisy.linkextraction;
17
18 import org.xml.sax.helpers.DefaultHandler JavaDoc;
19 import org.xml.sax.Attributes JavaDoc;
20 import org.xml.sax.SAXException JavaDoc;
21 import org.xml.sax.ContentHandler JavaDoc;
22 import org.outerj.daisy.repository.Part;
23
24 public class DaisyHtmlLinkExtractor extends AbstractLinkExtractor {
25     public ContentHandler JavaDoc getContentHandler(Part part, LinkCollector linkCollector, long defaultBranchId, long defaultLanguageId) {
26         return new DaisyHtmlLinkExtractionHandler(linkCollector);
27     }
28
29     public class DaisyHtmlLinkExtractionHandler extends DefaultHandler JavaDoc {
30         private int includeLevel = -1;
31         private StringBuffer JavaDoc includeBuffer;
32         private LinkCollector linkCollector;
33
34         public DaisyHtmlLinkExtractionHandler(LinkCollector linkCollector) {
35             this.linkCollector = linkCollector;
36         }
37
38         public void startElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc atts) throws SAXException JavaDoc {
39             if (namespaceURI.equals("")) {
40                 if (includeLevel > -1) {
41                     includeLevel++;
42                 } else if (localName.equals("a")) {
43                     String JavaDoc href = atts.getValue("href");
44                     linkCollector.addLink(LinkType.INLINE, href);
45                 } else if (localName.equals("img")) {
46                     String JavaDoc src = atts.getValue("src");
47                     linkCollector.addLink(LinkType.IMAGE, src);
48                 } else if (localName.equals("pre")) {
49                     String JavaDoc clazz = atts.getValue("class");
50                     if (clazz != null && clazz.equals("include")) {
51                         includeLevel = 0;
52                         includeBuffer = new StringBuffer JavaDoc();
53                     }
54                 }
55             }
56         }
57
58         public void endElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName) throws SAXException JavaDoc {
59             if (includeLevel > -1) {
60                 includeLevel--;
61                 if (includeLevel == -1) {
62                     linkCollector.addLink(LinkType.INCLUDE, includeBuffer.toString().trim());
63                 }
64             }
65         }
66
67         public void characters(char ch[], int start, int length) throws SAXException JavaDoc {
68             if (includeLevel > -1) {
69                 includeBuffer.append(ch, start, length);
70             }
71         }
72     }
73 }
74
Popular Tags