diff --git a/jOOQ/src/main/java/org/jooq/JSONFormat.java b/jOOQ/src/main/java/org/jooq/JSONFormat.java index db38325ba3..ca18dd1a5c 100644 --- a/jOOQ/src/main/java/org/jooq/JSONFormat.java +++ b/jOOQ/src/main/java/org/jooq/JSONFormat.java @@ -73,18 +73,20 @@ public final class JSONFormat { public final static JSONFormat DEFAULT_FOR_RESULTS = new JSONFormat(); public final static JSONFormat DEFAULT_FOR_RECORDS = new JSONFormat().header(false); - final boolean format; - final String newline; - final int globalIndent; - final int indent; - final String[] indented; - final boolean header; - final RecordFormat recordFormat; - final boolean wrapSingleColumnRecords; - final boolean quoteNested; + final boolean mutable; + boolean format; + String newline; + int globalIndent; + int indent; + String[] indented; + boolean header; + RecordFormat recordFormat; + boolean wrapSingleColumnRecords; + boolean quoteNested; public JSONFormat() { this( + false, false, "\n", 0, @@ -98,6 +100,7 @@ public final class JSONFormat { } private JSONFormat( + boolean mutable, boolean format, String newline, int globalIndent, @@ -108,6 +111,7 @@ public final class JSONFormat { boolean wrapSingleColumnRecords, boolean quoteNested ) { + this.mutable = mutable; this.format = format; this.newline = newline; this.globalIndent = globalIndent; @@ -124,22 +128,57 @@ public final class JSONFormat { this.quoteNested = quoteNested; } + /** + * Whether this configuration object is mutable. + */ + public final boolean mutable() { + return mutable; + } + + /** + * The new value for the mutable flag, defaulting to false. + */ + @NotNull + public final JSONFormat mutable(boolean newMutable) { + if (mutable ^ newMutable) + return new JSONFormat( + newMutable, + format, + newline, + globalIndent, + indent, + null, + header, + recordFormat, + wrapSingleColumnRecords, + quoteNested + ); + else + return this; + } + /** * The new value for the formatting flag, defaulting to false. */ @NotNull public final JSONFormat format(boolean newFormat) { - return new JSONFormat( - newFormat, - newline, - globalIndent, - indent, - null, - header, - recordFormat, - wrapSingleColumnRecords, - quoteNested - ); + if (mutable) { + format = newFormat; + return this; + } + else + return new JSONFormat( + mutable, + newFormat, + newline, + globalIndent, + indent, + null, + header, + recordFormat, + wrapSingleColumnRecords, + quoteNested + ); } /** @@ -154,17 +193,23 @@ public final class JSONFormat { */ @NotNull public final JSONFormat newline(String newNewline) { - return new JSONFormat( - format, - newNewline, - globalIndent, - indent, - indented, - header, - recordFormat, - wrapSingleColumnRecords, - quoteNested - ); + if (mutable) { + newline = newNewline; + return this; + } + else + return new JSONFormat( + mutable, + format, + newNewline, + globalIndent, + indent, + indented, + header, + recordFormat, + wrapSingleColumnRecords, + quoteNested + ); } /** @@ -180,17 +225,23 @@ public final class JSONFormat { */ @NotNull public final JSONFormat globalIndent(int newGlobalIndent) { - return new JSONFormat( - format, - newline, - newGlobalIndent, - indent, - null, - header, - recordFormat, - wrapSingleColumnRecords, - quoteNested - ); + if (mutable) { + globalIndent = newGlobalIndent; + return this; + } + else + return new JSONFormat( + mutable, + format, + newline, + newGlobalIndent, + indent, + null, + header, + recordFormat, + wrapSingleColumnRecords, + quoteNested + ); } /** @@ -205,17 +256,23 @@ public final class JSONFormat { */ @NotNull public final JSONFormat indent(int newIndent) { - return new JSONFormat( - format, - newline, - globalIndent, - newIndent, - null, - header, - recordFormat, - wrapSingleColumnRecords, - quoteNested - ); + if (mutable) { + indent = newIndent; + return this; + } + else + return new JSONFormat( + mutable, + format, + newline, + globalIndent, + newIndent, + null, + header, + recordFormat, + wrapSingleColumnRecords, + quoteNested + ); } /** @@ -246,17 +303,23 @@ public final class JSONFormat { */ @NotNull public final JSONFormat header(boolean newHeader) { - return new JSONFormat( - format, - newline, - globalIndent, - indent, - indented, - newHeader, - recordFormat, - wrapSingleColumnRecords, - quoteNested - ); + if (mutable) { + header = newHeader; + return this; + } + else + return new JSONFormat( + mutable, + format, + newline, + globalIndent, + indent, + indented, + newHeader, + recordFormat, + wrapSingleColumnRecords, + quoteNested + ); } /** @@ -273,17 +336,23 @@ public final class JSONFormat { */ @NotNull public final JSONFormat recordFormat(RecordFormat newRecordFormat) { - return new JSONFormat( - format, - newline, - globalIndent, - indent, - indented, - header, - newRecordFormat, - wrapSingleColumnRecords, - quoteNested - ); + if (mutable) { + recordFormat = newRecordFormat; + return this; + } + else + return new JSONFormat( + mutable, + format, + newline, + globalIndent, + indent, + indented, + header, + newRecordFormat, + wrapSingleColumnRecords, + quoteNested + ); } /** @@ -300,17 +369,23 @@ public final class JSONFormat { */ @NotNull public final JSONFormat wrapSingleColumnRecords(boolean newWrapSingleColumnRecords) { - return new JSONFormat( - format, - newline, - globalIndent, - indent, - indented, - header, - recordFormat, - newWrapSingleColumnRecords, - quoteNested - ); + if (mutable) { + wrapSingleColumnRecords = newWrapSingleColumnRecords; + return this; + } + else + return new JSONFormat( + mutable, + format, + newline, + globalIndent, + indent, + indented, + header, + recordFormat, + newWrapSingleColumnRecords, + quoteNested + ); } /** @@ -326,17 +401,23 @@ public final class JSONFormat { */ @NotNull public final JSONFormat quoteNested(boolean newQuoteNested) { - return new JSONFormat( - format, - newline, - globalIndent, - indent, - indented, - header, - recordFormat, - wrapSingleColumnRecords, - newQuoteNested - ); + if (mutable) { + quoteNested = newQuoteNested; + return this; + } + else + return new JSONFormat( + mutable, + format, + newline, + globalIndent, + indent, + indented, + header, + recordFormat, + wrapSingleColumnRecords, + newQuoteNested + ); } /** diff --git a/jOOQ/src/main/java/org/jooq/XMLFormat.java b/jOOQ/src/main/java/org/jooq/XMLFormat.java index 24b12253ca..29ed5229c4 100644 --- a/jOOQ/src/main/java/org/jooq/XMLFormat.java +++ b/jOOQ/src/main/java/org/jooq/XMLFormat.java @@ -54,18 +54,20 @@ public final class XMLFormat { public final static XMLFormat DEFAULT_FOR_RESULTS = new XMLFormat(); public final static XMLFormat DEFAULT_FOR_RECORDS = new XMLFormat().header(false).xmlns(false); - final boolean xmlns; - final boolean format; - final String newline; - final int globalIndent; - final int indent; - final String[] indented; - final boolean header; - final RecordFormat recordFormat; - final boolean quoteNested; + final boolean mutable; + boolean xmlns; + boolean format; + String newline; + int globalIndent; + int indent; + String[] indented; + boolean header; + RecordFormat recordFormat; + boolean quoteNested; public XMLFormat() { this( + false, true, false, "\n", @@ -79,6 +81,7 @@ public final class XMLFormat { } private XMLFormat( + boolean mutable, boolean xmlns, boolean format, String newline, @@ -89,6 +92,7 @@ public final class XMLFormat { RecordFormat recordFormat, boolean quoteNested ) { + this.mutable = mutable; this.xmlns = xmlns; this.format = format; this.newline = newline; @@ -105,22 +109,57 @@ public final class XMLFormat { this.quoteNested = quoteNested; } + /** + * Whether this configuration object is mutable. + */ + public final boolean mutable() { + return mutable; + } + + /** + * The new value for the mutable flag, defaulting to false. + */ + @NotNull + public final XMLFormat mutable(boolean newMutable) { + if (mutable ^ newMutable) + return new XMLFormat( + newMutable, + xmlns, + format, + newline, + globalIndent, + indent, + indented, + header, + recordFormat, + quoteNested + ); + else + return this; + } + /** * The new value for the xmlns flag, defaulting to true. */ @NotNull public final XMLFormat xmlns(boolean newXmlns) { - return new XMLFormat( - newXmlns, - format, - newline, - globalIndent, - indent, - indented, - header, - recordFormat, - quoteNested - ); + if (mutable) { + xmlns = newXmlns; + return this; + } + else + return new XMLFormat( + mutable, + newXmlns, + format, + newline, + globalIndent, + indent, + indented, + header, + recordFormat, + quoteNested + ); } /** @@ -135,17 +174,23 @@ public final class XMLFormat { */ @NotNull public final XMLFormat format(boolean newFormat) { - return new XMLFormat( - xmlns, - newFormat, - newline, - globalIndent, - indent, - null, - header, - recordFormat, - quoteNested - ); + if (mutable) { + format = newFormat; + return this; + } + else + return new XMLFormat( + mutable, + xmlns, + newFormat, + newline, + globalIndent, + indent, + null, + header, + recordFormat, + quoteNested + ); } /** @@ -160,17 +205,23 @@ public final class XMLFormat { */ @NotNull public final XMLFormat newline(String newNewline) { - return new XMLFormat( - xmlns, - format, - newNewline, - globalIndent, - indent, - indented, - header, - recordFormat, - quoteNested - ); + if (mutable) { + newline = newNewline; + return this; + } + else + return new XMLFormat( + mutable, + xmlns, + format, + newNewline, + globalIndent, + indent, + indented, + header, + recordFormat, + quoteNested + ); } /** @@ -186,17 +237,23 @@ public final class XMLFormat { */ @NotNull public final XMLFormat globalIndent(int newGlobalIndent) { - return new XMLFormat( - xmlns, - format, - newline, - newGlobalIndent, - indent, - null, - header, - recordFormat, - quoteNested - ); + if (mutable) { + globalIndent = newGlobalIndent; + return this; + } + else + return new XMLFormat( + mutable, + xmlns, + format, + newline, + newGlobalIndent, + indent, + null, + header, + recordFormat, + quoteNested + ); } /** @@ -211,17 +268,23 @@ public final class XMLFormat { */ @NotNull public final XMLFormat indent(int newIndent) { - return new XMLFormat( - xmlns, - format, - newline, - globalIndent, - newIndent, - null, - header, - recordFormat, - quoteNested - ); + if (mutable) { + indent = newIndent; + return this; + } + else + return new XMLFormat( + mutable, + xmlns, + format, + newline, + globalIndent, + newIndent, + null, + header, + recordFormat, + quoteNested + ); } /** @@ -257,17 +320,23 @@ public final class XMLFormat { */ @NotNull public final XMLFormat header(boolean newHeader) { - return new XMLFormat( - xmlns, - format, - newline, - globalIndent, - indent, - indented, - newHeader, - recordFormat, - quoteNested - ); + if (mutable) { + header = newHeader; + return this; + } + else + return new XMLFormat( + mutable, + xmlns, + format, + newline, + globalIndent, + indent, + indented, + newHeader, + recordFormat, + quoteNested + ); } /** @@ -283,17 +352,23 @@ public final class XMLFormat { */ @NotNull public final XMLFormat recordFormat(RecordFormat newRecordFormat) { - return new XMLFormat( - xmlns, - format, - newline, - globalIndent, - indent, - indented, - header, - newRecordFormat, - quoteNested - ); + if (mutable) { + recordFormat = newRecordFormat; + return this; + } + else + return new XMLFormat( + mutable, + xmlns, + format, + newline, + globalIndent, + indent, + indented, + header, + newRecordFormat, + quoteNested + ); } /** @@ -311,17 +386,23 @@ public final class XMLFormat { */ @NotNull public final XMLFormat quoteNested(boolean newQuoteNested) { - return new XMLFormat( - xmlns, - format, - newline, - globalIndent, - indent, - indented, - header, - recordFormat, - newQuoteNested - ); + if (mutable) { + quoteNested = newQuoteNested; + return this; + } + else + return new XMLFormat( + mutable, + xmlns, + format, + newline, + globalIndent, + indent, + indented, + header, + recordFormat, + newQuoteNested + ); } /** diff --git a/jOOQ/src/main/java/org/jooq/impl/AbstractRecord.java b/jOOQ/src/main/java/org/jooq/impl/AbstractRecord.java index 2d1a59139f..03e127dc1d 100644 --- a/jOOQ/src/main/java/org/jooq/impl/AbstractRecord.java +++ b/jOOQ/src/main/java/org/jooq/impl/AbstractRecord.java @@ -1039,8 +1039,7 @@ abstract class AbstractRecord extends AbstractStore implements Record { @Override public final void formatJSON(Writer writer, JSONFormat format) { - if (format.header()) - log.debug("JSONFormat.header currently not supported for Record.formatJSON()"); + format = format.mutable(true); try { switch (format.recordFormat()) { @@ -1063,8 +1062,7 @@ abstract class AbstractRecord extends AbstractStore implements Record { @Override public final void formatXML(Writer writer, XMLFormat format) { - if (format.header()) - log.debug("XMLFormat.header currently not supported for Record.formatXML()"); + format = format.mutable(true); try { AbstractResult.formatXMLRecord(writer, format, 0, this, fields); diff --git a/jOOQ/src/main/java/org/jooq/impl/AbstractResult.java b/jOOQ/src/main/java/org/jooq/impl/AbstractResult.java index 8050171893..b8a6ee802f 100644 --- a/jOOQ/src/main/java/org/jooq/impl/AbstractResult.java +++ b/jOOQ/src/main/java/org/jooq/impl/AbstractResult.java @@ -456,6 +456,8 @@ abstract class AbstractResult extends AbstractFormattable impl @Override public final void formatJSON(Writer writer, JSONFormat format) { + format = format.mutable(true); + try { String separator; int recordLevel = format.header() ? 2 : 1; @@ -673,7 +675,9 @@ abstract class AbstractResult extends AbstractFormattable impl writer.append(' '); } + int previous = format.globalIndent(); formatJSON0(record.get(index), writer, format.globalIndent(format.globalIndent() + format.indent() * (recordLevel + 1))); + format.globalIndent(previous); if (format.format() && format.wrapSingleColumnRecords() && size == 1) writer.append(' '); @@ -711,7 +715,9 @@ abstract class AbstractResult extends AbstractFormattable impl else if (format.wrapSingleColumnRecords()) writer.append(' '); + int previous = format.globalIndent(); formatJSON0(record.get(index), writer, format.globalIndent(format.globalIndent() + format.indent() * (recordLevel + 1))); + format.globalIndent(previous); if (format.format() && format.wrapSingleColumnRecords() && size == 1) writer.append(' '); @@ -733,6 +739,8 @@ abstract class AbstractResult extends AbstractFormattable impl @Override public final void formatXML(Writer writer, XMLFormat format) { + format = format.mutable(true); + String newline = format.newline(); int recordLevel = format.header() ? 2 : 1; @@ -837,7 +845,9 @@ abstract class AbstractResult extends AbstractFormattable impl if (value instanceof Formattable) { Formattable f = (Formattable) value; writer.append(newline).append(format.indentString(recordLevel + 2)); + int previous = format.globalIndent(); f.formatXML(writer, format.globalIndent(format.globalIndent() + format.indent() * (recordLevel + 2))); + format.globalIndent(previous); writer.append(newline).append(format.indentString(recordLevel + 1)); } else if (value instanceof XML && !format.quoteNested())