KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > frontend > FopImageSrcTransformer


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.frontend;
17
18 import org.apache.cocoon.transformation.AbstractTransformer;
19 import org.apache.cocoon.environment.SourceResolver;
20 import org.apache.cocoon.environment.Request;
21 import org.apache.cocoon.environment.ObjectModelHelper;
22 import org.apache.cocoon.environment.Session;
23 import org.apache.cocoon.ProcessingException;
24 import org.apache.avalon.framework.parameters.Parameters;
25 import org.xml.sax.SAXException JavaDoc;
26 import org.xml.sax.Attributes JavaDoc;
27 import org.xml.sax.helpers.AttributesImpl JavaDoc;
28 import org.outerj.daisy.frontend.components.siteconf.SiteConf;
29 import org.outerj.daisy.frontend.util.DaisyLinkUtil;
30 import org.outerj.daisy.util.Constants;
31
32 import java.util.Map JavaDoc;
33 import java.util.regex.Matcher JavaDoc;
34 import java.io.IOException JavaDoc;
35
36 /**
37  * Translate img/@src attributes containing "daisy:" references to URLs from which
38  * fop can retrieve the image. The URL also contains the jsessionid so that no
39  * unnecessary sessions are created and so that the proper authenticated user is used.
40  *
41  */

42 public class FopImageSrcTransformer extends AbstractTransformer {
43     private String JavaDoc basePath;
44     private String JavaDoc jSessionId;
45     private long documentBranchId;
46     private long documentLanguageId;
47     private SiteConf siteConf;
48
49     public void setup(SourceResolver sourceResolver, Map JavaDoc objectModel, String JavaDoc s, Parameters parameters) throws ProcessingException, SAXException JavaDoc, IOException JavaDoc {
50         Request request = ObjectModelHelper.getRequest(objectModel);
51         String JavaDoc server = RequestUtil.getServer(request);
52         String JavaDoc mountPoint = WikiHelper.getMountPoint(request);
53         siteConf = WikiHelper.getSiteConf(request);
54         Session session = request.getSession(false);
55         if (session != null) {
56             jSessionId = request.getSession(true).getId();
57         }
58
59         basePath = server + mountPoint + "/" + siteConf.getName();
60     }
61
62     public void startElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc attributes) throws SAXException JavaDoc {
63         if (namespaceURI.equals(Constants.DAISY_NAMESPACE) && localName.equals("document")) {
64             documentBranchId = Long.parseLong(attributes.getValue("branchId"));
65             documentLanguageId = Long.parseLong(attributes.getValue("languageId"));
66         } else if (namespaceURI.equals("") && localName.equals("img")) {
67             String JavaDoc src = attributes.getValue("src");
68             if (src != null) {
69                 Matcher JavaDoc matcher = Constants.DAISY_LINK_PATTERN.matcher(src);
70                 if (matcher.matches()) {
71                     String JavaDoc documentId = matcher.group(1);
72                     String JavaDoc versionSpec = matcher.group(7);
73                     if (versionSpec == null)
74                         versionSpec = "default";
75                     String JavaDoc newSrc = basePath + "/" + documentId + "/version/" + versionSpec + "/part/ImageData/data";
76                     if (jSessionId != null)
77                         newSrc += ";jsessionid=" + jSessionId;
78                     newSrc = newSrc + DaisyLinkUtil.getBranchLangQueryString(matcher, siteConf, documentBranchId, documentLanguageId);
79                     AttributesImpl JavaDoc newAttrs = new AttributesImpl JavaDoc(attributes);
80                     newAttrs.setAttribute(attributes.getIndex("src"), "", "src", "src", "CDATA", newSrc);
81                     attributes = newAttrs;
82                 }
83             }
84         }
85         super.startElement(namespaceURI, localName, qName, attributes);
86     }
87 }
88
Popular Tags