KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.outerj.daisy.repository.RepositoryException;
24
25 public class BookLinkExtractor extends AbstractLinkExtractor {
26     public ContentHandler JavaDoc getContentHandler(Part part, LinkCollector linkCollector, long defaultBranchId, long defaultLanguageId) {
27         return new BookLinkExtractionHandler(linkCollector, defaultBranchId, defaultLanguageId);
28     }
29
30     public class BookLinkExtractionHandler extends DefaultHandler JavaDoc {
31         private final LinkCollector linkCollector;
32         private final long defaultBranchId;
33         private final long defaultLanguageId;
34         private static final String JavaDoc BOOK_NS = "http://outerx.org/daisy/1.0#bookdef";
35
36         public BookLinkExtractionHandler(LinkCollector linkCollector, long defaultBranchId, long defaultLanguageId) {
37             this.linkCollector = linkCollector;
38             this.defaultBranchId = defaultBranchId;
39             this.defaultLanguageId = defaultLanguageId;
40         }
41
42         public void startElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc attributes) throws SAXException JavaDoc {
43             if ((localName.equals("section") || localName.equals("importNavigationTree")) && uri.equals(BOOK_NS)) {
44                 String JavaDoc idString = attributes.getValue(localName.equals("section") ? "documentId" : "id");
45                 long id;
46                 if (idString != null) {
47                     try {
48                         id = Long.parseLong(idString);
49                         String JavaDoc branch = attributes.getValue("branch");
50                         long branchId = defaultBranchId;
51                         if (branch != null)
52                             branchId = linkCollector.getBranchId(branch);
53                         String JavaDoc language = attributes.getValue("language");
54                         long languageId = defaultLanguageId;
55                         if (language != null)
56                             languageId = linkCollector.getLanguageId(language);
57                         linkCollector.addLink(LinkType.OTHER, id, branchId, languageId, -1);
58                     } catch (NumberFormatException JavaDoc e) {
59                         // ignore
60
} catch (RepositoryException e) {
61                         // invalid branch or language specified, ignore
62
}
63                 }
64             }
65         }
66     }
67 }
Popular Tags