Example usage for javax.xml.stream XMLStreamReader getAttributeValue

List of usage examples for javax.xml.stream XMLStreamReader getAttributeValue

Introduction

In this page you can find the example usage for javax.xml.stream XMLStreamReader getAttributeValue.

Prototype

public String getAttributeValue(int index);

Source Link

Document

Returns the value of the attribute at the index

Usage

From source file:org.apache.ddlutils.io.DatabaseIO.java

/**
 * Returns the value of the indicated attribute of the current element as a boolean.
 * If the value is not a valid boolean, then an exception is thrown.
 * //from w w  w .j a  v  a  2s .  c om
 * @param xmlReader    The xml reader
 * @param attributeIdx The index of the attribute
 * @return The attribute's value as a boolean
 */
private boolean getAttributeValueAsBoolean(XMLStreamReader xmlReader, int attributeIdx)
        throws DdlUtilsXMLException {
    String value = xmlReader.getAttributeValue(attributeIdx);

    if ("true".equalsIgnoreCase(value)) {
        return true;
    } else if ("false".equalsIgnoreCase(value)) {
        return false;
    } else {
        throw new DdlUtilsXMLException("Illegal boolean value '" + value + "' for attribute "
                + xmlReader.getAttributeLocalName(attributeIdx));
    }
}

From source file:org.apache.ddlutils.io.DatabaseIO.java

/**
 * Returns the value of the indicated attribute of the current element as a boolean.
 * If the value is not a valid boolean, then an exception is thrown.
 * //from   www  .  ja  v  a 2 s.  com
 * @param xmlReader    The xml reader
 * @param attributeIdx The index of the attribute
 * @return The attribute's value as a boolean
 */
private CascadeActionEnum getAttributeValueAsCascadeEnum(XMLStreamReader xmlReader, int attributeIdx)
        throws DdlUtilsXMLException {
    String value = xmlReader.getAttributeValue(attributeIdx);
    CascadeActionEnum enumValue = value == null ? null : CascadeActionEnum.getEnum(value.toLowerCase());

    if (enumValue == null) {
        throw new DdlUtilsXMLException("Illegal boolean value '" + value + "' for attribute "
                + xmlReader.getAttributeLocalName(attributeIdx));
    } else {
        return enumValue;
    }
}

From source file:org.apache.ddlutils.io.DataReader.java

/**
 * Reads a bean from the given xml stream reader.
 * /*from w  ww.j ava2 s  .co  m*/
 * @param xmlReader The reader
 */
private void readBean(XMLStreamReader xmlReader) throws XMLStreamException, DdlUtilsXMLException {
    QName elemQName = xmlReader.getName();
    Location location = xmlReader.getLocation();
    Map attributes = new HashMap();
    String tableName = null;

    for (int idx = 0; idx < xmlReader.getAttributeCount(); idx++) {
        QName attrQName = xmlReader.getAttributeName(idx);

        attributes.put(isCaseSensitive() ? attrQName.getLocalPart() : attrQName.getLocalPart().toLowerCase(),
                xmlReader.getAttributeValue(idx));
    }
    readColumnSubElements(xmlReader, attributes);

    if ("table".equals(elemQName.getLocalPart())) {
        tableName = (String) attributes.get("table-name");
    } else {
        tableName = elemQName.getLocalPart();
    }

    Table table = _model.findTable(tableName, isCaseSensitive());

    if (table == null) {
        _log.warn("Data XML contains an element " + elemQName + " at location " + location
                + " but there is no table defined with this name. This element will be ignored.");
    } else {
        DynaBean bean = _model.createDynaBeanFor(table);

        for (int idx = 0; idx < table.getColumnCount(); idx++) {
            Column column = table.getColumn(idx);
            String value = (String) attributes
                    .get(isCaseSensitive() ? column.getName() : column.getName().toLowerCase());

            if (value != null) {
                setColumnValue(bean, table, column, value);
            }
        }
        getSink().addBean(bean);
        consumeRestOfElement(xmlReader);
    }
}

From source file:org.apache.ddlutils.io.DataReader.java

/**
 * Reads the next column sub element that matches a column specified by the given table object from the xml reader into the given bean.
 *  /*from w  ww  .ja  v  a 2s  .c o m*/
 * @param xmlReader The reader
 * @param data      Where to store the values
 */
private void readColumnSubElement(XMLStreamReader xmlReader, Map data)
        throws XMLStreamException, DdlUtilsXMLException {
    QName elemQName = xmlReader.getName();
    Map attributes = new HashMap();
    boolean usesBase64 = false;

    for (int idx = 0; idx < xmlReader.getAttributeCount(); idx++) {
        QName attrQName = xmlReader.getAttributeName(idx);
        String value = xmlReader.getAttributeValue(idx);

        if (DatabaseIO.BASE64_ATTR_NAME.equals(attrQName.getLocalPart())) {
            if ("true".equalsIgnoreCase(value)) {
                usesBase64 = true;
            }
        } else {
            attributes.put(attrQName.getLocalPart(), value);
        }
    }

    int eventType = XMLStreamReader.START_ELEMENT;
    StringBuffer content = new StringBuffer();

    while (eventType != XMLStreamReader.END_ELEMENT) {
        eventType = xmlReader.next();
        if (eventType == XMLStreamReader.START_ELEMENT) {
            readColumnDataSubElement(xmlReader, attributes);
        } else if ((eventType == XMLStreamReader.CHARACTERS) || (eventType == XMLStreamReader.CDATA)
                || (eventType == XMLStreamReader.SPACE) || (eventType == XMLStreamReader.ENTITY_REFERENCE)) {
            content.append(xmlReader.getText());
        }
    }

    String value = content.toString().trim();

    if (usesBase64) {
        value = new String(Base64.decodeBase64(value.getBytes()));
    }

    String name = elemQName.getLocalPart();

    if ("table-name".equals(name)) {
        data.put("table-name", value);
    } else {
        if ("column".equals(name)) {
            name = (String) attributes.get("column-name");
        }
        if (attributes.containsKey("column-value")) {
            value = (String) attributes.get("column-value");
        }
        data.put(name, value);
    }
    consumeRestOfElement(xmlReader);
}

From source file:org.apache.ddlutils.io.DataReader.java

/**
 * Reads the next column-name or column-value sub element.
 *  // ww w.  j a v  a 2  s .  co m
 * @param xmlReader The reader
 * @param data      Where to store the values
 */
private void readColumnDataSubElement(XMLStreamReader xmlReader, Map data)
        throws XMLStreamException, DdlUtilsXMLException {
    QName elemQName = xmlReader.getName();
    boolean usesBase64 = false;

    for (int idx = 0; idx < xmlReader.getAttributeCount(); idx++) {
        QName attrQName = xmlReader.getAttributeName(idx);
        String value = xmlReader.getAttributeValue(idx);

        if (DatabaseIO.BASE64_ATTR_NAME.equals(attrQName.getLocalPart())) {
            if ("true".equalsIgnoreCase(value)) {
                usesBase64 = true;
            }
            break;
        }
    }

    String value = xmlReader.getElementText();

    if (value != null) {
        value = value.toString().trim();

        if (usesBase64) {
            value = new String(Base64.decodeBase64(value.getBytes()));
        }
    }

    String name = elemQName.getLocalPart();

    if ("column-name".equals(name)) {
        data.put("column-name", value);
    } else if ("column-value".equals(name)) {
        data.put("column-value", value);
    }
    consumeRestOfElement(xmlReader);
}

From source file:org.apache.ddlutils.io.DataReaderKeys.java

private Identity readBean(XMLStreamReader xmlReader) throws XMLStreamException, DdlUtilsXMLException {
    QName elemQName = xmlReader.getName();
    Table table = _model.findTable(elemQName.getLocalPart(), isCaseSensitive());

    if (table == null) {
        readOverElement(xmlReader);// www.jav  a 2s.  c o  m
        return null;
    }

    DynaBean bean = _model.createDynaBeanFor(table);
    for (int idx = 0; idx < xmlReader.getAttributeCount(); idx++) {
        QName attrQName = xmlReader.getAttributeName(idx);
        Column column = table.findColumn(attrQName.getLocalPart(), isCaseSensitive());

        if (column != null) {
            setColumnValue(bean, table, column, xmlReader.getAttributeValue(idx));
        }
    }
    readColumnSubElements(xmlReader, bean, table);
    //getSink().addBean(bean);
    //--get keys here
    Identity id = buildIdentityFromPKs(table, bean);
    //Hashmap keyval = new Hashmap();
    //keyval.put(id.,id);
    consumeRestOfElement(xmlReader);
    return id;
}

From source file:org.apache.ddlutils.io.DataReaderKeys.java

private void readColumnSubElement(XMLStreamReader xmlReader, DynaBean bean, Table table)
        throws XMLStreamException, DdlUtilsXMLException {
    QName elemQName = xmlReader.getName();
    boolean usesBase64 = false;

    for (int idx = 0; idx < xmlReader.getAttributeCount(); idx++) {
        QName attrQName = xmlReader.getAttributeName(idx);

        if (DatabaseIO.BASE64_ATTR_NAME.equals(attrQName.getLocalPart())
                && "true".equalsIgnoreCase(xmlReader.getAttributeValue(idx))) {
            usesBase64 = true;//from  w w w.  j av  a2 s  . co m
            break;
        }
    }

    Column column = table.findColumn(elemQName.getLocalPart(), isCaseSensitive());

    if (column == null) {
        _log.warn("Data XML contains an element " + elemQName + " at location " + xmlReader.getLocation()
                + " but there is no column defined in table " + table.getName()
                + " with this name. This element will be ignored.");
    } else {
        String value = xmlReader.getElementText();

        if (value != null) {
            value = value.trim();

            if (usesBase64) {
                value = new String(Base64.decodeBase64(value.getBytes()));
            }
            setColumnValue(bean, table, column, value);
        }
    }
    consumeRestOfElement(xmlReader);
}

From source file:org.apache.ode.utils.DOMUtils.java

private static void parse(XMLStreamReader reader, Document doc, Node parent) throws XMLStreamException {
    int event = reader.getEventType();

    while (reader.hasNext()) {
        switch (event) {
        case XMLStreamConstants.START_ELEMENT:
            // create element
            Element e = doc.createElementNS(reader.getNamespaceURI(), reader.getLocalName());
            if (reader.getPrefix() != null && reader.getPrefix() != "") {
                e.setPrefix(reader.getPrefix());
            }/*  w w  w  .j a  v a2s .c om*/
            parent.appendChild(e);

            // copy namespaces
            for (int ns = 0; ns < reader.getNamespaceCount(); ns++) {
                String uri = reader.getNamespaceURI(ns);
                String prefix = reader.getNamespacePrefix(ns);
                declare(e, uri, prefix);
            }

            // copy attributes
            for (int att = 0; att < reader.getAttributeCount(); att++) {
                String name = reader.getAttributeLocalName(att);
                String prefix = reader.getAttributePrefix(att);
                if (prefix != null && prefix.length() > 0) {
                    name = prefix + ":" + name;
                }
                Attr attr = doc.createAttributeNS(reader.getAttributeNamespace(att), name);
                attr.setValue(reader.getAttributeValue(att));
                e.setAttributeNode(attr);
            }
            // sub-nodes
            if (reader.hasNext()) {
                reader.next();
                parse(reader, doc, e);
            }
            if (parent instanceof Document) {
                while (reader.hasNext())
                    reader.next();
                return;
            }
            break;
        case XMLStreamConstants.END_ELEMENT:
            return;
        case XMLStreamConstants.CHARACTERS:
            if (parent != null) {
                parent.appendChild(doc.createTextNode(reader.getText()));
            }
            break;
        case XMLStreamConstants.COMMENT:
            if (parent != null) {
                parent.appendChild(doc.createComment(reader.getText()));
            }
            break;
        case XMLStreamConstants.CDATA:
            parent.appendChild(doc.createCDATASection(reader.getText()));
            break;
        case XMLStreamConstants.PROCESSING_INSTRUCTION:
            parent.appendChild(doc.createProcessingInstruction(reader.getPITarget(), reader.getPIData()));
            break;
        case XMLStreamConstants.ENTITY_REFERENCE:
            parent.appendChild(doc.createProcessingInstruction(reader.getPITarget(), reader.getPIData()));
            break;
        case XMLStreamConstants.NAMESPACE:
        case XMLStreamConstants.ATTRIBUTE:
            break;
        default:
            break;
        }

        if (reader.hasNext()) {
            event = reader.next();
        }
    }
}

From source file:org.apache.solr.handler.AnalysisRequestHandler.java

SolrInputDocument readDoc(XMLStreamReader parser) throws XMLStreamException {
    SolrInputDocument doc = new SolrInputDocument();

    StringBuilder text = new StringBuilder();
    String name = null;/*from   www.  j  av  a2s  .c o m*/
    String attrName = "";
    float boost = 1.0f;
    boolean isNull = false;
    while (true) {
        int event = parser.next();
        switch (event) {
        // Add everything to the text
        case XMLStreamConstants.SPACE:
        case XMLStreamConstants.CDATA:
        case XMLStreamConstants.CHARACTERS:
            text.append(parser.getText());
            break;

        case XMLStreamConstants.END_ELEMENT:
            if ("doc".equals(parser.getLocalName())) {
                return doc;
            } else if ("field".equals(parser.getLocalName())) {
                if (!isNull) {
                    doc.addField(name, text.toString(), boost);
                    boost = 1.0f;
                }
            }
            break;

        case XMLStreamConstants.START_ELEMENT:
            text.setLength(0);
            String localName = parser.getLocalName();
            if (!"field".equals(localName)) {
                log.warn("unexpected XML tag doc/" + localName);
                throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
                        "unexpected XML tag doc/" + localName);
            }

            String attrVal = "";
            for (int i = 0; i < parser.getAttributeCount(); i++) {
                attrName = parser.getAttributeLocalName(i);
                attrVal = parser.getAttributeValue(i);
                if ("name".equals(attrName)) {
                    name = attrVal;
                }
            }
            break;
        }
    }
}

From source file:org.apache.solr.handler.DocumentAnalysisRequestHandler.java

/**
 * Reads the document from the given xml stream reader. The following document format is expected:
 * <p/>/*from  w ww. j a v  a  2  s  . c  o  m*/
 * <pre><code>
 * &lt;doc&gt;
 *    &lt;field name="id"&gt;1&lt;/field&gt;
 *    &lt;field name="name"&gt;The Name&lt;/field&gt;
 *    &lt;field name="text"&gt;The Text Value&lt;/field&gt;
 * &lt;/doc&gt;
 * </code></pre>
 * <p/>
 * <p/>
 * <em>NOTE: each read document is expected to have at least one field which serves as the unique key.</em>
 *
 * @param reader The {@link XMLStreamReader} from which the document will be read.
 * @param schema The index schema. The schema is used to validate that the read document has a unique key field.
 *
 * @return The read document.
 *
 * @throws XMLStreamException When reading of the document fails.
 */
SolrInputDocument readDocument(XMLStreamReader reader, IndexSchema schema) throws XMLStreamException {
    SolrInputDocument doc = new SolrInputDocument();

    String uniqueKeyField = schema.getUniqueKeyField().getName();

    StringBuilder text = new StringBuilder();
    String fieldName = null;
    boolean hasId = false;

    while (true) {
        int event = reader.next();
        switch (event) {
        // Add everything to the text
        case XMLStreamConstants.SPACE:
        case XMLStreamConstants.CDATA:
        case XMLStreamConstants.CHARACTERS:
            text.append(reader.getText());
            break;

        case XMLStreamConstants.END_ELEMENT:
            if ("doc".equals(reader.getLocalName())) {
                if (!hasId) {
                    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
                            "All documents must contain a unique key value: '" + doc.toString() + "'");
                }
                return doc;
            } else if ("field".equals(reader.getLocalName())) {
                doc.addField(fieldName, text.toString(), DEFAULT_BOOST);
                if (uniqueKeyField.equals(fieldName)) {
                    hasId = true;
                }
            }
            break;

        case XMLStreamConstants.START_ELEMENT:
            text.setLength(0);
            String localName = reader.getLocalName();
            if (!"field".equals(localName)) {
                log.warn("unexpected XML tag doc/" + localName);
                throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
                        "unexpected XML tag doc/" + localName);
            }

            for (int i = 0; i < reader.getAttributeCount(); i++) {
                String attrName = reader.getAttributeLocalName(i);
                if ("name".equals(attrName)) {
                    fieldName = reader.getAttributeValue(i);
                }
            }
            break;
        }
    }
}