001: /*
002: * Copyright 2001-2006 C:1 Financial Services GmbH
003: *
004: * This software is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License Version 2.1, as published by the Free Software Foundation.
007: *
008: * This software is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
011: * Lesser General Public License for more details.
012: *
013: * You should have received a copy of the GNU Lesser General Public
014: * License along with this library; if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
016: */
017:
018: package de.finix.contelligent.action;
019:
020: import gnu.regexp.RE;
021: import gnu.regexp.REException;
022:
023: import java.io.IOException;
024: import java.util.Collection;
025: import java.util.Collections;
026:
027: import de.finix.contelligent.CallData;
028: import de.finix.contelligent.exception.ContelligentException;
029:
030: public class ActionParameter extends StoredObject implements Parameter {
031: private boolean required = true;
032:
033: private String defaultValue = "";
034:
035: private String regularExpression = "";
036:
037: private RE regExp;
038:
039: public Object getContentValue(CallData callData)
040: throws IOException, ContelligentException {
041: return getString(callData);
042: }
043:
044: public long lastModified(CallData callData) throws IOException {
045: return -1;
046: }
047:
048: public Collection getSensitiveCategories() {
049: return Collections.EMPTY_SET;
050: }
051:
052: public String getString(CallData callData)
053: throws ContelligentException, IOException {
054: return ctx.getPath().getName();
055: }
056:
057: public void setRequired(boolean required) {
058: this .required = required;
059: }
060:
061: public boolean getRequired() {
062: return required;
063: }
064:
065: public boolean isRequired() {
066: return required;
067: }
068:
069: public void setDefault(String defaultValue) {
070: this .defaultValue = defaultValue;
071: }
072:
073: public String getDefault() {
074: return defaultValue;
075: }
076:
077: public void setRegularExpression(String regExp) {
078: regularExpression = regExp;
079: this .regExp = null;
080: }
081:
082: public String getRegularExpression() {
083: return regularExpression;
084: }
085:
086: public boolean validate(String value) {
087: if (getRegularExpression().length() == 0) {
088: return true;
089: }
090: return getRegExp().isMatch(value);
091: }
092:
093: public boolean validate(String[] values) {
094: if (getRegularExpression().length() == 0) {
095: return true;
096: }
097: for (int i = 0; i < values.length; i++) {
098: if (!validate(values[i])) {
099: return false;
100: }
101: }
102: return true;
103: }
104:
105: public Object getObject(CallData callData) {
106: Object tmp = null;
107: if (scopeSession()) {
108: tmp = callData.getSessionAttribute(getSessionStorageKey());
109: if (tmp != null)
110: return tmp;
111: else
112: return new String[] { defaultValue };
113: } else {
114: tmp = callData.getParameterValues(getRequestStorageKey());
115: if (tmp != null)
116: return tmp;
117: else
118: return new String[] { defaultValue };
119: }
120: }
121:
122: protected RE getRegExp() {
123: if (regExp == null) {
124: try {
125: regExp = new RE(getRegularExpression());
126: } catch (REException e) {
127: try {
128: regExp = new RE("");
129: } catch (REException ee) {
130: // impossible
131: }
132: }
133: }
134: return regExp;
135: }
136: }
|