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:
18: package org.apache.cocoon.components.modules.input;
19:
20: import org.apache.avalon.framework.configuration.Configuration;
21: import org.apache.avalon.framework.configuration.ConfigurationException;
22: import org.apache.avalon.framework.thread.ThreadSafe;
23:
24: import java.util.Iterator;
25: import java.util.LinkedList;
26: import java.util.List;
27: import java.util.Map;
28: import java.util.Vector;
29:
30: /**
31: * RandomNumberModule returns a random number as a string.
32: * Configuration through child elements: "min", "max" setting
33: * range of random number. Defaults to "0" and "9999999999"
34: * respectively.
35: *
36: * @author <a href="mailto:haul@apache.org">Christian Haul</a>
37: * @version $Id: RandomNumberModule.java 433543 2006-08-22 06:22:54Z crossley $
38: */
39: public class RandomNumberModule extends AbstractInputModule implements
40: ThreadSafe {
41:
42: final static Vector returnNames;
43: static {
44: Vector tmp = new Vector();
45: tmp.add("randomNumber");
46: returnNames = tmp;
47: }
48:
49: public Object getAttribute(String name, Configuration modeConf,
50: Map objectModel) throws ConfigurationException {
51: long min = Long.parseLong((String) this .settings
52: .get("min", "0"));
53: long max = Long.parseLong((String) this .settings.get("max",
54: String.valueOf(Long.MAX_VALUE)));
55: if (modeConf != null) {
56: min = modeConf.getAttributeAsLong("max", min);
57: max = modeConf.getAttributeAsLong("max", max);
58:
59: //preferred
60: min = modeConf.getChild("min").getValueAsLong(min);
61: max = modeConf.getChild("max").getValueAsLong(max);
62: }
63: return Long.toString(min
64: + Math.round(Math.random() * (max - min)));
65: }
66:
67: public Iterator getAttributeNames(Configuration modeConf,
68: Map objectModel) throws ConfigurationException {
69: return RandomNumberModule.returnNames.iterator();
70: }
71:
72: public Object[] getAttributeValues(String name,
73: Configuration modeConf, Map objectModel)
74: throws ConfigurationException {
75: List values = new LinkedList();
76: values.add(this.getAttribute(name, modeConf, objectModel));
77: return values.toArray();
78: }
79: }
|