Example usage for javax.xml.stream XMLStreamReader getAttributeLocalName

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

Introduction

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

Prototype

public String getAttributeLocalName(int index);

Source Link

Document

Returns the localName of the attribute at the provided index

Usage

From source file:XmlStreamUtils.java

public static String printEvent(XMLStreamReader xmlr) {
    StringBuffer b = new StringBuffer();
    b.append("EVENT:[" + xmlr.getLocation().getLineNumber() + "][" + xmlr.getLocation().getColumnNumber()
            + "] ");
    b.append(getName(xmlr.getEventType()));
    b.append(" [");
    switch (xmlr.getEventType()) {
    case XMLStreamReader.START_ELEMENT:
        b.append("<");
        printName(xmlr, b);// w  w  w  . ja va2  s.  c om
        for (int i = 0; i < xmlr.getNamespaceCount(); i++) {
            b.append(" ");
            String n = xmlr.getNamespacePrefix(i);
            if ("xmlns".equals(n)) {
                b.append("xmlns=\"" + xmlr.getNamespaceURI(i) + "\"");
            } else {
                b.append("xmlns:" + n);
                b.append("=\"");
                b.append(xmlr.getNamespaceURI(i));
                b.append("\"");
            }
        }

        for (int i = 0; i < xmlr.getAttributeCount(); i++) {
            b.append(" ");
            printName(xmlr.getAttributePrefix(i), xmlr.getAttributeNamespace(i), xmlr.getAttributeLocalName(i),
                    b);
            b.append("=\"");
            b.append(xmlr.getAttributeValue(i));
            b.append("\"");
        }

        b.append(">");
        break;
    case XMLStreamReader.END_ELEMENT:
        b.append("</");
        printName(xmlr, b);
        for (int i = 0; i < xmlr.getNamespaceCount(); i++) {
            b.append(" ");
            String n = xmlr.getNamespacePrefix(i);
            if ("xmlns".equals(n)) {
                b.append("xmlns=\"" + xmlr.getNamespaceURI(i) + "\"");
            } else {
                b.append("xmlns:" + n);
                b.append("=\"");
                b.append(xmlr.getNamespaceURI(i));
                b.append("\"");
            }
        }
        b.append(">");
        break;
    case XMLStreamReader.SPACE:
    case XMLStreamReader.CHARACTERS:
        // b.append(xmlr.getText());
        int start = xmlr.getTextStart();
        int length = xmlr.getTextLength();
        b.append(new String(xmlr.getTextCharacters(), start, length));
        break;
    case XMLStreamReader.PROCESSING_INSTRUCTION:
        String target = xmlr.getPITarget();
        if (target == null)
            target = "";
        String data = xmlr.getPIData();
        if (data == null)
            data = "";
        b.append("<?");
        b.append(target + " " + data);
        b.append("?>");
        break;
    case XMLStreamReader.CDATA:
        b.append("<![CDATA[");
        if (xmlr.hasText())
            b.append(xmlr.getText());
        b.append("]]&gt;");
        break;

    case XMLStreamReader.COMMENT:
        b.append("<!--");
        if (xmlr.hasText())
            b.append(xmlr.getText());
        b.append("-->");
        break;
    case XMLStreamReader.ENTITY_REFERENCE:
        b.append(xmlr.getLocalName() + "=");
        if (xmlr.hasText())
            b.append("[" + xmlr.getText() + "]");
        break;
    case XMLStreamReader.START_DOCUMENT:
        b.append("<?xml");
        b.append(" version='" + xmlr.getVersion() + "'");
        b.append(" encoding='" + xmlr.getCharacterEncodingScheme() + "'");
        if (xmlr.isStandalone())
            b.append(" standalone='yes'");
        else
            b.append(" standalone='no'");
        b.append("?>");
        break;

    }
    b.append("]");
    return b.toString();
}

From source file:net.solarnetwork.util.JavaBeanXmlSerializer.java

private void parseElement(Map<String, Object> result, XMLStreamReader reader) {
    int attrCount = reader.getAttributeCount();
    for (int i = 0; i < attrCount; i++) {
        String name = reader.getAttributeLocalName(i);
        String val = reader.getAttributeValue(i);
        putMapValue(result, name, val);
    }/*ww  w .ja v a 2  s  .  c om*/
}

From source file:at.lame.hellonzb.parser.NzbParser.java

/**
 * This private method is used to create a new instance of DownloadFileSegment.
 * //  w w w  .  j  a v a2 s.  c  o  m
 * @param parser The parser object to query (XMLStreamReader)
 * @param file The current DownloadFile object
 * @return The new DownloadFileSegment object
 */
private DownloadFileSegment newDownloadFileSegment(XMLStreamReader parser, DownloadFile file) {
    long bytes = 0;
    int index = 0;

    for (int i = 0; i < parser.getAttributeCount(); i++) {
        String attName = parser.getAttributeLocalName(i).toLowerCase();
        if (attName.equals("bytes"))
            bytes = Long.parseLong(parser.getAttributeValue(i));
        else if (attName.equals("number"))
            index = Integer.parseInt(parser.getAttributeValue(i));
    }

    return new DownloadFileSegment(file, bytes, index, file.getGroups());
}

From source file:hudson.plugins.report.jck.parsers.JtregReportParser.java

private String findAttributeValue(XMLStreamReader in, String name) {
    int count = in.getAttributeCount();
    for (int i = 0; i < count; i++) {
        if (name.equals(in.getAttributeLocalName(i))) {
            return in.getAttributeValue(i);
        }/* w  w  w.  jav a 2s . com*/
    }
    return null;
}

From source file:at.lame.hellonzb.parser.NzbParser.java

/**
 * This private method is used to create a new instance of DownloadFile.
 * /*from w ww .j a v a 2  s  .  c  om*/
 * @param parser The parser object to query (XMLStreamReader)
 * @return The new DownloadFile object
 */
private DownloadFile newDownloadFile(XMLStreamReader parser) {
    String poster = "";
    String date = "";
    String subject = "";

    for (int i = 0; i < parser.getAttributeCount(); i++) {
        String attName = parser.getAttributeLocalName(i).toLowerCase();
        if (attName.equals("poster"))
            poster = parser.getAttributeValue(i);
        else if (attName.equals("date"))
            date = parser.getAttributeValue(i);
        else if (attName.equals("subject"))
            subject = parser.getAttributeValue(i);
    }

    return new DownloadFile(poster, date, subject, HelloNzbToolkit.getLastFilename(name));
}

From source file:Main.java

/**
 * Borrowed from org.apache.xml.security.test.stax.utils.XmlReaderToWriter
 *//* ww  w  .  ja  va  2  s  .  c om*/
public static void write(XMLStreamReader xmlr, XMLStreamWriter writer) throws XMLStreamException {
    switch (xmlr.getEventType()) {
    case XMLEvent.START_ELEMENT:
        final String localName = xmlr.getLocalName();
        final String namespaceURI = xmlr.getNamespaceURI();
        if (namespaceURI != null && namespaceURI.length() > 0) {
            final String prefix = xmlr.getPrefix();
            if (prefix != null) {
                writer.writeStartElement(prefix, localName, namespaceURI);
            } else {
                writer.writeStartElement(namespaceURI, localName);
            }
        } else {
            writer.writeStartElement(localName);
        }

        for (int i = 0, len = xmlr.getNamespaceCount(); i < len; i++) {
            String prefix = xmlr.getNamespacePrefix(i);
            if (prefix == null) {
                writer.writeDefaultNamespace(xmlr.getNamespaceURI(i));
            } else {
                writer.writeNamespace(prefix, xmlr.getNamespaceURI(i));
            }
        }

        for (int i = 0, len = xmlr.getAttributeCount(); i < len; i++) {
            final String attUri = xmlr.getAttributeNamespace(i);

            if (attUri != null && attUri.length() > 0) {
                final String prefix = xmlr.getAttributePrefix(i);
                if (prefix != null) {
                    writer.writeAttribute(prefix, attUri, xmlr.getAttributeLocalName(i),
                            xmlr.getAttributeValue(i));
                } else {
                    writer.writeAttribute(attUri, xmlr.getAttributeLocalName(i), xmlr.getAttributeValue(i));
                }
            } else {
                writer.writeAttribute(xmlr.getAttributeLocalName(i), xmlr.getAttributeValue(i));
            }

        }
        break;
    case XMLEvent.END_ELEMENT:
        writer.writeEndElement();
        break;
    case XMLEvent.SPACE:
    case XMLEvent.CHARACTERS:
        char[] text = new char[xmlr.getTextLength()];
        xmlr.getTextCharacters(0, text, 0, xmlr.getTextLength());
        writer.writeCharacters(text, 0, text.length);
        break;
    case XMLEvent.PROCESSING_INSTRUCTION:
        writer.writeProcessingInstruction(xmlr.getPITarget(), xmlr.getPIData());
        break;
    case XMLEvent.CDATA:
        writer.writeCData(xmlr.getText());
        break;
    case XMLEvent.COMMENT:
        writer.writeComment(xmlr.getText());
        break;
    case XMLEvent.ENTITY_REFERENCE:
        writer.writeEntityRef(xmlr.getLocalName());
        break;
    case XMLEvent.START_DOCUMENT:
        String encoding = xmlr.getCharacterEncodingScheme();
        String version = xmlr.getVersion();

        if (encoding != null && version != null) {
            writer.writeStartDocument(encoding, version);
        } else if (version != null) {
            writer.writeStartDocument(xmlr.getVersion());
        }
        break;
    case XMLEvent.END_DOCUMENT:
        writer.writeEndDocument();
        break;
    case XMLEvent.DTD:
        writer.writeDTD(xmlr.getText());
        break;
    }
}

From source file:com.microsoft.tfs.core.memento.XMLMemento.java

/**
 * Reads the current element from the given reader and returns the
 * {@link XMLMemento} read from its data. The {@link XMLStreamReader} must
 * be positioned at {@link XMLStreamConstants#START_ELEMENT}.
 *
 * @param reader//from w w  w.j ava 2 s .  c  o m
 *        the reader (must not be <code>null</code>)
 * @return the {@link XMLMemento} read from the stream
 * @throws XMLStreamException
 *         if an error occurred reading from the reader
 */
private synchronized void readFromElement(final XMLStreamReader reader) throws XMLStreamException {
    Check.notNull(reader, "reader"); //$NON-NLS-1$

    /*
     * Read all the attributes from the current element.
     */
    final int attributeCount = reader.getAttributeCount();
    for (int i = 0; i < attributeCount; i++) {
        putString(reader.getAttributeLocalName(i), reader.getAttributeValue(i));
    }

    /*
     * Process child nodes (which may be text or child Mementos).
     */
    String localName;
    int event;
    do {
        event = reader.next();

        if (event == XMLStreamConstants.START_ELEMENT) {
            localName = reader.getLocalName();

            final XMLMemento child = (XMLMemento) createChild(localName);
            child.readFromElement(reader);
        } else if (event == XMLStreamConstants.CHARACTERS) {
            putTextData(reader.getText());
        }
    } while (event != XMLStreamConstants.END_ELEMENT);
}

From source file:cz.muni.fi.mir.mathmlcanonicalization.MathMLCanonicalizer.java

/**
 * Loads configuration from XML file, overriding the properties.
 *///from w  w w  .  ja va 2s .c  o  m
private void loadXMLConfiguration(InputStream xmlConfigurationStream)
        throws ConfigException, XMLStreamException {
    assert xmlConfigurationStream != null;
    final XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    final XMLStreamReader reader = inputFactory.createXMLStreamReader(xmlConfigurationStream);

    boolean config = false;
    Module module = null;
    while (reader.hasNext()) {
        final int event = reader.next();
        switch (event) {
        case XMLStreamConstants.START_ELEMENT: {
            String name = reader.getLocalName();
            if (name.equals("config")) {
                config = true;
                break;
            }

            if (config && name.equals("module")) {
                if (reader.getAttributeCount() == 1) {
                    final String attributeName = reader.getAttributeLocalName(0);
                    final String attributeValue = reader.getAttributeValue(0);

                    if (attributeName.equals("name") && attributeValue != null) {
                        String fullyQualified = Settings.class.getPackage().getName() + ".modules."
                                + attributeValue;
                        try {
                            Class<?> moduleClass = Class.forName(fullyQualified);
                            module = (Module) moduleClass.newInstance();
                        } catch (InstantiationException ex) {
                            LOGGER.log(Level.SEVERE, ex.getMessage(), ex);
                            throw new ConfigException("cannot instantiate module " + attributeValue, ex);
                        } catch (IllegalAccessException ex) {
                            LOGGER.log(Level.SEVERE, ex.getMessage(), ex);
                            throw new ConfigException("cannot access module " + attributeValue, ex);
                        } catch (ClassNotFoundException ex) {
                            LOGGER.log(Level.SEVERE, ex.getMessage(), ex);
                            throw new ConfigException("cannot load module " + attributeValue, ex);
                        }
                    }
                }
            }

            if (config && name.equals("property")) {
                if (reader.getAttributeCount() == 1) {
                    final String attributeName = reader.getAttributeLocalName(0);
                    final String attributeValue = reader.getAttributeValue(0);

                    if (attributeName.equals("name") && attributeValue != null) {
                        if (module == null) {
                            if (Settings.isProperty(attributeValue)) {
                                Settings.setProperty(attributeValue, reader.getElementText());
                            } else {
                                throw new ConfigException("configuration not valid\n"
                                        + "Tried to override non-existing global property " + attributeValue);
                            }
                        } else {
                            if (module.isProperty(attributeValue)) {
                                module.setProperty(attributeValue, reader.getElementText());
                            } else {
                                throw new ConfigException("configuration not valid\n"
                                        + "configuration tried to override non-existing property "
                                        + attributeValue);
                            }
                        }
                    }
                }
            }

            break;
        }
        case XMLStreamConstants.END_ELEMENT: {
            if (config && reader.getLocalName().equals("module")) {
                addModule(module);

                module = null;
            }

            if (config && reader.getLocalName().equals("config")) {
                config = false;
            }
        }
        }
    }
}

From source file:com.hp.alm.ali.rest.client.AliRestClient.java

private List<String> getAttributeValues(InputStream xml, String elemName, String attrName) {
    try {// w w w  .j  a  va 2  s  . c  om
        XMLInputFactory factory = XmlUtils.createBasicInputFactory();
        XMLStreamReader parser;
        parser = factory.createXMLStreamReader(xml);
        List<String> result = new LinkedList<String>();
        while (true) {
            int event = parser.next();
            if (event == XMLStreamConstants.END_DOCUMENT) {
                parser.close();
                break;
            }
            if (event == XMLStreamConstants.START_ELEMENT && elemName.equals(parser.getLocalName())) {

                for (int i = 0; i < parser.getAttributeCount(); i++) {
                    String localName = parser.getAttributeLocalName(i);
                    if (attrName.equals(localName)) {
                        result.add(parser.getAttributeValue(i));
                        break;
                    }
                }
            }
        }
        return result;
    } catch (XMLStreamException e) {
        throw new RuntimeException(e);
    } finally {
        try {
            xml.close();
        } catch (IOException e) {
            // ignore
        }
    }
}

From source file:davmail.exchange.ews.EWSMethod.java

protected String getAttributeValue(XMLStreamReader reader, String attributeName) {
    for (int i = 0; i < reader.getAttributeCount(); i++) {
        if (attributeName.equals(reader.getAttributeLocalName(i))) {
            return reader.getAttributeValue(i);
        }//from  w w  w . j  a  va 2s  .  c o  m
    }
    return null;
}