001: /* Copyright (c) 2001 - 2007 TOPP - www.openplans.org. All rights reserved.
002: * This code is licensed under the GPL 2.0 license, availible at the root
003: * application directory.
004: */
005:
006: /*
007: * Created on Jan 14, 2004
008: *
009: * To change the template for this generated file go to
010: * Window - Preferences - Java - Code Generation - Code and Comments
011: */
012: package org.vfny.geoserver.config.validation;
013:
014: import org.geotools.validation.dto.ArgumentDTO;
015: import org.geotools.validation.dto.PlugInDTO;
016: import org.geotools.validation.dto.TestDTO;
017: import org.geotools.validation.xml.ArgHelper;
018: import org.geotools.validation.xml.ValidationException;
019: import java.beans.PropertyDescriptor;
020: import java.io.StringReader;
021: import java.util.HashMap;
022: import java.util.HashSet;
023: import java.util.Iterator;
024: import java.util.Map;
025: import java.util.Set;
026:
027: /**
028: * TestConfig purpose.
029: *
030: * <p>
031: * Used to represent a copy of the config information required for the UI.
032: * </p>
033: *
034: * @author dzwiers, Refractions Research, Inc.
035: * @author $Author: dmzwiers $ (last modification)
036: * @version $Id: TestConfig.java 6326 2007-03-15 18:36:40Z jdeolive $
037: */
038: public class TestConfig {
039: public final static String CURRENTLY_SELECTED_KEY = "selectedTest";
040:
041: /** the test name */
042: private String name;
043:
044: /** the test description */
045: private String description;
046:
047: /**
048: * The plug-in which contains the class definition and default runtime
049: * values
050: */
051: private PlugInConfig plugIn;
052:
053: /** lazily loaded */
054: private PropertyDescriptor[] pds;
055:
056: /**
057: * The set of runtime args for this particular test to override the
058: * defaults in the plug-in
059: */
060: private Map args;
061:
062: /**
063: * TestConfig constructor.
064: *
065: * <p>
066: * Does nothing
067: * </p>
068: */
069: public TestConfig() {
070: args = new HashMap();
071: }
072:
073: /**
074: * TestConfig constructor.
075: *
076: * <p>
077: * Creates a copy from the TestConfig specified.
078: * </p>
079: *
080: * @param t the data to copy
081: */
082: public TestConfig(TestConfig t) {
083: name = t.getName();
084: description = t.getDescription();
085: plugIn = new PlugInConfig(t.getPlugIn());
086: args = new HashMap();
087:
088: if (t.getArgs() != null) {
089: Iterator i = t.getArgs().keySet().iterator();
090:
091: while (i.hasNext()) {
092: String key = (String) i.next();
093:
094: //TODO clone value.
095: args.put(key, new ArgumentConfig((ArgumentConfig) t
096: .getArgs().get(key)));
097: }
098: }
099: }
100:
101: /**
102: * TestConfig constructor.
103: *
104: * <p>
105: * Creates a copy from the TestDTO specified.
106: * </p>
107: *
108: * @param t the data to copy
109: */
110: public TestConfig(TestDTO t, Map plugInConfigs) {
111: name = t.getName();
112: description = t.getDescription();
113: plugIn = (PlugInConfig) plugInConfigs.get(t.getPlugIn()
114: .getName());
115: args = new HashMap();
116:
117: if (t.getArgs() != null) {
118: Iterator i = t.getArgs().keySet().iterator();
119:
120: while (i.hasNext()) {
121: String key = (String) i.next();
122:
123: //TODO clone value.
124: args.put(key, new ArgumentConfig((ArgumentDTO) t
125: .getArgs().get(key)));
126: }
127: }
128: }
129:
130: /**
131: * Implementation of clone.
132: *
133: * @return A copy of this TestConfig
134: *
135: * @see java.lang.Object#clone()
136: */
137: public Object clone() {
138: return new TestConfig(this );
139: }
140:
141: /**
142: * Implementation of equals.
143: *
144: * @param obj
145: *
146: * @return true when they have the same data.
147: *
148: * @see java.lang.Object#equals(java.lang.Object)
149: */
150: public boolean equals(Object obj) {
151: if ((obj == null) || !(obj instanceof TestDTO)) {
152: return false;
153: }
154:
155: TestDTO t = (TestDTO) obj;
156: boolean r = true;
157:
158: if (name != null) {
159: r = r && (name.equals(t.getName()));
160: }
161:
162: if (description != null) {
163: r = r && (description.equals(t.getDescription()));
164: }
165:
166: if (plugIn == null) {
167: if (t.getPlugIn() != null) {
168: return false;
169: }
170: } else {
171: if (t.getPlugIn() != null) {
172: r = r && plugIn.equals(t.getPlugIn());
173: } else {
174: return false;
175: }
176: }
177:
178: if (args == null) {
179: if (t.getArgs() != null) {
180: return false;
181: }
182: } else {
183: if (t.getArgs() != null) {
184: r = r && args.equals(t.getArgs());
185: } else {
186: return false;
187: }
188: }
189:
190: return r;
191: }
192:
193: /**
194: * Implementation of hashCode.
195: *
196: * @return int hashcode
197: *
198: * @see java.lang.Object#hashCode()
199: */
200: public int hashCode() {
201: int r = 1;
202:
203: if (name != null) {
204: r *= name.hashCode();
205: }
206:
207: if (description != null) {
208: r *= description.hashCode();
209: }
210:
211: if (plugIn != null) {
212: r *= plugIn.hashCode();
213: }
214:
215: if (args != null) {
216: r *= args.hashCode();
217: }
218:
219: return r;
220: }
221:
222: /**
223: * toDTO purpose.
224: * <p>
225: * Clones this config as a DTO.
226: * </p>
227: * @see java.lang.Object#clone()
228: * @param plugIns Map of PlugInDTO objects
229: * @return TestDTO
230: */
231: public TestDTO toDTO(Map plugIns) {
232: TestDTO dto = new TestDTO();
233:
234: dto.setName(name);
235: dto.setDescription(description);
236: dto.setPlugIn((PlugInDTO) plugIns.get(plugIn.getName()));
237:
238: Map myArgs = new HashMap();
239:
240: if (this .args != null) {
241: Iterator i = this .args.keySet().iterator();
242:
243: while (i.hasNext()) {
244: String key = (String) i.next();
245: myArgs.put(key, ((ArgumentConfig) this .args.get(key))
246: .toDTO());
247: }
248: }
249:
250: dto.setArgs(myArgs);
251:
252: return dto;
253: }
254:
255: /**
256: * Access args property.
257: *
258: * @return Returns the args.
259: */
260: public Map getArgs() {
261: return args;
262: }
263:
264: /**
265: * Set args to args.
266: *
267: * @param args The args to set.
268: */
269: public void setArgs(Map args) {
270: Iterator i = args.keySet().iterator();
271:
272: while (i.hasNext())
273:
274: if (((ArgumentConfig) args.get(i.next())).isFinal()) {
275: throw new IllegalArgumentException(
276: "Cannot include final arguments as part of a test.");
277: }
278:
279: this .args = args;
280: }
281:
282: /**
283: * getArgStringValue purpose.
284: * <p>
285: * Returns a human friendly version
286: * </p>
287: * @param name
288: * @return
289: */
290: public String getArgStringValue(String name) {
291: ArgumentConfig ac = (ArgumentConfig) args.get(name);
292:
293: if (ac == null) {
294: return null;
295: }
296:
297: return ArgHelper.getArgumentStringEncoding(ac.getValue());
298: }
299:
300: /**
301: * getArgValue purpose.
302: * <p>
303: * Returns an Object version
304: * </p>
305: * @param name
306: * @return
307: */
308: public Object getArgValue(String name) {
309: ArgumentConfig ac = (ArgumentConfig) args.get(name);
310:
311: if (ac == null) {
312: return null;
313: }
314:
315: return ac.getValue();
316: }
317:
318: /**
319: * setArgStringValue purpose.
320: * <p>
321: * Stores a human friendly version. If this is a new Argument, then the type is String.
322: * </p>
323: * @param name
324: * @param value
325: * @return
326: */
327: public boolean setArgStringValue(String name, String value) {
328: ArgumentConfig ac = (ArgumentConfig) args.get(name);
329:
330: if ((value == null) || value.equals("")) {
331: args.remove(name);
332:
333: return true;
334: }
335:
336: if (ac == null) {
337: return addArgStringValue(name, value);
338: } else {
339: if (ac.isFinal()) {
340: throw new IllegalArgumentException(
341: "Cannot include final arguments as part of a test.");
342: }
343:
344: StringReader sr = new StringReader(value);
345:
346: try {
347: ac.setValue(ArgHelper.getArgumentInstance(ArgHelper
348: .getArgumentType(ac.getValue()), value));
349:
350: return true;
351: } catch (Exception e) {
352: e.printStackTrace();
353:
354: // error, log it
355: return false;
356: }
357: }
358: }
359:
360: /**
361: * setArgStringValue purpose.
362: * <p>
363: * Stores a human friendly version.
364: * </p>
365: * @param name
366: * @param value
367: * @return
368: */
369: public boolean addArgStringValue(String name, String value) {
370: PropertyDescriptor pd = getPropertyDescriptor(name);
371:
372: if (pd == null) {
373: return false;
374: }
375:
376: if ((value == null) || value.equals("")) {
377: args.remove(name);
378:
379: return false;
380: }
381:
382: Class cl = pd.getPropertyType();
383: ArgumentConfig ac = new ArgumentConfig();
384: ac.setName(name);
385:
386: try {
387: String argType = ArgHelper.getArgumentType(cl);
388: ac.setValue(ArgHelper.getArgumentInstance(argType, value));
389: } catch (ValidationException e) {
390: // TODO Auto-generated catch block
391: e.printStackTrace();
392:
393: return false;
394: }
395:
396: args.put(name, ac);
397:
398: return true;
399: }
400:
401: public Object createArg(String name, String value) throws Exception {
402: PropertyDescriptor pd = getPropertyDescriptor(name);
403:
404: if (pd == null) {
405: return null;
406: }
407:
408: if ((value == null) || value.equals("")) {
409: return null;
410: }
411:
412: Class cl = pd.getPropertyType();
413:
414: return ArgHelper.getArgumentInstance(ArgHelper
415: .getArgumentType(cl), value);
416: }
417:
418: /**
419: * setArgStringValue purpose.
420: * <p>
421: * Stores a human friendly version
422: * </p>
423: * @param name
424: * @param value
425: * @return
426: */
427: public boolean setArgValue(String name, Object value) {
428: if ((value == null) || value.equals("")) {
429: args.remove(name);
430:
431: return true;
432: }
433:
434: ArgumentConfig ac = (ArgumentConfig) args.get(name);
435:
436: if (ac == null) {
437: ac = new ArgumentConfig();
438: ac.setName(name);
439: args.put(name, ac);
440: }
441:
442: if (ac.isFinal()) {
443: throw new IllegalArgumentException(
444: "Cannot include final arguments as part of a test.");
445: }
446:
447: ac.setValue(value);
448:
449: return true;
450: }
451:
452: /**
453: * getPropertyDescriptors purpose.
454: * <p>
455: * Get the descriptors for this plugin's map of attributes
456: * </p>
457: * @return
458: */
459: public PropertyDescriptor[] getPropertyDescriptors() {
460: if (pds == null) {
461: PropertyDescriptor[] completeList = plugIn
462: .getPropertyDescriptors();
463: Set these = new HashSet();
464:
465: for (int i = 0; i < completeList.length; i++) {
466: PropertyDescriptor property = completeList[i];
467:
468: if (property.isHidden()) {
469: continue; // only for tool use
470: }
471:
472: if (property.isExpert()) {
473: continue; // limited to plugin definition
474: }
475:
476: if (property.getWriteMethod() == null) {
477: continue; // skip read-only properties
478: }
479:
480: if ("name".equals(property.getName())) {
481: continue; // not handled dynamically
482: }
483:
484: if ("description".equals(property.getName())) {
485: continue; // not handled dynamically
486: }
487:
488: these.add(property);
489: }
490:
491: Object[] ob = these.toArray();
492: pds = new PropertyDescriptor[ob.length];
493:
494: for (int i = 0; i < ob.length; i++)
495: pds[i] = (PropertyDescriptor) ob[i];
496: }
497:
498: return pds;
499: }
500:
501: /**
502: * PropertyDescriptor purpose.
503: * <p>
504: * Get the descriptor for this plugin's attribute named
505: * </p>
506: * @param name
507: * @return
508: */
509: public PropertyDescriptor getPropertyDescriptor(String name) {
510: if (name == null) {
511: throw new NullPointerException(
512: "name must be defined to get a PropertyDescriptor.");
513: }
514:
515: if (pds == null) {
516: pds = getPropertyDescriptors();
517: }
518:
519: for (int i = 0; i < pds.length; i++) {
520: if (name.equals(pds[i].getName())) {
521: return pds[i];
522: }
523: }
524:
525: return null;
526: }
527:
528: /**
529: * Access description property.
530: *
531: * @return Returns the description.
532: */
533: public String getDescription() {
534: return description;
535: }
536:
537: /**
538: * Set description to description.
539: *
540: * @param description The description to set.
541: */
542: public void setDescription(String description) {
543: this .description = description;
544: }
545:
546: /**
547: * Access name property.
548: *
549: * @return Returns the name.
550: */
551: public String getName() {
552: return name;
553: }
554:
555: /**
556: * Set name to name.
557: *
558: * @param name The name to set.
559: */
560: public void setName(String name) {
561: this .name = name;
562: }
563:
564: /**
565: * Access plugIn property.
566: *
567: * @return Returns the plugIn.
568: */
569: public PlugInConfig getPlugIn() {
570: return plugIn;
571: }
572:
573: /**
574: * Set plugIn to plugIn.
575: *
576: * @param plugIn The plugIn to set.
577: */
578: public void setPlugIn(PlugInConfig plugIn) {
579: this.plugIn = plugIn;
580: }
581: }
|