public TemplateModel wrap(Object object) throws TemplateModelException {
// check for feature collection
if (object instanceof FeatureCollection) {
// create a model with just one variable called 'features'
SimpleHash map = new SimpleHash();
map.put("features", new CollectionModel(DataUtilities.list((FeatureCollection) object), this));
map.put("type", wrap(((FeatureCollection) object).getSchema()));
return map;
} else if (object instanceof SimpleFeatureType) {
SimpleFeatureType ft = (SimpleFeatureType) object;
// create a variable "attributes" which his a list of all the
// attributes, but at the same time, is a map keyed by name
Map attributeMap = new LinkedHashMap();
for (int i = 0; i < ft.getAttributeCount(); i++) {
AttributeDescriptor type = ft.getDescriptor(i);
Map attribute = new HashMap();
attribute.put("name", type.getLocalName());
attribute.put("type", type.getType().getBinding().getName());
attribute.put("isGeometry", Boolean.valueOf(Geometry.class.isAssignableFrom(type.getType().getBinding())));
attributeMap.put(type.getLocalName(), attribute);
}
// build up the result, feature type is represented by its name an
// attributes
SimpleHash map = new SimpleHash();
map.put("attributes", new SequenceMapModel(attributeMap, this));
map.put("name", ft.getTypeName());
return map;
} else if (object instanceof SimpleFeature) {
SimpleFeature feature = (SimpleFeature) object;
// create the model
SimpleHash map = new SimpleHash();
// next create the Map representing the per attribute useful
// properties for a template
Map attributeMap = new FeatureAttributesMap(feature);
map.putAll(attributeMap);
Catalog cat = getCatalog();
if (cat != null){
NamespaceInfo ns = cat.getNamespaceByURI(
feature.getFeatureType().getName().getNamespaceURI()
);
if (ns != null){
FeatureTypeInfo info = cat.getResourceByName(
ns.getPrefix(),
feature.getFeatureType().getName().getLocalPart(),
FeatureTypeInfo.class
);
if (info != null){
map.put("type", info);
}
}
}
if (map.get("type") == null){
map.put("type", buildDummyFeatureTypeInfo(feature));
}
// Add the metadata after setting the attributes so they aren't masked by feature attributes
map.put("fid", feature.getID());
map.put("typeName", feature.getFeatureType().getTypeName());
// create a variable "attributes" which his a list of all the
// attributes, but at the same time, is a map keyed by name
map.put("attributes", new SequenceMapModel(attributeMap, this));
return map;
}
return super.wrap(object);