1 16 package org.apache.cocoon.portal.coplets.basket; 17 18 import java.io.ByteArrayInputStream ; 19 import java.io.IOException ; 20 import java.util.Map ; 21 22 import org.apache.avalon.framework.parameters.Parameters; 23 import org.apache.avalon.framework.service.ServiceException; 24 import org.apache.cocoon.ProcessingException; 25 import org.apache.cocoon.components.source.SourceUtil; 26 import org.apache.cocoon.environment.SourceResolver; 27 import org.apache.cocoon.portal.coplet.CopletInstanceData; 28 import org.apache.cocoon.portal.generation.AbstractCopletGenerator; 29 import org.apache.cocoon.xml.SaxBuffer; 30 import org.apache.cocoon.xml.XMLUtils; 31 import org.apache.excalibur.source.Source; 32 import org.apache.excalibur.xml.sax.SAXParser; 33 import org.xml.sax.InputSource ; 34 import org.xml.sax.SAXException ; 35 36 41 public class BasketContentGenerator 42 extends AbstractCopletGenerator { 43 44 45 protected String attributeName; 46 47 50 public void setup(SourceResolver resolver, 51 Map objectModel, 52 String src, 53 Parameters par) 54 throws ProcessingException, SAXException , IOException { 55 super.setup(resolver, objectModel, src, par); 56 57 this.attributeName = par.getParameter("attribute-name", null); 58 } 59 60 63 public void generate() 64 throws IOException , SAXException , ProcessingException { 65 boolean streamed = false; 66 SAXParser parser = null; 67 try { 68 parser = (SAXParser) this.manager.lookup(SAXParser.ROLE); 69 if ( this.attributeName != null ) { 70 CopletInstanceData cid = this.getCopletInstanceData(); 71 byte[] content = (byte[])cid.getAttribute(this.attributeName); 72 if ( content == null ) { 73 this.xmlConsumer.startDocument(); 74 XMLUtils.createElement(this.xmlConsumer, "p"); 75 this.xmlConsumer.endDocument(); 76 return; 77 } 78 try { 79 InputSource is = new InputSource (new ByteArrayInputStream (content)); 80 SaxBuffer buffer = new SaxBuffer(); 81 parser.parse(is, buffer); 82 streamed = true; 83 buffer.toSAX(this.xmlConsumer); 84 } catch (Exception ignore) { 85 } 87 } 88 if ( !streamed ) { 89 Source source = null; 90 try { 91 source = this.resolver.resolveURI(this.source); 92 parser.parse(SourceUtil.getInputSource(source), this.xmlConsumer); 93 } finally { 94 this.resolver.release(source); 95 } 96 } 97 } catch (ServiceException se) { 98 throw new ProcessingException("Unable to lookup parser.", se); 99 } finally { 100 this.manager.release(parser); 101 } 102 } 103 104 } 105 | Popular Tags |