01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.cocoon.samples.parentcm;
18:
19: import java.io.IOException;
20: import java.util.Date;
21: import java.util.Map;
22:
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;
30:
31: /**
32: * Generator for the parent component manager sample. The generator outputs
33: * a single tag <code><time><i>current time</i></time></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 433543 2006-08-22 06:22:54Z crossley $
39: */
40: public class Generator extends ServiceableGenerator {
41:
42: /**
43: * Current time.
44: */
45: private Date time;
46:
47: /**
48: * Looks up a <code>Time</code> component and obtains the current time.
49: */
50: public void setup(SourceResolver resolver, Map objectModel,
51: String src, Parameters par) throws ProcessingException,
52: SAXException, IOException {
53:
54: Time timeGiver = null;
55: try {
56: timeGiver = (Time) this .manager.lookup(Time.ROLE);
57: this .time = timeGiver.getTime();
58: } catch (ServiceException ce) {
59: throw new ProcessingException(
60: "Could not obtain current time.", ce);
61: } finally {
62: manager.release(timeGiver);
63: }
64: }
65:
66: /**
67: * Generate XML data.
68: */
69: public void generate() throws SAXException, ProcessingException {
70: contentHandler.startDocument();
71: contentHandler.startElement("", "time", "time",
72: XMLUtils.EMPTY_ATTRIBUTES);
73:
74: char[] text = this .time.toString().toCharArray();
75: contentHandler.characters(text, 0, text.length);
76:
77: contentHandler.endElement("", "time", "time");
78: contentHandler.endDocument();
79: }
80:
81: /**
82: * Prepare this object for another cycle.
83: */
84: public void recycle() {
85: this.time = null;
86: }
87: }
|