KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > samples > parentcm > Generator


1 /*
2  * Copyright 1999-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.samples.parentcm;
17
18 import java.io.IOException JavaDoc;
19 import java.util.Date JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import org.apache.avalon.excalibur.pool.Poolable;
23 import org.apache.avalon.framework.parameters.Parameters;
24 import org.apache.avalon.framework.service.ServiceException;
25 import org.apache.cocoon.ProcessingException;
26 import org.apache.cocoon.environment.SourceResolver;
27 import org.apache.cocoon.generation.ServiceableGenerator;
28 import org.apache.cocoon.xml.XMLUtils;
29 import org.xml.sax.SAXException JavaDoc;
30
31 /**
32  * Generator for the parent component manager sample. The generator outputs
33  * a single tag <code>&lt;time&gt;<i>current time</i>&lt;/time&gt;</code>.
34  * Where <code><i>current time</i></code> is the current time as obtained from the
35  * <code>Time</code> component.
36  *
37  * @author <a HREF="mailto:leo.sutic@inspireinfrastructure.com">Leo Sutic</a>
38  * @version $Id: Generator.java 278591 2005-09-04 13:24:23Z pier $
39  */

40 public class Generator extends ServiceableGenerator implements Poolable {
41
42     /**
43      * Current time.
44      */

45     private Date JavaDoc time;
46
47     /**
48      * Looks up a <code>Time</code> component and obtains the current time.
49      */

50     public void setup(SourceResolver resolver, Map JavaDoc objectModel, String JavaDoc src, Parameters par)
51         throws ProcessingException, SAXException JavaDoc, IOException JavaDoc {
52
53         Time timeGiver = null;
54         try {
55             timeGiver = (Time) this.manager.lookup(Time.ROLE);
56             this.time = timeGiver.getTime ();
57         } catch (ServiceException ce) {
58             throw new ProcessingException ("Could not obtain current time.", ce);
59         } finally {
60             manager.release(timeGiver);
61         }
62     }
63
64     /**
65      * Generate XML data.
66      */

67     public void generate()
68     throws SAXException JavaDoc, ProcessingException {
69         contentHandler.startDocument();
70         contentHandler.startElement("", "time", "time", XMLUtils.EMPTY_ATTRIBUTES);
71
72         char[] text = this.time.toString().toCharArray();
73         contentHandler.characters(text, 0, text.length);
74
75         contentHandler.endElement("", "time", "time");
76         contentHandler.endDocument();
77     }
78
79     /**
80      * Prepare this object for another cycle.
81      */

82     public void recycle () {
83         this.time = null;
84     }
85 }
86
87
88
Popular Tags