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; } }
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) { } }
/** * Looks up a list of {@link PropertyAccessor} for a particular object. * * <p>This method will return all accessors that is capable of handling the object and xpath * expression provided, no order is guaranteed. * * @param object The target object. * @param xpath An xpath expression denoting a property of the target object. * @param hints Hints to pass on to factories. * @return List of Property accessors, or <code>null</code> if object is null */ public static List<PropertyAccessor> findPropertyAccessors( Object object, String xpath, Class target, Hints hints) { if (object == null) return null; List<PropertyAccessor> list = new ArrayList<PropertyAccessor>(); for (PropertyAccessorFactory factory : FACTORY_CACHE) { PropertyAccessor accessor = factory.createPropertyAccessor(object.getClass(), xpath, target, hints); if (accessor != null && accessor.canHandle(object, xpath, target)) { list.add(accessor); } } return list; } }
public void testSetType() { try { accessor.set(type, "foo", new Object(), null); fail("trying to set attribute type should have thrown exception"); } catch (IllegalAttributeException e) { } }
/** * Looks up a {@link PropertyAccessor} for a particular object. * * <p>This method will return the first accessor that is capabile of handling the object and * xpath expression provided, no order is guaranteed. * * @param object The target object. * @param xpath An xpath expression denoting a property of the target object. * @param hints Hints to pass on to factories. * @return A property accessor, or <code>null</code> if one could not be found. * @deprecated Use findPropertyAccessors, returned property accessor might not work * @see findPropertyAccessors */ public static PropertyAccessor findPropertyAccessor( Object object, String xpath, Class target, Hints hints) { if (object == null) return null; for (PropertyAccessorFactory factory : FACTORY_CACHE) { PropertyAccessor accessor = factory.createPropertyAccessor(object.getClass(), xpath, target, hints); if (accessor != null && accessor.canHandle(object, xpath, target)) { return accessor; } } return null; }
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; }
public void testCanHandleType() { assertTrue(accessor.canHandle(type, "foo", null)); assertTrue(accessor.canHandle(type, "sf:foo", null)); assertTrue(accessor.canHandle(type, "foo[1]", null)); assertTrue(accessor.canHandle(type, "sf:foo[1]", null)); assertTrue(accessor.canHandle(type, "bar", null)); assertTrue(accessor.canHandle(type, COMPLEX_PROPERTY, null)); assertFalse(accessor.canHandle(type, "illegal", null)); assertFalse(accessor.canHandle(type, "sf:foo[0]", null)); assertFalse(accessor.canHandle(type, "sf:foo[2]", null)); }
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())); } } } }
|| !accessor.canHandle(obj, attPath, target) || !tryAccessor(accessor, obj, target, value, e)) { boolean success = false;
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; } }
PropertyAccessor accessor = factory.createPropertyAccessor(object.getClass(), xpath, target, hints); if (accessor != null && accessor.canHandle(object, xpath, target)) { list.add(accessor);
/** * 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); }
PropertyAccessor accessor = factory.createPropertyAccessor(object.getClass(), xpath, target, hints); if (accessor != null && accessor.canHandle(object, xpath, target)) { return accessor;
/** * 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); }
PropertyAccessor accessor = factory.createPropertyAccessor(object.getClass(), xpath, target, hints); if (accessor != null && accessor.canHandle(object, xpath, target)) { return accessor;
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 }
private PropertyAccessor getPropertyAccessor(Object obj, Class target) { if(lastAccessor == null) lastAccessor = PropertyAccessors.findPropertyAccessor( obj, attPath, target, hints ); else if(!lastAccessor.canHandle(obj, attPath, target)) lastAccessor = PropertyAccessors.findPropertyAccessor( obj, attPath, target, hints ); return lastAccessor; }