View Javadoc

1   package com.bradmcevoy.property;
2   
3   import com.bradmcevoy.http.CustomProperty;
4   import com.bradmcevoy.http.CustomPropertyResource;
5   import com.bradmcevoy.http.Resource;
6   import java.util.ArrayList;
7   import java.util.List;
8   import javax.xml.namespace.QName;
9   
10  /**
11   *
12   * @author brad
13   */
14  public class CustomPropertySource implements PropertySource {
15  
16      public Object getProperty( QName name, Resource r ) {
17          CustomProperty prop = lookupProperty( name, r );
18          if( prop != null ) {
19              return prop.getTypedValue();
20          } else {
21              return null;
22          }
23      }
24  
25      public PropertyMetaData getPropertyMetaData( QName name, Resource r ) {
26          CustomProperty prop = lookupProperty( name, r );
27          if( prop != null ) {
28              return new PropertyMetaData( PropertyAccessibility.WRITABLE, prop.getValueClass());
29          } else {
30              return PropertyMetaData.UNKNOWN;
31          }
32  
33      }
34  
35  
36      public void setProperty( QName name, Object value, Resource r ) {
37          CustomProperty prop = lookupProperty( name, r );
38          if( prop != null ) {
39              prop.setFormattedValue( value.toString() );
40          } else {
41              throw new RuntimeException( "property not found: " + name.getLocalPart() );
42          }
43      }
44  
45      public boolean hasProperty( QName name, Resource r ) {
46          CustomProperty prop = lookupProperty( name, r );
47          return prop != null;
48      }
49  
50      public void clearProperty( QName name, Resource r ) {
51          CustomProperty prop = lookupProperty( name, r );
52          prop.setFormattedValue( null );
53      }
54  
55      private CustomProperty lookupProperty( QName name, Resource r ) {
56          if( name == null) throw new IllegalArgumentException( "name is null");
57          if( r instanceof CustomPropertyResource ) {
58              CustomPropertyResource cpr = (CustomPropertyResource) r;
59              if( cpr.getNameSpaceURI() == null ) throw new IllegalArgumentException( "namespace uri is null on CPR");
60              if( cpr.getNameSpaceURI().equals( name.getNamespaceURI() ) ) {
61                  return cpr.getProperty( name.getLocalPart() );
62              } else {
63                  return null;
64              }
65          } else {
66              return null;
67          }
68  
69      }
70  
71      public List<QName> getAllPropertyNames( Resource r ) {
72          List<QName> list = new ArrayList<QName>();
73          if( r instanceof CustomPropertyResource ) {
74              CustomPropertyResource cpr = (CustomPropertyResource) r;
75              for( String n : cpr.getAllPropertyNames() ) {
76                  QName qname = new QName( cpr.getNameSpaceURI(), n);
77                  list.add( qname );
78              }
79          }
80          return list;
81      }
82      
83  }