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.wfsv.xml.v1_1_0;
006:
007: import java.util.ArrayList;
008: import java.util.List;
009: import java.util.logging.Level;
010: import java.util.logging.Logger;
011:
012: import org.eclipse.xsd.XSDElementDeclaration;
013: import org.eclipse.xsd.XSDFactory;
014: import org.eclipse.xsd.XSDParticle;
015: import org.eclipse.xsd.XSDSchema;
016: import org.eclipse.xsd.XSDTypeDefinition;
017: import org.geotools.data.VersioningFeatureSource;
018: import org.geotools.feature.Feature;
019: import org.geotools.feature.FeatureCollection;
020: import org.geotools.util.logging.Logging;
021: import org.geotools.xml.PropertyExtractor;
022: import org.geotools.xs.XSConfiguration;
023: import org.vfny.geoserver.global.Data;
024: import org.vfny.geoserver.global.FeatureTypeInfo;
025:
026: /**
027: * Extracts the extra four properties out of a versioned data type. To be used
028: * for encoding GetVersionedFeature output
029: * @author Andrea Aime
030: *
031: */
032: public class VersionedFeaturePropertyExtractor implements
033: PropertyExtractor {
034: private static final String XSD_SCHEMA = "http://www.w3.org/2001/XMLSchema";
035:
036: private static final Logger LOGGER = Logging
037: .getLogger("org.geoserver.wfsv.xml.v1_1_0");
038:
039: private static final XSDParticle CR_VERSION;
040:
041: private static final XSDParticle CR_AUTHOR;
042:
043: private static final XSDParticle CR_DATE;
044:
045: private static final XSDParticle CR_MESSAGE;
046:
047: private static final XSDParticle LU_VERSION;
048:
049: private static final XSDParticle LU_AUTHOR;
050:
051: private static final XSDParticle LU_DATE;
052:
053: private static final XSDParticle LU_MESSAGE;
054:
055: static {
056: XSDSchema schema = new XSConfiguration().schema();
057: CR_VERSION = particle(schema, "creationVersion", XSD_SCHEMA,
058: "string", true, 0, 1);
059: CR_AUTHOR = particle(schema, "createdBy", XSD_SCHEMA, "string",
060: true, 0, 1);
061: CR_DATE = particle(schema, "creationDate", XSD_SCHEMA,
062: "dateTime", true, 0, 1);
063: CR_MESSAGE = particle(schema, "creationMessage", XSD_SCHEMA,
064: "string", true, 0, 1);
065: LU_VERSION = particle(schema, "lastUpdateVersion", XSD_SCHEMA,
066: "string", true, 0, 1);
067: LU_AUTHOR = particle(schema, "lastUpdatedBy", XSD_SCHEMA,
068: "string", true, 0, 1);
069: LU_DATE = particle(schema, "lastUpdateDate", XSD_SCHEMA,
070: "dateTime", true, 0, 1);
071: LU_MESSAGE = particle(schema, "lastUpdateMessage", XSD_SCHEMA,
072: "string", true, 0, 1);
073: }
074:
075: static XSDParticle particle(XSDSchema schema, String elementName,
076: String typeNS, String typeName, boolean nillable,
077: int minOccurs, int maxOccurs) {
078: XSDFactory factory = XSDFactory.eINSTANCE;
079: XSDElementDeclaration element = factory
080: .createXSDElementDeclaration();
081: element.setName(elementName);
082: element.setNillable(nillable);
083:
084: XSDTypeDefinition type = schema.resolveTypeDefinition(typeNS,
085: typeName);
086: element.setTypeDefinition(type);
087:
088: XSDParticle particle = factory.createXSDParticle();
089: particle.setMinOccurs(minOccurs);
090: particle.setMaxOccurs(maxOccurs);
091: particle.setContent(element);
092: return particle;
093: }
094:
095: Data catalog;
096:
097: public VersionedFeaturePropertyExtractor(Data catalog) {
098: this .catalog = catalog;
099: }
100:
101: public boolean canHandle(Object object) {
102: try {
103: if (!(object instanceof Feature)
104: || object instanceof FeatureCollection)
105: return false;
106:
107: Feature f = (Feature) object;
108: FeatureTypeInfo info = catalog.getFeatureTypeInfo(f
109: .getFeatureType().getTypeName(), f.getFeatureType()
110: .getNamespace().toString());
111: return info != null
112: && info.getFeatureSource() instanceof VersioningFeatureSource;
113: } catch (Exception e) {
114: LOGGER
115: .log(
116: Level.FINE,
117: "Error occurred trying to determine versioning status of a feature type",
118: e);
119: return false;
120: }
121: }
122:
123: public List properties(Object object, XSDElementDeclaration elem) {
124: Feature f = (Feature) object;
125: List particles = new ArrayList();
126: particles.add(particleValue(f, CR_VERSION));
127: particles.add(particleValue(f, CR_AUTHOR));
128: particles.add(particleValue(f, CR_DATE));
129: particles.add(particleValue(f, CR_MESSAGE));
130: particles.add(particleValue(f, LU_VERSION));
131: particles.add(particleValue(f, LU_AUTHOR));
132: particles.add(particleValue(f, LU_DATE));
133: particles.add(particleValue(f, LU_MESSAGE));
134: return particles;
135: }
136:
137: private Object[] particleValue(Feature f, XSDParticle particle) {
138: return new Object[] {
139: particle,
140: f.getAttribute(((XSDElementDeclaration) particle
141: .getContent()).getName()) };
142: }
143:
144: }
|