Search in sources :

Example 36 with IndentedWriter

use of org.apache.jena.atlas.io.IndentedWriter in project jena by apache.

the class Shex method printSchema.

/**
 * Print shapes - the format details the internal structure
 */
public static void printSchema(ShexSchema shapes) {
    IndentedWriter iOut = IndentedWriter.clone(IndentedWriter.stdout);
    iOut.setLinePrefix("");
    Set<String> visited = new HashSet<>();
    if (shapes.getSource() != null)
        visited.add(shapes.getSource());
    printSchema(iOut, shapes, visited);
}
Also used : IndentedWriter(org.apache.jena.atlas.io.IndentedWriter) HashSet(java.util.HashSet)

Example 37 with IndentedWriter

use of org.apache.jena.atlas.io.IndentedWriter in project jena by apache.

the class WriterRIOTShaclc method write.

@Override
public void write(OutputStream out, Graph graph, PrefixMap prefixMap, String baseURI, Context context) {
    IndentedWriter iOut = new IndentedWriter(out);
    try {
        iOut.setUnitIndent(4);
        write(iOut, graph, prefixMap, baseURI, context);
    } finally {
        iOut.flush();
    }
}
Also used : IndentedWriter(org.apache.jena.atlas.io.IndentedWriter)

Example 38 with IndentedWriter

use of org.apache.jena.atlas.io.IndentedWriter in project jena by apache.

the class WriterRIOTShaclc method write.

@Override
public void write(Writer out, Graph graph, PrefixMap prefixMap, String baseURI, Context context) {
    IndentedWriter iOut = RiotLib.create(out);
    try {
        iOut.setAbsoluteIndent(4);
        write(iOut, graph, prefixMap, baseURI, context);
    } finally {
        iOut.flush();
    }
}
Also used : IndentedWriter(org.apache.jena.atlas.io.IndentedWriter)

Example 39 with IndentedWriter

use of org.apache.jena.atlas.io.IndentedWriter in project jena by apache.

the class CompactWriter method output.

public static void output(IndentedWriter out, NodeFormatter nodeFmt, ShapeOutputVisitor visitor, Shape sh) {
    List<Target> targetImplicitClasses = sh.getTargets().stream().filter(t -> t.getTargetType() == TargetType.implicitClass).collect(Collectors.toList());
    List<Target> targetClasses = sh.getTargets().stream().filter(t -> t.getTargetType() == TargetType.targetClass).collect(Collectors.toList());
    if (targetImplicitClasses.isEmpty()) {
        out.print("shape ");
        nodeFmt.format(out, sh.getShapeNode());
        if (!targetClasses.isEmpty()) {
            out.print(" ->");
            targetClasses.forEach(t -> {
                out.print(" ");
                nodeFmt.format(out, t.getObject());
            });
        }
    } else {
        if (targetImplicitClasses.size() > 1)
            CompactWriter.notShaclc("Multiple implicit classes");
        if (!targetClasses.isEmpty())
            CompactWriter.notShaclc("Implicit classes and targetClass");
        Target target = targetImplicitClasses.get(0);
        out.print("shapeClass ");
        nodeFmt.format(out, target.getObject());
    }
    out.println(" {");
    out.incIndent();
    sh.visit(visitor);
    out.decIndent();
    out.ensureStartOfLine();
    out.println("}");
}
Also used : CollectionUtils(org.apache.jena.atlas.lib.CollectionUtils) SHACLC(org.apache.jena.shacl.compact.SHACLC) RiotLib(org.apache.jena.riot.system.RiotLib) Collection(java.util.Collection) Prefixes(org.apache.jena.riot.system.Prefixes) IndentedLineBuffer(org.apache.jena.atlas.io.IndentedLineBuffer) NodeFormatterTTL(org.apache.jena.riot.out.NodeFormatterTTL) PrefixMapping(org.apache.jena.shared.PrefixMapping) Collectors(java.util.stream.Collectors) IndentedWriter(org.apache.jena.atlas.io.IndentedWriter) Constraint(org.apache.jena.shacl.parser.Constraint) List(java.util.List) ShaclException(org.apache.jena.shacl.ShaclException) PrefixMap(org.apache.jena.riot.system.PrefixMap) WriterConst(org.apache.jena.riot.writer.WriterConst) Target(org.apache.jena.shacl.engine.Target) TargetType(org.apache.jena.shacl.engine.TargetType) Shape(org.apache.jena.shacl.parser.Shape) NodeFormatter(org.apache.jena.riot.out.NodeFormatter) Shapes(org.apache.jena.shacl.Shapes) Target(org.apache.jena.shacl.engine.Target)

Example 40 with IndentedWriter

use of org.apache.jena.atlas.io.IndentedWriter in project jena by apache.

the class qexpr method main2.

public static void main2(String... argv) {
    CmdLineArgs cl = new CmdLineArgs(argv);
    ArgDecl helpDecl = new ArgDecl(ArgDecl.NoValue, "h", "help");
    cl.add(helpDecl);
    ArgDecl verboseDecl = new ArgDecl(ArgDecl.NoValue, "v", "verbose");
    cl.add(verboseDecl);
    ArgDecl versionDecl = new ArgDecl(ArgDecl.NoValue, "ver", "version", "V");
    cl.add(versionDecl);
    ArgDecl quietDecl = new ArgDecl(ArgDecl.NoValue, "q", "quiet");
    cl.add(quietDecl);
    ArgDecl reduceDecl = new ArgDecl(ArgDecl.NoValue, "reduce", "fold", "simplify");
    cl.add(reduceDecl);
    ArgDecl strictDecl = new ArgDecl(ArgDecl.NoValue, "strict");
    cl.add(strictDecl);
    ArgDecl printDecl = new ArgDecl(ArgDecl.HasValue, "print");
    cl.add(printDecl);
    try {
        cl.process();
    } catch (IllegalArgumentException ex) {
        System.err.println(ex.getMessage());
        usage(System.err);
        throw new CmdException();
    }
    if (cl.contains(helpDecl)) {
        usage();
        throw new TerminationException(0);
    }
    if (cl.contains(versionDecl)) {
        System.out.println("ARQ Version: " + ARQ.VERSION + " (Jena: " + Jena.VERSION + ")");
        throw new TerminationException(0);
    }
    // ==== General things
    boolean verbose = cl.contains(verboseDecl);
    boolean quiet = cl.contains(quietDecl);
    if (cl.contains(strictDecl))
        ARQ.setStrictMode();
    boolean actionCopySubstitute = cl.contains(reduceDecl);
    boolean actionPrintPrefix = false;
    boolean actionPrintSPARQL = false;
    boolean actionPrint = cl.contains(printDecl);
    for (String v : cl.getValues(printDecl)) {
        if (v.equalsIgnoreCase("prefix") || v.equalsIgnoreCase("op")) {
            actionPrintPrefix = true;
        } else if (v.equalsIgnoreCase("expr")) {
            actionPrintSPARQL = true;
        } else {
            System.err.println("Unknown print form: " + v);
            throw new TerminationException(0);
        }
    }
    for (int i = 0; i < cl.getNumPositional(); i++) {
        String exprStr = cl.getPositionalArg(i);
        exprStr = cl.indirect(exprStr);
        try {
            PrefixMapping pmap = PrefixMapping.Factory.create();
            pmap.setNsPrefixes(ARQConstants.getGlobalPrefixMap());
            pmap.setNsPrefix("", "http://example/");
            pmap.setNsPrefix("ex", "http://example/ns#");
            Expr expr = ExprUtils.parse(exprStr, pmap);
            if (actionPrint) {
                IndentedWriter iOut = IndentedWriter.stdout;
                if (actionPrintSPARQL) {
                    ExprUtils.fmtSPARQL(iOut, expr);
                    iOut.ensureStartOfLine();
                }
                if (actionPrintPrefix) {
                    WriterSSE.out(iOut, expr, new Prologue(pmap));
                    iOut.ensureStartOfLine();
                }
                iOut.flush();
                continue;
            }
            if (verbose)
                System.out.print(expr.toString() + " => ");
            try {
                if (actionCopySubstitute) {
                    Expr e = ExprLib.foldConstants(expr);
                    System.out.println(e);
                } else {
                    // Default action
                    ARQ.getContext().set(ARQConstants.sysCurrentTime, NodeFactoryExtra.nowAsDateTime());
                    FunctionEnv env = new ExecutionContext(ARQ.getContext(), null, null, null);
                    NodeValue r = expr.eval(null, env);
                    // System.out.println(r.asQuotedString()) ;
                    Node n = r.asNode();
                    String s = NodeFmtLib.displayStr(n);
                    System.out.println(s);
                }
            } catch (ExprEvalException ex) {
                System.out.println("Exception: " + ex.getMessage());
                throw new TerminationException(2);
            }
        } catch (QueryParseException ex) {
            System.err.println("Parse error: " + ex.getMessage());
            throw new TerminationException(2);
        }
    }
}
Also used : NodeValue(org.apache.jena.sparql.expr.NodeValue) CmdException(org.apache.jena.cmd.CmdException) Node(org.apache.jena.graph.Node) ArgDecl(org.apache.jena.cmd.ArgDecl) CmdLineArgs(org.apache.jena.cmd.CmdLineArgs) QueryParseException(org.apache.jena.query.QueryParseException) IndentedWriter(org.apache.jena.atlas.io.IndentedWriter) FunctionEnv(org.apache.jena.sparql.function.FunctionEnv) PrefixMapping(org.apache.jena.shared.PrefixMapping) TerminationException(org.apache.jena.cmd.TerminationException) Prologue(org.apache.jena.sparql.core.Prologue) ExecutionContext(org.apache.jena.sparql.engine.ExecutionContext) Expr(org.apache.jena.sparql.expr.Expr) ExprEvalException(org.apache.jena.sparql.expr.ExprEvalException)

Aggregations

IndentedWriter (org.apache.jena.atlas.io.IndentedWriter)57 SerializationContext (org.apache.jena.sparql.serializer.SerializationContext)5 TerminationException (org.apache.jena.cmd.TerminationException)4 Node (org.apache.jena.graph.Node)4 PrefixMapping (org.apache.jena.shared.PrefixMapping)4 CmdException (org.apache.jena.cmd.CmdException)3 Triple (org.apache.jena.graph.Triple)3 Syntax (org.apache.jena.query.Syntax)3 IOException (java.io.IOException)2 ServletOutputStream (javax.servlet.ServletOutputStream)2 Pair (org.apache.jena.atlas.lib.Pair)2 RiotException (org.apache.jena.riot.RiotException)2 RiotNotFoundException (org.apache.jena.riot.RiotNotFoundException)2 NodeFormatter (org.apache.jena.riot.out.NodeFormatter)2 NodeFormatterTTL (org.apache.jena.riot.out.NodeFormatterTTL)2 JenaException (org.apache.jena.shared.JenaException)2 ARQException (org.apache.jena.sparql.ARQException)2 Var (org.apache.jena.sparql.core.Var)2 Plan (org.apache.jena.sparql.engine.Plan)2 Expr (org.apache.jena.sparql.expr.Expr)2