KickJava   Java API By Example, From Geeks To Geeks.

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


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 "daisy:" links specified in the values of Java 5 style XML properties.
26  * (e.g. used for the book meta data).
27  */

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