KickJava   Java API By Example, From Geeks To Geeks.

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


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.outerx.daisy.x10Publisher.PublisherRequestDocument;
19 import org.apache.xmlbeans.XmlSaxHandler;
20 import org.apache.xmlbeans.XmlObject;
21 import org.apache.xmlbeans.XmlException;
22 import org.apache.cocoon.components.flow.util.PipelineUtil;
23 import org.apache.cocoon.components.LifecycleHelper;
24 import org.apache.avalon.framework.context.Context;
25 import org.apache.avalon.framework.service.ServiceManager;
26 import org.xml.sax.SAXException JavaDoc;
27 import org.xml.sax.ContentHandler JavaDoc;
28
29 /**
30  * Utility code to build a PublisherRequestDocument from the output of a Cocoon pipeline.
31  */

32 public class PublisherXmlRequestBuilder {
33     public static PublisherRequestDocument loadPublisherRequest(String JavaDoc name, Object JavaDoc params, ServiceManager serviceManager, Context context) throws Exception JavaDoc {
34         XmlSaxHandler xmlSaxHandler = XmlObject.Factory.newXmlSaxHandler();
35         try {
36             executePipeline(name, params, xmlSaxHandler.getContentHandler(), serviceManager, context);
37         } catch (Throwable JavaDoc e) {
38             throw new Exception JavaDoc("Error building publisher request for \"" + name + "\".", e);
39         }
40
41         XmlObject requestAsXmlObject = null;
42         try {
43             requestAsXmlObject = xmlSaxHandler.getObject();
44         } catch (XmlException e) {
45             throw new SAXException JavaDoc("Error retrieving publisher request as XmlObject.", e);
46         }
47
48         PublisherRequestDocument publisherRequestDocument = (PublisherRequestDocument)requestAsXmlObject.changeType(PublisherRequestDocument.type);
49         if (publisherRequestDocument == null)
50             throw new SAXException JavaDoc("Could not change the type of the XmlObject to PublisherRequestDocument.");
51
52         return publisherRequestDocument;
53     }
54
55     private static void executePipeline(String JavaDoc pipe, Object JavaDoc viewData, ContentHandler JavaDoc contentHandler, ServiceManager serviceManager, Context context) throws Exception JavaDoc {
56         PipelineUtil pipelineUtil = new PipelineUtil();
57         try {
58             LifecycleHelper.setupComponent(pipelineUtil, null, context, serviceManager, null, false);
59             pipelineUtil.processToSAX(pipe, viewData, contentHandler);
60         } finally {
61             LifecycleHelper.dispose(pipelineUtil);
62         }
63     }
64 }
65
Popular Tags