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: package org.geoserver.ows;
006:
007: import org.apache.commons.lang.builder.EqualsBuilder;
008: import org.apache.commons.lang.builder.HashCodeBuilder;
009: import org.geotools.util.Version;
010: import java.io.Reader;
011: import java.util.Map;
012:
013: import javax.xml.namespace.QName;
014:
015: /**
016: * Creates a request bean from xml.
017: * <p>
018: * A request bean is an object which captures the parameters of an operation
019: * being requested to a service.
020: * </p>
021: * <p>
022: * An xml request reader must declare the root element of xml documents that it
023: * is capable of reading. This is accomplished with {@link #getNamespace()} and
024: * {@link #getElement()}.
025: * </p>
026: *
027: * @author Justin Deoliveira, The Open Planning Project, jdeolive@openplans.org
028: *
029: */
030: public abstract class XmlRequestReader {
031: /**
032: * the qualified name of the element this reader can read.
033: */
034: final QName element;
035:
036: /**
037: * Appliction specific version number.
038: */
039: final Version version;
040:
041: /**
042: * Service identifier
043: */
044: final String serviceId;
045:
046: /**
047: * Creates the xml reader for the specified element.
048: *
049: * @param element The qualified name of the element the reader reads.
050: */
051: public XmlRequestReader(QName element) {
052: this (element, null, null);
053: }
054:
055: /**
056: * Creates the xml reader for the specified element.
057: *
058: * @param namespace The namespace of the element
059: * @param local The local name of the element
060: */
061: public XmlRequestReader(String namespace, String local) {
062: this (new QName(namespace, local));
063: }
064:
065: /**
066: *
067: * Creates the xml reader for the specified element of a particular version.
068: *
069: * @param element The qualified name of the element the reader reads.
070: * @param version The version of the element in which the reader supports,
071: * may be <code>null</code>.
072: */
073: public XmlRequestReader(QName element, Version version,
074: String serviceId) {
075: this .element = element;
076: this .version = version;
077: this .serviceId = serviceId;
078:
079: if (element == null) {
080: throw new NullPointerException("element");
081: }
082: }
083:
084: // /**
085: // *
086: // * Creates the xml reader for the specified element of a particular version.
087: // *
088: // * @param namespace The namespace of the element
089: // * @param local The local name of the element
090: // * @param version The version of the element in which the reader supports,
091: // * may be <code>null</code>.
092: // */
093: // public XmlRequestReader(String namespace, String local, Version version) {
094: // this(new QName(namespace, local), version, null);
095: // }
096:
097: /**
098: * @return The qualified name of the element that this reader reads.
099: */
100: public QName getElement() {
101: return element;
102: }
103:
104: /**
105: * @return The version of hte element that this reader reads.
106: */
107: public Version getVersion() {
108: return version;
109: }
110:
111: /**
112: * Reads the xml and initializes the request object.
113: * <p>
114: * The <tt>request</tt> parameter may be <code>null</code>, so in this case
115: * the request reader would be responsible for creating the request object,
116: * or throwing an exception if this is not supported.
117: * </p>
118: * <p>
119: * In the case of the <tt>request</tt> being non <code>null</code>, the
120: * request reader may chose to modify and return <tt>request</tt>, or create
121: * a new request object and return it.
122: * </p>
123: * <p>
124: * The <tt>kvp</tt> is used to support mixed style reading of the request
125: * object from xml and from a set of key value pairs. This map is often empty.
126: * </p>
127: */
128: public abstract Object read(Object request, Reader reader, Map kvp)
129: throws Exception;
130:
131: /**
132: * Two XmlReaders considered equal if namespace,element, and version properties
133: * are the same.
134: */
135: public boolean equals(Object obj) {
136: if (!(obj instanceof XmlRequestReader)) {
137: return false;
138: }
139:
140: XmlRequestReader other = (XmlRequestReader) obj;
141:
142: return new EqualsBuilder().append(element, other.element)
143: .append(version, other.version).append(serviceId,
144: other.serviceId).isEquals();
145: }
146:
147: /**
148: * Implementation of hashcode.
149: */
150: public int hashCode() {
151: return new HashCodeBuilder().append(element).append(version)
152: .append(serviceId).toHashCode();
153: }
154:
155: public String getServiceId() {
156: return serviceId;
157: }
158: }
|