0001: /*
0002: * GeoTools - OpenSource mapping toolkit
0003: * http://geotools.org
0004: * (C) 2004-2006, Geotools Project Managment Committee (PMC)
0005: * (C) 2004, TOPP - www.openplans.org
0006: *
0007: * This library is free software; you can redistribute it and/or
0008: * modify it under the terms of the GNU Lesser General Public
0009: * License as published by the Free Software Foundation; either
0010: * version 2.1 of the License, or (at your option) any later version.
0011: *
0012: * This library is distributed in the hope that it will be useful,
0013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
0014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
0015: * Lesser General Public License for more details.
0016: */
0017: package org.geotools.validation.xml;
0018:
0019: import java.io.IOException;
0020: import java.io.StringReader;
0021: import java.io.StringWriter;
0022: import java.net.URI;
0023: import java.net.URISyntaxException;
0024: import java.text.ParseException;
0025: import java.text.SimpleDateFormat;
0026: import java.util.Date;
0027:
0028: import javax.xml.parsers.ParserConfigurationException;
0029: import javax.xml.transform.TransformerException;
0030:
0031: import org.geotools.filter.ExpressionDOMParser;
0032: import org.geotools.filter.Filter;
0033: import org.geotools.filter.FilterDOMParser;
0034: import org.geotools.filter.FilterTransformer;
0035: import org.geotools.gml.producer.GeometryTransformer;
0036: import org.w3c.dom.Element;
0037: import org.xml.sax.SAXException;
0038:
0039: import com.vividsolutions.jts.geom.Envelope;
0040: import com.vividsolutions.jts.geom.Geometry;
0041:
0042: /**
0043: * ArgHelper purpose.
0044: *
0045: * @author dzwiers, Refractions Research, Inc.
0046: * @author $Author: dmzwiers $ (last modification)
0047: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/extension/validation/src/main/java/org/geotools/validation/xml/ArgHelper.java $
0048: * @version $Id: ArgHelper.java 22266 2006-10-19 11:30:55Z acuster $
0049: */
0050: public class ArgHelper {
0051: private static final Mapping[] argumentTypeMappings = {
0052: new FilterMapping(), new GeometryMapping(),
0053: new EnvelopeMapping(), new ShortMapping(),
0054: new IntegerMapping(), new LongMapping(),
0055: new FloatMapping(), new DoubleMapping(), new DateMapping(),
0056: new URIMapping(), new BooleanMapping(), new StringMapping() };
0057:
0058: /**
0059: * getArgumentInstance purpose.
0060: *
0061: * <p>
0062: * Returns an instance for the specified argument type from the Element
0063: * provided.
0064: * </p>
0065: *
0066: * @param elementName String the argument element name (type name).
0067: * @param value Element the element to create the Argument from.
0068: *
0069: * @return The Specified argument in Object form.
0070: *
0071: * @throws ValidationException DOCUMENT ME!
0072: * @throws NullPointerException DOCUMENT ME!
0073: */
0074: public static Object getArgumentInstance(String elementName,
0075: Element value) throws ValidationException {
0076: if (elementName == null) {
0077: throw new NullPointerException(
0078: "A Typename must be specified.");
0079: }
0080:
0081: for (int i = 0; i < argumentTypeMappings.length; i++) {
0082: if (elementName.equals(argumentTypeMappings[i]
0083: .getElementName())) {
0084: return argumentTypeMappings[i].getInstance(value);
0085: }
0086: }
0087:
0088: return null;
0089: }
0090:
0091: /**
0092: * getArgumentInstance purpose.
0093: *
0094: * <p>
0095: * Returns an instance for the specified argument type from the Element
0096: * provided.
0097: * </p>
0098: *
0099: * @param elementName String the argument element name (type name).
0100: * @param value String the element to create the Argument from.
0101: *
0102: * @return The Specified argument in Object form.
0103: *
0104: * @throws ValidationException DOCUMENT ME!
0105: * @throws NullPointerException DOCUMENT ME!
0106: */
0107: public static Object getArgumentInstance(String elementName,
0108: String value) throws ValidationException {
0109: if (elementName == null) {
0110: throw new NullPointerException(
0111: "A Typename must be specified.");
0112: }
0113:
0114: for (int i = 0; i < argumentTypeMappings.length; i++) {
0115: if (elementName.equals(argumentTypeMappings[i].getType())) {
0116: return argumentTypeMappings[i].getInstance(value);
0117: }
0118: }
0119:
0120: return null;
0121: }
0122:
0123: /**
0124: * getArgumentType purpose.
0125: *
0126: * <p>
0127: * Finds the appropriate argument type if one exists.
0128: * </p>
0129: *
0130: * @param o The Object to search for it's type.
0131: *
0132: * @return The Object type or "" if not found.
0133: *
0134: * @throws NullPointerException DOCUMENT ME!
0135: */
0136: public static String getArgumentType(Object o) {
0137: if (o == null) {
0138: throw new NullPointerException(
0139: "An argument instance must be specified.");
0140: }
0141:
0142: if (o instanceof Class) {
0143: for (int i = 0; i < argumentTypeMappings.length; i++)
0144: if (argumentTypeMappings[i].isClass((Class) o)) {
0145: return argumentTypeMappings[i].getType();
0146: }
0147: } else {
0148: for (int i = 0; i < argumentTypeMappings.length; i++)
0149: if (argumentTypeMappings[i].isClassInstance(o)) {
0150: return argumentTypeMappings[i].getType();
0151: }
0152: }
0153:
0154: return "";
0155: }
0156:
0157: /**
0158: * getArgumentEncoding purpose.
0159: *
0160: * <p>
0161: * Creates an XML encodeing of the Object if it is a known argument type.
0162: * </p>
0163: *
0164: * @param o Object the object to attempt to encode.
0165: *
0166: * @return an XML string if it is a known type, "" otherwise.
0167: *
0168: * @throws ValidationException DOCUMENT ME!
0169: * @throws NullPointerException DOCUMENT ME!
0170: */
0171: public static String getArgumentEncoding(Object o)
0172: throws ValidationException {
0173: if (o == null) {
0174: throw new NullPointerException(
0175: "An argument instance must be specified.");
0176: }
0177:
0178: for (int i = 0; i < argumentTypeMappings.length; i++)
0179: if (argumentTypeMappings[i].isClassInstance(o)) {
0180: return argumentTypeMappings[i].encode(o);
0181: }
0182:
0183: return "";
0184: }
0185:
0186: public static String getArgumentStringEncoding(Object o) {
0187: if (o == null) {
0188: throw new NullPointerException(
0189: "An argument instance must be specified.");
0190: }
0191:
0192: for (int i = 0; i < argumentTypeMappings.length; i++)
0193: if (argumentTypeMappings[i].isClassInstance(o)) {
0194: return argumentTypeMappings[i].toString(o);
0195: }
0196:
0197: return "";
0198: }
0199:
0200: /**
0201: * Mapping purpose.
0202: *
0203: * <p>
0204: * Used to mask attribute specific fucntions from the user.
0205: * </p>
0206: *
0207: * @author dzwiers, Refractions Research, Inc.
0208: * @author $Author: dmzwiers $ (last modification)
0209: * @version $Id: ArgHelper.java 22266 2006-10-19 11:30:55Z acuster $
0210: */
0211: protected interface Mapping {
0212: /**
0213: * getType purpose.
0214: *
0215: * <p>
0216: * Returns a constant type name.
0217: * </p>
0218: *
0219: * @return String a constant type name.
0220: */
0221: public abstract String getType();
0222:
0223: /**
0224: * getInstance purpose.
0225: *
0226: * <p>
0227: * Creates an instance of the appropriate type for this Mapping. This
0228: * is where type-dependant magic occurs
0229: * </p>
0230: *
0231: * @param value The Element to interpret.
0232: *
0233: * @return The particular argument type expected.
0234: */
0235: public abstract Object getInstance(Element value)
0236: throws ValidationException;
0237:
0238: /**
0239: * getInstance purpose.
0240: *
0241: * <p>
0242: * Creates an instance of the appropriate type for this Mapping. This
0243: * is where type-dependant magic occurs
0244: * </p>
0245: *
0246: * @param value The Element to interpret.
0247: *
0248: * @return The particular argument type expected.
0249: */
0250: public abstract Object getInstance(String value)
0251: throws ValidationException;
0252:
0253: /**
0254: * isClassInstance purpose.
0255: *
0256: * <p>
0257: * Tests to see if this class is of the expected type.
0258: * </p>
0259: *
0260: * @param obj The object to test.
0261: *
0262: * @return true when they are compatible
0263: */
0264: public abstract boolean isClassInstance(Object obj);
0265:
0266: public abstract boolean isClass(Class c);
0267:
0268: /**
0269: * encode purpose.
0270: *
0271: * <p>
0272: * Creates an XML String from the obj provided, if the object is of
0273: * the expected type for this mapping.
0274: * </p>
0275: *
0276: * @param obj The object to try and encode.
0277: *
0278: * @return An XML String if the type is correct, ClassCastException
0279: * otherwise.
0280: */
0281: public abstract String encode(Object obj)
0282: throws ValidationException;
0283:
0284: public abstract String toString(Object obj);
0285:
0286: /**
0287: * getElementName purpose.
0288: *
0289: * <p>
0290: * This is the name of the element represented.
0291: * </p>
0292: *
0293: */
0294: public abstract String getElementName();
0295: }
0296:
0297: /**
0298: * FilterMapping purpose.
0299: *
0300: * <p>
0301: * Represents the specifics for the Filter Argument type
0302: * </p>
0303: *
0304: * @author dzwiers, Refractions Research, Inc.
0305: * @author $Author: dmzwiers $ (last modification)
0306: * @version $Id: ArgHelper.java 22266 2006-10-19 11:30:55Z acuster $
0307: *
0308: * @see Mapping
0309: */
0310: protected static class FilterMapping implements Mapping {
0311: /**
0312: * Implementation of getType.
0313: *
0314: * @return the type name
0315: *
0316: * @see org.geotools.validation.xml.ArgHelper.Mapping#getType()
0317: */
0318: public String getType() {
0319: return "ogc:FilterType";
0320: }
0321:
0322: /**
0323: * Implementation of getElementName.
0324: *
0325: * @return the element name
0326: *
0327: * @see org.geotools.validation.xml.ArgHelper.Mapping#getElementName()
0328: */
0329: public String getElementName() {
0330: return "filter";
0331: }
0332:
0333: /**
0334: * Implementation of getInstance.
0335: *
0336: * @param value Element the element to parse into an instance of type
0337: * Filter
0338: *
0339: * @return Filter the filter instance if posible, null otherwise.
0340: *
0341: * @see org.geotools.validation.xml.ArgHelper.Mapping#getInstance(org.w3c.dom.Element)
0342: */
0343: public Object getInstance(Element value) {
0344: // value must be the node for "ogc:Filter"
0345: if ((value != null)
0346: && ((value = ReaderUtils
0347: .getFirstChildElement(value)) != null)) {
0348: return FilterDOMParser.parseFilter(value);
0349: }
0350:
0351: return null;
0352: }
0353:
0354: public Object getInstance(String elem) {
0355: Element value;
0356:
0357: try {
0358: StringReader sr = new StringReader(elem);
0359: value = ReaderUtils.loadConfig(sr);
0360: sr.close();
0361: } catch (IOException e) {
0362: // TODO Auto-generated catch block
0363: e.printStackTrace();
0364:
0365: return null;
0366: } catch (ParserConfigurationException e) {
0367: // TODO Auto-generated catch block
0368: e.printStackTrace();
0369:
0370: return null;
0371: } catch (SAXException e) {
0372: // TODO Auto-generated catch block
0373: e.printStackTrace();
0374:
0375: return null;
0376: }
0377:
0378: // value must be the node for "ogc:Filter"
0379: if ((value != null)
0380: && ((value = ReaderUtils
0381: .getFirstChildElement(value)) != null)) {
0382: return FilterDOMParser.parseFilter(value);
0383: }
0384:
0385: return null;
0386: }
0387:
0388: /**
0389: * Implementation of isClassInstance.
0390: *
0391: * @param c The Object to test
0392: *
0393: * @return true when both of type Filter
0394: *
0395: * @see org.geotools.validation.xml.ArgHelper.Mapping#isClassInstance(java.lang.Object)
0396: */
0397: public boolean isClassInstance(Object c) {
0398: return ((c != null) && c instanceof Filter);
0399: }
0400:
0401: public boolean isClass(Class c) {
0402: return ((c != null) && Filter.class.equals(c));
0403: }
0404:
0405: /**
0406: * Implementation of encode.
0407: *
0408: * @param obj An object to encode as a filter.
0409: *
0410: * @return String the XML encoding
0411: *
0412: * @throws ValidationException DOCUMENT ME!
0413: * @throws NullPointerException DOCUMENT ME!
0414: * @throws ClassCastException when obj is not of type Filter
0415: *
0416: * @see org.geotools.validation.xml.ArgHelper.Mapping#encode(java.lang.Object)
0417: */
0418: public String encode(Object obj) throws ValidationException {
0419: Filter f = null;
0420:
0421: if (obj == null) {
0422: throw new NullPointerException(
0423: "Cannot encode a null Filter.");
0424: }
0425:
0426: if (!(obj instanceof Filter)) {
0427: throw new ClassCastException("Cannot cast "
0428: + obj.getClass().toString() + " to a Filter.");
0429: }
0430:
0431: f = (Filter) obj;
0432:
0433: StringWriter sw = new StringWriter();
0434:
0435: try {
0436: sw.write((new FilterTransformer()).transform(f));
0437: } catch (TransformerException e) {
0438: throw new ValidationException(e);
0439: }
0440:
0441: return "<filter>\n" + sw.toString() + "</filter>\n";
0442: }
0443:
0444: public String toString(Object obj) {
0445: Filter f = null;
0446:
0447: if (obj == null) {
0448: throw new NullPointerException(
0449: "Cannot encode a null Filter.");
0450: }
0451:
0452: if (!(obj instanceof Filter)) {
0453: throw new ClassCastException("Cannot cast "
0454: + obj.getClass().toString() + " to a Filter.");
0455: }
0456:
0457: f = (Filter) obj;
0458:
0459: StringWriter sw = new StringWriter();
0460:
0461: try {
0462: sw.write((new FilterTransformer()).transform(f));
0463: } catch (TransformerException e) {
0464: return null;
0465: }
0466:
0467: return "<filter>\n" + sw.toString() + "</filter>\n";
0468: }
0469: }
0470:
0471: /**
0472: * GeometryMapping purpose.
0473: *
0474: * <p>
0475: * Represents the workings for a Geometry Mapping
0476: * </p>
0477: *
0478: * @author dzwiers, Refractions Research, Inc.
0479: * @author $Author: dmzwiers $ (last modification)
0480: * @version $Id: ArgHelper.java 22266 2006-10-19 11:30:55Z acuster $
0481: */
0482: protected static class GeometryMapping implements Mapping {
0483: /**
0484: * Implementation of getType.
0485: *
0486: * @return the type name
0487: *
0488: * @see org.geotools.validation.xml.ArgHelper.Mapping#getType()
0489: */
0490: public String getType() {
0491: return "gml:AbstractGeometryType";
0492: }
0493:
0494: /**
0495: * Implementation of getElementName.
0496: *
0497: * @return the element name
0498: *
0499: * @see org.geotools.validation.xml.ArgHelper.Mapping#getElementName()
0500: */
0501: public String getElementName() {
0502: return "geometry";
0503: }
0504:
0505: /**
0506: * Implementation of getInstance.
0507: *
0508: * @param value Element the element to parse into a Geometry.
0509: *
0510: * @return Geometry an instance of Geometry if one can be created, null
0511: * otherwise.
0512: *
0513: * @see org.geotools.validation.xml.ArgHelper.Mapping#getInstance(org.w3c.dom.Element)
0514: */
0515: public Object getInstance(Element value) {
0516: return ExpressionDOMParser.parseGML(value);
0517: }
0518:
0519: public Object getInstance(String value) {
0520: Element elem;
0521:
0522: try {
0523: StringReader sr = new StringReader(value);
0524: elem = ReaderUtils.loadConfig(sr);
0525: sr.close();
0526: } catch (IOException e) {
0527: // TODO Auto-generated catch block
0528: e.printStackTrace();
0529:
0530: return null;
0531: } catch (ParserConfigurationException e) {
0532: // TODO Auto-generated catch block
0533: e.printStackTrace();
0534:
0535: return null;
0536: } catch (SAXException e) {
0537: // TODO Auto-generated catch block
0538: e.printStackTrace();
0539:
0540: return null;
0541: }
0542:
0543: return ExpressionDOMParser.parseGML(elem);
0544: }
0545:
0546: /**
0547: * Implementation of isClassInstance.
0548: *
0549: * @param c The Object to test
0550: *
0551: * @return true when both of type Geometry
0552: *
0553: * @see org.geotools.validation.xml.ArgHelper.Mapping#isClassInstance(java.lang.Object)
0554: */
0555: public boolean isClassInstance(Object c) {
0556: return ((c != null) && c instanceof Geometry);
0557: }
0558:
0559: public boolean isClass(Class c) {
0560: return ((c != null) && Geometry.class.equals(c));
0561: }
0562:
0563: /**
0564: * Implementation of encode.
0565: *
0566: * @param obj An object to encode as a geometry.
0567: *
0568: * @return String the XML encoding
0569: *
0570: * @throws ValidationException when obj is not of type geometry
0571: *
0572: * @see org.geotools.validation.xml.ArgHelper.Mapping#encode(java.lang.Object)
0573: */
0574: public String encode(Object obj) throws ValidationException {
0575: StringWriter sw = new StringWriter();
0576: GeometryTransformer transformer = new GeometryTransformer();
0577:
0578: try {
0579: transformer.transform(obj, sw);
0580: } catch (TransformerException e) {
0581: throw new ValidationException(e);
0582: }
0583:
0584: return "<geometry>\n" + sw.toString() + "</geometry>\n";
0585: }
0586:
0587: public String toString(Object obj) {
0588: StringWriter sw = new StringWriter();
0589: GeometryTransformer transformer = new GeometryTransformer();
0590:
0591: try {
0592: transformer.transform(obj, sw);
0593: } catch (TransformerException e) {
0594: return null;
0595: }
0596:
0597: return "<geometry>\n" + sw.toString() + "</geometry>\n";
0598: }
0599: }
0600:
0601: /**
0602: * EnvelopeMapping purpose.
0603: *
0604: * <p>
0605: * Represents the workings for a Envelope Mapping
0606: * </p>
0607: *
0608: * @author dzwiers, Refractions Research, Inc.
0609: * @author $Author: dmzwiers $ (last modification)
0610: * @version $Id: ArgHelper.java 22266 2006-10-19 11:30:55Z acuster $
0611: */
0612: protected static class EnvelopeMapping implements Mapping {
0613: /**
0614: * Implementation of getType.
0615: *
0616: * @return the type name
0617: *
0618: * @see org.geotools.validation.xml.ArgHelper.Mapping#getType()
0619: */
0620: public String getType() {
0621: return "ogc:BBOXType";
0622: }
0623:
0624: /**
0625: * Implementation of getElementName.
0626: *
0627: * @return the element name
0628: *
0629: * @see org.geotools.validation.xml.ArgHelper.Mapping#getElementName()
0630: */
0631: public String getElementName() {
0632: return "bbox";
0633: }
0634:
0635: /**
0636: * Implementation of getInstance.
0637: *
0638: * @param bboxElem Element the element to parse into a Envelope.
0639: *
0640: * @return Geometry an instance of Envelope if one can be created, null
0641: * otherwise.
0642: *
0643: * @throws ValidationException DOCUMENT ME!
0644: * @throws NullPointerException DOCUMENT ME!
0645: *
0646: * @see org.geotools.validation.xml.ArgHelper.Mapping#getInstance(org.w3c.dom.Element)
0647: */
0648: public Object getInstance(Element bboxElem)
0649: throws ValidationException {
0650: if (bboxElem == null) {
0651: throw new NullPointerException(
0652: "The bounding Box element passed in was null");
0653: }
0654:
0655: try {
0656: boolean dynamic = ReaderUtils.getBooleanAttribute(
0657: bboxElem, "dynamic", false);
0658:
0659: if (!dynamic) {
0660: double minx = ReaderUtils.getDoubleAttribute(
0661: bboxElem, "minx", true);
0662: double miny = ReaderUtils.getDoubleAttribute(
0663: bboxElem, "miny", true);
0664: double maxx = ReaderUtils.getDoubleAttribute(
0665: bboxElem, "maxx", true);
0666: double maxy = ReaderUtils.getDoubleAttribute(
0667: bboxElem, "maxy", true);
0668:
0669: return new Envelope(minx, maxx, miny, maxy);
0670: }
0671: } catch (SAXException e) {
0672: throw new ValidationException(e);
0673: }
0674:
0675: return null;
0676: }
0677:
0678: public Object getInstance(String bbox)
0679: throws ValidationException {
0680: if (bbox == null) {
0681: throw new NullPointerException(
0682: "The bounding Box element passed in was null");
0683: }
0684:
0685: try {
0686: String[] tmp = bbox.split(",");
0687: double minx = Double.parseDouble(tmp[0].trim());
0688: double maxx = Double.parseDouble(tmp[1].trim());
0689: double miny = Double.parseDouble(tmp[2].trim());
0690: double maxy = Double.parseDouble(tmp[3].trim());
0691:
0692: return new Envelope(minx, maxx, miny, maxy);
0693: } catch (Exception e) {
0694: throw new ValidationException(e);
0695: }
0696: }
0697:
0698: /**
0699: * Implementation of isClassInstance.
0700: *
0701: * @param c The Object to test
0702: *
0703: * @return true when both of type Envelope
0704: *
0705: * @see org.geotools.validation.xml.ArgHelper.Mapping#isClassInstance(java.lang.Object)
0706: */
0707: public boolean isClassInstance(Object c) {
0708: return ((c != null) && c instanceof Envelope);
0709: }
0710:
0711: public boolean isClass(Class c) {
0712: return ((c != null) && Envelope.class.equals(c));
0713: }
0714:
0715: /**
0716: * Implementation of encode.
0717: *
0718: * @param obj An object to encode as a Envelope.
0719: *
0720: * @return String the XML encoding
0721: *
0722: * @throws NullPointerException DOCUMENT ME!
0723: * @throws ClassCastException when obj is not of type Envelope
0724: *
0725: * @see org.geotools.validation.xml.ArgHelper.Mapping#encode(java.lang.Object)
0726: */
0727: public String encode(Object obj) {
0728: if (obj == null) {
0729: throw new NullPointerException(
0730: "The bounding Box obj passed in was null");
0731: }
0732:
0733: if (!(obj instanceof Envelope)) {
0734: throw new ClassCastException(
0735: "Object of type Envelope was expected.");
0736: }
0737:
0738: String s = "";
0739: Envelope e = (Envelope) obj;
0740: s += "<bbox ";
0741:
0742: if (!e.isNull()) {
0743: s += "dynamic = \"false\" ";
0744: s += ("minx = \"" + e.getMinX() + "\" ");
0745: s += ("miny = \"" + e.getMinY() + "\" ");
0746: s += ("maxx = \"" + e.getMaxX() + "\" ");
0747: s += ("maxy = \"" + e.getMaxY() + "\" ");
0748: } else {
0749: s += "dynamic = \"true\" ";
0750: }
0751:
0752: s += " />\n";
0753:
0754: return s;
0755: }
0756:
0757: public String toString(Object obj) {
0758: if (obj == null) {
0759: throw new NullPointerException(
0760: "The bounding Box obj passed in was null");
0761: }
0762:
0763: if (!(obj instanceof Envelope)) {
0764: throw new ClassCastException(
0765: "Object of type Envelope was expected.");
0766: }
0767:
0768: String s = "";
0769: Envelope e = (Envelope) obj;
0770:
0771: if (!e.isNull()) {
0772: s += e.getMinX();
0773: s += ("," + e.getMaxX());
0774: s += ("," + e.getMinY());
0775: s += ("," + e.getMaxY());
0776: }
0777:
0778: return s;
0779: }
0780: }
0781:
0782: /**
0783: * ShortMapping purpose.
0784: *
0785: * <p>
0786: * Represents the workings for a Short Mapping
0787: * </p>
0788: *
0789: * @author dzwiers, Refractions Research, Inc.
0790: * @author $Author: dmzwiers $ (last modification)
0791: * @version $Id: ArgHelper.java 22266 2006-10-19 11:30:55Z acuster $
0792: */
0793: protected static class ShortMapping implements Mapping {
0794: /**
0795: * Implementation of getType.
0796: *
0797: * @return the type name
0798: *
0799: * @see org.geotools.validation.xml.ArgHelper.Mapping#getType()
0800: */
0801: public String getType() {
0802: return "xs:short";
0803: }
0804:
0805: /**
0806: * Implementation of getElementName.
0807: *
0808: * @return the element name
0809: *
0810: * @see org.geotools.validation.xml.ArgHelper.Mapping#getElementName()
0811: */
0812: public String getElementName() {
0813: return "short";
0814: }
0815:
0816: /**
0817: * Implementation of getInstance.
0818: *
0819: * @param elem Element the element to parse into a Short.
0820: *
0821: * @return Geometry an instance of Short if one can be created, null
0822: * otherwise.
0823: *
0824: * @throws NullPointerException DOCUMENT ME!
0825: *
0826: * @see org.geotools.validation.xml.ArgHelper.Mapping#getInstance(org.w3c.dom.Element)
0827: */
0828: public Object getInstance(Element elem) {
0829: if (elem == null) {
0830: throw new NullPointerException(
0831: "The short element passed in was null");
0832: }
0833:
0834: return new Short(ReaderUtils.getElementText(elem));
0835: }
0836:
0837: public Object getInstance(String value) {
0838: if (value == null) {
0839: throw new NullPointerException(
0840: "The short element passed in was null");
0841: }
0842:
0843: return new Short(value);
0844: }
0845:
0846: /**
0847: * Implementation of isClassInstance.
0848: *
0849: * @param c The Object to test
0850: *
0851: * @return true when both of type Short
0852: *
0853: * @see org.geotools.validation.xml.ArgHelper.Mapping#isClassInstance(java.lang.Object)
0854: */
0855: public boolean isClassInstance(Object c) {
0856: return ((c != null) && c instanceof Short);
0857: }
0858:
0859: public boolean isClass(Class c) {
0860: return ((c != null) && (Short.class.equals(c) || short.class
0861: .equals(c)));
0862: }
0863:
0864: /**
0865: * Implementation of encode.
0866: *
0867: * @param obj An object to encode as a short.
0868: *
0869: * @return String the XML encoding
0870: *
0871: * @throws NullPointerException DOCUMENT ME!
0872: * @throws ClassCastException when obj is not of type short
0873: *
0874: * @see org.geotools.validation.xml.ArgHelper.Mapping#encode(java.lang.Object)
0875: */
0876: public String encode(Object obj) {
0877: if (obj == null) {
0878: throw new NullPointerException(
0879: "The short obj passed in was null");
0880: }
0881:
0882: if (!(obj instanceof Short)) {
0883: throw new ClassCastException(
0884: "Object of type Short was expected.");
0885: }
0886:
0887: return "<short>" + ((Short) obj).toString() + "</short>\n";
0888: }
0889:
0890: public String toString(Object o) {
0891: return o.toString();
0892: }
0893: }
0894:
0895: /**
0896: * IntegerMapping purpose.
0897: *
0898: * <p>
0899: * Represents the workings for a Integer Mapping
0900: * </p>
0901: *
0902: * @author dzwiers, Refractions Research, Inc.
0903: * @author $Author: dmzwiers $ (last modification)
0904: * @version $Id: ArgHelper.java 22266 2006-10-19 11:30:55Z acuster $
0905: */
0906: protected static class IntegerMapping implements Mapping {
0907: /**
0908: * Implementation of getType.
0909: *
0910: * @return the type name
0911: *
0912: * @see org.geotools.validation.xml.ArgHelper.Mapping#getType()
0913: */
0914: public String getType() {
0915: return "xs:integer";
0916: }
0917:
0918: /**
0919: * Implementation of getElementName.
0920: *
0921: * @return the element name
0922: *
0923: * @see org.geotools.validation.xml.ArgHelper.Mapping#getElementName()
0924: */
0925: public String getElementName() {
0926: return "integer";
0927: }
0928:
0929: /**
0930: * Implementation of getInstance.
0931: *
0932: * @param elem Element the element to parse into a Integer.
0933: *
0934: * @return Geometry an instance of Integer if one can be created, null
0935: * otherwise.
0936: *
0937: * @throws NullPointerException DOCUMENT ME!
0938: *
0939: * @see org.geotools.validation.xml.ArgHelper.Mapping#getInstance(org.w3c.dom.Element)
0940: */
0941: public Object getInstance(Element elem) {
0942: if (elem == null) {
0943: throw new NullPointerException(
0944: "The integer passed in was null");
0945: }
0946:
0947: return new Integer(ReaderUtils.getElementText(elem));
0948: }
0949:
0950: public Object getInstance(String value) {
0951: if (value == null) {
0952: throw new NullPointerException(
0953: "The integer passed in was null");
0954: }
0955:
0956: return new Integer(value);
0957: }
0958:
0959: /**
0960: * Implementation of isClassInstance.
0961: *
0962: * @param c The Object to test
0963: *
0964: * @return true when both of type Integer
0965: *
0966: * @see org.geotools.validation.xml.ArgHelper.Mapping#isClassInstance(java.lang.Object)
0967: */
0968: public boolean isClassInstance(Object c) {
0969: return ((c != null) && c instanceof Integer);
0970: }
0971:
0972: public boolean isClass(Class c) {
0973: return ((c != null) && (Integer.class.equals(c) || int.class
0974: .equals(c)));
0975: }
0976:
0977: /**
0978: * Implementation of encode.
0979: *
0980: * @param obj An object to encode as a Integer.
0981: *
0982: * @return String the XML encoding
0983: *
0984: * @throws NullPointerException DOCUMENT ME!
0985: * @throws ClassCastException when obj is not of type Integer
0986: *
0987: * @see org.geotools.validation.xml.ArgHelper.Mapping#encode(java.lang.Object)
0988: */
0989: public String encode(Object obj) {
0990: if (obj == null) {
0991: throw new NullPointerException(
0992: "The integer obj passed in was null");
0993: }
0994:
0995: if (!(obj instanceof Integer)) {
0996: throw new ClassCastException(
0997: "Object of type Integer was expected.");
0998: }
0999:
1000: return "<integer>" + ((Integer) obj).toString()
1001: + "</integer>\n";
1002: }
1003:
1004: public String toString(Object o) {
1005: return o.toString();
1006: }
1007: }
1008:
1009: /**
1010: * LongMapping purpose.
1011: *
1012: * <p>
1013: * Represents the workings for a Long Mapping
1014: * </p>
1015: *
1016: * @author dzwiers, Refractions Research, Inc.
1017: * @author $Author: dmzwiers $ (last modification)
1018: * @version $Id: ArgHelper.java 22266 2006-10-19 11:30:55Z acuster $
1019: */
1020: protected static class LongMapping implements Mapping {
1021: /**
1022: * Implementation of getType.
1023: *
1024: * @return the type name
1025: *
1026: * @see org.geotools.validation.xml.ArgHelper.Mapping#getType()
1027: */
1028: public String getType() {
1029: return "xs:long";
1030: }
1031:
1032: /**
1033: * Implementation of getElementName.
1034: *
1035: * @return the element name
1036: *
1037: * @see org.geotools.validation.xml.ArgHelper.Mapping#getElementName()
1038: */
1039: public String getElementName() {
1040: return "long";
1041: }
1042:
1043: /**
1044: * Implementation of getInstance.
1045: *
1046: * @param elem Element the element to parse into a Long.
1047: *
1048: * @return Geometry an instance of Long if one can be created, null
1049: * otherwise.
1050: *
1051: * @throws NullPointerException DOCUMENT ME!
1052: *
1053: * @see org.geotools.validation.xml.ArgHelper.Mapping#getInstance(org.w3c.dom.Element)
1054: */
1055: public Object getInstance(Element elem) {
1056: if (elem == null) {
1057: throw new NullPointerException(
1058: "The long passed in was null");
1059: }
1060:
1061: return new Long(ReaderUtils.getElementText(elem));
1062: }
1063:
1064: public Object getInstance(String value) {
1065: if (value == null) {
1066: throw new NullPointerException(
1067: "The long passed in was null");
1068: }
1069:
1070: return new Long(value);
1071: }
1072:
1073: /**
1074: * Implementation of isClassInstance.
1075: *
1076: * @param c The Object to test
1077: *
1078: * @return true when both of type Long
1079: *
1080: * @see org.geotools.validation.xml.ArgHelper.Mapping#isClassInstance(java.lang.Object)
1081: */
1082: public boolean isClassInstance(Object c) {
1083: return ((c != null) && c instanceof Long);
1084: }
1085:
1086: public boolean isClass(Class c) {
1087: return ((c != null) && (Long.class.equals(c) || long.class
1088: .equals(c)));
1089: }
1090:
1091: /**
1092: * Implementation of encode.
1093: *
1094: * @param obj An object to encode as a Integer.
1095: *
1096: * @return String the XML encoding
1097: *
1098: * @throws NullPointerException DOCUMENT ME!
1099: * @throws ClassCastException when obj is not of type Integer
1100: *
1101: * @see org.geotools.validation.xml.ArgHelper.Mapping#encode(java.lang.Object)
1102: */
1103: public String encode(Object obj) {
1104: if (obj == null) {
1105: throw new NullPointerException(
1106: "The long obj passed in was null");
1107: }
1108:
1109: if (!(obj instanceof Long)) {
1110: throw new ClassCastException(
1111: "Object of type Long was expected.");
1112: }
1113:
1114: return "<long>" + ((Long) obj).toString() + "</long>\n";
1115: }
1116:
1117: public String toString(Object o) {
1118: return o.toString();
1119: }
1120: }
1121:
1122: /**
1123: * FloatMapping purpose.
1124: *
1125: * <p>
1126: * Represents the workings for a Float Mapping
1127: * </p>
1128: *
1129: * @author dzwiers, Refractions Research, Inc.
1130: * @author $Author: dmzwiers $ (last modification)
1131: * @version $Id: ArgHelper.java 22266 2006-10-19 11:30:55Z acuster $
1132: */
1133: protected static class FloatMapping implements Mapping {
1134: /**
1135: * Implementation of getType.
1136: *
1137: * @return the type name
1138: *
1139: * @see org.geotools.validation.xml.ArgHelper.Mapping#getType()
1140: */
1141: public String getType() {
1142: return "xs:float";
1143: }
1144:
1145: /**
1146: * Implementation of getElementName.
1147: *
1148: * @return the element name
1149: *
1150: * @see org.geotools.validation.xml.ArgHelper.Mapping#getElementName()
1151: */
1152: public String getElementName() {
1153: return "float";
1154: }
1155:
1156: /**
1157: * Implementation of getInstance.
1158: *
1159: * @param elem Element the element to parse into a Float.
1160: *
1161: * @return Geometry an instance of Float if one can be created, null
1162: * otherwise.
1163: *
1164: * @throws NullPointerException DOCUMENT ME!
1165: *
1166: * @see org.geotools.validation.xml.ArgHelper.Mapping#getInstance(org.w3c.dom.Element)
1167: */
1168: public Object getInstance(Element elem) {
1169: if (elem == null) {
1170: throw new NullPointerException(
1171: "The float passed in was null");
1172: }
1173:
1174: return new Float(ReaderUtils.getElementText(elem));
1175: }
1176:
1177: public Object getInstance(String elem) {
1178: if (elem == null) {
1179: throw new NullPointerException(
1180: "The float passed in was null");
1181: }
1182:
1183: return new Float(elem);
1184: }
1185:
1186: /**
1187: * Implementation of isClassInstance.
1188: *
1189: * @param c The Object to test
1190: *
1191: * @return true when both of type Long
1192: *
1193: * @see org.geotools.validation.xml.ArgHelper.Mapping#isClassInstance(java.lang.Object)
1194: */
1195: public boolean isClassInstance(Object c) {
1196: return ((c != null) && c instanceof Float);
1197: }
1198:
1199: public boolean isClass(Class c) {
1200: return ((c != null) && (Float.class.equals(c) || float.class
1201: .equals(c)));
1202: }
1203:
1204: /**
1205: * Implementation of encode.
1206: *
1207: * @param obj An object to encode as a Float.
1208: *
1209: * @return String the XML encoding
1210: *
1211: * @throws NullPointerException DOCUMENT ME!
1212: * @throws ClassCastException when obj is not of type Float
1213: *
1214: * @see org.geotools.validation.xml.ArgHelper.Mapping#encode(java.lang.Object)
1215: */
1216: public String encode(Object obj) {
1217: if (obj == null) {
1218: throw new NullPointerException(
1219: "The float obj passed in was null");
1220: }
1221:
1222: if (!(obj instanceof Float)) {
1223: throw new ClassCastException(
1224: "Object of type Long was expected.");
1225: }
1226:
1227: return "<float>" + ((Float) obj).toString() + "</float>\n";
1228: }
1229:
1230: public String toString(Object o) {
1231: return o.toString();
1232: }
1233: }
1234:
1235: /**
1236: * DoubleMapping purpose.
1237: *
1238: * <p>
1239: * Represents the workings for a Double Mapping
1240: * </p>
1241: *
1242: * @author dzwiers, Refractions Research, Inc.
1243: * @author $Author: dmzwiers $ (last modification)
1244: * @version $Id: ArgHelper.java 22266 2006-10-19 11:30:55Z acuster $
1245: */
1246: protected static class DoubleMapping implements Mapping {
1247: /**
1248: * Implementation of getType.
1249: *
1250: * @return the type name
1251: *
1252: * @see org.geotools.validation.xml.ArgHelper.Mapping#getType()
1253: */
1254: public String getType() {
1255: return "xs:double";
1256: }
1257:
1258: /**
1259: * Implementation of getElementName.
1260: *
1261: * @return the element name
1262: *
1263: * @see org.geotools.validation.xml.ArgHelper.Mapping#getElementName()
1264: */
1265: public String getElementName() {
1266: return "double";
1267: }
1268:
1269: /**
1270: * Implementation of getInstance.
1271: *
1272: * @param elem Element the element to parse into a Double.
1273: *
1274: * @return Geometry an instance of Double if one can be created, null
1275: * otherwise.
1276: *
1277: * @throws NullPointerException DOCUMENT ME!
1278: *
1279: * @see org.geotools.validation.xml.ArgHelper.Mapping#getInstance(org.w3c.dom.Element)
1280: */
1281: public Object getInstance(Element elem) {
1282: if (elem == null) {
1283: throw new NullPointerException(
1284: "The double passed in was null");
1285: }
1286:
1287: return new Double(ReaderUtils.getElementText(elem));
1288: }
1289:
1290: public Object getInstance(String elem) {
1291: if (elem == null) {
1292: throw new NullPointerException(
1293: "The double passed in was null");
1294: }
1295:
1296: return new Double(elem);
1297: }
1298:
1299: /**
1300: * Implementation of isClassInstance.
1301: *
1302: * @param c The Object to test
1303: *
1304: * @return true when both of type Long
1305: *
1306: * @see org.geotools.validation.xml.ArgHelper.Mapping#isClassInstance(java.lang.Object)
1307: */
1308: public boolean isClassInstance(Object c) {
1309: return ((c != null) && c instanceof Double);
1310: }
1311:
1312: public boolean isClass(Class c) {
1313: return ((c != null) && (Double.class.equals(c) || double.class
1314: .equals(c)));
1315: }
1316:
1317: /**
1318: * Implementation of encode.
1319: *
1320: * @param obj An object to encode as a Double.
1321: *
1322: * @return String the XML encoding
1323: *
1324: * @throws NullPointerException DOCUMENT ME!
1325: * @throws ClassCastException when obj is not of type Double
1326: *
1327: * @see org.geotools.validation.xml.ArgHelper.Mapping#encode(java.lang.Object)
1328: */
1329: public String encode(Object obj) {
1330: if (obj == null) {
1331: throw new NullPointerException(
1332: "The double obj passed in was null");
1333: }
1334:
1335: if (!(obj instanceof Double)) {
1336: throw new ClassCastException(
1337: "Object of type Long was expected.");
1338: }
1339:
1340: return "<double>" + ((Double) obj).toString()
1341: + "</double>\n";
1342: }
1343:
1344: public String toString(Object o) {
1345: return o.toString();
1346: }
1347: }
1348:
1349: /**
1350: * DateMapping purpose.
1351: *
1352: * <p>
1353: * Represents the workings for a Date Mapping
1354: * </p>
1355: *
1356: * @author dzwiers, Refractions Research, Inc.
1357: * @author $Author: dmzwiers $ (last modification)
1358: * @version $Id: ArgHelper.java 22266 2006-10-19 11:30:55Z acuster $
1359: */
1360: protected static class DateMapping implements Mapping {
1361: /**
1362: * Implementation of getType.
1363: *
1364: * @return the type name
1365: *
1366: * @see org.geotools.validation.xml.ArgHelper.Mapping#getType()
1367: */
1368: public String getType() {
1369: return "xs:dateTime";
1370: }
1371:
1372: /**
1373: * Implementation of getElementName.
1374: *
1375: * @return the element name
1376: *
1377: * @see org.geotools.validation.xml.ArgHelper.Mapping#getElementName()
1378: */
1379: public String getElementName() {
1380: return "dateTime";
1381: }
1382:
1383: /**
1384: * Implementation of getInstance.
1385: *
1386: * @param elem Element the element to parse into a Date.
1387: *
1388: * @return Geometry an instance of Date if one can be created, null
1389: * otherwise.
1390: *
1391: * @throws ValidationException DOCUMENT ME!
1392: * @throws NullPointerException DOCUMENT ME!
1393: *
1394: * @see org.geotools.validation.xml.ArgHelper.Mapping#getInstance(org.w3c.dom.Element)
1395: */
1396: public Object getInstance(Element elem)
1397: throws ValidationException {
1398: if (elem == null) {
1399: throw new NullPointerException(
1400: "The dateTime passed in was null");
1401: }
1402:
1403: SimpleDateFormat sdf = new SimpleDateFormat(
1404: "yyyy-mm-dd'T'hh:mm:ssZ");
1405:
1406: try {
1407: return sdf.parse(ReaderUtils.getElementText(elem));
1408: } catch (ParseException e) {
1409: throw new ValidationException(e);
1410: }
1411: }
1412:
1413: public Object getInstance(String elem)
1414: throws ValidationException {
1415: if (elem == null) {
1416: throw new NullPointerException(
1417: "The dateTime passed in was null");
1418: }
1419:
1420: SimpleDateFormat sdf = new SimpleDateFormat(
1421: "yyyy-mm-dd'T'hh:mm:ssZ");
1422:
1423: try {
1424: return sdf.parse(elem);
1425: } catch (ParseException e) {
1426: throw new ValidationException(e);
1427: }
1428: }
1429:
1430: /**
1431: * Implementation of isClassInstance.
1432: *
1433: * @param c The Object to test
1434: *
1435: * @return true when both of type Long
1436: *
1437: * @see org.geotools.validation.xml.ArgHelper.Mapping#isClassInstance(java.lang.Object)
1438: */
1439: public boolean isClassInstance(Object c) {
1440: return ((c != null) && c instanceof Date);
1441: }
1442:
1443: public boolean isClass(Class c) {
1444: return ((c != null) && Date.class.equals(c));
1445: }
1446:
1447: /**
1448: * Implementation of encode.
1449: *
1450: * @param obj An object to encode as a Date.
1451: *
1452: * @return String the XML encoding
1453: *
1454: * @throws NullPointerException DOCUMENT ME!
1455: * @throws ClassCastException when obj is not of type Date
1456: *
1457: * @see org.geotools.validation.xml.ArgHelper.Mapping#encode(java.lang.Object)
1458: */
1459: public String encode(Object obj) {
1460: if (obj == null) {
1461: throw new NullPointerException(
1462: "The dateTime obj passed in was null");
1463: }
1464:
1465: if (!(obj instanceof Date)) {
1466: throw new ClassCastException(
1467: "Object of type Long was expected.");
1468: }
1469:
1470: SimpleDateFormat sdf = new SimpleDateFormat(
1471: "yyyy-mm-dd'T'hh:mm:ssZ");
1472:
1473: return "<dateTime>" + sdf.format((Date) obj)
1474: + "</dateTime>\n";
1475: }
1476:
1477: public String toString(Object o) {
1478: SimpleDateFormat sdf = new SimpleDateFormat(
1479: "yyyy-mm-dd'T'hh:mm:ssZ");
1480:
1481: return sdf.format((Date) o);
1482: }
1483: }
1484:
1485: /**
1486: * URIMapping purpose.
1487: *
1488: * <p>
1489: * Represents the workings for a URI Mapping
1490: * </p>
1491: *
1492: * @author dzwiers, Refractions Research, Inc.
1493: * @author $Author: dmzwiers $ (last modification)
1494: * @version $Id: ArgHelper.java 22266 2006-10-19 11:30:55Z acuster $
1495: */
1496: protected static class URIMapping implements Mapping {
1497: /**
1498: * Implementation of getType.
1499: *
1500: * @return the type name
1501: *
1502: * @see org.geotools.validation.xml.ArgHelper.Mapping#getType()
1503: */
1504: public String getType() {
1505: return "xs:anyURI";
1506: }
1507:
1508: /**
1509: * Implementation of getElementName.
1510: *
1511: * @return the element name
1512: *
1513: * @see org.geotools.validation.xml.ArgHelper.Mapping#getElementName()
1514: */
1515: public String getElementName() {
1516: return "anyURI";
1517: }
1518:
1519: /**
1520: * Implementation of getInstance.
1521: *
1522: * @param elem Element the element to parse into a URI.
1523: *
1524: * @return Geometry an instance of URI if one can be created, null
1525: * otherwise.
1526: *
1527: * @throws ValidationException DOCUMENT ME!
1528: * @throws NullPointerException DOCUMENT ME!
1529: *
1530: * @see org.geotools.validation.xml.ArgHelper.Mapping#getInstance(org.w3c.dom.Element)
1531: */
1532: public Object getInstance(Element elem)
1533: throws ValidationException {
1534: if (elem == null) {
1535: throw new NullPointerException(
1536: "The anyUri passed in was null");
1537: }
1538:
1539: try {
1540: return new URI(ReaderUtils.getElementText(elem).trim());
1541: } catch (URISyntaxException e) {
1542: throw new ValidationException(e);
1543: }
1544: }
1545:
1546: public Object getInstance(String value)
1547: throws ValidationException {
1548: if (value == null) {
1549: throw new NullPointerException(
1550: "The anyUri passed in was null");
1551: }
1552:
1553: try {
1554: return new URI(value);
1555: } catch (URISyntaxException e) {
1556: throw new ValidationException(e);
1557: }
1558: }
1559:
1560: /**
1561: * Implementation of isClassInstance.
1562: *
1563: * @param c The Object to test
1564: *
1565: * @return true when both of type Long
1566: *
1567: * @see org.geotools.validation.xml.ArgHelper.Mapping#isClassInstance(java.lang.Object)
1568: */
1569: public boolean isClassInstance(Object c) {
1570: return ((c != null) && c instanceof URI);
1571: }
1572:
1573: public boolean isClass(Class c) {
1574: return ((c != null) && URI.class.equals(c));
1575: }
1576:
1577: /**
1578: * Implementation of encode.
1579: *
1580: * @param obj An object to encode as a URI.
1581: *
1582: * @return String the XML encoding
1583: *
1584: * @throws NullPointerException DOCUMENT ME!
1585: * @throws ClassCastException when obj is not of type URI
1586: *
1587: * @see org.geotools.validation.xml.ArgHelper.Mapping#encode(java.lang.Object)
1588: */
1589: public String encode(Object obj) {
1590: if (obj == null) {
1591: throw new NullPointerException(
1592: "The anyUri obj passed in was null");
1593: }
1594:
1595: if (!(obj instanceof URI)) {
1596: throw new ClassCastException(
1597: "Object of type Long was expected.");
1598: }
1599:
1600: return "<anyURI>" + ((URI) obj).toString() + "</anyURI>\n";
1601: }
1602:
1603: public String toString(Object o) {
1604: return o.toString();
1605: }
1606: }
1607:
1608: /**
1609: * BooleanMapping purpose.
1610: *
1611: * <p>
1612: * Represents the workings for a Boolean Mapping
1613: * </p>
1614: *
1615: * @author dzwiers, Refractions Research, Inc.
1616: * @author $Author: dmzwiers $ (last modification)
1617: * @version $Id: ArgHelper.java 22266 2006-10-19 11:30:55Z acuster $
1618: */
1619: protected static class BooleanMapping implements Mapping {
1620: /**
1621: * Implementation of getType.
1622: *
1623: * @return the type name
1624: *
1625: * @see org.geotools.validation.xml.ArgHelper.Mapping#getType()
1626: */
1627: public String getType() {
1628: return "xs:boolean";
1629: }
1630:
1631: /**
1632: * Implementation of getElementName.
1633: *
1634: * @return the element name
1635: *
1636: * @see org.geotools.validation.xml.ArgHelper.Mapping#getElementName()
1637: */
1638: public String getElementName() {
1639: return "boolean";
1640: }
1641:
1642: /**
1643: * Implementation of getInstance.
1644: *
1645: * @param elem Element the element to parse into a Boolean.
1646: *
1647: * @return Geometry an instance of Boolean if one can be created, null
1648: * otherwise.
1649: *
1650: * @throws NullPointerException DOCUMENT ME!
1651: *
1652: * @see org.geotools.validation.xml.ArgHelper.Mapping#getInstance(org.w3c.dom.Element)
1653: */
1654: public Object getInstance(Element elem) {
1655: if (elem == null) {
1656: throw new NullPointerException(
1657: "The boolean passed in was null");
1658: }
1659:
1660: return new Boolean(ReaderUtils.getElementText(elem));
1661: }
1662:
1663: public Object getInstance(String elem) {
1664: if (elem == null) {
1665: throw new NullPointerException(
1666: "The boolean passed in was null");
1667: }
1668:
1669: return new Boolean(elem);
1670: }
1671:
1672: /**
1673: * Implementation of isClassInstance.
1674: *
1675: * @param c The Object to test
1676: *
1677: * @return true when both of type Long
1678: *
1679: * @see org.geotools.validation.xml.ArgHelper.Mapping#isClassInstance(java.lang.Object)
1680: */
1681: public boolean isClassInstance(Object c) {
1682: return ((c != null) && c instanceof Boolean);
1683: }
1684:
1685: public boolean isClass(Class c) {
1686: return ((c != null) && (Boolean.class.equals(c) || boolean.class
1687: .equals(c)));
1688: }
1689:
1690: /**
1691: * Implementation of encode.
1692: *
1693: * @param obj An object to encode as a Boolean.
1694: *
1695: * @return String the XML encoding
1696: *
1697: * @throws NullPointerException DOCUMENT ME!
1698: * @throws ClassCastException when obj is not of type Boolean
1699: *
1700: * @see org.geotools.validation.xml.ArgHelper.Mapping#encode(java.lang.Object)
1701: */
1702: public String encode(Object obj) {
1703: if (obj == null) {
1704: throw new NullPointerException(
1705: "The boolean obj passed in was null");
1706: }
1707:
1708: if (!(obj instanceof Boolean)) {
1709: throw new ClassCastException(
1710: "Object of type Long was expected.");
1711: }
1712:
1713: return "<boolean>" + ((Boolean) obj).toString()
1714: + "</boolean>\n";
1715: }
1716:
1717: public String toString(Object o) {
1718: return o.toString();
1719: }
1720: }
1721:
1722: /**
1723: * StringMapping purpose.
1724: *
1725: * <p>
1726: * Represents the workings for a String Mapping
1727: * </p>
1728: *
1729: * @author dzwiers, Refractions Research, Inc.
1730: * @author $Author: dmzwiers $ (last modification)
1731: * @version $Id: ArgHelper.java 22266 2006-10-19 11:30:55Z acuster $
1732: */
1733: protected static class StringMapping implements Mapping {
1734: /**
1735: * Implementation of getType.
1736: *
1737: * @return the type name
1738: *
1739: * @see org.geotools.validation.xml.ArgHelper.Mapping#getType()
1740: */
1741: public String getType() {
1742: return "xs:string";
1743: }
1744:
1745: /**
1746: * Implementation of getElementName.
1747: *
1748: * @return the element name
1749: *
1750: * @see org.geotools.validation.xml.ArgHelper.Mapping#getElementName()
1751: */
1752: public String getElementName() {
1753: return "string";
1754: }
1755:
1756: /**
1757: * Implementation of getInstance.
1758: *
1759: * @param elem Element the element to parse into a String.
1760: *
1761: * @return Geometry an instance of String if one can be created, null
1762: * otherwise.
1763: *
1764: * @throws NullPointerException DOCUMENT ME!
1765: *
1766: * @see org.geotools.validation.xml.ArgHelper.Mapping#getInstance(org.w3c.dom.Element)
1767: */
1768: public Object getInstance(Element elem) {
1769: if (elem == null) {
1770: throw new NullPointerException(
1771: "The string passed in was null");
1772: }
1773:
1774: return new String(ReaderUtils.getElementText(elem));
1775: }
1776:
1777: public Object getInstance(String value) {
1778: if (value == null) {
1779: throw new NullPointerException(
1780: "The string passed in was null");
1781: }
1782:
1783: return new String(value);
1784: }
1785:
1786: /**
1787: * Implementation of isClassInstance.
1788: *
1789: * @param c The Object to test
1790: *
1791: * @return true when both of type Long
1792: *
1793: * @see org.geotools.validation.xml.ArgHelper.Mapping#isClassInstance(java.lang.Object)
1794: */
1795: public boolean isClassInstance(Object c) {
1796: return ((c != null) && c instanceof String);
1797: }
1798:
1799: public boolean isClass(Class c) {
1800: return ((c != null) && String.class.equals(c));
1801: }
1802:
1803: /**
1804: * Implementation of encode.
1805: *
1806: * @param obj An object to encode as a String.
1807: *
1808: * @return String the XML encoding
1809: *
1810: * @throws NullPointerException DOCUMENT ME!
1811: * @throws ClassCastException when obj is not of type String
1812: *
1813: * @see org.geotools.validation.xml.ArgHelper.Mapping#encode(java.lang.Object)
1814: */
1815: public String encode(Object obj) {
1816: if (obj == null) {
1817: throw new NullPointerException(
1818: "The string obj passed in was null");
1819: }
1820:
1821: if (!(obj instanceof String)) {
1822: throw new ClassCastException(
1823: "Object of type Long was expected.");
1824: }
1825:
1826: return "<string>" + ((String) obj).toString()
1827: + "</string>\n";
1828: }
1829:
1830: public String toString(Object o) {
1831: return o.toString();
1832: }
1833: }
1834: }
|