private StringBuilder appendValue(StringBuilder sb, Literal lit) { sb.append('"'); sb.append(lit.getLabel().replace("\"", "\\\"")); sb.append('"'); if (Literals.isLanguageLiteral(lit)) { sb.append('@'); sb.append(lit.getLanguage().get()); } else { sb.append("^^<"); sb.append(lit.getDatatype().stringValue()); sb.append('>'); } return sb; }
/** * Returns the result of {@link #getBooleanValue(Literal, boolean) getBooleanValue((Literal)value, * fallback)} in case the supplied value is a literal, returns the fallback value otherwise. */ public static boolean getBooleanValue(Value v, boolean fallback) { if (v instanceof Literal) { return getBooleanValue((Literal)v, fallback); } else { return fallback; } }
public Literal createLiteral(String label, String language) { if (!Literals.isValidLanguageTag(language)) { throw new IllegalArgumentException("Not a valid language tag: " + language); } return delegate.createLiteral(label, language); }
/** * Converts a {@link Literal} into a {@link RyaType} representation of the * {@code literal}. * @param literal the {@link Literal} to convert. * @return the {@link RyaType} representation of the {@code literal}. */ public static RyaType convertLiteral(final Literal literal) { if (literal == null) { return null; } if (literal.getDatatype() != null) { if (Literals.isLanguageLiteral(literal)) { final String language = literal.getLanguage().get(); if (Literals.isValidLanguageTag(language)) { return new RyaType(literal.getDatatype(), literal.stringValue(), language); } else { log.warn("Invalid language (" + LogUtils.clean(language) + ") found in Literal. Defaulting to: " + UNDETERMINED_LANGUAGE); // Replace invalid language with "und" return new RyaType(literal.getDatatype(), literal.stringValue(), UNDETERMINED_LANGUAGE); } } return new RyaType(literal.getDatatype(), literal.stringValue()); } return new RyaType(literal.stringValue()); }
RepositoryInfo repInfo = new RepositoryInfo(); String id = Literals.getLabel(bindingSet.getValue("id"), null); String description = Literals.getLabel(bindingSet.getValue("title"), null); boolean readable = Literals.getBooleanValue(bindingSet.getValue("readable"), false); boolean writable = Literals.getBooleanValue(bindingSet.getValue("writable"), false);
/** * Creates a typed {@link Literal} 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 throws a * {@link LiteralUtilException}. Recognized types are {@link Boolean}, {@link Byte}, {@link Double}, * {@link Float}, {@link Integer}, {@link Long}, {@link Short}, {@link XMLGregorianCalendar } , and * {@link Date}. * * @param valueFactory * @param object * an object to be converted to a typed literal. * @return a typed literal representation of the supplied object. * @throws LiteralUtilException * If the literal could not be created. * @throws NullPointerException * If the object was null. */ public static Literal createLiteralOrFail(ValueFactory valueFactory, Object object) throws LiteralUtilException { return createLiteral(valueFactory, object, true); }
/** * Returns the result of {@link #getLabel(Literal, String) getLabel((Literal)v, fallback} in case the * supplied value is a literal, returns the fallback value otherwise. */ public static String getLabel(Value v, String fallback) { return v instanceof Literal ? getLabel((Literal)v, fallback) : fallback; }
RepositoryInfo repInfo = new RepositoryInfo(); String id = Literals.getLabel(bindingSet.getValue("id"), null); String description = Literals.getLabel(bindingSet.getValue("title"), null); boolean readable = Literals.getBooleanValue(bindingSet.getValue("readable"), false); boolean writable = Literals.getBooleanValue(bindingSet.getValue("writable"), false);
/** * Creates a typed {@link Literal} 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 throws a * {@link LiteralUtilException}. Recognized types are {@link Boolean}, {@link Byte}, {@link Double}, * {@link Float}, {@link Integer}, {@link Long}, {@link Short}, {@link XMLGregorianCalendar } , and * {@link Date}. * * @param valueFactory * @param object * an object to be converted to a typed literal. * @return a typed literal representation of the supplied object. * @throws LiteralUtilException * If the literal could not be created. * @throws NullPointerException * If the object was null. */ public static Literal createLiteralOrFail(ValueFactory valueFactory, Object object) throws LiteralUtilException { return createLiteral(valueFactory, object, true); }
/** * Returns the result of {@link #getLabel(Literal, String) getLabel((Literal)v, fallback} in case the * supplied value is a literal, returns the fallback value otherwise. */ public static String getLabel(Value v, String fallback) { return v instanceof Literal ? getLabel((Literal)v, fallback) : fallback; }
private StringBuilder appendValue(StringBuilder sb, Literal lit) { sb.append('"'); sb.append(lit.getLabel().replace("\"", "\\\"")); sb.append('"'); if (Literals.isLanguageLiteral(lit)) { sb.append('@'); sb.append(lit.getLanguage().get()); } else { sb.append("^^<"); sb.append(lit.getDatatype().stringValue()); sb.append('>'); } return sb; }
@Override public Literal createLiteral(String label, String language) { if (!Literals.isValidLanguageTag(language)) { throw new IllegalArgumentException("Not a valid language tag: " + language); } return delegate.createLiteral(label, language); }
/** * Returns the result of {@link #getBooleanValue(Literal, boolean) getBooleanValue((Literal)value, * fallback)} in case the supplied value is a literal, returns the fallback value otherwise. */ public static boolean getBooleanValue(Value v, boolean fallback) { if (v instanceof Literal) { return getBooleanValue((Literal)v, fallback); } else { return fallback; } }
/** * Creates a typed {@link Literal} 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 returns 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}. * * @param valueFactory * @param object * an object to be converted to a typed literal. * @return a typed literal representation of the supplied object. * @throws NullPointerException * If the object was null. */ public static Literal createLiteral(ValueFactory valueFactory, Object object) { try { return createLiteral(valueFactory, object, false); } catch (LiteralUtilException e) { // This should not happen by design throw new IllegalStateException(e); } }
private StringBuilder appendValue(StringBuilder sb, Literal lit) { sb.append('"'); sb.append(lit.getLabel().replace("\"", "\\\"")); sb.append('"'); if (Literals.isLanguageLiteral(lit)) { sb.append('@'); sb.append(lit.getLanguage().get()); } else { sb.append("^^<"); sb.append(lit.getDatatype().stringValue()); sb.append('>'); } return sb; }
/** * Validates the language based on the data type. * <p> * This will do one of the following: * <ul> * <li>Return the original {@code language} if the {@code dataType} is * {@link RDF#LANGSTRING} and it's of a VALID format.</li> * <li>Returns {@link UNDETERMINED_LANGUAGE} if the {@code dataType} is * {@link RDF#LANGSTRING} and it's of an INVALID format.</li> * <li>Return {@code null} if the dataType is NOT {@link RDF#LANGSTRING}.</li> * </ul> * @param language the language to validate. * @param dataType the {@link IRI} data type to validate against. * @return the validated language. */ public static String validateLanguage(final String language, final IRI dataType) { String result = null; if (RDF.LANGSTRING.equals(dataType)) { if (language != null && Literals.isValidLanguageTag(language)) { result = language; } else { result = UNDETERMINED_LANGUAGE; } } return result; } }
@Override public boolean isWritable() throws RepositoryException { if (!isInitialized()) { throw new IllegalStateException("HTTPRepository not initialized."); } boolean isWritable = false; try (RDF4JProtocolSession client = createHTTPClient()) { final String repositoryURL = client.getRepositoryURL(); try (TupleQueryResult repositoryList = client.getRepositoryList()) { while (repositoryList.hasNext()) { final BindingSet bindingSet = repositoryList.next(); final Value uri = bindingSet.getValue("uri"); if (uri != null && uri.stringValue().equals(repositoryURL)) { isWritable = Literals.getBooleanValue(bindingSet.getValue("writable"), false); break; } } } } catch (QueryEvaluationException e) { throw new RepositoryException(e); } catch (IOException e) { throw new RepositoryException(e); } return isWritable; }
/** * Creates a typed {@link Literal} 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 returns 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}. * * @param valueFactory * @param object * an object to be converted to a typed literal. * @return a typed literal representation of the supplied object. * @throws NullPointerException * If the object was null. */ public static Literal createLiteral(ValueFactory valueFactory, Object object) { try { return createLiteral(valueFactory, object, false); } catch (LiteralUtilException e) { // This should not happen by design throw new IllegalStateException(e); } }
/** * Checks whether the supplied literal is a "simple literal". A "simple literal" is a literal with no * language tag and the datatype {@link XMLSchema#STRING}. * * @see <a href="http://www.w3.org/TR/sparql11-query/#simple_literal">SPARQL Simple Literal * Documentation</a> */ public static boolean isSimpleLiteral(Literal l) { return !Literals.isLanguageLiteral(l) && l.getDatatype().equals(XMLSchema.STRING); }
@Override public RyaType deserialize(final byte[] bytes) throws RyaTypeResolverException { if (!deserializable(bytes)) { throw new RyaTypeResolverException("Bytes not deserializable"); } final RyaType rt = newInstance(); rt.setDataType(getRyaDataType()); String data = new String(bytes, 0, bytes.length - 2, StandardCharsets.UTF_8); if (RDF.LANGSTRING.equals(rt.getDataType())) { final int langDelimiterPos = data.lastIndexOf(LiteralLanguageUtils.LANGUAGE_DELIMITER); final String parsedData = data.substring(0, langDelimiterPos); final String language = data.substring(langDelimiterPos + 1, data.length()); if (language != null && Literals.isValidLanguageTag(language)) { rt.setLanguage(language); } else { rt.setLanguage(LiteralLanguageUtils.UNDETERMINED_LANGUAGE); } data = parsedData; } rt.setData(deserializeData(data)); return rt; }