1 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 ; 23 import org.xml.sax.SAXException ; 24 import org.xml.sax.helpers.AttributesImpl ; 25 import org.outerj.daisy.util.Constants; 26 27 import java.util.regex.Matcher ; 28 import java.util.Map ; 29 import java.io.IOException ; 30 31 public class BookLinkTransformer extends AbstractTransformer { 32 private long documentId; 33 34 public void setup(SourceResolver sourceResolver, Map map, String string, Parameters parameters) throws ProcessingException, SAXException , IOException { 35 this.documentId = -1; 36 } 37 38 public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { 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 atts = translateLink(atts, "crossRefBookTarget"); 52 } 53 } 54 super.startElement(namespaceURI, localName, qName, atts); 55 } 56 57 public void endElement(String namespaceURI, String localName, String qName) throws SAXException { 58 if (namespaceURI.equals(Constants.DAISY_NAMESPACE) && localName.equals("document")) 59 documentId = -1; 60 super.endElement(namespaceURI, localName, qName); 61 } 62 63 Attributes translateLink(Attributes attributes, String linkAttrName) { 64 String href = attributes.getValue(linkAttrName); 65 if (href != null) { 66 Matcher matcher = Constants.DAISY_LINK_PATTERN.matcher(href); 67 if (matcher.matches()) { 68 String newHref; 71 String documentId = matcher.group(1); 72 73 String 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 newAttributes = new AttributesImpl (attributes); 83 newAttributes.setAttribute(newAttributes.getIndex("", linkAttrName), "", linkAttrName, linkAttrName, "CDATA", newHref); 84 attributes = newAttributes; 85 } else if (href.startsWith("#") && !href.startsWith("#dsy")) { 86 String newHref = "#dsy" + documentId + "_" + href.substring(1); 87 AttributesImpl newAttributes = new AttributesImpl (attributes); 88 newAttributes.setAttribute(newAttributes.getIndex("", linkAttrName), "", linkAttrName, linkAttrName, "CDATA", newHref); 89 attributes = newAttributes; 90 } 91 } 92 return attributes; 93 } 94 } 95 | Popular Tags |