1 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 ; 27 import org.xml.sax.ContentHandler ; 28 29 32 public class PublisherXmlRequestBuilder { 33 public static PublisherRequestDocument loadPublisherRequest(String name, Object params, ServiceManager serviceManager, Context context) throws Exception { 34 XmlSaxHandler xmlSaxHandler = XmlObject.Factory.newXmlSaxHandler(); 35 try { 36 executePipeline(name, params, xmlSaxHandler.getContentHandler(), serviceManager, context); 37 } catch (Throwable e) { 38 throw new Exception ("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 ("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 ("Could not change the type of the XmlObject to PublisherRequestDocument."); 51 52 return publisherRequestDocument; 53 } 54 55 private static void executePipeline(String pipe, Object viewData, ContentHandler contentHandler, ServiceManager serviceManager, Context context) throws Exception { 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 |