KickJava   Java API By Example, From Geeks To Geeks.

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


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.cocoon.components.flow.FlowHelper;
22 import org.apache.cocoon.xml.AttributesImpl;
23 import org.apache.avalon.framework.parameters.Parameters;
24 import org.apache.commons.jxpath.JXPathContext;
25 import org.apache.excalibur.source.Source;
26 import org.xml.sax.SAXException JavaDoc;
27 import org.xml.sax.Attributes JavaDoc;
28 import org.outerj.daisy.books.store.BookInstance;
29 import org.outerj.daisy.books.publisher.impl.BookInstanceLayout;
30
31 import java.util.Map JavaDoc;
32 import java.io.IOException JavaDoc;
33
34 public class PdfImageLinkTransformer extends AbstractTransformer {
35     private BookInstance bookInstance;
36     private String JavaDoc publicationTypesUri;
37     private String JavaDoc publicationOutputName;
38     private static final String JavaDoc XSL_FO_NS = "http://www.w3.org/1999/XSL/Format";
39
40     public void setup(SourceResolver sourceResolver, Map JavaDoc objectModel, String JavaDoc string, Parameters parameters) throws ProcessingException, SAXException JavaDoc, IOException JavaDoc {
41         Object JavaDoc flowContext = FlowHelper.getContextObject(objectModel);
42         JXPathContext jxpc = JXPathContext.newContext(flowContext);
43         bookInstance = (BookInstance)jxpc.getValue("/bookInstance");
44         if (bookInstance == null)
45             throw new ProcessingException("Missing 'bookInstance' in flow context.");
46
47         publicationOutputName = (String JavaDoc)jxpc.getValue("/publicationOutputName");
48
49         Source source = null;
50         try {
51             // Since this transformer should only be used in publication type sitemaps, resolving just "" should give
52
// the location of the publication type directory.
53
source = sourceResolver.resolveURI("");
54             publicationTypesUri = source.getURI();
55         } finally {
56             sourceResolver.release(source);
57         }
58     }
59
60     public void startElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc attributes) throws SAXException JavaDoc {
61         if (namespaceURI.equals(XSL_FO_NS) && localName.equals("external-graphic")) {
62             attributes = handleLinkAttribute(attributes, "src");
63         }
64         super.startElement(namespaceURI, localName, qName, attributes);
65     }
66
67     private Attributes JavaDoc handleLinkAttribute(Attributes JavaDoc attributes, String JavaDoc linkAttrName) {
68         String JavaDoc linkAttr = attributes.getValue(linkAttrName);
69         if (linkAttr != null) {
70             String JavaDoc uri = linkAttr;
71             if (linkAttr.startsWith("bookinstance:")) {
72                 uri = bookInstance.getResourceURI(linkAttr.substring("bookinstance:".length())).toString();
73             } else if (linkAttr.startsWith("publication:")) {
74                 uri = bookInstance.getResourceURI(BookInstanceLayout.getPublicationOutputPath(publicationOutputName) + linkAttr.substring("publication:".length())).toString();
75             } else if (linkAttr.startsWith("publicationtype:")) {
76                 uri = publicationTypesUri + "/" + linkAttr.substring("publicationtype:".length());
77             }
78             AttributesImpl newAttrs = new AttributesImpl(attributes);
79             newAttrs.setValue(attributes.getIndex(linkAttrName), "url(" + uri + ")");
80             attributes = newAttrs;
81         }
82         return attributes;
83     }
84 }
85
Popular Tags