This is an automated email from the ASF dual-hosted git repository. andy pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/jena.git
commit 69ad039da5fd69348f0f0e4209b1fe95dee82743 Author: Andy Seaborne <[email protected]> AuthorDate: Tue Mar 17 12:45:36 2026 +0000 IndentedWriter: Provided flush-on-write mode --- .../apache/jena/atlas/io/IndentedLineBuffer.java | 12 +- .../org/apache/jena/atlas/io/IndentedWriter.java | 262 +++++++++++++++------ .../src/main/java/arq/cmdline/ModQueryOut.java | 84 +++---- jena-cmds/src/main/java/arq/query.java | 11 +- jena-cmds/src/main/java/arq/sse.java | 24 +- jena-cmds/src/main/java/arq/sse_query.java | 36 +-- .../fuseki/validation/html/ValidatorHtmlLib.java | 14 +- 7 files changed, 281 insertions(+), 162 deletions(-) diff --git a/jena-base/src/main/java/org/apache/jena/atlas/io/IndentedLineBuffer.java b/jena-base/src/main/java/org/apache/jena/atlas/io/IndentedLineBuffer.java index c27d1800a3..f832537465 100644 --- a/jena-base/src/main/java/org/apache/jena/atlas/io/IndentedLineBuffer.java +++ b/jena-base/src/main/java/org/apache/jena/atlas/io/IndentedLineBuffer.java @@ -18,24 +18,24 @@ * * SPDX-License-Identifier: Apache-2.0 */ - package org.apache.jena.atlas.io; import java.io.StringWriter ; -/** IndentLineBuffer is a buffer that records an indent level - * and uses that to insert a prefix at each line. - * It can also insert line numbers at the beginning of lines. +/** + * IndentLineBuffer is a buffer that records an indent level and uses that to insert + * a prefix at each line. It can also insert line numbers at the beginning of lines. */ - public class IndentedLineBuffer extends IndentedWriter { protected final StringWriter sw ; public IndentedLineBuffer() { this(false) ; } + @Deprecated(forRemoval = true) public IndentedLineBuffer(boolean withLineNumbers) { - super(new StringWriter(), withLineNumbers) ; + super(new StringWriter()) ; sw = (StringWriter)super.out ; + setLineNumbers(withLineNumbers); } public String asString() { return sw.toString() ; } diff --git a/jena-base/src/main/java/org/apache/jena/atlas/io/IndentedWriter.java b/jena-base/src/main/java/org/apache/jena/atlas/io/IndentedWriter.java index 43f46d1923..e135dbc0e4 100644 --- a/jena-base/src/main/java/org/apache/jena/atlas/io/IndentedWriter.java +++ b/jena-base/src/main/java/org/apache/jena/atlas/io/IndentedWriter.java @@ -20,6 +20,7 @@ */ package org.apache.jena.atlas.io; + import static java.lang.String.format; import java.io.IOException; @@ -31,15 +32,14 @@ import org.apache.jena.atlas.lib.Closeable; /** * A {@link AWriter} that records what the current indentation level is, and uses - * that to insert a prefix at each line. - * It can also insert line numbers at the beginning of lines. + * that to insert a prefix at each line. It can also insert line numbers at the + * beginning of lines. * <p> * An {@code IndentedWriter} flushed, bit does not close, the underlying output a * stream on close. */ -public class IndentedWriter extends AWriterBase implements AWriter, Closeable -{ +public class IndentedWriter extends AWriterBase implements AWriter, Closeable { /** Stdout wrapped in an IndentedWriter - no line numbers */ public static final IndentedWriter stdout = new IndentedWriter(System.out); /** Stderr wrapped in an IndentedWriter - no line numbers */ @@ -68,7 +68,8 @@ public class IndentedWriter extends AWriterBase implements AWriter, Closeable // unitIndent and padChar are ignored private String padString = null; - // String at the beginning of each line, before the line number and before the indentation. + // String at the beginning of each line, before the line number and before the + // indentation. private String linePrefix = null; protected boolean lineNumbers = false; @@ -77,6 +78,12 @@ public class IndentedWriter extends AWriterBase implements AWriter, Closeable // The latter is more efficient at scale but requires a flush at the end. private boolean flushOnNewline = false; + // Flush after very print/write operation. + // This is inefficient but when developing structured writers, + // buffering can hide how far the output got before a problem + // investigation. + private boolean flushOnWrite = false; + // Internal state. protected boolean startingNewLine = true; private String endOfLineMarker = null; @@ -84,22 +91,28 @@ public class IndentedWriter extends AWriterBase implements AWriter, Closeable protected int column = 0; protected int row = 1; - /** Construct a UTF8 IndentedWriter around an OutputStream */ - public IndentedWriter(OutputStream outStream) { this(outStream, false); } + /** Construct a UTF-8 IndentedWriter around an OutputStream */ + public IndentedWriter(OutputStream outStream) { + this(makeWriter(outStream)); + } - /** Construct a UTF8 IndentedWriter around an OutputStream */ + /** Construct a UTF-8 IndentedWriter around an OutputStream */ + @Deprecated(forRemoval = true) public IndentedWriter(OutputStream outStream, boolean withLineNumbers) { - this(makeWriter(outStream), withLineNumbers); + this(makeWriter(outStream)); + setLineNumbers(withLineNumbers); } - /** Create an independent copy of the {@code IndentedWriter}. - * Changes to the configuration of the copy will not affect the original {@code IndentedWriter}. - * This include indentation level. - * <br/>Row and column counters are reset. - * <br/>Indent is initially. zero. - * <br/>They do share the underlying output {@link Writer}. - * @param other - * @return IndentedWriter + /** + * Create an independent copy of an {@code IndentedWriter}. Changes to the + * configuration of the copy will not affect the original {@code IndentedWriter}. + * This include indentation level. <br/> + * Row and column counters are reset. <br/> + * Indent is initially. zero. <br/> + * They do share the underlying output {@link Writer}. + * + * @param other + * @return IndentedWriter */ public static IndentedWriter clone(IndentedWriter other) { IndentedWriter dup = new IndentedWriter(other.out); @@ -110,6 +123,7 @@ public class IndentedWriter extends AWriterBase implements AWriter, Closeable dup.lineNumbers = other.lineNumbers; dup.flatMode = other.flatMode; dup.flushOnNewline = other.flushOnNewline; + dup.flushOnWrite = other.flushOnWrite; return dup; } @@ -123,20 +137,21 @@ public class IndentedWriter extends AWriterBase implements AWriter, Closeable } /** - * Using {@link java.io.Writer Java I/O Writers} directly is discouraged - * and may case character encoding issues. + * Using {@link java.io.Writer Java I/O Writers} directly is discouraged and may + * case character encoding issues. */ protected IndentedWriter(Writer writer) { - this(writer, false); + out = writer; } /** - * Using {@link java.io.Writer Java I/O Writers} directly is discouraged - * and may case character encoding issues. + * Using {@link java.io.Writer Java I/O Writers} directly is discouraged and may + * case character encoding issues. */ + @Deprecated(forRemoval = true) protected IndentedWriter(Writer writer, boolean withLineNumbers) { - out = writer; - lineNumbers = withLineNumbers; + this(writer); + setLineNumbers(withLineNumbers); startingNewLine = true; } @@ -149,26 +164,44 @@ public class IndentedWriter extends AWriterBase implements AWriter, Closeable write$(str); return; } - for ( int i = 0; i < str.length(); i++ ) + for ( int i = 0 ; i < str.length() ; i++ ) printOneChar(str.charAt(i)); } @Override - public void printf(String formatStr, Object... args) { + public void printf(String formatStr, Object...args) { print(format(formatStr, args)); } @Override - public void print(char ch) { printOneChar(ch); } - public void print(Object obj) { print(String.valueOf(obj)); } + public void print(char ch) { + printOneChar(ch); + } + + public void print(Object obj) { + print(String.valueOf(obj)); + } @Override - public void println(String str) { print(str); newline(); } - public void println(char ch) { print(ch); newline(); } - public void println(Object obj) { print(String.valueOf(obj)); newline(); } + public void println(String str) { + print(str); + newline(); + } + + public void println(char ch) { + print(ch); + newline(); + } + + public void println(Object obj) { + print(String.valueOf(obj)); + newline(); + } @Override - public void println() { newline(); } + public void println() { + newline(); + } @Override public void print(char[] cbuf) { @@ -179,14 +212,14 @@ public class IndentedWriter extends AWriterBase implements AWriter, Closeable /** Print a string N times */ public void print(String s, int n) { - for ( int i = 0; i < n; i++ ) + for ( int i = 0 ; i < n ; i++ ) print(s); } /** Print a char N times */ public void print(char ch, int n) { lineStart(); - for ( int i = 0; i < n; i++ ) + for ( int i = 0 ; i < n ; i++ ) printOneChar(ch); } @@ -209,17 +242,33 @@ public class IndentedWriter extends AWriterBase implements AWriter, Closeable // newline if ( ch == '\n' || ch == '\r' ) { newline(); + if ( flushOnWrite && ! flushOnNewline ) + flush(); return; } write$(ch); column += 1; + if ( flushOnWrite ) + flush(); } - private void write$(char ch) - { try { out.write(ch); } catch (IOException ex) { IO.exception(ex); } } + // write : no newline processing is done (use printOneChar) + private void write$(char ch) { + try { + out.write(ch); + } catch (IOException ex) { + IO.exception(ex); + } + } - private void write$(String s) - { try { out.write(s); } catch (IOException ex) { IO.exception(ex); } } + // write : the string must npt include newlines. + private void write$(String s) { + try { + out.write(s); + } catch (IOException ex) { + IO.exception(ex); + } + } public void newline() { lineStart(); @@ -237,14 +286,18 @@ public class IndentedWriter extends AWriterBase implements AWriter, Closeable flush(); } - private boolean atStartOfLine() { return column <= currentIndent; } + private boolean atStartOfLine() { + return column <= currentIndent; + } public void ensureStartOfLine() { if ( !atStartOfLine() ) newline(); } - public boolean atLineStart() { return startingNewLine; } + public boolean atLineStart() { + return startingNewLine; + } // A line is prefix?number?content. private void lineStart() { @@ -270,12 +323,14 @@ public class IndentedWriter extends AWriterBase implements AWriter, Closeable @Override public void close() { // flush, not close. - // IndentedWriters wrap an underlying output, often a short term additional layer. + // IndentedWriters wrap an underlying output, often a short term, additional layer. flush(); } @Override - public void flush() { IO.flush(out); } + public void flush() { + IO.flush(out); + } /** Pad to the indent (if we are before it) */ public void pad() { @@ -284,13 +339,17 @@ public class IndentedWriter extends AWriterBase implements AWriter, Closeable padInternal(); } - /** Pad to a given number of columns EXCLUDING the indent. + /** + * Pad to a given number of columns EXCLUDING the indent. * * @param col Column number (first column is 1). */ - public void pad(int col) { pad(col, false); } + public void pad(int col) { + pad(col, false); + } - /** Pad to a given number of columns maybe including the indent. + /** + * Pad to a given number of columns maybe including the indent. * * @param col Column number (first column is 1). * @param absoluteColumn Whether to include the indent @@ -300,7 +359,7 @@ public class IndentedWriter extends AWriterBase implements AWriter, Closeable if ( !absoluteColumn ) col = col + currentIndent; int spaces = col - column; - for ( int i = 0; i < spaces; i++ ) { + for ( int i = 0 ; i < spaces ; i++ ) { write$(' '); // Always a space. column++; } @@ -308,12 +367,12 @@ public class IndentedWriter extends AWriterBase implements AWriter, Closeable private void padInternal() { if ( padString == null ) { - for ( int i = column; i < currentIndent; i++ ) { + for ( int i = column ; i < currentIndent ; i++ ) { write$(padChar); column++; } } else { - for ( int i = column; i < currentIndent; i += padString.length() ) { + for ( int i = column ; i < currentIndent ; i += padString.length() ) { write$(padString); column += padString.length(); } @@ -321,11 +380,14 @@ public class IndentedWriter extends AWriterBase implements AWriter, Closeable } /** Get row/line (counts from 1) */ - public int getRow() { return row; } + public int getRow() { + return row; + } - /** Get the absolute column. - * This is the location where the next character on the line will be printed. - * The IndentedWriter may not yet have padded to this place. + /** + * Get the absolute column. This is the location where the next character on the + * line will be printed. The IndentedWriter may not yet have padded to this + * place. */ public int getCol() { if ( currentIndent > column ) @@ -333,13 +395,17 @@ public class IndentedWriter extends AWriterBase implements AWriter, Closeable return column; } - public void incIndent() { incIndent(unitIndent); } + public void incIndent() { + incIndent(unitIndent); + } public void incIndent(int x) { currentIndent += x; } - public void decIndent() { decIndent(unitIndent); } + public void decIndent() { + decIndent(unitIndent); + } public void decIndent(int x) { currentIndent -= x; @@ -354,13 +420,20 @@ public class IndentedWriter extends AWriterBase implements AWriter, Closeable return 0; } - protected IndentedWriter self() { return this; } + protected IndentedWriter self() { + return this; + } /** Get indent from the left hand edge */ - public int getAbsoluteIndent() { return currentIndent; } + public int getAbsoluteIndent() { + return currentIndent; + } /** Set indent from the left hand edge. Returns {@code this}. */ - public IndentedWriter setAbsoluteIndent(int x) { currentIndent = x; return self(); } + public IndentedWriter setAbsoluteIndent(int x) { + currentIndent = x; + return self(); + } public boolean hasLineNumbers() { return lineNumbers; @@ -371,37 +444,71 @@ public class IndentedWriter extends AWriterBase implements AWriter, Closeable return self(); } - public String getEndOfLineMarker() { return endOfLineMarker; } + public String getEndOfLineMarker() { + return endOfLineMarker; + } - /** Set the marker included at end of line - set to null for "none". Usually used for debugging. */ - public IndentedWriter setEndOfLineMarker(String marker) { endOfLineMarker = marker; return self(); } + /** + * Set the marker included at end of line - set to null for "none". Usually used + * for debugging. + */ + public IndentedWriter setEndOfLineMarker(String marker) { + endOfLineMarker = marker; + return self(); + } + + /** Flat mode - print without NL, for a more compact representation */ + public boolean inFlatMode() { + return flatMode; + } /** Flat mode - print without NL, for a more compact representation */ - public boolean inFlatMode() { return flatMode; } - /** Flat mode - print without NL, for a more compact representation*/ public IndentedWriter setFlatMode(boolean flatMode) { this.flatMode = flatMode; return self(); } + /** Flush after every print/write operation. Thsio can be expensive but when developing structured writers, **/ + public boolean getFlushOnWrite() { + return flushOnWrite; + } + + /* Set to flush after every print/write operation. This can be expensive but when developing structured writers, + * buffering makes it harder to trace progress. + */ + public IndentedWriter setFlushOnWrite(boolean flushOnWrite) { + this.flushOnWrite = flushOnWrite; + return self(); + } + /** Flush on newline **/ - public boolean getFlushOnNewline() { return flushOnNewline; } + public boolean getFlushOnNewline() { + return flushOnNewline; + } - /** Flush on newline in this code. - * This is set for {@link IndentedWriter#stdout} and {@link IndentedWriter#stderr} - * but not by default otherwise. The underlying output, if it is a {@link PrintStream} - * may also have a flush on newline as well (e.g {@link System#out}). + /** + * Flush on newline in this code. This is set for {@link IndentedWriter#stdout} + * and {@link IndentedWriter#stderr} but not by default otherwise. The underlying + * output, if it is a {@link PrintStream} may also have a flush on newline as + * well (e.g {@link System#out}). */ public IndentedWriter setFlushOnNewline(boolean flushOnNewline) { this.flushOnNewline = flushOnNewline; return self(); } - public char getPadChar() { return padChar; } + public char getPadChar() { + return padChar; + } - public IndentedWriter setPadChar(char ch) { this.padChar = ch; return self(); } + public IndentedWriter setPadChar(char ch) { + this.padChar = ch; + return self(); + } - public String getPadString() { return padString; } + public String getPadString() { + return padString; + } public void setPadString(String str) { this.padString = str; @@ -419,7 +526,9 @@ public class IndentedWriter extends AWriterBase implements AWriter, Closeable return self(); } - public int getUnitIndent() { return unitIndent; } + public int getUnitIndent() { + return unitIndent; + } public IndentedWriter setUnitIndent(int x) { unitIndent = x; @@ -429,10 +538,13 @@ public class IndentedWriter extends AWriterBase implements AWriter, Closeable private int widthLineNumber = 3; /** Width of the number field */ - public int getNumberWidth() { return widthLineNumber; } + public int getNumberWidth() { + return widthLineNumber; + } - /** Set the width of the number field. - * There is also a single space after the number not included in this setting. + /** + * Set the width of the number field. There is also a single space after the + * number not included in this setting. */ public IndentedWriter setNumberWidth(int widthOfNumbers) { widthLineNumber = widthOfNumbers; @@ -443,7 +555,7 @@ public class IndentedWriter extends AWriterBase implements AWriter, Closeable if ( !lineNumbers ) return; String s = Integer.toString(row); - for ( int i = 0; i < widthLineNumber - s.length(); i++ ) + for ( int i = 0 ; i < widthLineNumber - s.length() ; i++ ) write$(' '); write$(s); write$(' '); diff --git a/jena-cmds/src/main/java/arq/cmdline/ModQueryOut.java b/jena-cmds/src/main/java/arq/cmdline/ModQueryOut.java index 1fbc9e19bd..c688fa5a44 100644 --- a/jena-cmds/src/main/java/arq/cmdline/ModQueryOut.java +++ b/jena-cmds/src/main/java/arq/cmdline/ModQueryOut.java @@ -21,7 +21,6 @@ package arq.cmdline; - import org.apache.jena.atlas.io.IndentedWriter; import org.apache.jena.cmd.ArgDecl; import org.apache.jena.cmd.CmdArgModule; @@ -31,64 +30,65 @@ import org.apache.jena.query.Query; import org.apache.jena.query.Syntax; import org.apache.jena.sparql.util.QueryOutputUtils; -public class ModQueryOut extends ModBase -{ - protected final ArgDecl queryOutputSyntaxDecl = new ArgDecl(ArgDecl.HasValue, "out", "format"); - protected final ArgDecl queryNumberDecl = new ArgDecl(ArgDecl.NoValue, "num", "number"); +public class ModQueryOut extends ModBase { + protected final ArgDecl queryOutputSyntaxDecl = new ArgDecl(ArgDecl.HasValue, "out", "format"); + protected final ArgDecl queryNumberDecl = new ArgDecl(ArgDecl.NoValue, "num", "number"); private Syntax outputSyntax = Syntax.syntaxSPARQL; private boolean lineNumbers = false; - + @Override - public void registerWith(CmdGeneral cmdLine) - { + public void registerWith(CmdGeneral cmdLine) { cmdLine.getUsage().startCategory("Output"); - cmdLine.add(queryOutputSyntaxDecl, "--out, --format", "Output syntax"); + cmdLine.add(queryOutputSyntaxDecl, "--out, --format", "Output syntax"); cmdLine.add(queryNumberDecl, "--num", "Print line numbers"); } @Override - public void processArgs(CmdArgModule cmdline) throws IllegalArgumentException - { - if ( cmdline.contains(queryOutputSyntaxDecl) ) - { + public void processArgs(CmdArgModule cmdline) throws IllegalArgumentException { + if ( cmdline.contains(queryOutputSyntaxDecl) ) { // short name String s = cmdline.getValue(queryOutputSyntaxDecl); Syntax syn = Syntax.lookup(s); if ( syn == null ) - cmdline.cmdError("Unrecognized syntax: "+s); - outputSyntax = syn; - } - + cmdline.cmdError("Unrecognized syntax: " + s); + outputSyntax = syn; + } + lineNumbers = cmdline.contains(queryNumberDecl); } - - public Syntax getOutputSyntax() - { + + public Syntax getOutputSyntax() { return outputSyntax; } - public void output(Query query) - { output(out(), query); } - - public void output(IndentedWriter out, Query query) - { QueryOutputUtils.printQuery(out, query, outputSyntax); } - - public void outputOp(Query query, boolean printOptimized) - { outputOp(out(), query, printOptimized); } - - public void outputOp(IndentedWriter out, Query query, boolean printOptimized) - { QueryOutputUtils.printOp(out, query, printOptimized); } - - public void outputQuad(Query query, boolean printOptimized) - { outputQuad(out(), query, printOptimized); } - - public void outputQuad(IndentedWriter out, Query query, boolean printOptimized) - { QueryOutputUtils.printQuad(out, query, printOptimized); } - - private IndentedWriter out() - { - return new IndentedWriter(System.out, lineNumbers); + public void output(Query query) { + output(out(), query); } - + + public void output(IndentedWriter out, Query query) { + QueryOutputUtils.printQuery(out, query, outputSyntax); + } + + public void outputOp(Query query, boolean printOptimized) { + outputOp(out(), query, printOptimized); + } + + public void outputOp(IndentedWriter out, Query query, boolean printOptimized) { + QueryOutputUtils.printOp(out, query, printOptimized); + } + + public void outputQuad(Query query, boolean printOptimized) { + outputQuad(out(), query, printOptimized); + } + + public void outputQuad(IndentedWriter out, Query query, boolean printOptimized) { + QueryOutputUtils.printQuad(out, query, printOptimized); + } + + @SuppressWarnings("resource") + private IndentedWriter out() { + return new IndentedWriter(System.out).setLineNumbers(lineNumbers); + } + } diff --git a/jena-cmds/src/main/java/arq/query.java b/jena-cmds/src/main/java/arq/query.java index fbbae8b5cb..3250f8330c 100644 --- a/jena-cmds/src/main/java/arq/query.java +++ b/jena-cmds/src/main/java/arq/query.java @@ -222,11 +222,12 @@ public class query extends CmdARQ try { Query query = getQuery(); if ( isVerbose() ) { - IndentedWriter out = new IndentedWriter(resultsDest, true); - query.serialize(out); - out.setLineNumbers(false); - out.println(); - out.flush(); + try ( IndentedWriter out = new IndentedWriter(resultsDest) ) { + out.setLineNumbers(true); + query.serialize(out); + out.setLineNumbers(false); + out.println(); + } } if ( isQuiet() ) diff --git a/jena-cmds/src/main/java/arq/sse.java b/jena-cmds/src/main/java/arq/sse.java index afd0d065b2..0e6a39a338 100644 --- a/jena-cmds/src/main/java/arq/sse.java +++ b/jena-cmds/src/main/java/arq/sse.java @@ -63,7 +63,7 @@ public class sse extends CmdARQ_SSE print = !contains(noPrintDecl); if ( contains(numberDecl) ) lineNumbers = getValue(numberDecl).equalsIgnoreCase("on"); - + if ( contains(noResolveDecl) ) SSE.setUseResolver(false); } @@ -89,22 +89,22 @@ public class sse extends CmdARQ_SSE { if ( ! print ) return; - + if ( item == null ) { System.err.println("No expression"); throw new TerminationException(9); } divider(); - IndentedWriter out = new IndentedWriter(System.out, lineNumbers); - - // Need to check if used. - //PrefixMapping pmap = SSE.getDefaultPrefixMapWrite(); - PrefixMapping pmap = null; - SerializationContext sCxt = new SerializationContext(pmap); - ItemWriter.write(out, item, sCxt); - //item.output(out); - out.ensureStartOfLine(); - out.flush(); + try ( IndentedWriter out = new IndentedWriter(System.out) ) { + out.setLineNumbers(lineNumbers); + // Need to check if used. + //PrefixMapping pmap = SSE.getDefaultPrefixMapWrite(); + PrefixMapping pmap = null; + SerializationContext sCxt = new SerializationContext(pmap); + ItemWriter.write(out, item, sCxt); + //item.output(out); + out.ensureStartOfLine(); + } } } diff --git a/jena-cmds/src/main/java/arq/sse_query.java b/jena-cmds/src/main/java/arq/sse_query.java index bd79ad6221..f9d0d2667a 100644 --- a/jena-cmds/src/main/java/arq/sse_query.java +++ b/jena-cmds/src/main/java/arq/sse_query.java @@ -52,9 +52,9 @@ public class sse_query extends CmdARQ // 2 - This is then calls on the QueryExecution parts // 3 - Printing plan - uses a verbose prefix setting. Scan to see what's in use. // WriterOp.reducePrologue(prologue, op) => prologue. - + protected final ArgDecl printDecl = new ArgDecl(ArgDecl.HasValue, "print"); - + ModAlgebra modAlgebra = new ModAlgebra(); ModDataset modDataset = new ModDatasetGeneralAssembler(); ModResultsOut modResults = new ModResultsOut(); @@ -63,12 +63,12 @@ public class sse_query extends CmdARQ boolean printOp = false; boolean printPlan = false; - + public static void main (String... argv) { new sse_query(argv).mainRun(); } - + public sse_query(String[] argv) { super(argv); @@ -88,18 +88,18 @@ public class sse_query extends CmdARQ for (String arg : getValues(printDecl)) { if ( arg.equalsIgnoreCase("op") || - arg.equalsIgnoreCase("alg") || + arg.equalsIgnoreCase("alg") || arg.equalsIgnoreCase("algebra") ) { printOp = true; } else if ( arg.equalsIgnoreCase("plan")) { printPlan = true; } else throw new CmdException("Not a recognized print form: "+arg+" : Choices are: query, op, quad"); } - + } - + @Override protected String getCommandName() { return Lib.className(this); } - + @Override protected String getSummary() { return getCommandName()+" --data=<file> --query=<query>"; } @@ -111,7 +111,7 @@ public class sse_query extends CmdARQ if ( needDivider ) System.out.println(divider); needDivider = true; } - + @Override protected void exec() { @@ -136,9 +136,11 @@ public class sse_query extends CmdARQ if ( printOp ) { divider(); - IndentedWriter out = new IndentedWriter(System.out, true); - op.output(out); - out.flush(); + try ( IndentedWriter out = new IndentedWriter(System.out)) { + out.setLineNumbers(true); + op.output(out); + out.flush(); + } } if ( printPlan ) @@ -146,9 +148,11 @@ public class sse_query extends CmdARQ QueryIterator qIter = Algebra.exec(op, dsg); Plan plan = new PlanOp(op, null, qIter); divider(); - IndentedWriter out = new IndentedWriter(System.out, false); - plan.output(out); - out.flush(); + try ( IndentedWriter out = new IndentedWriter(System.out)) { + out.setLineNumbers(true); + plan.output(out); + out.flush(); + } } //return; } @@ -159,6 +163,6 @@ public class sse_query extends CmdARQ long time = modTime.endTimer(); if ( modTime.timingEnabled() ) System.out.println("Time: "+modTime.timeStr(time)); - } + } } diff --git a/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/validation/html/ValidatorHtmlLib.java b/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/validation/html/ValidatorHtmlLib.java index 4e22260dd4..8e962a4593 100644 --- a/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/validation/html/ValidatorHtmlLib.java +++ b/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/validation/html/ValidatorHtmlLib.java @@ -52,12 +52,14 @@ public class ValidatorHtmlLib { public static void output(ServletOutputStream outStream, Consumer<IndentedLineBuffer> content, boolean lineNumbers) throws IOException { startFixed(outStream); - IndentedLineBuffer out = new IndentedLineBuffer(lineNumbers); - content.accept(out); - out.flush(); - String x = htmlQuote(out.asString()); - byte b[] = x.getBytes(StandardCharsets.UTF_8); - outStream.write(b); + try ( IndentedLineBuffer out = new IndentedLineBuffer() ) { + out.setLineNumbers(lineNumbers); + content.accept(out); + out.flush(); + String x = htmlQuote(out.asString()); + byte b[] = x.getBytes(StandardCharsets.UTF_8); + outStream.write(b); + } finishFixed(outStream); }
