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.xml.ArgHelper;
017: import org.geotools.validation.xml.ValidationException;
018: import java.beans.BeanInfo;
019: import java.beans.Introspector;
020: import java.beans.PropertyDescriptor;
021: import java.io.StringReader;
022: import java.util.HashMap;
023: import java.util.Iterator;
024: import java.util.Map;
025:
026: /**
027: * PlugInConfig purpose.
028: *
029: * <p>
030: * Used to represent a copy of the config information required for the UI.
031: * </p>
032: *
033: * @author dzwiers, Refractions Research, Inc.
034: * @author $Author: dmzwiers $ (last modification)
035: * @version $Id: PlugInConfig.java 6326 2007-03-15 18:36:40Z jdeolive $
036: */
037: public class PlugInConfig {
038: public static final String CONFIG_KEY = "Validation.PlugIn";
039:
040: /** the plug-in name */
041: private String name;
042:
043: /** the plug-in description */
044: private String description;
045:
046: /** the class name this plug-in represents */
047: private String className;
048:
049: /** the default arguments */
050: private Map args;
051:
052: /**
053: * PlugInConfig constructor.
054: *
055: * <p>
056: * Does nothing.
057: * </p>
058: */
059: public PlugInConfig() {
060: args = new HashMap();
061: }
062:
063: /**
064: * PlugInConfig constructor.
065: *
066: * <p>
067: * Creates a copy of the PlugInConfig passed in in this object.
068: * </p>
069: *
070: * @param pi
071: */
072: public PlugInConfig(PlugInConfig pi) {
073: name = pi.getName();
074: description = pi.getDescription();
075: className = pi.getClassName();
076: args = new HashMap();
077:
078: if (pi.getArgs() != null) {
079: Iterator i = pi.getArgs().keySet().iterator();
080:
081: while (i.hasNext()) {
082: String key = (String) i.next();
083:
084: //TODO clone value.
085: args.put(key, new ArgumentConfig((ArgumentConfig) pi
086: .getArgs().get(key)));
087: }
088: }
089: }
090:
091: /**
092: * PlugInConfig constructor.
093: *
094: * <p>
095: * Creates a copy of the PlugInDTO passed in in this object.
096: * </p>
097: *
098: * @param pi
099: */
100: public PlugInConfig(PlugInDTO pi) {
101: name = pi.getName();
102: description = pi.getDescription();
103: className = pi.getClassName();
104: args = new HashMap();
105:
106: if (pi.getArgs() != null) {
107: Iterator i = pi.getArgs().keySet().iterator();
108:
109: while (i.hasNext()) {
110: String key = (String) i.next();
111:
112: //TODO clone value.
113: args.put(key, new ArgumentConfig((ArgumentDTO) pi
114: .getArgs().get(key)));
115: }
116: }
117: }
118:
119: /**
120: * Implementation of clone.
121: *
122: * @return a copy of this class.
123: *
124: * @see java.lang.Object#clone()
125: */
126: public Object clone() {
127: return new PlugInConfig(this );
128: }
129:
130: /**
131: * Implementation of equals.
132: *
133: * @param obj
134: *
135: * @return true when the two objects are equal.
136: *
137: * @see java.lang.Object#equals(java.lang.Object)
138: */
139: public boolean equals(Object obj) {
140: if ((obj == null) || !(obj instanceof PlugInDTO)) {
141: return false;
142: }
143:
144: PlugInDTO pi = (PlugInDTO) obj;
145: boolean r = true;
146:
147: if (name != null) {
148: r = r && (name.equals(pi.getName()));
149: }
150:
151: if (description != null) {
152: r = r && (description.equals(pi.getDescription()));
153: }
154:
155: if (className != null) {
156: r = r && (className.equals(pi.getClassName()));
157: }
158:
159: if (args == null) {
160: if (pi.getArgs() != null) {
161: return false;
162: }
163: } else {
164: if (pi.getArgs() != null) {
165: r = r && args.equals(pi.getArgs());
166: } else {
167: return false;
168: }
169: }
170:
171: return r;
172: }
173:
174: /**
175: * Implementation of hashCode.
176: *
177: * @return the hashcode.
178: *
179: * @see java.lang.Object#hashCode()
180: */
181: public int hashCode() {
182: int i = 1;
183:
184: if (name != null) {
185: i *= name.hashCode();
186: }
187:
188: if (description != null) {
189: i *= description.hashCode();
190: }
191:
192: if (className != null) {
193: i *= className.hashCode();
194: }
195:
196: if (args != null) {
197: i *= args.hashCode();
198: }
199:
200: return i;
201: }
202:
203: /**
204: * toDTO purpose.
205: * <p>
206: * Clones this config as a DTO.
207: * </p>
208: * @see java.lang.Object#clone()
209: * @return PlugInDTO
210: */
211: public PlugInDTO toDTO() {
212: PlugInDTO dto = new PlugInDTO();
213:
214: dto.setName(name);
215: dto.setDescription(description);
216: dto.setClassName(className);
217:
218: Map myArgs = new HashMap();
219:
220: if (this .args != null) {
221: Iterator i = this .args.keySet().iterator();
222:
223: while (i.hasNext()) {
224: String key = (String) i.next();
225:
226: myArgs.put(key, ((ArgumentConfig) this .args.get(key))
227: .toDTO());
228: }
229: }
230:
231: dto.setArgs(myArgs);
232:
233: return dto;
234: }
235:
236: /**
237: * Access args property.
238: *
239: * @return Returns the args.
240: */
241: public Map getArgs() {
242: return args;
243: }
244:
245: /**
246: * Set args to args.
247: *
248: * @param args The args to set.
249: */
250: public void setArgs(Map args) {
251: this .args = args;
252: }
253:
254: /**
255: * getArgStringValue purpose.
256: * <p>
257: * Returns a human friendly version
258: * </p>
259: * @param name
260: * @return
261: */
262: public String getArgStringValue(String name) {
263: ArgumentConfig ac = (ArgumentConfig) args.get(name);
264:
265: if (ac == null) {
266: return null;
267: }
268:
269: return ArgHelper.getArgumentStringEncoding(ac.getValue());
270: }
271:
272: /**
273: * getArgValue purpose.
274: * <p>
275: * Returns an Object version
276: * </p>
277: * @param name
278: * @return
279: */
280: public Object getArgValue(String name) {
281: ArgumentConfig ac = (ArgumentConfig) args.get(name);
282:
283: if (ac == null) {
284: return null;
285: }
286:
287: return ac.getValue();
288: }
289:
290: /**
291: * setArgStringValue purpose.
292: * <p>
293: * Stores a human friendly version
294: * </p>
295: * @param name
296: * @param value
297: * @return
298: */
299: public boolean setArgStringValue(String name, String value) {
300: if ((value == null) || value.equals("")) {
301: args.remove(name);
302:
303: return true;
304: }
305:
306: ArgumentConfig ac = (ArgumentConfig) args.get(name);
307:
308: if (ac == null) {
309: return addArgStringValue(name, value);
310: } else {
311: if (ac.isFinal()) {
312: throw new IllegalArgumentException(
313: "Cannot include final arguments as part of a test.");
314: }
315:
316: StringReader sr = new StringReader(value);
317:
318: try {
319: ac.setValue(ArgHelper.getArgumentInstance(ArgHelper
320: .getArgumentType(ac.getValue()), value));
321:
322: return true;
323: } catch (Exception e) {
324: e.printStackTrace();
325:
326: // error, log it
327: return false;
328: }
329: }
330: }
331:
332: /**
333: * setArgStringValue purpose.
334: * <p>
335: * Stores a human friendly version.
336: * </p>
337: * @param name
338: * @param value
339: * @return
340: */
341: public boolean addArgStringValue(String name, String value) {
342: if ((value == null) || value.equals("")) {
343: args.remove(name);
344:
345: return false;
346: }
347:
348: PropertyDescriptor pd = getPropertyDescriptor(name);
349:
350: if (pd == null) {
351: return false;
352: }
353:
354: Class cl = pd.getPropertyType();
355: ArgumentConfig ac = new ArgumentConfig();
356: ac.setName(name);
357:
358: try {
359: ac.setValue(ArgHelper.getArgumentInstance(ArgHelper
360: .getArgumentType(cl), value));
361: } catch (ValidationException e) {
362: // TODO Auto-generated catch block
363: e.printStackTrace();
364:
365: return false;
366: }
367:
368: args.put(name, ac);
369:
370: return true;
371: }
372:
373: public Object createArg(String name, String value) throws Exception {
374: PropertyDescriptor pd = getPropertyDescriptor(name);
375:
376: if (pd == null) {
377: return null;
378: }
379:
380: if ((value == null) || value.equals("")) {
381: return null;
382: }
383:
384: Class cl = pd.getPropertyType();
385:
386: return ArgHelper.getArgumentInstance(ArgHelper
387: .getArgumentType(cl), value);
388: }
389:
390: /**
391: * setArgStringValue purpose.
392: * <p>
393: * Stores a human friendly version
394: * </p>
395: * @param name
396: * @param value
397: * @return
398: */
399: public boolean setArgValue(String name, Object value) {
400: if ((value == null) || value.equals("")) {
401: args.remove(name);
402:
403: return true;
404: }
405:
406: ArgumentConfig ac = (ArgumentConfig) args.get(name);
407:
408: if (ac == null) {
409: ac = new ArgumentConfig();
410: ac.setName(name);
411: args.put(name, ac);
412: }
413:
414: if (ac.isFinal()) {
415: throw new IllegalArgumentException(
416: "Cannot include final arguments as part of a test.");
417: }
418:
419: ac.setValue(value);
420:
421: return true;
422: }
423:
424: /**
425: * getPropertyDescriptors purpose.
426: * <p>
427: * Get the descriptors for this plugin's map of attributes
428: * </p>
429: * @return
430: */
431: public PropertyDescriptor[] getPropertyDescriptors() {
432: try {
433: Class plugIn = this .getClass().getClassLoader().loadClass(
434: className);
435: BeanInfo bi = Introspector.getBeanInfo(plugIn);
436:
437: return bi.getPropertyDescriptors();
438: } catch (Exception e) {
439: e.printStackTrace();
440:
441: return null;
442: }
443: }
444:
445: /**
446: * PropertyDescriptor purpose.
447: * <p>
448: * Get the descriptor for this plugin's attribute named
449: * </p>
450: * @param name
451: * @return
452: */
453: public PropertyDescriptor getPropertyDescriptor(String name) {
454: if (name == null) {
455: throw new NullPointerException(
456: "name must be defined to get a PropertyDescriptor.");
457: }
458:
459: PropertyDescriptor[] pds = getPropertyDescriptors();
460:
461: for (int i = 0; i < pds.length; i++) {
462: if (name.equals(pds[i].getName())) {
463: return pds[i];
464: }
465: }
466:
467: return null;
468: }
469:
470: /**
471: * Access className property.
472: *
473: * @return Returns the className.
474: */
475: public String getClassName() {
476: return className;
477: }
478:
479: /**
480: * Set className to className.
481: *
482: * @param className The className to set.
483: */
484: public void setClassName(String className) {
485: this .className = className;
486: }
487:
488: /**
489: * Access description property.
490: *
491: * @return Returns the description.
492: */
493: public String getDescription() {
494: return description;
495: }
496:
497: /**
498: * Set description to description.
499: *
500: * @param description The description to set.
501: */
502: public void setDescription(String description) {
503: this .description = description;
504: }
505:
506: /**
507: * Access name property.
508: *
509: * @return Returns the name.
510: */
511: public String getName() {
512: return name;
513: }
514:
515: /**
516: * Set name to name.
517: *
518: * @param name The name to set.
519: */
520: public void setName(String name) {
521: this.name = name;
522: }
523: }
|