KickJava   Java API By Example, From Geeks To Geeks.

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


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.ContentHandler JavaDoc;
19 import org.xml.sax.Attributes JavaDoc;
20 import org.xml.sax.SAXException JavaDoc;
21 import org.xml.sax.helpers.DefaultHandler JavaDoc;
22 import org.outerj.daisy.repository.Part;
23
24 /**
25  * Link extractor that extracts links from the values of properties specified in the book publication specfications.
26  */

27 public class BookPublicationsLinkExtractor extends AbstractLinkExtractor {
28     public ContentHandler JavaDoc getContentHandler(Part part, LinkCollector linkCollector, long defaultBranchId, long defaultLanguageId) {
29         return new BookPublicationsLinkExtractionHandler(linkCollector);
30     }
31
32     public class BookPublicationsLinkExtractionHandler extends DefaultHandler JavaDoc {
33         private final LinkCollector linkCollector;
34         private StringBuffer JavaDoc buffer;
35         private int nestingLevel = 0;
36
37         public BookPublicationsLinkExtractionHandler(LinkCollector linkCollector) {
38             this.linkCollector = linkCollector;
39         }
40
41         public void startElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc attributes) throws SAXException JavaDoc {
42             nestingLevel++;
43             if (nestingLevel == 4 && localName.equals("entry") && uri.equals("http://outerx.org/daisy/1.0#bookpubspecs")) {
44                 buffer = new StringBuffer JavaDoc();
45             }
46         }
47
48         public void characters(char ch[], int start, int length) throws SAXException JavaDoc {
49             if (buffer != null)
50                 buffer.append(ch, start, length);
51         }
52
53         public void endElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName) throws SAXException JavaDoc {
54             if (nestingLevel == 4 && buffer != null) {
55                 if (buffer.length() > 0)
56                     linkCollector.addLink(LinkType.OTHER, buffer.toString().trim());
57                 buffer = null;
58             }
59             nestingLevel--;
60         }
61     }
62 }
Popular Tags