KickJava   Java API By Example, From Geeks To Geeks.

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


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.ProcessingException;
21 import org.apache.cocoon.xml.AttributesImpl;
22 import org.apache.avalon.framework.parameters.Parameters;
23 import org.xml.sax.SAXException JavaDoc;
24 import org.xml.sax.Attributes JavaDoc;
25 import org.xml.sax.Locator JavaDoc;
26 import org.outerj.daisy.util.Constants;
27
28 import java.util.Map JavaDoc;
29 import java.util.regex.Pattern JavaDoc;
30 import java.util.regex.Matcher JavaDoc;
31 import java.io.IOException JavaDoc;
32
33 public class CrossRefParserTransformer extends AbstractTransformer {
34     private static final Pattern JavaDoc CROSS_REF_PATTERN = Pattern.compile("^([^:]+):(.+)$");
35     private StringBuffer JavaDoc crossRefBuffer;
36     private int nesting;
37     private int crossRefNesting;
38
39     public void setup(SourceResolver sourceResolver, Map JavaDoc map, String JavaDoc string, Parameters parameters) throws ProcessingException, SAXException JavaDoc, IOException JavaDoc {
40         this.crossRefBuffer = null;
41         this.nesting = 0;
42         this.crossRefNesting = -1;
43     }
44
45     public void startElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc attributes) throws SAXException JavaDoc {
46         nesting++;
47         if (uri.equals("") && localName.equals("span") && "crossreference".equals(attributes.getValue("class")) && crossRefNesting == -1) {
48             crossRefBuffer = new StringBuffer JavaDoc();
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 JavaDoc {
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 JavaDoc uri, String JavaDoc localName, String JavaDoc qName) throws SAXException JavaDoc {
64         if (nesting == crossRefNesting) {
65             String JavaDoc crossref = crossRefBuffer.toString().trim();
66             Matcher JavaDoc crossRefMatcher = CROSS_REF_PATTERN.matcher(crossref);
67
68             String JavaDoc crossRefType;
69             String JavaDoc crossRefTarget = null;
70             String JavaDoc crossRefBookTarget = null;
71
72             if (crossRefMatcher.matches()) {
73                 crossRefType = crossRefMatcher.group(1);
74                 crossRefTarget = crossRefMatcher.group(2);
75                 Matcher JavaDoc targetMatcher = Constants.DAISY_LINK_PATTERN.matcher(crossRefTarget);
76                 if (targetMatcher.matches()) {
77                     String JavaDoc targetDocId = targetMatcher.group(1);
78                     String JavaDoc 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                 // invalid crossref
91
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 JavaDoc locator) {
115         if (crossRefNesting == -1)
116             super.setDocumentLocator(locator);
117     }
118
119     public void startDocument() throws SAXException JavaDoc {
120         if (crossRefNesting == -1)
121             super.startDocument();
122     }
123
124     public void endDocument() throws SAXException JavaDoc {
125         if (crossRefNesting == -1)
126             super.endDocument();
127     }
128
129     public void startPrefixMapping(String JavaDoc string, String JavaDoc string1) throws SAXException JavaDoc {
130         if (crossRefNesting == -1)
131             super.startPrefixMapping(string, string1);
132     }
133
134     public void endPrefixMapping(String JavaDoc string) throws SAXException JavaDoc {
135         if (crossRefNesting == -1)
136             super.endPrefixMapping(string);
137     }
138
139     public void ignorableWhitespace(char[] chars, int i, int i1) throws SAXException JavaDoc {
140         if (crossRefNesting == -1)
141             super.ignorableWhitespace(chars, i, i1);
142     }
143
144     public void processingInstruction(String JavaDoc string, String JavaDoc string1) throws SAXException JavaDoc {
145         if (crossRefNesting == -1)
146             super.processingInstruction(string, string1);
147     }
148
149     public void skippedEntity(String JavaDoc string) throws SAXException JavaDoc {
150         if (crossRefNesting == -1)
151             super.skippedEntity(string);
152     }
153
154     public void startDTD(String JavaDoc string, String JavaDoc string1, String JavaDoc string2) throws SAXException JavaDoc {
155         if (crossRefNesting == -1)
156             super.startDTD(string, string1, string2);
157     }
158
159     public void endDTD() throws SAXException JavaDoc {
160         if (crossRefNesting == -1)
161             super.endDTD();
162     }
163
164     public void startEntity(String JavaDoc string) throws SAXException JavaDoc {
165         if (crossRefNesting == -1)
166             super.startEntity(string);
167     }
168
169     public void endEntity(String JavaDoc string) throws SAXException JavaDoc {
170         if (crossRefNesting == -1)
171             super.endEntity(string);
172     }
173
174     public void startCDATA() throws SAXException JavaDoc {
175         if (crossRefNesting == -1)
176             super.startCDATA();
177     }
178
179     public void endCDATA() throws SAXException JavaDoc {
180         if (crossRefNesting == -1)
181             super.endCDATA();
182     }
183
184     public void comment(char[] chars, int i, int i1) throws SAXException JavaDoc {
185         if (crossRefNesting == -1)
186             super.comment(chars, i, i1);
187     }
188 }
189
Popular Tags