Search in sources :

Example 1 with RiotParseException

use of org.apache.jena.riot.RiotParseException in project jena by apache.

the class TokenizerText method hasNext.

@Override
public final boolean hasNext() {
    if (finished)
        return false;
    if (token != null)
        return true;
    try {
        skip();
        if (reader.eof()) {
            // close();
            finished = true;
            return false;
        }
        token = parseToken();
        if (token == null) {
            // close();
            finished = true;
            return false;
        }
        return true;
    } catch (AtlasException ex) {
        if (ex.getCause() != null) {
            if (ex.getCause().getClass() == java.nio.charset.MalformedInputException.class)
                throw new RiotParseException("Bad character encoding", reader.getLineNum(), reader.getColNum());
            throw new RiotParseException("Bad input stream [" + ex.getCause() + "]", reader.getLineNum(), reader.getColNum());
        }
        throw new RiotParseException("Bad input stream", reader.getLineNum(), reader.getColNum());
    }
}
Also used : RiotParseException(org.apache.jena.riot.RiotParseException) AtlasException(org.apache.jena.atlas.AtlasException)

Example 2 with RiotParseException

use of org.apache.jena.riot.RiotParseException in project jena by apache.

the class LangEngine method nextToken.

protected final Token nextToken() {
    if (eof())
        return tokenEOF;
    // Tokenizer errors appear here!
    try {
        Token t = peekIter.next();
        currLine = t.getLine();
        currCol = t.getColumn();
        return t;
    } catch (RiotParseException ex) {
        // Intercept to log it.
        raiseException(ex);
        throw ex;
    } catch (AtlasException ex) {
        // Bad I/O
        RiotParseException ex2 = new RiotParseException(ex.getMessage(), -1, -1);
        raiseException(ex2);
        throw ex2;
    }
}
Also used : RiotParseException(org.apache.jena.riot.RiotParseException) Token(org.apache.jena.riot.tokens.Token) AtlasException(org.apache.jena.atlas.AtlasException)

Example 3 with RiotParseException

use of org.apache.jena.riot.RiotParseException in project jena by apache.

the class Upload method incomingData.

public static UploadDetails incomingData(HttpAction action, StreamRDF dest) {
    ContentType ct = FusekiLib.getContentType(action);
    if (ct == null) {
        ServletOps.errorBadRequest("No content type");
        return null;
    }
    if (matchContentType(ctMultipartFormData, ct)) {
        return fileUploadWorker(action, dest);
    }
    // Single graph (or quads) in body.
    String base = ActionLib.wholeRequestURL(action.request);
    Lang lang = RDFLanguages.contentTypeToLang(ct.getContentType());
    if (lang == null) {
        ServletOps.errorBadRequest("Unknown content type for triples: " + ct);
        return null;
    }
    InputStream input = null;
    try {
        input = action.request.getInputStream();
    } catch (IOException ex) {
        IO.exception(ex);
    }
    int len = action.request.getContentLength();
    StreamRDFCounting countingDest = StreamRDFLib.count(dest);
    try {
        ActionSPARQL.parse(action, countingDest, input, lang, base);
        UploadDetails details = new UploadDetails(countingDest.count(), countingDest.countTriples(), countingDest.countQuads());
        action.log.info(format("[%d] Body: Content-Length=%d, Content-Type=%s, Charset=%s => %s : %s", action.id, len, ct.getContentType(), ct.getCharset(), lang.getName(), details.detailsStr()));
        return details;
    } catch (RiotParseException ex) {
        action.log.info(format("[%d] Body: Content-Length=%d, Content-Type=%s, Charset=%s => %s : %s", action.id, len, ct.getContentType(), ct.getCharset(), lang.getName(), ex.getMessage()));
        throw ex;
    }
}
Also used : RiotParseException(org.apache.jena.riot.RiotParseException) WebContent.matchContentType(org.apache.jena.riot.WebContent.matchContentType) ContentType(org.apache.jena.atlas.web.ContentType) GZIPInputStream(java.util.zip.GZIPInputStream) InputStream(java.io.InputStream) StreamRDFCounting(org.apache.jena.riot.lang.StreamRDFCounting) Lang(org.apache.jena.riot.Lang) IOException(java.io.IOException)

Example 4 with RiotParseException

use of org.apache.jena.riot.RiotParseException in project jena by apache.

the class Upload method fileUploadWorker.

/**  Process an HTTP upload of RDF files (triples or quads)
     *   Stream straight into a graph or dataset -- unlike SPARQL_Upload the destination
     *   is known at the start of the multipart file body
     */
public static UploadDetails fileUploadWorker(HttpAction action, StreamRDF dest) {
    String base = ActionLib.wholeRequestURL(action.request);
    ServletFileUpload upload = new ServletFileUpload();
    //log.info(format("[%d] Upload: Field=%s ignored", action.id, fieldName)) ;
    // Overall counting.
    StreamRDFCounting countingDest = StreamRDFLib.count(dest);
    try {
        FileItemIterator iter = upload.getItemIterator(action.request);
        while (iter.hasNext()) {
            FileItemStream fileStream = iter.next();
            if (fileStream.isFormField()) {
                // Ignore?
                String fieldName = fileStream.getFieldName();
                InputStream stream = fileStream.openStream();
                String value = Streams.asString(stream, "UTF-8");
                ServletOps.errorBadRequest(format("Only files accepted in multipart file upload (got %s=%s)", fieldName, value));
            }
            //Ignore the field name.
            //String fieldName = fileStream.getFieldName();
            InputStream stream = fileStream.openStream();
            // Process the input stream
            String contentTypeHeader = fileStream.getContentType();
            ContentType ct = ContentType.create(contentTypeHeader);
            Lang lang = null;
            if (!matchContentType(ctTextPlain, ct))
                lang = RDFLanguages.contentTypeToLang(ct.getContentType());
            if (lang == null) {
                String name = fileStream.getName();
                if (name == null || name.equals(""))
                    ServletOps.errorBadRequest("No name for content - can't determine RDF syntax");
                lang = RDFLanguages.filenameToLang(name);
                if (name.endsWith(".gz"))
                    stream = new GZIPInputStream(stream);
            }
            if (lang == null)
                // Desperate.
                lang = RDFLanguages.RDFXML;
            String printfilename = fileStream.getName();
            if (printfilename == null || printfilename.equals(""))
                printfilename = "<none>";
            // Before
            // action.log.info(format("[%d] Filename: %s, Content-Type=%s, Charset=%s => %s", 
            //                        action.id, printfilename,  ct.getContentType(), ct.getCharset(), lang.getName())) ;
            // count just this step
            StreamRDFCounting countingDest2 = StreamRDFLib.count(countingDest);
            try {
                ActionSPARQL.parse(action, countingDest2, stream, lang, base);
                UploadDetails details1 = new UploadDetails(countingDest2.count(), countingDest2.countTriples(), countingDest2.countQuads());
                action.log.info(format("[%d] Filename: %s, Content-Type=%s, Charset=%s => %s : %s", action.id, printfilename, ct.getContentType(), ct.getCharset(), lang.getName(), details1.detailsStr()));
            } catch (RiotParseException ex) {
                action.log.info(format("[%d] Filename: %s, Content-Type=%s, Charset=%s => %s : %s", action.id, printfilename, ct.getContentType(), ct.getCharset(), lang.getName(), ex.getMessage()));
                throw ex;
            }
        }
    } catch (ActionErrorException ex) {
        throw ex;
    } catch (Exception ex) {
        ServletOps.errorOccurred(ex.getMessage());
    }
    // Overall results.
    UploadDetails details = new UploadDetails(countingDest.count(), countingDest.countTriples(), countingDest.countQuads());
    return details;
}
Also used : RiotParseException(org.apache.jena.riot.RiotParseException) WebContent.matchContentType(org.apache.jena.riot.WebContent.matchContentType) ContentType(org.apache.jena.atlas.web.ContentType) GZIPInputStream(java.util.zip.GZIPInputStream) InputStream(java.io.InputStream) Lang(org.apache.jena.riot.Lang) IOException(java.io.IOException) RiotParseException(org.apache.jena.riot.RiotParseException) GZIPInputStream(java.util.zip.GZIPInputStream) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) FileItemStream(org.apache.commons.fileupload.FileItemStream) StreamRDFCounting(org.apache.jena.riot.lang.StreamRDFCounting) FileItemIterator(org.apache.commons.fileupload.FileItemIterator)

Example 5 with RiotParseException

use of org.apache.jena.riot.RiotParseException in project jena by apache.

the class JSONInputIterator method nextToken.

protected final Token nextToken() {
    if (eof())
        return tokenEOF;
    // Tokenizer errors appear here!
    try {
        Token t = peekIter.next();
        currLine = t.getLine();
        currCol = t.getColumn();
        return t;
    } catch (RiotParseException ex) {
        // Intercept to log it.
        raiseException(ex);
        throw ex;
    } catch (AtlasException ex) {
        // Bad I/O
        RiotParseException ex2 = new RiotParseException(ex.getMessage(), -1, -1);
        raiseException(ex2);
        throw ex2;
    }
}
Also used : RiotParseException(org.apache.jena.riot.RiotParseException) Token(org.apache.jena.riot.tokens.Token) AtlasException(org.apache.jena.atlas.AtlasException)

Aggregations

RiotParseException (org.apache.jena.riot.RiotParseException)5 AtlasException (org.apache.jena.atlas.AtlasException)3 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 GZIPInputStream (java.util.zip.GZIPInputStream)2 ContentType (org.apache.jena.atlas.web.ContentType)2 Lang (org.apache.jena.riot.Lang)2 WebContent.matchContentType (org.apache.jena.riot.WebContent.matchContentType)2 StreamRDFCounting (org.apache.jena.riot.lang.StreamRDFCounting)2 Token (org.apache.jena.riot.tokens.Token)2 FileItemIterator (org.apache.commons.fileupload.FileItemIterator)1 FileItemStream (org.apache.commons.fileupload.FileItemStream)1 ServletFileUpload (org.apache.commons.fileupload.servlet.ServletFileUpload)1