Search in sources :

Example 1 with VisitorException

use of org.openrdf.query.parser.sparql.ast.VisitorException in project QueryAnalysis by Wikidata.

the class OutputHandlerAnonymizer method writeLine.

@Override
public void writeLine(String queryToAnalyze, Validity validityStatus, String userAgent, String timeStamp, long currentLine, int currentDay, String currentFile) throws IOException {
    List<Object> line = new ArrayList<>();
    QueryHandler queryHandler = queryHandlerFactory.getQueryHandler(validityStatus, currentLine, currentDay, queryToAnalyze, userAgent, currentFile, threadNumber);
    if (queryHandler.getValidityStatus().equals(QueryHandler.Validity.VALID)) {
        ASTQueryContainer qc;
        try {
            qc = SyntaxTreeBuilder.parseQuery(queryToAnalyze);
        } catch (TokenMgrError | ParseException e) {
            logger.error("Failed to parse the query although it was found valid - this is a serious bug.", e);
            return;
        }
        try {
            StandardizingSPARQLParser.debug(qc);
            StringEscapesProcessor.process(qc);
            BaseDeclProcessor.process(qc, OpenRDFQueryHandler.BASE_URI);
            StandardizingPrefixDeclProcessor.process(qc);
            StandardizingSPARQLParser.anonymize(qc);
        } catch (MalformedQueryException e) {
            logger.error("Failed to debug or anonymize query. " + queryToAnalyze);
        }
        String renderedQueryString;
        try {
            renderedQueryString = qc.jjtAccept(new RenderVisitor(), "").toString();
        } catch (VisitorException e) {
            logger.error("Failed to render the query.", e);
            return;
        }
        try {
            new StandardizingSPARQLParser().parseQuery(renderedQueryString, OpenRDFQueryHandler.BASE_URI);
        } catch (MalformedQueryException e) {
            String queryName = this.threadNumber + "_" + this.failedQueriesNumber + ".query";
            logger.error("Anonymized query was not valid anymore. " + queryName, e);
            try (BufferedWriter bw = new BufferedWriter(new FileWriter(this.outputFile.substring(0, this.outputFile.lastIndexOf("/")) + "failedQueriesFolder/" + queryName))) {
                bw.write(queryToAnalyze);
                this.failedQueriesNumber++;
            } catch (IOException i) {
                logger.error("Could not write the failed query to failed queries folder.", i);
            }
            return;
        } catch (ClassCastException e) {
            logger.error("Unexpected class cast exception after anonymization.", e);
        }
        String encodedRenderedQueryString;
        try {
            encodedRenderedQueryString = URLEncoder.encode(renderedQueryString, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            logger.error("Apparently this system does not support UTF-8. Please fix this before running the program again.");
            return;
        }
        line.add(encodedRenderedQueryString);
        line.add(timeStamp);
        if (queryHandler.getSourceCategory().equals(QueryHandler.SourceCategory.USER)) {
            line.add("organic");
        } else {
            line.add("robotic");
        }
        if (QueryHandler.isOrganicUserAgent(queryHandler.getUserAgent())) {
            line.add("browser");
        } else if (Anonymizer.allowedToolNames.contains(queryHandler.getToolName())) {
            line.add(queryHandler.getToolName());
        } else if (Anonymizer.allowedUserAgents.containsKey(queryHandler.getUserAgent())) {
            line.add(Anonymizer.allowedUserAgents.get(queryHandler.getUserAgent()));
        } else {
            line.add("other");
        }
        csvPrinter.printRecord(line);
    }
}
Also used : OpenRDFQueryHandler(query.OpenRDFQueryHandler) QueryHandler(query.QueryHandler) ArrayList(java.util.ArrayList) ASTQueryContainer(org.openrdf.query.parser.sparql.ast.ASTQueryContainer) TokenMgrError(org.openrdf.query.parser.sparql.ast.TokenMgrError) RenderVisitor(openrdffork.RenderVisitor) StandardizingSPARQLParser(openrdffork.StandardizingSPARQLParser) MalformedQueryException(org.openrdf.query.MalformedQueryException) ParseException(org.openrdf.query.parser.sparql.ast.ParseException) VisitorException(org.openrdf.query.parser.sparql.ast.VisitorException)

Aggregations

ArrayList (java.util.ArrayList)1 RenderVisitor (openrdffork.RenderVisitor)1 StandardizingSPARQLParser (openrdffork.StandardizingSPARQLParser)1 MalformedQueryException (org.openrdf.query.MalformedQueryException)1 ASTQueryContainer (org.openrdf.query.parser.sparql.ast.ASTQueryContainer)1 ParseException (org.openrdf.query.parser.sparql.ast.ParseException)1 TokenMgrError (org.openrdf.query.parser.sparql.ast.TokenMgrError)1 VisitorException (org.openrdf.query.parser.sparql.ast.VisitorException)1 OpenRDFQueryHandler (query.OpenRDFQueryHandler)1 QueryHandler (query.QueryHandler)1