001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.generation;
018:
019: import java.io.IOException;
020: import java.io.Serializable;
021: import java.util.Map;
022:
023: import org.apache.avalon.framework.CascadingRuntimeException;
024: import org.apache.avalon.framework.context.Context;
025: import org.apache.avalon.framework.context.ContextException;
026: import org.apache.avalon.framework.context.Contextualizable;
027: import org.apache.avalon.framework.parameters.ParameterException;
028: import org.apache.avalon.framework.parameters.Parameters;
029: import org.apache.cocoon.ProcessingException;
030: import org.apache.cocoon.caching.CacheableProcessingComponent;
031: import org.apache.cocoon.environment.ObjectModelHelper;
032: import org.apache.cocoon.environment.SourceResolver;
033: import org.apache.cocoon.xml.XMLConsumer;
034: import org.apache.excalibur.source.SourceValidity;
035: import org.apache.excalibur.source.impl.validity.NOPValidity;
036: import org.xml.sax.SAXException;
037: import org.xml.sax.helpers.AttributesImpl;
038:
039: /**
040: * Increment context attribute "count" for testing purposes.
041: *
042: * This generator always returns a VALID validity, and the cache key is given as
043: * a sitemap parameter.
044: *
045: * @version $Id: IncrementGenerator.java 433543 2006-08-22 06:22:54Z crossley $
046: */
047: public class IncrementGenerator implements Contextualizable, Generator,
048: CacheableProcessingComponent {
049:
050: XMLConsumer consumer;
051: Map objectModel;
052: String key;
053: Context context;
054:
055: public void generate() throws IOException, SAXException,
056: ProcessingException {
057: increment(objectModel, "count");
058:
059: consumer.startDocument();
060: consumer.startElement("", "node", "node", new AttributesImpl());
061: consumer.endElement("", "node", "node");
062: consumer.endDocument();
063: }
064:
065: public Context getAvalonContext() {
066: return context;
067: }
068:
069: public void setConsumer(XMLConsumer consumer) {
070: this .consumer = consumer;
071: }
072:
073: public void setup(SourceResolver resolver, Map objectModel,
074: String src, Parameters par) throws ProcessingException,
075: SAXException, IOException {
076: this .objectModel = objectModel;
077: try {
078: this .key = par.getParameter("key");
079: } catch (ParameterException e) {
080: throw new CascadingRuntimeException(
081: "Could not find parameter key", e);
082: }
083: }
084:
085: public static void increment(Map objectModel, String key) {
086: org.apache.cocoon.environment.Context context = ObjectModelHelper
087: .getContext(objectModel);
088: Integer count = (Integer) context.getAttribute(key);
089: if (count == null) {
090: count = new Integer(0);
091: }
092: count = new Integer(count.intValue() + 1);
093: context.setAttribute(key, count);
094: }
095:
096: public Serializable getKey() {
097: return key;
098: }
099:
100: public SourceValidity getValidity() {
101: return NOPValidity.SHARED_INSTANCE;
102: }
103:
104: public void contextualize(Context context) throws ContextException {
105: this.context = context;
106: }
107: }
|