KickJava   Java API By Example, From Geeks To Geeks.

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


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.ObjectModelHelper;
21 import org.apache.cocoon.environment.Request;
22 import org.apache.cocoon.ProcessingException;
23 import org.apache.cocoon.xml.IncludeXMLConsumer;
24 import org.apache.avalon.framework.parameters.Parameters;
25 import org.apache.avalon.framework.service.Serviceable;
26 import org.apache.avalon.framework.service.ServiceManager;
27 import org.apache.avalon.framework.service.ServiceException;
28 import org.apache.avalon.framework.context.Contextualizable;
29 import org.apache.avalon.framework.context.Context;
30 import org.apache.avalon.framework.context.ContextException;
31 import org.apache.avalon.framework.activity.Disposable;
32 import org.apache.xmlbeans.XmlObject;
33 import org.apache.xmlbeans.XmlSaxHandler;
34 import org.apache.xmlbeans.XmlException;
35 import org.xml.sax.SAXException JavaDoc;
36 import org.xml.sax.Attributes JavaDoc;
37 import org.xml.sax.Locator JavaDoc;
38 import org.xml.sax.ContentHandler JavaDoc;
39 import org.outerj.daisy.frontend.components.siteconf.SiteConf;
40 import org.outerj.daisy.repository.Repository;
41 import org.outerj.daisy.repository.RepositoryException;
42 import org.outerj.daisy.publisher.Publisher;
43 import org.outerx.daisy.x10Publisher.PublisherRequestDocument;
44
45 import java.util.Map JavaDoc;
46 import java.io.IOException JavaDoc;
47
48 /**
49  * A transformer which intercepts publisher requests (p:publisherRequest elements),
50  * sends them to the publisher component, and inserts the publishers' response into
51  * the SAX stream (in place of the request).
52  */

53 public class PublisherTransformer extends AbstractTransformer implements Serviceable, Contextualizable, Disposable {
54     private static final String JavaDoc NAMESPACE = "http://outerx.org/daisy/1.0#publisher";
55     private ServiceManager serviceManager;
56     private Repository repository;
57     private boolean inPublisherRequest;
58     private XmlSaxHandler xmlSaxHandler;
59     private int publisherRequestElementNestingCount;
60     private Request request;
61     private Context context;
62     private PageContext pageContext;
63
64     public void service(ServiceManager serviceManager) throws ServiceException {
65         this.serviceManager = serviceManager;
66     }
67
68     public void contextualize(Context context) throws ContextException {
69         this.context = context;
70     }
71
72     public void setup(SourceResolver sourceResolver, Map JavaDoc objectModel, String JavaDoc src, Parameters parameters) throws ProcessingException, SAXException JavaDoc, IOException JavaDoc {
73         request = ObjectModelHelper.getRequest(objectModel);
74         try {
75             this.repository = WikiHelper.getRepository(request, serviceManager);
76         } catch (Exception JavaDoc e) {
77             throw new ProcessingException(e);
78         }
79
80         this.inPublisherRequest = false;
81         this.pageContext = new PageContext(WikiHelper.getMountPoint(request), WikiHelper.getSiteConf(request),
82                 repository, WikiHelper.getLayoutType(request), WikiHelper.getSkin(request), context);
83     }
84
85     public void dispose() {
86         this.repository = null;
87         this.xmlSaxHandler = null;
88         this.request = null;
89         this.pageContext = null;
90     }
91
92     public void startElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc atts) throws SAXException JavaDoc {
93         if (inPublisherRequest) {
94             xmlSaxHandler.getContentHandler().startElement(namespaceURI, localName, qName, atts);
95             publisherRequestElementNestingCount++;
96         } else if (localName.equals("publisherRequest") && namespaceURI.equals(NAMESPACE)) {
97             inPublisherRequest = true;
98             publisherRequestElementNestingCount = 0;
99             xmlSaxHandler = XmlObject.Factory.newXmlSaxHandler();
100             xmlSaxHandler.getContentHandler().startElement(namespaceURI, localName, qName, atts);
101         } else {
102             super.startElement(namespaceURI, localName, qName, atts);
103         }
104     }
105
106     public void endElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName) throws SAXException JavaDoc {
107         if (inPublisherRequest && publisherRequestElementNestingCount == 0) {
108             xmlSaxHandler.getContentHandler().endElement(namespaceURI, localName, qName);
109             inPublisherRequest = false;
110
111             // serialize the request and send it to the publisher
112
XmlObject requestAsXmlObject;
113             try {
114                 requestAsXmlObject = xmlSaxHandler.getObject();
115             } catch (XmlException e) {
116                 throw new SAXException JavaDoc("Error getting recorded publisher request as XmlObject.", e);
117             }
118
119             PublisherRequestDocument publisherRequestDocument = (PublisherRequestDocument)requestAsXmlObject.changeType(PublisherRequestDocument.type);
120             if (publisherRequestDocument == null)
121                 throw new SAXException JavaDoc("Could not change the type of the XmlObject to PublisherRequestDocument.");
122
123
124             Publisher publisher = (Publisher)repository.getExtension("Publisher");
125             try {
126                 publisher.processRequest(publisherRequestDocument, new IncludeXMLConsumer(createPreparedDocumentsHandler(contentHandler)));
127             } catch (RepositoryException e) {
128                 throw new SAXException JavaDoc("Error calling publisher.", e);
129             }
130
131             xmlSaxHandler = null;
132
133         } else if (inPublisherRequest) {
134             publisherRequestElementNestingCount--;
135             xmlSaxHandler.getContentHandler().endElement(namespaceURI, localName, qName);
136         } else {
137             super.endElement(namespaceURI, localName, qName);
138         }
139     }
140
141     private PreparedDocumentsHandler createPreparedDocumentsHandler(ContentHandler JavaDoc consumer) throws SAXException JavaDoc {
142         String JavaDoc mountPoint = WikiHelper.getMountPoint(request);
143         SiteConf siteConf = WikiHelper.getSiteConf(request);
144         String JavaDoc daisyCocoonPath = WikiHelper.getDaisyCocoonPath(request);
145         String JavaDoc basePath = mountPoint + "/" + siteConf + "/";
146
147         Repository repository;
148         try {
149             repository = WikiHelper.getRepository(request, serviceManager);
150         } catch (Exception JavaDoc e) {
151             throw new SAXException JavaDoc(e);
152         }
153         String JavaDoc publishType = (String JavaDoc)request.getAttribute("documentStylingPublishType");
154
155         DocumentTypeSpecificStyler.StylesheetProvider stylesheetProvider = new WikiStylesheetProvider(publishType, serviceManager);
156
157         DocumentTypeSpecificStyler documentTypeSpecificStyler = new DocumentTypeSpecificStyler(publishType, basePath, daisyCocoonPath, stylesheetProvider, pageContext, repository, context, serviceManager);
158         return new PreparedDocumentsHandler(consumer, request, documentTypeSpecificStyler);
159     }
160
161     public void setDocumentLocator(Locator JavaDoc locator) {
162         if (!inPublisherRequest)
163             super.setDocumentLocator(locator);
164     }
165
166     public void startDocument() throws SAXException JavaDoc {
167         super.startDocument();
168     }
169
170     public void endDocument() throws SAXException JavaDoc {
171         super.endDocument();
172     }
173
174     public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri) throws SAXException JavaDoc {
175         if (inPublisherRequest)
176             xmlSaxHandler.getContentHandler().startPrefixMapping(prefix, uri);
177         else
178             super.startPrefixMapping(prefix, uri);
179     }
180
181     public void endPrefixMapping(String JavaDoc prefix) throws SAXException JavaDoc {
182         if (inPublisherRequest)
183             xmlSaxHandler.getContentHandler().endPrefixMapping(prefix);
184         else
185             super.endPrefixMapping(prefix);
186     }
187
188     public void characters(char ch[], int start, int length) throws SAXException JavaDoc {
189         if (inPublisherRequest)
190             xmlSaxHandler.getContentHandler().characters(ch, start, length);
191         else
192             super.characters(ch, start, length);
193     }
194
195     public void ignorableWhitespace(char[] chars, int start, int length) throws SAXException JavaDoc {
196         if (!inPublisherRequest)
197             super.ignorableWhitespace(chars, start, length);
198     }
199
200     public void processingInstruction(String JavaDoc s, String JavaDoc s1) throws SAXException JavaDoc {
201         if (!inPublisherRequest)
202             super.processingInstruction(s, s1);
203     }
204
205     public void skippedEntity(String JavaDoc s) throws SAXException JavaDoc {
206         if (!inPublisherRequest)
207             super.skippedEntity(s);
208     }
209
210     public void startDTD(String JavaDoc s, String JavaDoc s1, String JavaDoc s2) throws SAXException JavaDoc {
211         if (!inPublisherRequest)
212             super.startDTD(s, s1, s2);
213     }
214
215     public void endDTD() throws SAXException JavaDoc {
216         if (!inPublisherRequest)
217             super.endDTD();
218     }
219
220     public void startEntity(String JavaDoc s) throws SAXException JavaDoc {
221         if (!inPublisherRequest)
222             super.startEntity(s);
223     }
224
225     public void endEntity(String JavaDoc s) throws SAXException JavaDoc {
226         if (!inPublisherRequest)
227             super.endEntity(s);
228     }
229
230     public void startCDATA() throws SAXException JavaDoc {
231         if (!inPublisherRequest)
232             super.startCDATA();
233     }
234
235     public void endCDATA() throws SAXException JavaDoc {
236         if (!inPublisherRequest)
237             super.endCDATA();
238     }
239
240     public void comment(char[] chars, int i, int i1) throws SAXException JavaDoc {
241         if (!inPublisherRequest)
242             super.comment(chars, i, i1);
243     }
244
245 }
246
Popular Tags