KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > portal > coplets > basket > BasketContentGenerator


1 /*
2  * Copyright 2004-2005 The Apache Software Foundation.
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.apache.cocoon.portal.coplets.basket;
17
18 import java.io.ByteArrayInputStream JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.util.Map JavaDoc;
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 JavaDoc;
34 import org.xml.sax.SAXException JavaDoc;
35
36 /**
37  * This generator displays the content of one item.
38  *
39  * @version CVS $Id: BasketContentGenerator.java 125056 2005-01-13 10:33:37Z cziegeler $
40  */

41 public class BasketContentGenerator
42 extends AbstractCopletGenerator {
43     
44     /** This is the attribute name containing the content */
45     protected String JavaDoc attributeName;
46     
47     /* (non-Javadoc)
48      * @see org.apache.cocoon.sitemap.SitemapModelComponent#setup(org.apache.cocoon.environment.SourceResolver, java.util.Map, java.lang.String, org.apache.avalon.framework.parameters.Parameters)
49      */

50     public void setup(SourceResolver resolver,
51                       Map JavaDoc objectModel,
52                       String JavaDoc src,
53                       Parameters par)
54     throws ProcessingException, SAXException JavaDoc, IOException JavaDoc {
55         super.setup(resolver, objectModel, src, par);
56
57         this.attributeName = par.getParameter("attribute-name", null);
58     }
59
60     /* (non-Javadoc)
61      * @see org.apache.cocoon.generation.Generator#generate()
62      */

63     public void generate()
64     throws IOException JavaDoc, SAXException JavaDoc, 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 JavaDoc is = new InputSource JavaDoc(new ByteArrayInputStream JavaDoc(content));
80                     SaxBuffer buffer = new SaxBuffer();
81                     parser.parse(is, buffer);
82                     streamed = true;
83                     buffer.toSAX(this.xmlConsumer);
84                 } catch (Exception JavaDoc ignore) {
85                     // ignore
86
}
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