001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2004-2006, Geotools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: */
016: package org.geotools.xml.wfs;
017:
018: import java.io.IOException;
019: import java.net.URI;
020: import java.util.Map;
021:
022: import javax.naming.OperationNotSupportedException;
023:
024: import org.geotools.data.Query;
025: import org.geotools.filter.Filter;
026: import org.geotools.xml.PrintHandler;
027: import org.geotools.xml.filter.FilterSchema;
028: import org.geotools.xml.gml.GMLComplexTypes;
029: import org.geotools.xml.schema.Attribute;
030: import org.geotools.xml.schema.ComplexType;
031: import org.geotools.xml.schema.Element;
032: import org.geotools.xml.schema.ElementGrouping;
033: import org.geotools.xml.schema.ElementValue;
034: import org.geotools.xml.schema.Sequence;
035: import org.geotools.xml.schema.Type;
036: import org.geotools.xml.schema.impl.AttributeGT;
037: import org.geotools.xml.schema.impl.SequenceGT;
038: import org.geotools.xml.wfs.WFSSchema.WFSAttribute;
039: import org.geotools.xml.wfs.WFSSchema.WFSComplexType;
040: import org.geotools.xml.wfs.WFSSchema.WFSElement;
041: import org.geotools.xml.xsi.XSISimpleTypes;
042: import org.xml.sax.Attributes;
043: import org.xml.sax.SAXException;
044: import org.xml.sax.helpers.AttributesImpl;
045:
046: /**
047: * <p>
048: * DOCUMENT ME!
049: * </p>
050: *
051: * @author dzwiers
052: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/plugin/wfs/src/main/java/org/geotools/xml/wfs/WFSBasicComplexTypes.java $
053: */
054: public class WFSBasicComplexTypes {
055: public final static String LOCK_KEY = "WFSBasicComplexTypes.LOCKID.KEY";
056:
057: /**
058: * <p>
059: * This class represents an GetFeatureTypeType within the WFS Schema. This
060: * includes both the data and parsing functionality associated with a
061: * GetFeatureTypeType.
062: * </p>
063: *
064: * @see WFSComplexType
065: */
066: static class GetFeatureType extends WFSComplexType {
067: // singleton instance
068: private static final WFSComplexType instance = new GetFeatureType();
069: private static Element[] elems = new Element[] { new WFSElement(
070: "Query", QueryType.getInstance(), 1, Integer.MAX_VALUE,
071: false, null), };
072: private static Sequence seq = new SequenceGT(elems);
073: private static Attribute[] attrs = new Attribute[] {
074: new WFSAttribute("version", XSISimpleTypes.String
075: .getInstance(), Attribute.REQUIRED) {
076: public String getFixed() {
077: return "1.0.0";
078: }
079: },
080: new WFSAttribute("service", XSISimpleTypes.String
081: .getInstance(), Attribute.REQUIRED) {
082: public String getFixed() {
083: return "WFS";
084: }
085: },
086: new WFSAttribute("outputFormat", XSISimpleTypes.String
087: .getInstance(), Attribute.REQUIRED) {
088: public String getFixed() {
089: return "GML2";
090: }
091: },
092: new WFSAttribute("maxFeatures",
093: XSISimpleTypes.PositiveInteger.getInstance(),
094: Attribute.OPTIONAL) };
095:
096: public static WFSComplexType getInstance() {
097: return instance;
098: }
099:
100: /**
101: * @see org.geotools.xml.schema.ComplexType#getAttributes()
102: */
103: public Attribute[] getAttributes() {
104: return attrs;
105: }
106:
107: /**
108: * @see org.geotools.xml.schema.ComplexType#getChild()
109: */
110: public ElementGrouping getChild() {
111: return seq;
112: }
113:
114: /**
115: * @see org.geotools.xml.schema.ComplexType#getChildElements()
116: */
117: public Element[] getChildElements() {
118: return elems;
119: }
120:
121: /**
122: * @see org.geotools.xml.schema.Type#getValue(org.geotools.xml.schema.Element,
123: * org.geotools.xml.schema.ElementValue[],
124: * org.xml.sax.Attributes, java.util.Map)
125: */
126: public Object getValue(Element element, ElementValue[] value,
127: Attributes attrs1, Map hints) {
128: return null;
129: }
130:
131: /**
132: * @see org.geotools.xml.schema.Type#getName()
133: */
134: public String getName() {
135: return "GetFeatureType";
136: }
137:
138: /**
139: * @see org.geotools.xml.schema.Type#getInstanceType()
140: */
141: public Class getInstanceType() {
142: return Query.class;
143: }
144:
145: /**
146: * @see org.geotools.xml.schema.Type#canEncode(org.geotools.xml.schema.Element,
147: * java.lang.Object, java.util.Map)
148: */
149: public boolean canEncode(Element element, Object value,
150: Map hints) {
151: if ((element.getType() != null)
152: && getName().equals(element.getType().getName())) {
153: return (value == null) || value instanceof Query;
154: }
155:
156: return false;
157: }
158:
159: /**
160: * @see org.geotools.xml.schema.Type#encode(org.geotools.xml.schema.Element,
161: * java.lang.Object, org.geotools.xml.PrintHandler,
162: * java.util.Map)
163: */
164: public void encode(Element element, Object value,
165: PrintHandler output, Map hints) throws IOException,
166: OperationNotSupportedException {
167: if (canEncode(element, value, hints)) {
168: AttributesImpl attributes = new AttributesImpl();
169: attributes.addAttribute(WFSSchema.NAMESPACE.toString(),
170: attrs[0].getName(), null, "string", attrs[0]
171: .getFixed());
172: attributes.addAttribute(WFSSchema.NAMESPACE.toString(),
173: attrs[1].getName(), null, "string", attrs[1]
174: .getFixed());
175: attributes.addAttribute(WFSSchema.NAMESPACE.toString(),
176: attrs[2].getName(), null, "string", attrs[2]
177: .getFixed());
178:
179: Query query = (Query) value;
180:
181: if ((query != null)
182: && (query.getMaxFeatures() != Query.DEFAULT_MAX)) {
183: attributes.addAttribute(WFSSchema.NAMESPACE
184: .toString(), attrs[3].getName(), null,
185: "integer", "" + query.getMaxFeatures());
186: }
187:
188: output.startElement(element.getNamespace(), element
189: .getName(), attributes);
190:
191: if (elems[0].getType()
192: .canEncode(elems[0], value, hints)) {
193: elems[0].getType().encode(elems[0], value, output,
194: hints);
195: }
196:
197: output.endElement(element.getNamespace(), element
198: .getName());
199: } else {
200: throw new OperationNotSupportedException(
201: "not a valid value/element for a DescribeFeatureTypeType.");
202: }
203: }
204: }
205:
206: /**
207: * <p>
208: * This class represents an DescribeFeatureType within the WFS Schema. This
209: * includes both the data and parsing functionality associated with a
210: * DescribeFeatureType.
211: * </p>
212: *
213: * @see WFSComplexType
214: */
215: static class DescribeFeatureTypeType extends WFSComplexType {
216: // singleton instance
217: private static final WFSComplexType instance = new DescribeFeatureTypeType();
218: private static Element[] elems = new Element[] { new WFSElement(
219: "TypeName", XSISimpleTypes.QName.getInstance(), 0,
220: Integer.MAX_VALUE, false, null), };
221: private static Sequence seq = new SequenceGT(elems);
222: private static Attribute[] attrs = new Attribute[] {
223: new WFSAttribute("version", XSISimpleTypes.String
224: .getInstance(), Attribute.REQUIRED) {
225: public String getFixed() {
226: return "1.0.0";
227: }
228: },
229: new WFSAttribute("service", XSISimpleTypes.String
230: .getInstance(), Attribute.REQUIRED) {
231: public String getFixed() {
232: return "WFS";
233: }
234: },
235: new WFSAttribute("outputFormat", XSISimpleTypes.String
236: .getInstance(), Attribute.REQUIRED) {
237: public String getFixed() {
238: return "XMLSCHEMA";
239: }
240: }, };
241:
242: public static WFSComplexType getInstance() {
243: return instance;
244: }
245:
246: /**
247: * @see org.geotools.xml.schema.ComplexType#getAttributes()
248: */
249: public Attribute[] getAttributes() {
250: return attrs;
251: }
252:
253: /**
254: * @see org.geotools.xml.schema.ComplexType#getChild()
255: */
256: public ElementGrouping getChild() {
257: return seq;
258: }
259:
260: /**
261: * @see org.geotools.xml.schema.ComplexType#getChildElements()
262: */
263: public Element[] getChildElements() {
264: return elems;
265: }
266:
267: /**
268: * @see org.geotools.xml.schema.Type#getValue(org.geotools.xml.schema.Element,
269: * org.geotools.xml.schema.ElementValue[],
270: * org.xml.sax.Attributes, java.util.Map)
271: */
272: public Object getValue(Element element, ElementValue[] value,
273: Attributes attrs1, Map hints) {
274: return null;
275: }
276:
277: /**
278: * @see org.geotools.xml.schema.Type#getName()
279: */
280: public String getName() {
281: return "DescribeFeatureTypeType";
282: }
283:
284: /**
285: * @see org.geotools.xml.schema.Type#getInstanceType()
286: */
287: public Class getInstanceType() {
288: return String[].class;
289: }
290:
291: /**
292: * @see org.geotools.xml.schema.Type#canEncode(org.geotools.xml.schema.Element,
293: * java.lang.Object, java.util.Map)
294: */
295: public boolean canEncode(Element element, Object value,
296: Map hints) {
297: if ((element.getType() != null)
298: && getName().equals(element.getType().getName())) {
299: return value instanceof String[];
300: }
301:
302: return false;
303: }
304:
305: /**
306: * @see org.geotools.xml.schema.Type#encode(org.geotools.xml.schema.Element,
307: * java.lang.Object, org.geotools.xml.PrintHandler,
308: * java.util.Map)
309: */
310: public void encode(Element element, Object value,
311: PrintHandler output, Map hints) throws IOException,
312: OperationNotSupportedException {
313: if (canEncode(element, value, hints)) {
314: AttributesImpl attributes = new AttributesImpl();
315: attributes.addAttribute(WFSSchema.NAMESPACE.toString(),
316: attrs[0].getName(), null, "string", attrs[0]
317: .getFixed());
318: attributes.addAttribute(WFSSchema.NAMESPACE.toString(),
319: attrs[1].getName(), null, "string", attrs[1]
320: .getFixed());
321: attributes.addAttribute(WFSSchema.NAMESPACE.toString(),
322: attrs[2].getName(), null, "string", attrs[2]
323: .getFixed());
324:
325: output.startElement(element.getNamespace(), element
326: .getName(), attributes);
327:
328: String[] strs = (String[]) value;
329:
330: for (int i = 0; i < strs.length; i++)
331: XSISimpleTypes.QName.getInstance().encode(elems[0],
332: strs[i], output, hints);
333:
334: output.endElement(element.getNamespace(), element
335: .getName());
336: } else {
337: throw new OperationNotSupportedException(
338: "not a valid value/element for a DescribeFeatureTypeType.");
339: }
340: }
341: }
342:
343: /**
344: * <p>
345: * This class represents an GetCapabilitiesType within the WFS Schema. This
346: * includes both the data and parsing functionality associated with a
347: * GetCapabilitiesType.
348: * </p>
349: *
350: * @see WFSComplexType
351: */
352: static class GetCapabilitiesType extends WFSComplexType {
353: // singleton instance
354: private static final WFSComplexType instance = new GetCapabilitiesType();
355: private static Attribute[] attrs = new Attribute[] {
356: new WFSAttribute("version", XSISimpleTypes.String
357: .getInstance(), Attribute.REQUIRED) {
358: public String getFixed() {
359: return "1.0.0";
360: }
361: },
362: new WFSAttribute("service", XSISimpleTypes.String
363: .getInstance(), Attribute.REQUIRED) {
364: public String getFixed() {
365: return "WFS";
366: }
367: } };
368:
369: public static WFSComplexType getInstance() {
370: return instance;
371: }
372:
373: /**
374: * @see org.geotools.xml.schema.ComplexType#getAttributes()
375: */
376: public Attribute[] getAttributes() {
377: return attrs;
378: }
379:
380: /**
381: * @see org.geotools.xml.schema.ComplexType#getChild()
382: */
383: public ElementGrouping getChild() {
384: return null;
385: }
386:
387: /**
388: * @see org.geotools.xml.schema.ComplexType#getChildElements()
389: */
390: public Element[] getChildElements() {
391: return null;
392: }
393:
394: /**
395: * @see org.geotools.xml.schema.Type#getValue(org.geotools.xml.schema.Element,
396: * org.geotools.xml.schema.ElementValue[],
397: * org.xml.sax.Attributes, java.util.Map)
398: */
399: public Object getValue(Element element, ElementValue[] value,
400: Attributes attrs1, Map hints) {
401: return null;
402: }
403:
404: /**
405: * @see org.geotools.xml.schema.Type#getName()
406: */
407: public String getName() {
408: return "GetCapabilitiesType";
409: }
410:
411: /**
412: * @see org.geotools.xml.schema.Type#getInstanceType()
413: */
414: public Class getInstanceType() {
415: return null;
416: }
417:
418: /**
419: * @see org.geotools.xml.schema.Type#canEncode(org.geotools.xml.schema.Element,
420: * java.lang.Object, java.util.Map)
421: */
422: public boolean canEncode(Element element, Object value,
423: Map hints) {
424: return (element.getType() != null)
425: && getName().equals(element.getType().getName())
426: && (value == null);
427: }
428:
429: /**
430: * @see org.geotools.xml.schema.Type#encode(org.geotools.xml.schema.Element,
431: * java.lang.Object, org.geotools.xml.PrintHandler,
432: * java.util.Map)
433: */
434: public void encode(Element element, Object value,
435: PrintHandler output, Map hints) throws IOException {
436: AttributesImpl attributes = new AttributesImpl();
437: attributes.addAttribute(WFSSchema.NAMESPACE.toString(),
438: attrs[0].getName(), null, "string", attrs[0]
439: .getFixed());
440: attributes.addAttribute(WFSSchema.NAMESPACE.toString(),
441: attrs[1].getName(), null, "string", attrs[1]
442: .getFixed());
443:
444: output.element(element.getNamespace(), element.getName(),
445: attributes);
446: }
447: }
448:
449: static class QueryType extends WFSComplexType {
450: // singleton instance
451: private static final WFSComplexType instance = new QueryType();
452: private static Element[] elems = new Element[] {
453:
454: // PropertyName -- used to limit attributes
455: new WFSElement(
456: FilterSchema.getInstance().getElements()[34],
457: 0, Integer.MAX_VALUE) {
458: public URI getNamespace() {
459: return FilterSchema.NAMESPACE;
460: }
461: },
462:
463: // Filter -- used to limit features
464: new WFSElement(
465: FilterSchema.getInstance().getElements()[2], 0,
466: Integer.MAX_VALUE) {
467: public URI getNamespace() {
468: return FilterSchema.NAMESPACE;
469: }
470: } };
471: private static Sequence seq = new SequenceGT(elems);
472: private static Attribute[] attrs = new Attribute[] {
473: new WFSAttribute("handle", XSISimpleTypes.String
474: .getInstance(), Attribute.OPTIONAL),
475: new WFSAttribute("typeName", XSISimpleTypes.QName
476: .getInstance(), Attribute.REQUIRED),
477: new WFSAttribute("featureVersion",
478: XSISimpleTypes.String.getInstance(),
479: Attribute.OPTIONAL) };
480:
481: public static WFSComplexType getInstance() {
482: return instance;
483: }
484:
485: /**
486: * @see org.geotools.xml.schema.ComplexType#getAttributes()
487: */
488: public Attribute[] getAttributes() {
489: return attrs;
490: }
491:
492: /**
493: * @see org.geotools.xml.schema.ComplexType#getChild()
494: */
495: public ElementGrouping getChild() {
496: return seq;
497: }
498:
499: /**
500: * @see org.geotools.xml.schema.ComplexType#getChildElements()
501: */
502: public Element[] getChildElements() {
503: return elems;
504: }
505:
506: /**
507: * @see org.geotools.xml.schema.Type#getValue(org.geotools.xml.schema.Element,
508: * org.geotools.xml.schema.ElementValue[],
509: * org.xml.sax.Attributes, java.util.Map)
510: */
511: public Object getValue(Element element, ElementValue[] value,
512: Attributes attrs1, Map hints) {
513: return null;
514: }
515:
516: /**
517: * @see org.geotools.xml.schema.Type#getName()
518: */
519: public String getName() {
520: return "QueryType";
521: }
522:
523: /**
524: * @see org.geotools.xml.schema.Type#getInstanceType()
525: */
526: public Class getInstanceType() {
527: return Query.class;
528: }
529:
530: /**
531: * @see org.geotools.xml.schema.Type#canEncode(org.geotools.xml.schema.Element,
532: * java.lang.Object, java.util.Map)
533: */
534: public boolean canEncode(Element element, Object value,
535: Map hints) {
536: if ((element.getType() != null)
537: && getName().equals(element.getType().getName())) {
538: return (value == null) || value instanceof Query;
539: }
540:
541: return false;
542: }
543:
544: /**
545: * @see org.geotools.xml.schema.Type#encode(org.geotools.xml.schema.Element,
546: * java.lang.Object, org.geotools.xml.PrintHandler,
547: * java.util.Map)
548: */
549: public void encode(Element element, Object value,
550: PrintHandler output, Map hints) throws IOException,
551: OperationNotSupportedException {
552: if (canEncode(element, value, hints)) {
553: Query query = (Query) value;
554:
555: AttributesImpl attributes = new AttributesImpl();
556:
557: if ((query.getHandle() != null)
558: && !"".equals(query.getHandle())) {
559: attributes.addAttribute(WFSSchema.NAMESPACE
560: .toString(), attrs[0].getName(), null,
561: "string", query.getHandle());
562: }
563:
564: // TODO this is a QName I think ... check it out
565: attributes.addAttribute(WFSSchema.NAMESPACE.toString(),
566: attrs[1].getName(), null, "string", query
567: .getTypeName());
568:
569: try {
570: if ((query.getVersion() != null)
571: && !"".equals(query.getVersion())) {
572: attributes.addAttribute(WFSSchema.NAMESPACE
573: .toString(), attrs[2].getName(), null,
574: "string", query.getVersion());
575: }
576: } catch (Throwable t) {
577: // ...
578: }
579:
580: output.startElement(element.getNamespace(), element
581: .getName(), attributes);
582:
583: String[] propNames = query.getPropertyNames();
584:
585: if (Query.ALL_NAMES != propNames) {
586: if (propNames != null) {
587: for (int i = 0; i < propNames.length; i++)
588: elems[0].getType().encode(elems[0],
589: propNames[i], output, hints);
590: }
591: }
592:
593: if (Filter.INCLUDE != query.getFilter()) {
594: if ((query.getFilter() != null)
595: && elems[1].getType().canEncode(elems[1],
596: query.getFilter(), hints)) {
597: elems[1].getType().encode(elems[1],
598: query.getFilter(), output, hints);
599: }
600: }
601:
602: output.endElement(element.getNamespace(), element
603: .getName());
604: } else {
605: throw new OperationNotSupportedException(
606: "not a valid value/element for a DescribeFeatureTypeType.");
607: }
608: }
609: }
610:
611: static class FeatureCollectionType extends WFSComplexType {
612: // singleton instance
613: private static final WFSComplexType instance = new FeatureCollectionType();
614:
615: public static WFSComplexType getInstance() {
616: return instance;
617: }
618:
619: /**
620: * @see org.geotools.xml.schema.ComplexType#getAttributes()
621: */
622: public Attribute[] getAttributes() {
623: return new Attribute[] { new AttributeGT(null, "lockId",
624: WFSSchema.NAMESPACE, XSISimpleTypes.String
625: .getInstance(), Attribute.OPTIONAL, null,
626: null, false), };
627: }
628:
629: /**
630: * @see org.geotools.xml.schema.ComplexType#getChild()
631: */
632: public ElementGrouping getChild() {
633: return ((ComplexType) getParent()).getChild();
634: }
635:
636: /**
637: * @see org.geotools.xml.schema.ComplexType#getParent()
638: */
639: public Type getParent() {
640: return GMLComplexTypes.AbstractFeatureCollectionType
641: .getInstance();
642: }
643:
644: /**
645: * @see org.geotools.xml.schema.ComplexType#getChildElements()
646: */
647: public Element[] getChildElements() {
648: return null;
649: }
650:
651: /**
652: * @throws SAXException
653: * @throws OperationNotSupportedException
654: * @see org.geotools.xml.schema.Type#getValue(org.geotools.xml.schema.Element,
655: * org.geotools.xml.schema.ElementValue[],
656: * org.xml.sax.Attributes, java.util.Map)
657: */
658: public Object getValue(Element element, ElementValue[] value,
659: Attributes attrs, Map hints)
660: throws OperationNotSupportedException, SAXException {
661: String lock = null;
662: lock = attrs.getValue("", "lockID");
663:
664: if ((lock == null) || "".equals(lock)) {
665: lock = attrs.getValue(WFSSchema.NAMESPACE.toString(),
666: "lockID");
667: }
668:
669: if ((hints != null) && (lock != null) && (!"".equals(lock))) {
670: hints.put(LOCK_KEY, lock);
671: }
672:
673: return getParent().getValue(element, value, attrs, hints);
674: }
675:
676: /**
677: * @see org.geotools.xml.schema.Type#getName()
678: */
679: public String getName() {
680: return "FeatureCollectionType";
681: }
682:
683: /**
684: * @see org.geotools.xml.schema.Type#getInstanceType()
685: */
686: public Class getInstanceType() {
687: return getParent().getInstanceType();
688: }
689:
690: /**
691: * @see org.geotools.xml.schema.Type#canEncode(org.geotools.xml.schema.Element,
692: * java.lang.Object, java.util.Map)
693: */
694: public boolean canEncode(Element element, Object value,
695: Map hints) {
696: return getParent().canEncode(element, value, hints);
697: }
698:
699: /**
700: * @see org.geotools.xml.schema.Type#encode(org.geotools.xml.schema.Element,
701: * java.lang.Object, org.geotools.xml.PrintHandler,
702: * java.util.Map)
703: */
704: public void encode(Element element, Object value,
705: PrintHandler output, Map hints) throws IOException,
706: OperationNotSupportedException {
707: // TODO add the lockId attribute
708: getParent().encode(element, value, output, hints);
709: }
710: }
711: }
|