View Javadoc

1   package uk.ac.cam.spectra;
2   
3   import java.net.URI;
4   import java.net.URISyntaxException;
5   
6   import org.apache.log4j.Logger;
7   
8   import com.hp.hpl.jena.rdf.model.Literal;
9   import com.hp.hpl.jena.rdf.model.Model;
10  import com.hp.hpl.jena.rdf.model.Property;
11  import com.hp.hpl.jena.rdf.model.RDFNode;
12  import com.hp.hpl.jena.rdf.model.Resource;
13  
14  /**
15   * Basic literal property, supporting string value literals.
16   * 
17   * @author Jim Downing
18   */
19  public class LiteralProperty implements MetadataProperty {
20  
21      /**
22       * 
23       */
24      private static final long serialVersionUID = -631886322076317469L;
25  
26      private static final Logger LOG = Logger.getLogger(LiteralProperty.class);
27  
28      protected String namespace;
29  
30      protected String element;
31  
32      public LiteralProperty(String ns, String el) {
33          this.namespace = ns;
34          this.element = el;
35      }
36  
37      public String getElement() {
38          return element;
39      }
40  
41      public String getNamespace() {
42          return namespace;
43      }
44  
45      /**
46       * Base literals don't have an xsi type.
47       */
48      public String getXsiType() {
49          return null;
50      }
51  
52      public URI toURI() {
53          try {
54              return new URI(new StringBuilder(namespace).append(element)
55                      .toString());
56          } catch (URISyntaxException e) {
57              LOG.error("Coding error in LiteralProperty: " + e.getMessage(), e);
58              throw new RuntimeException("Coding error in LiteralProperty: "
59                      + e.getMessage(), e);
60          }
61      }
62  
63      public String toURIString() {
64          return toURI().toString();
65      }
66  
67      public final boolean isLiteral() {
68          return true;
69      }
70  
71      /**
72       * @param value
73       *            the value you wish to set
74       * @param m
75       *            the target RDF model
76       */
77      public boolean valid(RDFNode value, Model m) {
78          Property pred = asProperty(m);
79          if (!pred.getURI().equals(toURI().toString())) {
80              LOG.debug("Predicates not equal. Expected: " + toURI() + ". Got: "
81                      + pred.getURI());
82              return false;
83          } else if (value.isLiteral()) {
84              if (!isLiteral()) {
85                  LOG.debug("Expected literal type. " + value);
86                  return false;
87              }
88          } else {
89              if (isLiteral()) {
90                  LOG.debug("Expected non-literal type. " + value);
91                  return false;
92              }
93          }
94          return true;
95      }
96  
97      public Property asProperty(Model m) {
98          return m.createProperty(toURI().toString());
99      }
100 
101     public String decode(RDFNode value) {
102         Literal l = (Literal) value;
103         return l.getLexicalForm();
104     }
105 
106     public void add(Resource resource, Object value) {
107         resource.addProperty(asProperty(resource.getModel()), value.toString());
108     }
109 
110     @Override
111     public int hashCode() {
112         final int PRIME = 31;
113         int result = 1;
114         result = PRIME * result + ((element == null) ? 0 : element.hashCode());
115         result = PRIME * result
116                 + ((namespace == null) ? 0 : namespace.hashCode());
117         return result;
118     }
119 
120     @Override
121     public boolean equals(Object obj) {
122         if (this == obj) {
123             return true;
124         }
125         if (obj == null) {
126             return false;
127         }
128         if (getClass() != obj.getClass()) {
129             return false;
130         }
131         final LiteralProperty other = (LiteralProperty) obj;
132         if (element == null) {
133             if (other.element != null)
134                 return false;
135         } else if (!element.equals(other.element))
136             return false;
137         if (namespace == null) {
138             if (other.namespace != null)
139                 return false;
140         } else if (!namespace.equals(other.namespace))
141             return false;
142         return true;
143     }
144 
145 }