001: /**
002: *
003: */package org.geotools.feature.iso.xpath;
004:
005: import java.util.Collection;
006: import java.util.Iterator;
007: import java.util.List;
008: import java.util.Map;
009: import java.util.Map.Entry;
010:
011: import org.apache.commons.jxpath.DynamicPropertyHandler;
012: import org.geotools.feature.iso.Types;
013: import org.opengis.feature.Attribute;
014: import org.opengis.feature.ComplexAttribute;
015: import org.opengis.feature.type.AttributeDescriptor;
016: import org.opengis.feature.type.ComplexType;
017: import org.opengis.feature.type.Name;
018: import org.xml.sax.Attributes;
019:
020: /**
021: * JXPath property handler that works on Attribute.
022: *
023: * @author Gabriel Roldan
024: * @author Justin Deoliveira
025: */
026: public class AttributePropertyHandler implements DynamicPropertyHandler {
027:
028: public String[] getPropertyNames(Object o) {
029: Attribute att = (Attribute) o;
030:
031: // we only work on complex attributes
032: if (att instanceof ComplexAttribute) {
033:
034: ComplexType type = (ComplexType) att.getType();
035: Collection attributes = type.attributes();
036:
037: String[] propNames = new String[attributes.size()];
038: int i = 0;
039: for (Iterator itr = attributes.iterator(); itr.hasNext(); i++) {
040: AttributeDescriptor descriptor = (AttributeDescriptor) itr
041: .next();
042:
043: // JD: this ignores namespaces
044: propNames[i] = descriptor.getName().getLocalPart();
045: }
046:
047: return propNames;
048: }
049:
050: return null;
051: }
052:
053: public Object getProperty(Object o, String propName) {
054: Object value = null;
055:
056: Attribute att = (Attribute) o;
057:
058: // the Filter spec says the xpath expresion may or may not
059: // start with the Feature name. If it does, it is the self
060: // location path
061: AttributeDescriptor descriptor = att.getDescriptor();
062: String attName;
063: if (descriptor == null) {
064: attName = att.getType().getName().getLocalPart();
065: } else {
066: attName = descriptor.getName().getLocalPart();
067: }
068: if (propName.equals(attName)
069: || propName.startsWith(attName + "/")) {
070: return o;
071: }
072:
073: if (o instanceof ComplexAttribute) {
074: ComplexAttribute attribute = (ComplexAttribute) o;
075: Name name = Types.typeName(propName);
076: List found;
077: try {
078: found = attribute.get(name);
079: } catch (NullPointerException e) {
080: e.printStackTrace();
081: throw e;
082: }
083: value = found.size() == 0 ? null
084: : (found.size() == 1 ? found.get(0) : found);
085:
086: // FIXME HACK: this is due to the Filter subsystem not dealing with
087: // PropertyHandler returning attribute, hence can't, for example,
088: // compare
089: // an Attribute with a Literal
090: /*
091: if (value instanceof Attribute && !(value instanceof ComplexAttribute)) {
092: value = ((Attribute) value).get();
093: }
094: */
095: }
096:
097: if (value == null && descriptor != null) {
098: if ("id".equals(propName)) {
099: value = att.getID();
100: } else {
101: String[] scopedAttName = propName.split(":");
102: attName = scopedAttName[scopedAttName.length - 1];
103:
104: Map attributes = (Map) descriptor
105: .getUserData(Attributes.class);
106: if (attributes != null) {
107: for (Iterator it = attributes.entrySet().iterator(); it
108: .hasNext();) {
109: Map.Entry entry = (Entry) it.next();
110: Name key = (Name) entry.getKey();
111: if (attName.equals(key.getLocalPart())) {
112: value = entry.getValue();
113: break;
114: }
115: }
116: }
117: }
118: }
119: return value;
120: }
121:
122: public void setProperty(Object att, String name, Object value) {
123: // Attribute attribute = (Attribute)att;
124: // if (att instanceof ComplexAttribute) {
125: //
126: // }
127: // else {
128: // //just set the value
129: //
130: // }
131: //
132: // if(!(attribute instanceof ComplexAttribute)){
133: // if(!propertyName.equals(attribute.getType().getName().getLocalPart())){
134: // throw new IllegalArgumentException("only self reference to type
135: // allowed for simple attributes");
136: // }
137: // attribute.set(value);
138: // }else{
139: // ComplexAttribute complex = (ComplexAttribute)attribute;
140: // List/*<Attribute>*/atts = complex.get(new
141: // org.geotools.util.AttributeName(propertyName));
142: // if(atts.size() == 0){
143: // throw new IllegalArgumentException("No attributes of type " +
144: // propertyName + " found");
145: // }
146: // ((Attribute)atts.get(0)).set(value);
147: // }
148: }
149: }
|