Search in sources :

Example 1 with XML

use of org.jooq.XML in project jOOQ by jOOQ.

the class DefaultParseContext method parseFieldXMLQueryIf.

private final Field<?> parseFieldXMLQueryIf() {
    if (parseFunctionNameIf("XMLQUERY")) {
        parse('(');
        Field<String> xpath = (Field<String>) parseField();
        XMLPassingMechanism m = parseXMLPassingMechanism();
        Field<XML> xml = (Field<XML>) parseField();
        parseKeywordIf("RETURNING CONTENT");
        parse(')');
        if (m == BY_REF)
            return xmlquery(xpath).passingByRef(xml);
        else
            return xmlquery(xpath).passing(xml);
    }
    return null;
}
Also used : GroupField(org.jooq.GroupField) TableField(org.jooq.TableField) Field(org.jooq.Field) SortField(org.jooq.SortField) SelectField(org.jooq.SelectField) XMLPassingMechanism(org.jooq.impl.QOM.XMLPassingMechanism) XML(org.jooq.XML)

Example 2 with XML

use of org.jooq.XML in project jOOQ by jOOQ.

the class AbstractResult method formatXMLRecord.

static final void formatXMLRecord(Writer writer, XMLFormat format, int recordLevel, Record record, AbstractRow<?> fields) throws java.io.IOException {
    String newline = format.newline();
    writer.append("<record");
    if (format.xmlns()) {
        format = format.xmlns(false);
        writer.append(" xmlns=\"" + Constants.NS_EXPORT + "\"");
    }
    writer.append(">");
    int size = fields.size();
    for (int index = 0; index < size; index++) {
        Object value = record.get(index);
        writer.append(newline).append(format.indentString(recordLevel + 1));
        String tag = format.recordFormat() == COLUMN_NAME_ELEMENTS ? escapeXML(fields.field(index).getName()) : "value";
        writer.append("<" + tag);
        if (format.recordFormat() == VALUE_ELEMENTS_WITH_FIELD_ATTRIBUTE) {
            writer.append(" field=\"");
            writer.append(escapeXML(fields.field(index).getName()));
            writer.append("\"");
        }
        if (value == null) {
            writer.append("/>");
        } else {
            writer.append(">");
            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())
                writer.append(((XML) value).data());
            else
                writer.append(escapeXML(format0(value, false, false)));
            writer.append("</" + tag + ">");
        }
    }
    writer.append(newline).append(format.indentString(recordLevel)).append("</record>");
}
Also used : XML(org.jooq.XML) Formattable(org.jooq.Formattable)

Example 3 with XML

use of org.jooq.XML in project jOOQ by jOOQ.

the class AbstractResult method format0.

/**
 * @param value The value to be formatted
 * @param visual Whether the formatted output is to be consumed visually
 *            (HTML, TEXT) or by a machine (CSV, JSON, XML)
 */
private static final String format0(Object value, boolean changed, boolean visual) {
    // [#2741] TODO: This logic will be externalised in new SPI
    String formatted = changed && visual ? "*" : "";
    if (value == null) {
        formatted += visual ? "{null}" : null;
    } else if (value.getClass() == byte[].class) {
        formatted += DatatypeConverter.printBase64Binary((byte[]) value);
    } else if (value.getClass().isArray()) {
        // [#6545] Nested arrays are handled recursively
        formatted += Arrays.stream((Object[]) value).map(f -> format0(f, false, visual)).collect(joining(", ", "[", "]"));
    } else if (value instanceof EnumType) {
        EnumType e = (EnumType) value;
        formatted += e.getLiteral();
    } else if (value instanceof List) {
        List<?> l = (List<?>) value;
        formatted += l.stream().map(f -> format0(f, false, visual)).collect(joining(", ", "[", "]"));
    } else if (value instanceof Record) {
        Record r = (Record) value;
        formatted += Arrays.stream(r.intoArray()).map(f -> format0(f, false, visual)).collect(joining(", ", "(", ")"));
    } else // [#6080] Support formatting of nested ROWs
    if (value instanceof Param) {
        formatted += format0(((Param<?>) value).getValue(), false, visual);
    } else // [#5238] Oracle DATE is really a TIMESTAMP(0)...
    if (value instanceof Date) {
        Date d = (Date) value;
        String date = value.toString();
        if (Date.valueOf(date).equals(value))
            formatted += date;
        else
            formatted += new Timestamp(d.getTime());
    } else {
        formatted += value.toString();
    }
    return formatted;
}
Also used : Arrays(java.util.Arrays) Row(org.jooq.Row) Display(org.jooq.ChartFormat.Display) Table(org.jooq.Table) DatatypeConverter(jakarta.xml.bind.DatatypeConverter) VALUE_ELEMENTS_WITH_FIELD_ATTRIBUTE(org.jooq.XMLFormat.RecordFormat.VALUE_ELEMENTS_WITH_FIELD_ATTRIBUTE) IOException(org.jooq.exception.IOException) Document(org.w3c.dom.Document) StringUtils.leftPad(org.jooq.tools.StringUtils.leftPad) DSLContext(org.jooq.DSLContext) AttributesImpl(org.xml.sax.helpers.AttributesImpl) StringUtils.rightPad(org.jooq.tools.StringUtils.rightPad) DSL.name(org.jooq.impl.DSL.name) Timestamp(java.sql.Timestamp) SettingsTools.renderLocale(org.jooq.conf.SettingsTools.renderLocale) Constants(org.jooq.Constants) Field(org.jooq.Field) TableRecord(org.jooq.TableRecord) Math.min(java.lang.Math.min) ChartFormat(org.jooq.ChartFormat) Result(org.jooq.Result) Cursor(org.jooq.Cursor) Collectors.joining(java.util.stream.Collectors.joining) DocumentFragment(org.w3c.dom.DocumentFragment) List(java.util.List) TableField(org.jooq.TableField) COLUMN_NAME_ELEMENTS(org.jooq.XMLFormat.RecordFormat.COLUMN_NAME_ELEMENTS) SAXException(org.xml.sax.SAXException) Writer(java.io.Writer) Math.max(java.lang.Math.max) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) JSONValue(org.jooq.tools.json.JSONValue) XML(org.jooq.XML) EnumType(org.jooq.EnumType) Deque(java.util.Deque) FormattingProvider(org.jooq.FormattingProvider) ArrayList(java.util.ArrayList) JSONFormat(org.jooq.JSONFormat) DSL.insertInto(org.jooq.impl.DSL.insertInto) Schema(org.jooq.Schema) Attributes(org.xml.sax.Attributes) CSVFormat(org.jooq.CSVFormat) ContentHandler(org.xml.sax.ContentHandler) Record(org.jooq.Record) InputSource(org.xml.sax.InputSource) XMLFormat(org.jooq.XMLFormat) NodeList(org.w3c.dom.NodeList) Iterator(java.util.Iterator) JSON(org.jooq.JSON) TXTFormat(org.jooq.TXTFormat) Formattable(org.jooq.Formattable) StringUtils(org.jooq.tools.StringUtils) Date(java.sql.Date) Param(org.jooq.Param) DefaultHandler(org.xml.sax.helpers.DefaultHandler) Configuration(org.jooq.Configuration) StringUtils.abbreviate(org.jooq.tools.StringUtils.abbreviate) Element(org.w3c.dom.Element) StringReader(java.io.StringReader) TreeMap(java.util.TreeMap) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) JSONB(org.jooq.JSONB) ArrayDeque(java.util.ArrayDeque) DSL.table(org.jooq.impl.DSL.table) Collections(java.util.Collections) EnumType(org.jooq.EnumType) Param(org.jooq.Param) List(java.util.List) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList) TableRecord(org.jooq.TableRecord) Record(org.jooq.Record) Timestamp(java.sql.Timestamp) Date(java.sql.Date)

Example 4 with XML

use of org.jooq.XML in project jOOQ by jOOQ.

the class DefaultParseContext method parsePredicateXMLExistsIf.

private final Condition parsePredicateXMLExistsIf() {
    if (parseKeywordIf("XMLEXISTS")) {
        parse('(');
        Field<String> xpath = (Field<String>) parseField();
        XMLPassingMechanism m = parseXMLPassingMechanism();
        Field<XML> xml = (Field<XML>) parseField();
        parse(')');
        if (m == BY_REF)
            return xmlexists(xpath).passingByRef(xml);
        else if (m == BY_VALUE)
            return xmlexists(xpath).passingByValue(xml);
        else
            return xmlexists(xpath).passing(xml);
    }
    return null;
}
Also used : GroupField(org.jooq.GroupField) TableField(org.jooq.TableField) Field(org.jooq.Field) SortField(org.jooq.SortField) SelectField(org.jooq.SelectField) XMLPassingMechanism(org.jooq.impl.QOM.XMLPassingMechanism) XML(org.jooq.XML)

Example 5 with XML

use of org.jooq.XML in project jOOQ by jOOQ.

the class AbstractResult method intoXML.

@Override
public final Document intoXML(XMLFormat format) {
    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.newDocument();
        Element eResult = document.createElement("result");
        if (format.xmlns())
            eResult.setAttribute("xmlns", Constants.NS_EXPORT);
        document.appendChild(eResult);
        Element eRecordParent = eResult;
        if (format.header()) {
            Element eFields = document.createElement("fields");
            eResult.appendChild(eFields);
            for (Field<?> field : fields.fields.fields) {
                Element eField = document.createElement("field");
                if (field instanceof TableField) {
                    TableField<?, ?> f = (TableField<?, ?>) field;
                    Table<?> table = f.getTable();
                    if (table != null) {
                        Schema schema = table.getSchema();
                        if (schema != null) {
                            eField.setAttribute("schema", schema.getName());
                        }
                        eField.setAttribute("table", table.getName());
                    }
                }
                eField.setAttribute("name", field.getName());
                eField.setAttribute("type", field.getDataType().getTypeName().toUpperCase(renderLocale(configuration.settings())));
                eFields.appendChild(eField);
            }
            Element eRecords = document.createElement("records");
            eResult.appendChild(eRecords);
            eRecordParent = eRecords;
        }
        for (Record record : this) {
            Element eRecord = document.createElement("record");
            eRecordParent.appendChild(eRecord);
            int size = fields.size();
            for (int index = 0; index < size; index++) {
                Field<?> field = fields.field(index);
                Object value = record.get(index);
                String tag = format.recordFormat() == COLUMN_NAME_ELEMENTS ? escapeXML(fields.field(index).getName()) : "value";
                Element eValue = document.createElement(tag);
                if (format.recordFormat() == VALUE_ELEMENTS_WITH_FIELD_ATTRIBUTE)
                    eValue.setAttribute("field", field.getName());
                eRecord.appendChild(eValue);
                if (value != null)
                    if (value instanceof XML && !format.quoteNested())
                        eValue.appendChild(createContent(builder, document, ((XML) value).data()));
                    else
                        eValue.setTextContent(format0(value, false, false));
            }
        }
        return document;
    } catch (ParserConfigurationException ignore) {
        throw new RuntimeException(ignore);
    }
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Element(org.w3c.dom.Element) Schema(org.jooq.Schema) Document(org.w3c.dom.Document) TableField(org.jooq.TableField) DocumentBuilder(javax.xml.parsers.DocumentBuilder) XML(org.jooq.XML) TableRecord(org.jooq.TableRecord) Record(org.jooq.Record) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Aggregations

XML (org.jooq.XML)5 TableField (org.jooq.TableField)4 Field (org.jooq.Field)3 DocumentBuilder (javax.xml.parsers.DocumentBuilder)2 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 Formattable (org.jooq.Formattable)2 GroupField (org.jooq.GroupField)2 Record (org.jooq.Record)2 DatatypeConverter (jakarta.xml.bind.DatatypeConverter)1 StringReader (java.io.StringReader)1 Writer (java.io.Writer)1 Math.max (java.lang.Math.max)1 Math.min (java.lang.Math.min)1 Date (java.sql.Date)1 Timestamp (java.sql.Timestamp)1 ArrayDeque (java.util.ArrayDeque)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1