private boolean tryAccessor( PropertyAccessor accessor, Object obj, Class<?> target, AtomicReference<Object> value, AtomicReference<Exception> ex) { try { value.set(accessor.get(obj, attPath, target)); return true; } catch (Exception e) { ex.set(e); return false; } }
private GeometryDescriptor getDefaultGeometryDescriptor(FeatureType featureType, String xpath) { // directly instantiating the factory I need instead of scanning the registry improves perf. FeaturePropertyAccessorFactory accessorFactory = new FeaturePropertyAccessorFactory(); Hints hints = new Hints(PropertyAccessorFactory.NAMESPACE_CONTEXT, namespaces); PropertyAccessor accessor = accessorFactory.createPropertyAccessor( featureType.getClass(), xpath, GeometryAttribute.class, hints); GeometryDescriptor geom = null; if (accessor != null) { try { geom = accessor.get(featureType, xpath, GeometryDescriptor.class); } catch (Exception e) { LOGGER.finest( String.format( "Exception occurred retrieving geometry descriptor " + "at x-path \"%s\": %s", xpath, e.getMessage())); } } return geom; }
private void checkPropetyExistenceAndType( FeatureTypeMapping mapping, String xpath, Class<?> expectedType) { FeatureType featureType = (FeatureType) mapping.getTargetFeature().getType(); FeaturePropertyAccessorFactory accessorFactory = new FeaturePropertyAccessorFactory(); Hints hints = new Hints(PropertyAccessorFactory.NAMESPACE_CONTEXT, mapping.getNamespaces()); PropertyAccessor accessor = accessorFactory.createPropertyAccessor( featureType.getClass(), xpath, Object.class, hints); if (accessor != null) { Object descr = null; try { descr = accessor.get(featureType, xpath, Object.class); } catch (Exception e) { throw new IllegalArgumentException( String.format( "Attribute \"%s\" not found in type \"%s\"", xpath, mappings.getTargetFeature().getName().toString()), e); } if (!(expectedType.isAssignableFrom(descr.getClass()))) { throw new IllegalArgumentException( String.format( "Attribute descriptor for \"%s\" if of type \"%s\", but it should be of type \"%s\"", xpath, descr.getClass().getName(), expectedType.getName())); } } } }
public void testGet() { assertEquals(Integer.valueOf(1), accessor.get(feature, "foo", null)); assertEquals(Integer.valueOf(1), accessor.get(feature, "sf:foo", null)); assertEquals(Integer.valueOf(1), accessor.get(feature, "foo[1]", null)); assertEquals(Integer.valueOf(1), accessor.get(feature, "sf:foo[1]", null)); assertEquals(new Double(2.0), accessor.get(feature, "bar", null)); assertEquals(new Double(3.0), accessor.get(feature, COMPLEX_PROPERTY, null)); assertEquals( "fid", SimpleFeaturePropertyAccessorFactory.FID_ACCESS.get(feature, "@id", null)); assertEquals( "fid", SimpleFeaturePropertyAccessorFactory.FID_ACCESS.get(feature, "@gml:id", null)); assertFalse(accessor.canHandle(feature, "illegal", null)); assertNull(accessor.get(feature, "illegal", null)); }
public void testSet() { try { accessor.set(feature, "foo", Integer.valueOf(2), null); } catch (IllegalAttributeException e) { fail(); } assertEquals(Integer.valueOf(2), accessor.get(feature, "foo", null)); try { accessor.set(feature, "bar", new Double(1.0), null); } catch (IllegalAttributeException e) { fail(); } assertEquals(new Double(1.0), accessor.get(feature, "bar", null)); try { accessor.set(feature, "@id", "fid2", null); fail("Should have thrown exception trying to set fid"); } catch (IllegalAttributeException e) { } }
private boolean tryAccessor(PropertyAccessor accessor, Object obj, Class target, AtomicReference<Object> value, AtomicReference<Exception> ex) { try { value.set(accessor.get(obj, attPath, target)); return true; } catch (Exception e) { ex.set(e); return false; } }
/** * Gets the value of this attribute from the passed feature. * * @param feature Feature from which to extract attribute value. */ public Object evaluate(Feature feature) { PropertyAccessor accessor = PropertyAccessors.findPropertyAccessor( feature, attPath, null, null ); if ( accessor == null ) { //JD:not throwing exception to remain backwards compatabile, just returnign null return null; // throw new IllegalArgumentException( // "Could not find property accessor for: (" + feature + "," + attPath + ")" // ); } return accessor.get( feature, attPath, null ); //return feature.getAttribute(attPath); }
/** * Determines whether or not the given feature's ID matches this filter. * <p> * In order to get the object's ID, the {@link PropertyAccessor} capable of * dealing with <code>feature</code> has to support the request of the * expression <code>"@id"</code> * </p> * * @param feature * Specified feature to examine. * * @return <tt>true</tt> if the feature's ID matches an fid held by this * filter, <tt>false</tt> otherwise. * @see SimpleFeaturePropertyAccessorFactory */ public boolean evaluate(Object feature) { if (feature == null) { return false; } final Set fids = fids(); final String attPath = "@id"; PropertyAccessor accessor = PropertyAccessors.findPropertyAccessor(feature, attPath, null, null); if (accessor == null) { return false; } Object id = accessor.get(feature, attPath, null); return fids.contains(id); }
public Object evaluate(Object obj, Class target) { PropertyAccessor accessor = getPropertyAccessor(obj, target); if ( accessor == null ) { return null; //JD:not throwing exception to remain backwards compatabile, just returnign null } Object propertyValue = accessor.get( obj, attPath, target ); if(target == null) return propertyValue; Value value = new Value( propertyValue ); return value.value( target ); // pull into the requested shape }
public void testGetAnyGeometry() throws Exception { SimpleFeatureTypeBuilder tb = new SimpleFeatureTypeBuilder(); tb.setName("test"); tb.setCRS(null); tb.add("g1", Point.class); tb.add("g2", Point.class); tb.setDefaultGeometry("g1"); SimpleFeatureType type = tb.buildFeatureType(); SimpleFeatureBuilder b = new SimpleFeatureBuilder(type); b.set("g1", null); Point p = new GeometryFactory().createPoint(new Coordinate(0, 0)); b.set("g2", p); SimpleFeature feature = b.buildFeature(null); assertNull(feature.getDefaultGeometry()); assertEquals( p, SimpleFeaturePropertyAccessorFactory.DEFAULT_GEOMETRY_ACCESS.get( feature, "", Geometry.class)); } }