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.ProcessingException; 21 import org.apache.cocoon.xml.AttributesImpl; 22 import org.apache.avalon.framework.parameters.Parameters; 23 import org.xml.sax.SAXException ; 24 import org.xml.sax.Attributes ; 25 import org.xml.sax.Locator ; 26 import org.outerj.daisy.util.Constants; 27 28 import java.util.Map ; 29 import java.util.regex.Pattern ; 30 import java.util.regex.Matcher ; 31 import java.io.IOException ; 32 33 public class CrossRefParserTransformer extends AbstractTransformer { 34 private static final Pattern CROSS_REF_PATTERN = Pattern.compile("^([^:]+):(.+)$"); 35 private StringBuffer crossRefBuffer; 36 private int nesting; 37 private int crossRefNesting; 38 39 public void setup(SourceResolver sourceResolver, Map map, String string, Parameters parameters) throws ProcessingException, SAXException , IOException { 40 this.crossRefBuffer = null; 41 this.nesting = 0; 42 this.crossRefNesting = -1; 43 } 44 45 public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { 46 nesting++; 47 if (uri.equals("") && localName.equals("span") && "crossreference".equals(attributes.getValue("class")) && crossRefNesting == -1) { 48 crossRefBuffer = new StringBuffer (); 49 crossRefNesting = nesting; 50 } else if (crossRefNesting == -1) { 51 super.startElement(uri, localName, qName, attributes); 52 } 53 } 54 55 public void characters(char[] chars, int start, int length) throws SAXException { 56 if (crossRefNesting != -1) { 57 crossRefBuffer.append(chars, start, length); 58 } else { 59 super.characters(chars, start, length); 60 } 61 } 62 63 public void endElement(String uri, String localName, String qName) throws SAXException { 64 if (nesting == crossRefNesting) { 65 String crossref = crossRefBuffer.toString().trim(); 66 Matcher crossRefMatcher = CROSS_REF_PATTERN.matcher(crossref); 67 68 String crossRefType; 69 String crossRefTarget = null; 70 String crossRefBookTarget = null; 71 72 if (crossRefMatcher.matches()) { 73 crossRefType = crossRefMatcher.group(1); 74 crossRefTarget = crossRefMatcher.group(2); 75 Matcher targetMatcher = Constants.DAISY_LINK_PATTERN.matcher(crossRefTarget); 76 if (targetMatcher.matches()) { 77 String targetDocId = targetMatcher.group(1); 78 String fragId = targetMatcher.group(8); 79 crossRefBookTarget = "#dsy" + targetDocId; 80 if (fragId != null) 81 crossRefBookTarget = crossRefBookTarget + "_" + fragId.substring(1); 82 } else if (crossRefTarget.startsWith("#")) { 83 crossRefBookTarget = crossRefTarget; 84 } else { 85 crossRefType = "invalid"; 86 crossRefTarget = null; 87 crossRefBookTarget = null; 88 } 89 } else { 90 crossRefType = "invalid"; 92 } 93 94 AttributesImpl crossRefAttrs = new AttributesImpl(); 95 crossRefAttrs.addCDATAAttribute("class", "crossreference"); 96 if (crossRefType != null) 97 crossRefAttrs.addCDATAAttribute("crossRefType", crossRefType); 98 if (crossRefTarget != null) 99 crossRefAttrs.addCDATAAttribute("crossRefTarget", crossRefTarget); 100 if (crossRefBookTarget != null) 101 crossRefAttrs.addCDATAAttribute("crossRefBookTarget", crossRefBookTarget); 102 super.startElement("", "span", "span", crossRefAttrs); 103 super.characters(crossref.toCharArray(), 0, crossref.length()); 104 super.endElement("", "span", "span"); 105 106 crossRefNesting = -1; 107 crossRefBuffer = null; 108 } else if (crossRefNesting == -1) { 109 super.endElement(uri, localName, qName); 110 } 111 nesting--; 112 } 113 114 public void setDocumentLocator(Locator locator) { 115 if (crossRefNesting == -1) 116 super.setDocumentLocator(locator); 117 } 118 119 public void startDocument() throws SAXException { 120 if (crossRefNesting == -1) 121 super.startDocument(); 122 } 123 124 public void endDocument() throws SAXException { 125 if (crossRefNesting == -1) 126 super.endDocument(); 127 } 128 129 public void startPrefixMapping(String string, String string1) throws SAXException { 130 if (crossRefNesting == -1) 131 super.startPrefixMapping(string, string1); 132 } 133 134 public void endPrefixMapping(String string) throws SAXException { 135 if (crossRefNesting == -1) 136 super.endPrefixMapping(string); 137 } 138 139 public void ignorableWhitespace(char[] chars, int i, int i1) throws SAXException { 140 if (crossRefNesting == -1) 141 super.ignorableWhitespace(chars, i, i1); 142 } 143 144 public void processingInstruction(String string, String string1) throws SAXException { 145 if (crossRefNesting == -1) 146 super.processingInstruction(string, string1); 147 } 148 149 public void skippedEntity(String string) throws SAXException { 150 if (crossRefNesting == -1) 151 super.skippedEntity(string); 152 } 153 154 public void startDTD(String string, String string1, String string2) throws SAXException { 155 if (crossRefNesting == -1) 156 super.startDTD(string, string1, string2); 157 } 158 159 public void endDTD() throws SAXException { 160 if (crossRefNesting == -1) 161 super.endDTD(); 162 } 163 164 public void startEntity(String string) throws SAXException { 165 if (crossRefNesting == -1) 166 super.startEntity(string); 167 } 168 169 public void endEntity(String string) throws SAXException { 170 if (crossRefNesting == -1) 171 super.endEntity(string); 172 } 173 174 public void startCDATA() throws SAXException { 175 if (crossRefNesting == -1) 176 super.startCDATA(); 177 } 178 179 public void endCDATA() throws SAXException { 180 if (crossRefNesting == -1) 181 super.endCDATA(); 182 } 183 184 public void comment(char[] chars, int i, int i1) throws SAXException { 185 if (crossRefNesting == -1) 186 super.comment(chars, i, i1); 187 } 188 } 189 | Popular Tags |