Search in sources :

Example 31 with ARQException

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

the class TSVInput 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");
        // 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 32 with ARQException

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

the class CSVInput method booleanFromCSV.

public static boolean booleanFromCSV(InputStream in) {
    CSVParser parser = CSVParser.create(in);
    final List<Var> vars = vars(parser);
    if (vars.size() != 1) {
        throw new ARQException("CSV Boolean Results malformed: variables line='" + vars + "'");
    }
    if (!vars.get(0).getName().equals("_askResult")) {
        FmtLog.warn(log, "Boolean result variable is '%s', not '_askResult'", vars.get(0).getName());
    }
    List<String> line = parser.parse1();
    if (line.size() != 1) {
        throw new ARQException("CSV Boolean Results malformed: data line='" + line + "'");
    }
    String str = line.get(0);
    boolean b;
    if (str.equalsIgnoreCase("true") || str.equalsIgnoreCase("yes"))
        b = true;
    else if (str.equalsIgnoreCase("false") || str.equalsIgnoreCase("no"))
        b = false;
    else {
        throw new ARQException("CSV Boolean Results malformed, expected one of - true yes false no - but got " + str);
    }
    List<String> line2 = parser.parse1();
    if (line2 != null) {
        FmtLog.warn(log, "Extra rows: first is " + line2);
    }
    return b;
}
Also used : ARQException(org.apache.jena.sparql.ARQException) Var(org.apache.jena.sparql.core.Var) CSVParser(org.apache.jena.atlas.csv.CSVParser)

Example 33 with ARQException

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

the class CSVOutput method format.

@Override
public void format(OutputStream out, ResultSet resultSet) {
    try {
        Writer w = FileUtils.asUTF8(out);
        NodeToLabelMap bnodes = new NodeToLabelMap();
        w = new BufferedWriter(w);
        String sep = null;
        List<String> varNames = resultSet.getResultVars();
        List<Var> vars = new ArrayList<>(varNames.size());
        // Convert to Vars and output the header line.
        for (String v : varNames) {
            if (sep != null)
                w.write(sep);
            else
                sep = ",";
            w.write(csvSafe(v));
            vars.add(Var.alloc(v));
        }
        w.write(NL);
        // Data output
        for (; resultSet.hasNext(); ) {
            sep = null;
            Binding b = resultSet.nextBinding();
            for (Var v : vars) {
                if (sep != null)
                    w.write(sep);
                sep = ",";
                Node n = b.get(v);
                if (n != null)
                    output(w, n, bnodes);
            }
            w.write(NL);
        }
        w.flush();
    } catch (IOException ex) {
        throw new ARQException(ex);
    }
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) ARQException(org.apache.jena.sparql.ARQException) Var(org.apache.jena.sparql.core.Var) Node(org.apache.jena.graph.Node) ArrayList(java.util.ArrayList) IOException(java.io.IOException) NodeToLabelMap(org.apache.jena.sparql.util.NodeToLabelMap) BufferedWriter(java.io.BufferedWriter) Writer(java.io.Writer) BufferedWriter(java.io.BufferedWriter)

Example 34 with ARQException

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

the class UpdateProcessRemote method execute.

@Override
public void execute() {
    // Validation
    if (this.getEndpoint() == null)
        throw new ARQException("Null endpoint for remote update");
    if (this.getUpdateRequest() == null)
        throw new ARQException("Null update request for remote update");
    // Build endpoint URL
    String endpoint = this.getEndpoint();
    String querystring = this.getQueryString();
    if (querystring != null && !querystring.equals("")) {
        endpoint = endpoint.contains("?") ? endpoint + "&" + querystring : endpoint + "?" + querystring;
    }
    // Execution
    String reqStr = this.getUpdateRequest().toString();
    HttpOp.execHttpPost(endpoint, WebContent.contentTypeSPARQLUpdate, reqStr, getClient(), getHttpContext());
}
Also used : ARQException(org.apache.jena.sparql.ARQException)

Example 35 with ARQException

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

the class Metadata method read.

// Protect all classloader choosing -- sometimes systems mess with even the system class loader.
private static void read(Properties properties, String resourceName) {
    // Armour-plate this - classloaders and using them can be blocked by some environments.
    try {
        ClassLoader classLoader = null;
        try {
            classLoader = SystemUtils.chooseClassLoader();
        } catch (ARQException ex) {
        }
        if (classLoader == null) {
            try {
                classLoader = Metadata.class.getClassLoader();
            } catch (ARQException ex) {
            }
        }
        if (classLoader == null) {
            Log.error(Metadata.class, "No classloader");
            return;
        }
        InputStream in = classLoader.getResourceAsStream(resourceName);
        if (in == null)
            // In development, there is no properties file.
            return;
        try {
            properties.loadFromXML(in);
        } catch (InvalidPropertiesFormatException ex) {
            throw new ARQException("Invalid properties file", ex);
        } catch (IOException ex) {
            throw new ARQException("Metadata ==> IOException", ex);
        }
    } catch (Throwable ex) {
        Log.error(Metadata.class, "Unexpected Thorwable", ex);
        return;
    }
}
Also used : ARQException(org.apache.jena.sparql.ARQException) InvalidPropertiesFormatException(java.util.InvalidPropertiesFormatException) InputStream(java.io.InputStream) IOException(java.io.IOException)

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