KickJava   Java API By Example, From Geeks To Geeks.

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


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.avalon.framework.parameters.Parameters;
22 import org.xml.sax.SAXException JavaDoc;
23 import org.xml.sax.Attributes JavaDoc;
24 import org.xml.sax.helpers.AttributesImpl JavaDoc;
25 import org.outerj.daisy.util.Constants;
26
27 import java.util.Map JavaDoc;
28 import java.io.IOException JavaDoc;
29
30 /**
31  * This transform prefixes all element IDs with "dsy" + documentId + "_", in order
32  * to have unique names when combining several documents on one page, and to avoid
33  * conflicts with any other IDs that might appear on a HTML page (outside of the
34  * document content).
35  *
36  * <p>This goes together with the {@link DaisyLinkTransformer} which adjust
37  * fragment identifiers in daisy links.
38  */

39 public class IDAbsolutizerTransformer extends AbstractTransformer {
40     private boolean inPart;
41     private long documentId;
42
43     public void setup(SourceResolver sourceResolver, Map JavaDoc map, String JavaDoc s, Parameters parameters) throws ProcessingException, SAXException JavaDoc, IOException JavaDoc {
44         this.inPart = false;
45     }
46
47     public void startElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc attributes) throws SAXException JavaDoc {
48         if (namespaceURI.equals(Constants.DAISY_NAMESPACE)) {
49             if (localName.equals("part")) {
50                 inPart = true;
51             } else if (localName.equals("document")) {
52                 documentId = Long.parseLong(attributes.getValue("id"));
53             }
54         }
55
56         int index;
57         if (inPart && namespaceURI.equals("") && (index = attributes.getIndex("id")) != -1) {
58             AttributesImpl JavaDoc newAttrs = new AttributesImpl JavaDoc(attributes);
59             String JavaDoc id = newAttrs.getValue(index);
60             newAttrs.setValue(index, "dsy" + documentId + "_" + id);
61             attributes = newAttrs;
62         }
63
64         super.startElement(namespaceURI, localName, qName, attributes);
65     }
66
67     public void endElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName) throws SAXException JavaDoc {
68         if (namespaceURI.equals(Constants.DAISY_NAMESPACE) && localName.equals("part")) {
69             inPart = false;
70         }
71         super.endElement(namespaceURI, localName, qName);
72     }
73 }
74
Popular Tags