[#5854] Support indented XML output in formatXML()
This commit is contained in:
parent
d8f6c9aaf6
commit
b7c2f72581
@ -618,6 +618,14 @@ public interface Result<R extends Record> extends List<R>, Attachable {
|
||||
*/
|
||||
String formatXML();
|
||||
|
||||
/**
|
||||
* Get this result formatted as XML.
|
||||
*
|
||||
* @see <a
|
||||
* href="http://www.jooq.org/xsd/jooq-export-2.6.0.xsd">http://www.jooq.org/xsd/jooq-export-2.6.0.xsd</a>
|
||||
*/
|
||||
String formatXML(XMLFormat format);
|
||||
|
||||
/**
|
||||
* Get this result as a set of <code>INSERT</code> statements.
|
||||
* <p>
|
||||
@ -728,6 +736,13 @@ public interface Result<R extends Record> extends List<R>, Attachable {
|
||||
*/
|
||||
void formatXML(OutputStream stream) throws IOException;
|
||||
|
||||
/**
|
||||
* Like {@link #formatXML(XMLFormat)}, but the data is output onto an {@link OutputStream}.
|
||||
*
|
||||
* @throws IOException - an unchecked wrapper for {@link java.io.IOException}, if anything goes wrong.
|
||||
*/
|
||||
void formatXML(OutputStream stream, XMLFormat format) throws IOException;
|
||||
|
||||
/**
|
||||
* Like {@link #formatInsert()}, but the data is output onto an {@link OutputStream}.
|
||||
*
|
||||
@ -833,6 +848,13 @@ public interface Result<R extends Record> extends List<R>, Attachable {
|
||||
*/
|
||||
void formatXML(Writer writer) throws IOException;
|
||||
|
||||
/**
|
||||
* Like {@link #formatXML(XMLFormat)}, but the data is output onto a {@link Writer}.
|
||||
*
|
||||
* @throws IOException - an unchecked wrapper for {@link java.io.IOException}, if anything goes wrong.
|
||||
*/
|
||||
void formatXML(Writer writer, XMLFormat format) throws IOException;
|
||||
|
||||
/**
|
||||
* Like {@link #formatInsert()}, but the data is output onto a {@link Writer}.
|
||||
*
|
||||
|
||||
@ -115,6 +115,7 @@ import org.jooq.Table;
|
||||
import org.jooq.TableField;
|
||||
import org.jooq.TableRecord;
|
||||
import org.jooq.UpdatableRecord;
|
||||
import org.jooq.XMLFormat;
|
||||
import org.jooq.exception.IOException;
|
||||
import org.jooq.exception.InvalidResultException;
|
||||
import org.jooq.tools.Convert;
|
||||
@ -968,24 +969,46 @@ final class ResultImpl<R extends Record> implements Result<R>, AttachableInterna
|
||||
|
||||
@Override
|
||||
public final String formatXML() {
|
||||
return formatXML(new XMLFormat());
|
||||
}
|
||||
|
||||
@Override
|
||||
public final String formatXML(XMLFormat format) {
|
||||
StringWriter writer = new StringWriter();
|
||||
formatXML(writer);
|
||||
formatXML(writer, format);
|
||||
return writer.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void formatXML(OutputStream stream) {
|
||||
formatXML(new OutputStreamWriter(stream));
|
||||
formatXML(stream, new XMLFormat());
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void formatXML(OutputStream stream, XMLFormat format) {
|
||||
formatXML(new OutputStreamWriter(stream), format);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void formatXML(Writer writer) {
|
||||
formatXML(writer, new XMLFormat());
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void formatXML(Writer writer, XMLFormat format) {
|
||||
String newline = format.format() ? format.newline() : "";
|
||||
String[] indent = {
|
||||
format.format() ? rightPad("", format.indent() * 1) : "",
|
||||
format.format() ? rightPad("", format.indent() * 2) : "",
|
||||
format.format() ? rightPad("", format.indent() * 3) : ""
|
||||
};
|
||||
|
||||
try {
|
||||
writer.append("<result xmlns=\"http://www.jooq.org/xsd/jooq-export-3.7.0.xsd\">");
|
||||
writer.append("<fields>");
|
||||
writer.append("<result xmlns=\"http://www.jooq.org/xsd/jooq-export-3.7.0.xsd\">")
|
||||
.append(newline).append(indent[0]).append("<fields>");
|
||||
|
||||
for (Field<?> field : fields.fields) {
|
||||
writer.append("<field");
|
||||
writer.append(newline).append(indent[1]).append("<field");
|
||||
|
||||
if (field instanceof TableField) {
|
||||
Table<?> table = ((TableField<?, ?>) field).getTable();
|
||||
@ -1013,16 +1036,16 @@ final class ResultImpl<R extends Record> implements Result<R>, AttachableInterna
|
||||
writer.append("\"/>");
|
||||
}
|
||||
|
||||
writer.append("</fields>");
|
||||
writer.append("<records>");
|
||||
writer.append(newline).append(indent[0]).append("</fields>");
|
||||
writer.append(newline).append(indent[0]).append("<records>");
|
||||
|
||||
for (Record record : this) {
|
||||
writer.append("<record>");
|
||||
writer.append(newline).append(indent[1]).append("<record>");
|
||||
|
||||
for (int index = 0; index < fields.fields.length; index++) {
|
||||
Object value = record.get(index);
|
||||
|
||||
writer.append("<value field=\"");
|
||||
writer.append(newline).append(indent[2]).append("<value field=\"");
|
||||
writer.append(escapeXML(fields.fields[index].getName()));
|
||||
writer.append("\"");
|
||||
|
||||
@ -1036,11 +1059,11 @@ final class ResultImpl<R extends Record> implements Result<R>, AttachableInterna
|
||||
}
|
||||
}
|
||||
|
||||
writer.append("</record>");
|
||||
writer.append(newline).append(indent[1]).append("</record>");
|
||||
}
|
||||
|
||||
writer.append("</records>");
|
||||
writer.append("</result>");
|
||||
writer.append(newline).append(indent[0]).append("</records>");
|
||||
writer.append(newline).append("</result>");
|
||||
|
||||
writer.flush();
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user