1 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 ; 36 import org.xml.sax.Attributes ; 37 import org.xml.sax.Locator ; 38 import org.xml.sax.ContentHandler ; 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 ; 46 import java.io.IOException ; 47 48 53 public class PublisherTransformer extends AbstractTransformer implements Serviceable, Contextualizable, Disposable { 54 private static final String 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 objectModel, String src, Parameters parameters) throws ProcessingException, SAXException , IOException { 73 request = ObjectModelHelper.getRequest(objectModel); 74 try { 75 this.repository = WikiHelper.getRepository(request, serviceManager); 76 } catch (Exception 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 namespaceURI, String localName, String qName, Attributes atts) throws SAXException { 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 namespaceURI, String localName, String qName) throws SAXException { 107 if (inPublisherRequest && publisherRequestElementNestingCount == 0) { 108 xmlSaxHandler.getContentHandler().endElement(namespaceURI, localName, qName); 109 inPublisherRequest = false; 110 111 XmlObject requestAsXmlObject; 113 try { 114 requestAsXmlObject = xmlSaxHandler.getObject(); 115 } catch (XmlException e) { 116 throw new SAXException ("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 ("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 ("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 consumer) throws SAXException { 142 String mountPoint = WikiHelper.getMountPoint(request); 143 SiteConf siteConf = WikiHelper.getSiteConf(request); 144 String daisyCocoonPath = WikiHelper.getDaisyCocoonPath(request); 145 String basePath = mountPoint + "/" + siteConf + "/"; 146 147 Repository repository; 148 try { 149 repository = WikiHelper.getRepository(request, serviceManager); 150 } catch (Exception e) { 151 throw new SAXException (e); 152 } 153 String publishType = (String )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 locator) { 162 if (!inPublisherRequest) 163 super.setDocumentLocator(locator); 164 } 165 166 public void startDocument() throws SAXException { 167 super.startDocument(); 168 } 169 170 public void endDocument() throws SAXException { 171 super.endDocument(); 172 } 173 174 public void startPrefixMapping(String prefix, String uri) throws SAXException { 175 if (inPublisherRequest) 176 xmlSaxHandler.getContentHandler().startPrefixMapping(prefix, uri); 177 else 178 super.startPrefixMapping(prefix, uri); 179 } 180 181 public void endPrefixMapping(String prefix) throws SAXException { 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 { 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 { 196 if (!inPublisherRequest) 197 super.ignorableWhitespace(chars, start, length); 198 } 199 200 public void processingInstruction(String s, String s1) throws SAXException { 201 if (!inPublisherRequest) 202 super.processingInstruction(s, s1); 203 } 204 205 public void skippedEntity(String s) throws SAXException { 206 if (!inPublisherRequest) 207 super.skippedEntity(s); 208 } 209 210 public void startDTD(String s, String s1, String s2) throws SAXException { 211 if (!inPublisherRequest) 212 super.startDTD(s, s1, s2); 213 } 214 215 public void endDTD() throws SAXException { 216 if (!inPublisherRequest) 217 super.endDTD(); 218 } 219 220 public void startEntity(String s) throws SAXException { 221 if (!inPublisherRequest) 222 super.startEntity(s); 223 } 224 225 public void endEntity(String s) throws SAXException { 226 if (!inPublisherRequest) 227 super.endEntity(s); 228 } 229 230 public void startCDATA() throws SAXException { 231 if (!inPublisherRequest) 232 super.startCDATA(); 233 } 234 235 public void endCDATA() throws SAXException { 236 if (!inPublisherRequest) 237 super.endCDATA(); 238 } 239 240 public void comment(char[] chars, int i, int i1) throws SAXException { 241 if (!inPublisherRequest) 242 super.comment(chars, i, i1); 243 } 244 245 } 246 | Popular Tags |