001: /*
002: * JFolder, Copyright 2001-2006 Gary Steinmetz
003: *
004: * Distributable under LGPL license.
005: * See terms of license at gnu.org.
006: */
007:
008: package org.jfolder.common.utils.web;
009:
010: //base classes
011: import java.util.ArrayList;
012: import java.util.HashMap;
013: import java.util.Iterator;
014:
015: //project specific classes
016: import org.jfolder.common.ImmutableByteArray;
017:
018: //other classes
019:
020: public class ParameterSet {
021:
022: //
023: private HashMap paramSet = null;
024: private ImmutableByteArray content = null;
025: private String encoding = null;
026:
027: protected ParameterSet() {
028: this .paramSet = new HashMap();
029: }
030:
031: protected void addContent(ImmutableByteArray inContent) {
032: this .content = inContent;
033: }
034:
035: public ImmutableByteArray getContent() {
036: return this .content;
037: }
038:
039: protected void setEncoding(String inEncoding) {
040: this .encoding = inEncoding;
041: }
042:
043: public String getEncoding() {
044: return this .encoding;
045: }
046:
047: //
048: public void addFileUpload(String inName, String inFile,
049: byte inContent[]) {
050: //
051: ParameterHolder ph = new ParameterHolder();
052: //
053: ph.setName(inName);
054: ph.setFileName(inFile);
055: ph.setValue(inContent);
056: //
057: addParameterHolder(ph);
058: }
059:
060: public void addParameter(String inName, String inValue) {
061: //
062: ParameterHolder ph = new ParameterHolder();
063: //
064: ph.setName(inName);
065: ph.setValue(inValue);
066: //
067: addParameterHolder(ph);
068: }
069:
070: //
071: protected void addParameterHolder(ParameterHolder inPh) {
072:
073: ArrayList params = null;
074:
075: //if (this.pSet.get(inPh.getName().toUpperCase()) != null) {
076: if (this .paramSet.get(inPh.getName()) != null) {
077: //params = (ArrayList)this.pSet.get(inPh.getName().toUpperCase());
078: params = (ArrayList) this .paramSet.get(inPh.getName());
079: } else {
080: params = new ArrayList();
081: //this.pSet.put(inPh.getName().toUpperCase(), params);
082: this .paramSet.put(inPh.getName(), params);
083: }
084:
085: params.add(inPh);
086: }
087:
088: public void removeParameterHolderIfPresent(String inName) {
089: //
090: if (this .paramSet.get(inName) != null) {
091: //
092: this .paramSet.remove(inName);
093: }
094: }
095:
096: public boolean isParameterPresent(String inName) {
097: //return (this.pSet.get(inName.toUpperCase()) != null);
098: return (this .paramSet.get(inName) != null);
099: }
100:
101: public boolean isParameterString(String inName) {
102:
103: boolean outValue = false;
104:
105: if (isParameterPresent(inName)) {
106: //ArrayList values = (ArrayList)this.pSet.get(inName.toUpperCase());
107: ArrayList values = (ArrayList) this .paramSet.get(inName);
108: ParameterHolder ph = (ParameterHolder) values.get(0);
109: outValue = ph.isValueString();
110: }
111:
112: return outValue;
113: }
114:
115: public boolean isParameterUpload(String inName) {
116:
117: boolean outValue = false;
118:
119: if (isParameterPresent(inName)) {
120: //ArrayList values = (ArrayList)this.pSet.get(inName.toUpperCase());
121: ArrayList values = (ArrayList) this .paramSet.get(inName);
122: ParameterHolder ph = (ParameterHolder) values.get(0);
123: outValue = ph.isValueUpload()
124: && (ph.getFileName().length() > 0);
125: }
126:
127: return outValue;
128: }
129:
130: public String getParameter(String inName) {
131: return getParameter(inName, 0);
132: }
133:
134: public String getParameter(String inName, int inIndex) {
135:
136: String outValue = null;
137:
138: if (isParameterPresent(inName)) {
139: //ArrayList values = (ArrayList)this.pSet.get(inName.toUpperCase());
140: ArrayList values = (ArrayList) this .paramSet.get(inName);
141: ParameterHolder ph = (ParameterHolder) values.get(inIndex);
142: outValue = ph.getValueAsString();
143: }
144:
145: return outValue;
146: }
147:
148: public int getParameterValuesLength(String inName) {
149:
150: int outValue = 0;
151:
152: if (isParameterPresent(inName)) {
153: //ArrayList values = (ArrayList)this.pSet.get(inName.toUpperCase());
154: ArrayList values = (ArrayList) this .paramSet.get(inName);
155: outValue = values.size();
156: }
157:
158: return outValue;
159: }
160:
161: public byte[] getParameterAsUpload(String inName) {
162: return getParameterAsUpload(inName, 0);
163: }
164:
165: public byte[] getParameterAsUpload(String inName, int inIndex) {
166:
167: byte outValue[] = null;
168:
169: if (isParameterPresent(inName)) {
170: //ArrayList values = (ArrayList)this.pSet.get(inName.toUpperCase());
171: ArrayList values = (ArrayList) this .paramSet.get(inName);
172: ParameterHolder ph = (ParameterHolder) values.get(inIndex);
173: outValue = ph.getValueAsBytes();
174: }
175:
176: return outValue;
177: }
178:
179: public String getUploadParameterName(String inName) {
180: return getUploadParameterName(inName, 0);
181: }
182:
183: public String getUploadParameterName(String inName, int inIndex) {
184:
185: String outValue = null;
186:
187: if (isParameterPresent(inName)) {
188: //ArrayList values = (ArrayList)this.pSet.get(inName.toUpperCase());
189: ArrayList values = (ArrayList) this .paramSet.get(inName);
190: ParameterHolder ph = (ParameterHolder) values.get(inIndex);
191: outValue = ph.getLocalFileName();
192: }
193:
194: return outValue;
195: }
196:
197: public Iterator getParameterNames() {
198: return this.paramSet.keySet().iterator();
199: }
200:
201: }
|