Search in sources :

Example 41 with ARQException

use of org.apache.jena.sparql.ARQException in project jena by apache.

the class ResultSetUtils method resultSetToStringList.

/**
 * Extracts a List filled with the binding of selectElement variable for each
 * query solution, turned into a string (URIs or lexical forms).
 * Exhausts the result set.  Create a rewindable one to use multiple times.
 * @see org.apache.jena.query.ResultSetFactory
 */
public static List<String> resultSetToStringList(ResultSet rs, String selectElement, String literalOrResource) {
    // feature suggested by James Howison
    List<String> items = new ArrayList<>();
    while (rs.hasNext()) {
        QuerySolution qs = rs.nextSolution();
        RDFNode rn = qs.get(selectElement);
        if (rn.isLiteral())
            items.add(((Literal) rn).getLexicalForm());
        else if (rn.isURIResource())
            items.add(((Resource) rn).getURI());
        else if (rn.isAnon()) {
            items.add(((Resource) rn).getId().getLabelString());
        } else
            throw new ARQException("Unknow thing in results : " + rn);
    }
    return items;
}
Also used : QuerySolution(org.apache.jena.query.QuerySolution) ARQException(org.apache.jena.sparql.ARQException) Literal(org.apache.jena.rdf.model.Literal) ArrayList(java.util.ArrayList) Resource(org.apache.jena.rdf.model.Resource) RDFNode(org.apache.jena.rdf.model.RDFNode)

Example 42 with ARQException

use of org.apache.jena.sparql.ARQException in project jena by apache.

the class RowSetReaderCSV method vars.

private static List<Var> vars(CSVParser parser) {
    final List<Var> vars = new ArrayList<>();
    List<String> varNames = parser.parse1();
    if (varNames == null)
        throw new ARQException("SPARQL CSV Results malformed, input is empty");
    for (String vn : varNames) {
        vars.add(Var.alloc(vn));
    }
    return vars;
}
Also used : ARQException(org.apache.jena.sparql.ARQException) Var(org.apache.jena.sparql.core.Var) ArrayList(java.util.ArrayList)

Example 43 with ARQException

use of org.apache.jena.sparql.ARQException in project jena by apache.

the class RowSetReaderTSV method booleanFromTSV.

/**
 * Reads SPARQL Boolean result from TSV
 * @param in Input Stream
 * @return boolean
 */
public static boolean booleanFromTSV(InputStream in) {
    BufferedReader reader = IO.asBufferedUTF8(in);
    String str = null;
    try {
        // First try to parse the header
        str = reader.readLine();
        if (str == null)
            throw new ARQException("TSV Boolean Results malformed, input is empty");
        // Remove extraneous white space
        str = str.trim();
        // Expect a header row with single ?_askResult variable
        if (!str.equals("?_askResult"))
            throw new ARQException("TSV Boolean Results malformed, did not get expected ?_askResult header row");
        // end header.
        // Then try to parse the boolean result
        str = reader.readLine();
        if (str == null)
            throw new ARQException("TSV Boolean Results malformed, unexpected end of input after header row");
        str = str.trim();
        if (str.equalsIgnoreCase("true") || str.equalsIgnoreCase("yes")) {
            return true;
        } else if (str.equalsIgnoreCase("false") || str.equalsIgnoreCase("no")) {
            return false;
        } else {
            throw new ARQException("TSV Boolean Results malformed, expected one of - true yes false no - but got " + str);
        }
    } catch (IOException ex) {
        throw new ARQException(ex);
    }
}
Also used : ARQException(org.apache.jena.sparql.ARQException) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException)

Example 44 with ARQException

use of org.apache.jena.sparql.ARQException in project jena by apache.

the class ARQMgt_X method register.

private static void register(ObjectName objName, Object bean) {
    try {
        // (Does not cope with multiple loads running in parallel.)
        if (mbs.isRegistered(objName)) {
            try {
                mbs.unregisterMBean(objName);
            } catch (InstanceNotFoundException ex) {
            }
        }
        log.debug("Register MBean: " + objName);
        mbs.registerMBean(bean, objName);
        // remember ...
        mgtObjects.put(objName, bean);
    } catch (NotCompliantMBeanException ex) {
        log.warn("Failed to register (NotCompliantMBeanException)'" + objName.getCanonicalName() + "': " + ex.getMessage());
        throw new ARQException("Failed to register '" + objName.getCanonicalName() + "': " + ex.getMessage(), ex);
    } catch (InstanceAlreadyExistsException ex) {
        log.warn("Failed to register (InstanceAlreadyExistsException)'" + objName.getCanonicalName() + "': " + ex.getMessage());
        throw new ARQException("Failed to register '" + objName.getCanonicalName() + "': " + ex.getMessage(), ex);
    } catch (MBeanRegistrationException ex) {
        log.warn("Failed to register (MBeanRegistrationException)'" + objName.getCanonicalName() + "': " + ex.getMessage());
        throw new ARQException("Failed to register '" + objName.getCanonicalName() + "': " + ex.getMessage(), ex);
    }
}
Also used : ARQException(org.apache.jena.sparql.ARQException)

Example 45 with ARQException

use of org.apache.jena.sparql.ARQException in project jena by apache.

the class GSP method uploadTriples.

/**
 * Send a file of triples to a URL.
 */
private static void uploadTriples(HttpClient httpClient, String gspUrl, String file, String fileExtContentType, Map<String, String> headers, Push mode) {
    Lang lang = RDFLanguages.contentTypeToLang(fileExtContentType);
    if (lang == null)
        throw new ARQException("Not a recognized as an RDF format: " + fileExtContentType);
    if (RDFLanguages.isQuads(lang) && !RDFLanguages.isTriples(lang))
        throw new ARQException("Can't load quads into a graph");
    if (!RDFLanguages.isTriples(lang))
        throw new ARQException("Not an RDF format: " + file + " (lang=" + lang + ")");
    pushFile(httpClient, gspUrl, file, fileExtContentType, headers, mode);
}
Also used : ARQException(org.apache.jena.sparql.ARQException) Lang(org.apache.jena.riot.Lang)

Aggregations

ARQException (org.apache.jena.sparql.ARQException)48 IOException (java.io.IOException)11 Var (org.apache.jena.sparql.core.Var)7 ArrayList (java.util.ArrayList)5 Lang (org.apache.jena.riot.Lang)5 BufferedReader (java.io.BufferedReader)4 ServletOutputStream (javax.servlet.ServletOutputStream)4 Node (org.apache.jena.graph.Node)4 Syntax (org.apache.jena.query.Syntax)4 RDFNode (org.apache.jena.rdf.model.RDFNode)4 IndentedWriter (org.apache.jena.atlas.io.IndentedWriter)2 Triple (org.apache.jena.graph.Triple)2 Query (org.apache.jena.query.Query)2 Model (org.apache.jena.rdf.model.Model)2 Resource (org.apache.jena.rdf.model.Resource)2 RiotException (org.apache.jena.riot.RiotException)2 HttpParams (org.apache.jena.sparql.engine.http.HttpParams)2 Item (org.apache.jena.sparql.sse.Item)2 UpdateRequest (org.apache.jena.update.UpdateRequest)2 BufferedWriter (java.io.BufferedWriter)1