1 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 ; 26 import org.xml.sax.Attributes ; 27 import org.xml.sax.helpers.AttributesImpl ; 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 ; 33 import java.util.regex.Matcher ; 34 import java.io.IOException ; 35 36 42 public class FopImageSrcTransformer extends AbstractTransformer { 43 private String basePath; 44 private String jSessionId; 45 private long documentBranchId; 46 private long documentLanguageId; 47 private SiteConf siteConf; 48 49 public void setup(SourceResolver sourceResolver, Map objectModel, String s, Parameters parameters) throws ProcessingException, SAXException , IOException { 50 Request request = ObjectModelHelper.getRequest(objectModel); 51 String server = RequestUtil.getServer(request); 52 String 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 namespaceURI, String localName, String qName, Attributes attributes) throws SAXException { 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 src = attributes.getValue("src"); 68 if (src != null) { 69 Matcher matcher = Constants.DAISY_LINK_PATTERN.matcher(src); 70 if (matcher.matches()) { 71 String documentId = matcher.group(1); 72 String versionSpec = matcher.group(7); 73 if (versionSpec == null) 74 versionSpec = "default"; 75 String 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 newAttrs = new AttributesImpl (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 |