/** * Add an RDF statement with the predicate and object to the model, using the current subject and graph * (either named or default). * * @param predicate * the statement's predicate. This can be defined either as a prefixed name string (e.g. * "ex:john"), or as a full IRI (e.g. "http://example.org/john"). If supplied as a prefixed name, * the {@link ModelBuilder} will need to have a namespace mapping for the prefix. * @param object * the statement's object. If the supplied object is a {@link BNode}, {@link IRI}, or * {@link Literal}, the object is used directly. If it is a prefixed name String with a known * prefix, it is mapped to an IRI. Otherwise a typed {@link Literal} is created out of the supplied * object, mapping the runtime type of the object to the appropriate XML Schema type. If no mapping * is available, the method creates a literal with the string representation of the supplied object * as the value, and {@link XMLSchema#STRING} as the datatype. Recognized types are {@link Boolean} * , {@link Byte}, {@link Double}, {@link Float}, {@link Integer}, {@link Long}, {@link Short}, * {@link XMLGregorianCalendar } , and {@link Date}. * @return this {@link ModelBuilder} * @throws ModelException * if the current subject is not set using {@link #subject(Resource)} or {@link #subject(String)}. */ public ModelBuilder add(String predicate, Object object) { return add(mapToIRI(predicate), object); }
/** * Set the current graph in which to add new statements to the supplied named graph. This method clears * the current subject. * * @param namedGraph * a named graph identifier. This can be defined either as a prefixed name string (e.g. "ex:john"), * or as a full IRI (e.g. "http://example.org/john"). If supplied as a prefixed name, the * {@link ModelBuilder} will need to have a namespace mapping for the prefix. * @return this {@link ModelBuilder} */ public ModelBuilder namedGraph(String prefixedNameOrIRI) { return namedGraph(mapToIRI(prefixedNameOrIRI)); }
/** * Set the subject about which statements are to be added to the model, defined by a prefixed name or an * IRI reference. * * @param subject * the subject resource about which statements are to be added. This can be defined either as a * prefixed name string (e.g. "ex:john"), or as a full IRI (e.g. "http://example.org/john"). If * supplied as a prefixed name, the {@link ModelBuilder} will need to have a namespace mapping for * the prefix. * @return the {@link ModelBuilder} */ public ModelBuilder subject(String prefixedNameOrIri) { return subject(mapToIRI(prefixedNameOrIri)); }
private IRI mapToIRI(String prefixedNameOrIRI) { if (prefixedNameOrIRI.indexOf(':') < 0) { throw new ModelException("not a valid prefixed name or IRI: " + prefixedNameOrIRI); } IRI iri = convertPrefixedName(prefixedNameOrIRI); if (iri == null) { iri = SimpleValueFactory.getInstance().createIRI(prefixedNameOrIRI); } return iri; }
/** * Add an RDF statement with the predicate and object to the model, using the current subject and graph * (either named or default). * * @param predicate * the statement's predicate. * @param object * the statement's object. If the supplied object is a {@link BNode}, {@link IRI}, or * {@link Literal}, the object is used directly. If it is a prefixed name String with a known * prefix, it is mapped to an IRI. Otherwise a typed {@link Literal} is created out of the supplied * object, mapping the runtime type of the object to the appropriate XML Schema type. If no mapping * is available, the method creates a literal with the string representation of the supplied object * as the value, and {@link XMLSchema#STRING} as the datatype. Recognized types are {@link Boolean} * , {@link Byte}, {@link Double}, {@link Float}, {@link Integer}, {@link Long}, {@link Short}, * {@link XMLGregorianCalendar } , and {@link Date}. * @return this {@link ModelBuilder} * @throws ModelException * if the current subject is not set using {@link #subject(Resource)} or {@link #subject(String)}. */ public ModelBuilder add(IRI predicate, Object object) { if (currentSubject == null) { throw new ModelException("subject not set"); } return add(currentSubject, predicate, object); }
/** * Convert the given prefixed name string to an IRI if possible. * * @param prefixedName * a prefixed name string, e.g. "rdf:type" * @return the IRI corresponding to the prefixed name, or {@code null} if the supplied string couldn't be * converted. */ private IRI convertPrefixedName(String prefixedName) { if (prefixedName.indexOf(':') < 0) { return null; } final String prefix = prefixedName.substring(0, prefixedName.indexOf(':')); final ValueFactory vf = SimpleValueFactory.getInstance(); for (Namespace ns : model.getNamespaces()) { if (prefix.equals(ns.getPrefix())) { return vf.createIRI(ns.getName(), prefixedName.substring(prefixedName.indexOf(':') + 1)); } } // try mapping using some of the default / well-known namespaces for (Namespace ns : getDefaultNamespaces()) { if (prefix.equals(ns.getPrefix())) { model.setNamespace(ns); return vf.createIRI(ns.getName(), prefixedName.substring(prefixedName.indexOf(':') + 1)); } } return null; }
private IRI mapToIRI(String prefixedNameOrIRI) { if (prefixedNameOrIRI.indexOf(':') < 0) { throw new ModelException("not a valid prefixed name or IRI: " + prefixedNameOrIRI); } IRI iri = convertPrefixedName(prefixedNameOrIRI); if (iri == null) { iri = SimpleValueFactory.getInstance().createIRI(prefixedNameOrIRI); } return iri; }
/** * Add an RDF statement with the predicate and object to the model, using the current subject and graph * (either named or default). * * @param predicate * the statement's predicate. * @param object * the statement's object. If the supplied object is a {@link BNode}, {@link IRI}, or * {@link Literal}, the object is used directly. If it is a prefixed name String with a known * prefix, it is mapped to an IRI. Otherwise a typed {@link Literal} is created out of the supplied * object, mapping the runtime type of the object to the appropriate XML Schema type. If no mapping * is available, the method creates a literal with the string representation of the supplied object * as the value, and {@link XMLSchema#STRING} as the datatype. Recognized types are {@link Boolean} * , {@link Byte}, {@link Double}, {@link Float}, {@link Integer}, {@link Long}, {@link Short}, * {@link XMLGregorianCalendar } , and {@link Date}. * @return this {@link ModelBuilder} * @throws ModelException * if the current subject is not set using {@link #subject(Resource)} or {@link #subject(String)}. */ public ModelBuilder add(IRI predicate, Object object) { if (currentSubject == null) { throw new ModelException("subject not set"); } return add(currentSubject, predicate, object); }
/** * Convert the given prefixed name string to an IRI if possible. * * @param prefixedName * a prefixed name string, e.g. "rdf:type" * @return the IRI corresponding to the prefixed name, or {@code null} if the supplied string couldn't be * converted. */ private IRI convertPrefixedName(String prefixedName) { if (prefixedName.indexOf(':') < 0) { return null; } final String prefix = prefixedName.substring(0, prefixedName.indexOf(':')); final ValueFactory vf = SimpleValueFactory.getInstance(); for (Namespace ns : model.getNamespaces()) { if (prefix.equals(ns.getPrefix())) { return vf.createIRI(ns.getName(), prefixedName.substring(prefixedName.indexOf(':') + 1)); } } // try mapping using some of the default / well-known namespaces for (Namespace ns : getDefaultNamespaces()) { if (prefix.equals(ns.getPrefix())) { model.setNamespace(ns); return vf.createIRI(ns.getName(), prefixedName.substring(prefixedName.indexOf(':') + 1)); } } return null; }
/** * Add an RDF statement with the given subject, predicate and object to the model, using the current graph * (either named or default). * * @param subject * the statement's subject. This can be defined either as a prefixed name string (e.g. "ex:john"), * or as a full IRI (e.g. "http://example.org/john"). If supplied as a prefixed name, the * {@link ModelBuilder} will need to have a namespace mapping for the prefix. * @param predicate * the statement's predicate * @param object * the statement's object. If the supplied object is a {@link BNode}, {@link IRI}, or * {@link Literal}, the object is used directly. If it is a prefixed name String with a known * prefix, it is mapped to an IRI. Otherwise a typed {@link Literal} is created out of the supplied * object, mapping the runtime type of the object to the appropriate XML Schema type. If no mapping * is available, the method creates a literal with the string representation of the supplied object * as the value, and {@link XMLSchema#STRING} as the datatype. Recognized types are {@link Boolean} * , {@link Byte}, {@link Double}, {@link Float}, {@link Integer}, {@link Long}, {@link Short}, * {@link XMLGregorianCalendar } , and {@link Date}. * @return this {@link ModelBuilder} * @see #namedGraph(Resource) * @see #defaultGraph() * @see Literals#createLiteral(ValueFactory, Object) */ public ModelBuilder add(String subject, IRI predicate, Object object) { return add(mapToIRI(subject), predicate, object); }
/** * Set the current graph in which to add new statements to the supplied named graph. This method clears * the current subject. * * @param namedGraph * a named graph identifier. This can be defined either as a prefixed name string (e.g. "ex:john"), * or as a full IRI (e.g. "http://example.org/john"). If supplied as a prefixed name, the * {@link ModelBuilder} will need to have a namespace mapping for the prefix. * @return this {@link ModelBuilder} */ public ModelBuilder namedGraph(String prefixedNameOrIRI) { return namedGraph(mapToIRI(prefixedNameOrIRI)); }
/** * Set the subject about which statements are to be added to the model, defined by a prefixed name or an * IRI reference. * * @param subject * the subject resource about which statements are to be added. This can be defined either as a * prefixed name string (e.g. "ex:john"), or as a full IRI (e.g. "http://example.org/john"). If * supplied as a prefixed name, the {@link ModelBuilder} will need to have a namespace mapping for * the prefix. * @return the {@link ModelBuilder} */ public ModelBuilder subject(String prefixedNameOrIri) { return subject(mapToIRI(prefixedNameOrIri)); }
objectValue = convertPrefixedName((String)object);
/** * Add an RDF statement with the predicate and object to the model, using the current subject and graph * (either named or default). * * @param predicate * the statement's predicate. This can be defined either as a prefixed name string (e.g. * "ex:john"), or as a full IRI (e.g. "http://example.org/john"). If supplied as a prefixed name, * the {@link ModelBuilder} will need to have a namespace mapping for the prefix. * @param object * the statement's object. If the supplied object is a {@link BNode}, {@link IRI}, or * {@link Literal}, the object is used directly. If it is a prefixed name String with a known * prefix, it is mapped to an IRI. Otherwise a typed {@link Literal} is created out of the supplied * object, mapping the runtime type of the object to the appropriate XML Schema type. If no mapping * is available, the method creates a literal with the string representation of the supplied object * as the value, and {@link XMLSchema#STRING} as the datatype. Recognized types are {@link Boolean} * , {@link Byte}, {@link Double}, {@link Float}, {@link Integer}, {@link Long}, {@link Short}, * {@link XMLGregorianCalendar } , and {@link Date}. * @return this {@link ModelBuilder} * @throws ModelException * if the current subject is not set using {@link #subject(Resource)} or {@link #subject(String)}. */ public ModelBuilder add(String predicate, Object object) { return add(mapToIRI(predicate), object); }
objectValue = convertPrefixedName((String)object);
/** * Add an RDF statement with the given subject, predicate and object to the model, using the current graph * (either named or default). * * @param subject * the statement's subject. This can be defined either as a prefixed name string (e.g. "ex:john"), * or as a full IRI (e.g. "http://example.org/john"). If supplied as a prefixed name, the * {@link ModelBuilder} will need to have a namespace mapping for the prefix. * @param predicate * the statement's predicate. This can be defined either as a prefixed name string (e.g. * "ex:john"), or as a full IRI (e.g. "http://example.org/john"). If supplied as a prefixed name, * the {@link ModelBuilder} will need to have a namespace mapping for the prefix. * @param object * the statement's object. If the supplied object is a {@link BNode}, {@link IRI}, or * {@link Literal}, the object is used directly. If it is a prefixed name String with a known * prefix, it is mapped to an IRI. Otherwise a typed {@link Literal} is created out of the supplied * object, mapping the runtime type of the object to the appropriate XML Schema type. If no mapping * is available, the method creates a literal with the string representation of the supplied object * as the value, and {@link XMLSchema#STRING} as the datatype. Recognized types are {@link Boolean} * , {@link Byte}, {@link Double}, {@link Float}, {@link Integer}, {@link Long}, {@link Short}, * {@link XMLGregorianCalendar } , and {@link Date}. * @return this {@link ModelBuilder} * @see #namedGraph(Resource) * @see #defaultGraph() * @see Literals#createLiteral(ValueFactory, Object) */ public ModelBuilder add(String subject, String predicate, Object object) { return add(mapToIRI(subject), mapToIRI(predicate), object); }
/** * Add an RDF statement with the given subject, predicate and object to the model, using the current graph * (either named or default). * * @param subject * the statement's subject. This can be defined either as a prefixed name string (e.g. "ex:john"), * or as a full IRI (e.g. "http://example.org/john"). If supplied as a prefixed name, the * {@link ModelBuilder} will need to have a namespace mapping for the prefix. * @param predicate * the statement's predicate * @param object * the statement's object. If the supplied object is a {@link BNode}, {@link IRI}, or * {@link Literal}, the object is used directly. If it is a prefixed name String with a known * prefix, it is mapped to an IRI. Otherwise a typed {@link Literal} is created out of the supplied * object, mapping the runtime type of the object to the appropriate XML Schema type. If no mapping * is available, the method creates a literal with the string representation of the supplied object * as the value, and {@link XMLSchema#STRING} as the datatype. Recognized types are {@link Boolean} * , {@link Byte}, {@link Double}, {@link Float}, {@link Integer}, {@link Long}, {@link Short}, * {@link XMLGregorianCalendar } , and {@link Date}. * @return this {@link ModelBuilder} * @see #namedGraph(Resource) * @see #defaultGraph() * @see Literals#createLiteral(ValueFactory, Object) */ public ModelBuilder add(String subject, IRI predicate, Object object) { return add(mapToIRI(subject), predicate, object); }
/** * Add an RDF statement with the given subject, predicate and object to the model, using the current graph * (either named or default). * * @param subject * the statement's subject. This can be defined either as a prefixed name string (e.g. "ex:john"), * or as a full IRI (e.g. "http://example.org/john"). If supplied as a prefixed name, the * {@link ModelBuilder} will need to have a namespace mapping for the prefix. * @param predicate * the statement's predicate. This can be defined either as a prefixed name string (e.g. * "ex:john"), or as a full IRI (e.g. "http://example.org/john"). If supplied as a prefixed name, * the {@link ModelBuilder} will need to have a namespace mapping for the prefix. * @param object * the statement's object. If the supplied object is a {@link BNode}, {@link IRI}, or * {@link Literal}, the object is used directly. If it is a prefixed name String with a known * prefix, it is mapped to an IRI. Otherwise a typed {@link Literal} is created out of the supplied * object, mapping the runtime type of the object to the appropriate XML Schema type. If no mapping * is available, the method creates a literal with the string representation of the supplied object * as the value, and {@link XMLSchema#STRING} as the datatype. Recognized types are {@link Boolean} * , {@link Byte}, {@link Double}, {@link Float}, {@link Integer}, {@link Long}, {@link Short}, * {@link XMLGregorianCalendar } , and {@link Date}. * @return this {@link ModelBuilder} * @see #namedGraph(Resource) * @see #defaultGraph() * @see Literals#createLiteral(ValueFactory, Object) */ public ModelBuilder add(String subject, String predicate, Object object) { return add(mapToIRI(subject), mapToIRI(predicate), object); }