Class RDFUtil


  • public class RDFUtil
    extends Object
    Misc. collection of Model/Resouce level utilities
    Author:
    Dave Reynolds
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static org.apache.jena.rdf.model.Property[] descriptionProps  
      static org.apache.jena.rdf.model.Property[] labelProps  
    • Constructor Summary

      Constructors 
      Constructor Description
      RDFUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static Set<org.apache.jena.rdf.model.Property> allPropertiesOf​(org.apache.jena.rdf.model.Resource r)
      Return a collection of all the distinct properties of the given resource
      static List<org.apache.jena.rdf.model.Resource> allResourceValues​(org.apache.jena.rdf.model.Resource resource, org.apache.jena.rdf.model.Property p)
      Return a list of all the resource-values of the property on the resource
      static org.apache.jena.rdf.model.Property asProperty​(org.apache.jena.rdf.model.Resource r)
      Create a property with the same URI as the given resource
      static org.apache.jena.rdf.model.RDFNode asRDFNode​(Object val)
      Convert an arbitrary value to a RDF node.
      static long asTimestamp​(org.apache.jena.rdf.model.RDFNode n)
      Convert a datetime literal to a unix style date stamp
      static void copyProperty​(org.apache.jena.rdf.model.Resource src, org.apache.jena.rdf.model.Resource dest, org.apache.jena.rdf.model.Property p)
      Copy all values of the given property from the source to the dest resource
      static void copyProperty​(org.apache.jena.rdf.model.Resource src, org.apache.jena.rdf.model.Resource dest, org.apache.jena.rdf.model.Property p, org.apache.jena.rdf.model.Property newp)
      Copy all values of p from src to values of newp on dest.
      static org.apache.jena.rdf.model.RDFNode deserialize​(String ser)
      Decode a serlialized RDFNode, not associated with any useful model
      static String findLangMatchValue​(org.apache.jena.rdf.model.Resource root, String language, org.apache.jena.rdf.model.Property... props)
      Checks the list of properties for one for which we can return a string which either matches the given language or a plain-literal
      static org.apache.jena.rdf.model.Resource findRoot​(org.apache.jena.rdf.model.Model m)
      Return the root resource of a model (or the first root we find if there are multiple).
      static List<org.apache.jena.rdf.model.Resource> findRoots​(org.apache.jena.rdf.model.Model m)
      Return all the root resources of a model
      static org.apache.jena.rdf.model.Literal fromDateTime​(long date)
      Create a datetime literal representing the given Date (in unix ms since epoch style)
      static org.apache.jena.rdf.model.RDFNode getAPropertyValue​(org.apache.jena.rdf.model.Resource root, org.apache.jena.rdf.model.Property[] props)
      Return the first available value for the given list of properties
      static boolean getBooleanValue​(org.apache.jena.rdf.model.Resource r, org.apache.jena.rdf.model.Property p, boolean ifAbsent)
      Answer the boolean value of property p on resource r.
      static String getDescription​(org.apache.jena.rdf.model.Resource root)
      Return a description for the given resource
      static String getDescription​(org.apache.jena.rdf.model.Resource root, String lang)
      Return a description for the given resource
      static Double getDoubleValue​(org.apache.jena.rdf.model.Resource x, org.apache.jena.rdf.model.Property p)
      Answer the double value of property p on resource x or null if there isn't one.
      static int getIntValue​(org.apache.jena.rdf.model.Resource x, org.apache.jena.rdf.model.Property p, int ifAbsent)
      Answer the integer value of property p on resource x, or ifAbsent if there isn't one.
      static String getLabel​(org.apache.jena.rdf.model.RDFNode node)
      Return a label for the given node
      static String getLabel​(org.apache.jena.rdf.model.Resource root)
      Return a label for the given resource
      static String getLabel​(org.apache.jena.rdf.model.Resource root, String language)
      Return a label for the given resource
      static String getLexicalForm​(org.apache.jena.rdf.model.RDFNode value)
      Return the lexical form of a node.
      static String getLocalname​(String uri)
      Return a useful version of the local name for a resource.
      static String getLocalname​(org.apache.jena.rdf.model.Resource r)
      Return a useful version of the local name for a resource.
      static Long getLongValue​(org.apache.jena.rdf.model.Resource x, org.apache.jena.rdf.model.Property p)
      Answer the long value of property p on resource x or null if there isn't one.
      static String getNamespace​(org.apache.jena.rdf.model.Resource r)
      Return a useful version of the namespace resource.
      static Number getNumericValue​(org.apache.jena.rdf.model.Resource x, org.apache.jena.rdf.model.Property p)  
      static org.apache.jena.rdf.model.Resource getResourceValue​(org.apache.jena.rdf.model.Resource subject, org.apache.jena.rdf.model.Property prop)
      Return the value of a resource on a property as a resource, or null if there isn't a resource value.
      static List<org.apache.jena.rdf.model.Resource> getResourceValues​(org.apache.jena.rdf.model.Resource subject, org.apache.jena.rdf.model.Property prop)
      Return all the resource values of the property on a resource.
      static String getStringValue​(org.apache.jena.rdf.model.Resource r, org.apache.jena.rdf.model.Property p)
      Return one of the values of the property on the resource in string form.
      static String getStringValue​(org.apache.jena.rdf.model.Resource r, org.apache.jena.rdf.model.Property p, String defaultValue)
      Return one of the values of the property on the resource in string form.
      static org.apache.jena.rdf.model.Resource getSubject​(org.apache.jena.rdf.model.Model model, org.apache.jena.rdf.model.Property prop, org.apache.jena.rdf.model.RDFNode object)
      Find the singleton subject of the given property/value pair.
      static org.apache.jena.rdf.model.Model mapNamespace​(org.apache.jena.rdf.model.Model model, String originalNS, String replacementNS)
      Map a namespace used in a model
      static String serlialize​(org.apache.jena.rdf.model.RDFNode node)
      Serialize a (URI or literal) RDFNode to a string which can be later parsed
      static void timestamp​(org.apache.jena.rdf.model.Resource resource, org.apache.jena.rdf.model.Property prop)
      Timestamp a resource using current time
    • Field Detail

      • labelProps

        public static final org.apache.jena.rdf.model.Property[] labelProps
      • descriptionProps

        public static final org.apache.jena.rdf.model.Property[] descriptionProps
    • Constructor Detail

      • RDFUtil

        public RDFUtil()
    • Method Detail

      • getAPropertyValue

        public static org.apache.jena.rdf.model.RDFNode getAPropertyValue​(org.apache.jena.rdf.model.Resource root,
                                                                          org.apache.jena.rdf.model.Property[] props)
        Return the first available value for the given list of properties
      • findLangMatchValue

        public static String findLangMatchValue​(org.apache.jena.rdf.model.Resource root,
                                                String language,
                                                org.apache.jena.rdf.model.Property... props)
        Checks the list of properties for one for which we can return a string which either matches the given language or a plain-literal
      • getLabel

        public static String getLabel​(org.apache.jena.rdf.model.Resource root)
        Return a label for the given resource
      • getLabel

        public static String getLabel​(org.apache.jena.rdf.model.Resource root,
                                      String language)
        Return a label for the given resource
      • getLabel

        public static String getLabel​(org.apache.jena.rdf.model.RDFNode node)
        Return a label for the given node
      • getDescription

        public static String getDescription​(org.apache.jena.rdf.model.Resource root)
        Return a description for the given resource
      • getDescription

        public static String getDescription​(org.apache.jena.rdf.model.Resource root,
                                            String lang)
        Return a description for the given resource
      • getSubject

        public static org.apache.jena.rdf.model.Resource getSubject​(org.apache.jena.rdf.model.Model model,
                                                                    org.apache.jena.rdf.model.Property prop,
                                                                    org.apache.jena.rdf.model.RDFNode object)
        Find the singleton subject of the given property/value pair. Return null if there is no match. Return an arbitrary match if there are multiples
      • getStringValue

        public static String getStringValue​(org.apache.jena.rdf.model.Resource r,
                                            org.apache.jena.rdf.model.Property p,
                                            String defaultValue)
        Return one of the values of the property on the resource in string form. If there are no values return the defaultValue. If the value is not a String but is a literal return it's lexical form. If it is a resource return it's URI.
      • getStringValue

        public static String getStringValue​(org.apache.jena.rdf.model.Resource r,
                                            org.apache.jena.rdf.model.Property p)
        Return one of the values of the property on the resource in string form. If there are no values return null.
      • getLexicalForm

        public static String getLexicalForm​(org.apache.jena.rdf.model.RDFNode value)
        Return the lexical form of a node. This is the lexical form of a literal, the URI of a URIResource or the annonID of a bNode.
      • getResourceValue

        public static org.apache.jena.rdf.model.Resource getResourceValue​(org.apache.jena.rdf.model.Resource subject,
                                                                          org.apache.jena.rdf.model.Property prop)
        Return the value of a resource on a property as a resource, or null if there isn't a resource value.
      • getResourceValues

        public static List<org.apache.jena.rdf.model.Resource> getResourceValues​(org.apache.jena.rdf.model.Resource subject,
                                                                                 org.apache.jena.rdf.model.Property prop)
        Return all the resource values of the property on a resource.
      • getIntValue

        public static int getIntValue​(org.apache.jena.rdf.model.Resource x,
                                      org.apache.jena.rdf.model.Property p,
                                      int ifAbsent)
        Answer the integer value of property p on resource x, or ifAbsent if there isn't one.
      • getLongValue

        public static Long getLongValue​(org.apache.jena.rdf.model.Resource x,
                                        org.apache.jena.rdf.model.Property p)
        Answer the long value of property p on resource x or null if there isn't one.
      • getDoubleValue

        public static Double getDoubleValue​(org.apache.jena.rdf.model.Resource x,
                                            org.apache.jena.rdf.model.Property p)
        Answer the double value of property p on resource x or null if there isn't one.
      • getNumericValue

        public static Number getNumericValue​(org.apache.jena.rdf.model.Resource x,
                                             org.apache.jena.rdf.model.Property p)
      • getBooleanValue

        public static boolean getBooleanValue​(org.apache.jena.rdf.model.Resource r,
                                              org.apache.jena.rdf.model.Property p,
                                              boolean ifAbsent)
        Answer the boolean value of property p on resource r. If there is no p-value, or the p-value is not a literal, return ifAbsent. Otherwise return true if the literal has spelling "true" or "yes", false if it has the spelling "false" or "no", and an unspecified value otherwise.
      • getLocalname

        public static String getLocalname​(String uri)
        Return a useful version of the local name for a resource. Not restricted to an NCName
      • getLocalname

        public static String getLocalname​(org.apache.jena.rdf.model.Resource r)
        Return a useful version of the local name for a resource. Not restricted to an NCName
      • getNamespace

        public static String getNamespace​(org.apache.jena.rdf.model.Resource r)
        Return a useful version of the namespace resource. Not restricted to treating the localname as an NCName
      • asRDFNode

        public static org.apache.jena.rdf.model.RDFNode asRDFNode​(Object val)
        Convert an arbitrary value to a RDF node.
        • An existing node is returned unchanged
        • A string beginning http: is converted to a resource
        • A string ending with @lang becomes a lang-tagged literal
        • Other strings become plain literals
        • All other non-null values are passed to the literal factory
        • Null becomes a new bnode
        Parameters:
        val - Any value
        Returns:
        val converted to an RDFNode
      • timestamp

        public static void timestamp​(org.apache.jena.rdf.model.Resource resource,
                                     org.apache.jena.rdf.model.Property prop)
        Timestamp a resource using current time
      • copyProperty

        public static void copyProperty​(org.apache.jena.rdf.model.Resource src,
                                        org.apache.jena.rdf.model.Resource dest,
                                        org.apache.jena.rdf.model.Property p)
        Copy all values of the given property from the source to the dest resource
      • copyProperty

        public static void copyProperty​(org.apache.jena.rdf.model.Resource src,
                                        org.apache.jena.rdf.model.Resource dest,
                                        org.apache.jena.rdf.model.Property p,
                                        org.apache.jena.rdf.model.Property newp)
        Copy all values of p from src to values of newp on dest.
      • fromDateTime

        public static org.apache.jena.rdf.model.Literal fromDateTime​(long date)
        Create a datetime literal representing the given Date (in unix ms since epoch style)
      • asTimestamp

        public static long asTimestamp​(org.apache.jena.rdf.model.RDFNode n)
        Convert a datetime literal to a unix style date stamp
      • findRoot

        public static org.apache.jena.rdf.model.Resource findRoot​(org.apache.jena.rdf.model.Model m)
        Return the root resource of a model (or the first root we find if there are multiple). Return null there is no root.
      • findRoots

        public static List<org.apache.jena.rdf.model.Resource> findRoots​(org.apache.jena.rdf.model.Model m)
        Return all the root resources of a model
      • allPropertiesOf

        public static Set<org.apache.jena.rdf.model.Property> allPropertiesOf​(org.apache.jena.rdf.model.Resource r)
        Return a collection of all the distinct properties of the given resource
      • asProperty

        public static org.apache.jena.rdf.model.Property asProperty​(org.apache.jena.rdf.model.Resource r)
        Create a property with the same URI as the given resource
      • allResourceValues

        public static List<org.apache.jena.rdf.model.Resource> allResourceValues​(org.apache.jena.rdf.model.Resource resource,
                                                                                 org.apache.jena.rdf.model.Property p)
        Return a list of all the resource-values of the property on the resource
      • serlialize

        public static String serlialize​(org.apache.jena.rdf.model.RDFNode node)
        Serialize a (URI or literal) RDFNode to a string which can be later parsed
      • deserialize

        public static org.apache.jena.rdf.model.RDFNode deserialize​(String ser)
        Decode a serlialized RDFNode, not associated with any useful model
      • mapNamespace

        public static org.apache.jena.rdf.model.Model mapNamespace​(org.apache.jena.rdf.model.Model model,
                                                                   String originalNS,
                                                                   String replacementNS)
        Map a namespace used in a model