KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > books > publisher > impl > cocooncomponents > BookLinkTransformer


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.books.publisher.impl.cocooncomponents;
17
18 import org.apache.cocoon.transformation.AbstractTransformer;
19 import org.apache.cocoon.environment.SourceResolver;
20 import org.apache.cocoon.ProcessingException;
21 import org.apache.avalon.framework.parameters.Parameters;
22 import org.xml.sax.Attributes JavaDoc;
23 import org.xml.sax.SAXException JavaDoc;
24 import org.xml.sax.helpers.AttributesImpl JavaDoc;
25 import org.outerj.daisy.util.Constants;
26
27 import java.util.regex.Matcher JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.io.IOException JavaDoc;
30
31 public class BookLinkTransformer extends AbstractTransformer {
32     private long documentId;
33
34     public void setup(SourceResolver sourceResolver, Map JavaDoc map, String JavaDoc string, Parameters parameters) throws ProcessingException, SAXException JavaDoc, IOException JavaDoc {
35         this.documentId = -1;
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(Constants.DAISY_NAMESPACE)) {
40             if (localName.equals("document")) {
41                 documentId = Long.parseLong(atts.getValue("id"));
42             } else if (documentId != -1 && localName.equals("link")) {
43                 atts = translateLink(atts, "target");
44             }
45         }
46         if (documentId != -1 && namespaceURI.equals("")) {
47             if (localName.equals("a")) {
48                 atts = translateLink(atts, "href");
49             } else if (localName.equals("span") && "crossreference".equals(atts.getValue("class"))) {
50                 // This assumes cross references have been handled by the CrossRefParserTransformer.
51
atts = translateLink(atts, "crossRefBookTarget");
52             }
53         }
54         super.startElement(namespaceURI, localName, qName, atts);
55     }
56
57     public void endElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName) throws SAXException JavaDoc {
58         if (namespaceURI.equals(Constants.DAISY_NAMESPACE) && localName.equals("document"))
59             documentId = -1;
60         super.endElement(namespaceURI, localName, qName);
61     }
62
63     Attributes JavaDoc translateLink(Attributes JavaDoc attributes, String JavaDoc linkAttrName) {
64         String JavaDoc href = attributes.getValue(linkAttrName);
65         if (href != null) {
66             Matcher JavaDoc matcher = Constants.DAISY_LINK_PATTERN.matcher(href);
67             if (matcher.matches()) {
68                 // Note: branch, language and version information of the links is completely ignored.
69
// It assumed that a book will only contain one variant/version of a document.
70
String JavaDoc newHref;
71                 String JavaDoc documentId = matcher.group(1);
72
73                 String JavaDoc fragmentIdentifier = matcher.group(8);
74                 if (fragmentIdentifier != null && !fragmentIdentifier.startsWith("#dsy")) {
75                     newHref = "#dsy" + documentId + "_" + fragmentIdentifier.substring(1);
76                 } else if (fragmentIdentifier != null) {
77                     newHref = fragmentIdentifier;
78                 } else {
79                     newHref = "#dsy" + documentId;
80                 }
81
82                 AttributesImpl JavaDoc newAttributes = new AttributesImpl JavaDoc(attributes);
83                 newAttributes.setAttribute(newAttributes.getIndex("", linkAttrName), "", linkAttrName, linkAttrName, "CDATA", newHref);
84                 attributes = newAttributes;
85             } else if (href.startsWith("#") && !href.startsWith("#dsy")) {
86                 String JavaDoc newHref = "#dsy" + documentId + "_" + href.substring(1);
87                 AttributesImpl JavaDoc newAttributes = new AttributesImpl JavaDoc(attributes);
88                 newAttributes.setAttribute(newAttributes.getIndex("", linkAttrName), "", linkAttrName, linkAttrName, "CDATA", newHref);
89                 attributes = newAttributes;
90             }
91         }
92         return attributes;
93     }
94 }
95
Popular Tags