KickJava   Java API By Example, From Geeks To Geeks.

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


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.environment.Request;
21 import org.apache.cocoon.environment.ObjectModelHelper;
22 import org.apache.cocoon.ProcessingException;
23 import org.apache.cocoon.xml.IncludeXMLConsumer;
24 import org.apache.cocoon.xml.EmbeddedXMLPipe;
25 import org.apache.cocoon.xml.AttributesImpl;
26 import org.apache.avalon.framework.parameters.Parameters;
27 import org.apache.avalon.excalibur.pool.Recyclable;
28 import org.xml.sax.SAXException JavaDoc;
29 import org.xml.sax.Attributes JavaDoc;
30
31 import java.util.Map JavaDoc;
32 import java.io.IOException JavaDoc;
33
34 /**
35  * Transformer that inlines included documents on the location of inclusion.
36  */

37 public class IncludePreparedDocumentsTransformer extends AbstractTransformer implements Recyclable {
38     private static final String JavaDoc PUBLISHER_NAMESPACE = "http://outerx.org/daisy/1.0#publisher";
39     private Request request;
40     private PreparedDocuments preparedDocuments;
41
42     public void setup(SourceResolver sourceResolver, Map JavaDoc objectMap, String JavaDoc s, Parameters parameters) throws ProcessingException, SAXException JavaDoc, IOException JavaDoc {
43         request = ObjectModelHelper.getRequest(objectMap);
44     }
45
46     public void recycle() {
47         super.recycle();
48         this.request = null;
49         this.preparedDocuments = null;
50     }
51
52     public void startElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc atts) throws SAXException JavaDoc {
53         if (namespaceURI.equals("") && localName.equals("insertStyledDocument")) {
54             String JavaDoc styledResultsId = atts.getValue("styledResultsId");
55             if (styledResultsId == null)
56                 throw new SAXException JavaDoc("Missing styledResultsId on insertStyledDocument element");
57             preparedDocuments = (PreparedDocuments)request.getAttribute(styledResultsId);
58             if (preparedDocuments == null)
59                 throw new SAXException JavaDoc("PreparedDocuments not found in request attribute \"" + styledResultsId + "\".");
60             PreparedDocuments.PreparedDocument preparedDocument = preparedDocuments.getPreparedDocument(1);
61             if (preparedDocument == null)
62                 throw new SAXException JavaDoc("Missing root (1) preparedDocument.");
63             preparedDocument.getSaxBuffer().toSAX(new IncludeXMLConsumer(this));
64         } else if (namespaceURI.equals(PUBLISHER_NAMESPACE) && localName.equals("daisyPreparedInclude")) {
65             int id = Integer.parseInt(atts.getValue("id"));
66             PreparedDocuments.PreparedDocument preparedDocument = preparedDocuments.getPreparedDocument(id);
67             if (preparedDocument == null) {
68                 AttributesImpl errorAttrs = new AttributesImpl();
69                 errorAttrs.addCDATAAttribute("class", "daisy-error");
70                 this.startElement("", "p", "p", errorAttrs);
71                 String JavaDoc message = "Unexpected error in IncludePreparedDocumentsTransformer: missing preparedDocument: " + id;
72                 this.characters(message.toCharArray(), 0, message.length());
73                 this.endElement("", "p", "p");
74             } else {
75                 preparedDocument.getSaxBuffer().toSAX(new EmbeddedXMLPipe(this));
76             }
77         } else {
78             super.startElement(namespaceURI, localName, qName, atts);
79         }
80     }
81
82     public void endElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName) throws SAXException JavaDoc {
83         if (namespaceURI.equals("") && localName.equals("insertStyledDocument")) {
84             preparedDocuments = null;
85             // ignore element
86
} else if (namespaceURI.equals(PUBLISHER_NAMESPACE) && localName.equals("daisyPreparedInclude")) {
87             // ignore element
88
} else {
89             super.endElement(namespaceURI, localName, qName);
90         }
91     }
92 }
93
Popular Tags