Example usage for javax.xml.stream XMLStreamReader getLocalName

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

Introduction

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

Prototype

public String getLocalName();

Source Link

Document

Returns the (local) name of the current event.

Usage

From source file:org.orbisgis.coremap.layerModel.mapcatalog.Workspace.java

/**
 * Read the parser and feed the provided list with workspaces
 * @param mapContextList Writable, empty list of RemoteMapContext
 * @param parser Opened parser/*  www.j  a va 2s .  c  o m*/
 * @throws XMLStreamException 
 */
public void parseXML(List<RemoteMapContext> mapContextList, XMLStreamReader parser)
        throws XMLStreamException, UnsupportedEncodingException {
    List<String> hierarchy = new ArrayList<String>();
    RemoteMapContext curMapContext = null;
    Locale curLocale = null;
    StringBuilder characters = new StringBuilder();
    for (int event = parser.next(); event != XMLStreamConstants.END_DOCUMENT; event = parser.next()) {
        // For each XML elements
        switch (event) {
        case XMLStreamConstants.START_ELEMENT:
            hierarchy.add(parser.getLocalName());
            if (RemoteCommons.endsWith(hierarchy, "contexts", "context")) {
                curMapContext = new RemoteOwsMapContext(cParams);
                curMapContext.setWorkspaceName(workspaceName);
            }
            // Parse attributes
            for (int attributeId = 0; attributeId < parser.getAttributeCount(); attributeId++) {
                String attributeName = parser.getAttributeLocalName(attributeId);
                if (attributeName.equals("id")) {
                    curMapContext.setId(Integer.parseInt(parser.getAttributeValue(attributeId)));
                } else if (attributeName.equals("date")) {
                    String attributeValue = parser.getAttributeValue(attributeId);
                    try {
                        curMapContext.setDate(parseDate(attributeValue));
                    } catch (ParseException ex) {
                        LOGGER.warn(I18N.tr("Cannot parse the provided date {0}", attributeValue), ex);
                    }
                } else if (attributeName.equals("lang")) {
                    curLocale = LocalizedText.forLanguageTag(parser.getAttributeValue(attributeId));
                }
            }
            break;
        case XMLStreamConstants.END_ELEMENT:
            if (RemoteCommons.endsWith(hierarchy, "contexts", "context")) {
                mapContextList.add(curMapContext);
                curMapContext = null;
            } else if (RemoteCommons.endsWith(hierarchy, "contexts", "context", "title")) {
                Locale descLocale = Locale.getDefault();
                if (curLocale != null) {
                    descLocale = curLocale;
                }
                curMapContext.getDescription().addTitle(descLocale,
                        StringEscapeUtils.unescapeHtml4(characters.toString().trim()));
            } else if (RemoteCommons.endsWith(hierarchy, "contexts", "context", "abstract")) {
                Locale descLocale = Locale.getDefault();
                if (curLocale != null) {
                    descLocale = curLocale;
                }
                curMapContext.getDescription().addAbstract(descLocale,
                        StringEscapeUtils.unescapeHtml4(characters.toString().trim()));
            }
            characters = new StringBuilder();
            curLocale = null;
            hierarchy.remove(hierarchy.size() - 1);
            break;
        case XMLStreamConstants.CHARACTERS:
            characters.append(StringEscapeUtils.unescapeHtml4(parser.getText()));
            break;
        }
    }
}

From source file:org.osaf.cosmo.model.text.BaseXhtmlFormat.java

protected boolean isDiv(XMLStreamReader reader) {
    return reader.getLocalName().equals("div");
}

From source file:org.osaf.cosmo.model.text.BaseXhtmlFormat.java

protected boolean isSpan(XMLStreamReader reader) {
    return reader.getLocalName().equals("span");
}

From source file:org.osaf.cosmo.xml.DomReader.java

private static Element readElement(Document d, XMLStreamReader reader) throws XMLStreamException {
    Element e = null;// ww w . j av a 2s .co m

    String local = reader.getLocalName();
    String ns = reader.getNamespaceURI();
    if (ns != null) {
        String prefix = reader.getPrefix();
        String qualified = prefix != null ? prefix + ":" + local : local;
        e = d.createElementNS(ns, qualified);
    } else {
        e = d.createElement(local);
    }

    //if (log.isDebugEnabled())
    //log.debug("Reading element " + e.getTagName());

    for (int i = 0; i < reader.getAttributeCount(); i++) {
        Attr a = readAttribute(i, d, reader);
        if (a.getNamespaceURI() != null)
            e.setAttributeNodeNS(a);
        else
            e.setAttributeNode(a);
    }

    return e;
}

From source file:org.pentaho.di.trans.steps.webservices.WebService.java

private void compatibleProcessRows(InputStream anXml, Object[] rowData, RowMetaInterface rowMeta,
        boolean ignoreNamespacePrefix, String encoding) throws KettleException {

    // First we should get the complete string
    // The problem is that the string can contain XML or any other format such as HTML saying the service is no longer
    // available.
    // We're talking about a WEB service here.
    // As such, to keep the original parsing scheme, we first read the content.
    // Then we create an input stream from the content again.
    // It's elaborate, but that way we can report on the failure more correctly.
    ///*from  w w  w  .  j a va2  s  .  co  m*/
    String response = readStringFromInputStream(anXml, encoding);

    // Create a new reader to feed into the XML Input Factory below...
    //
    StringReader stringReader = new StringReader(response.toString());

    // TODO Very empirical : see if we can do something better here
    try {
        XMLInputFactory vFactory = XMLInputFactory.newInstance();
        XMLStreamReader vReader = vFactory.createXMLStreamReader(stringReader);

        Object[] outputRowData = RowDataUtil.allocateRowData(data.outputRowMeta.size());
        int outputIndex = 0;

        boolean processing = false;
        boolean oneValueRowProcessing = false;
        for (int event = vReader.next(); vReader.hasNext(); event = vReader.next()) {
            switch (event) {
            case XMLStreamConstants.START_ELEMENT:

                // Start new code
                // START_ELEMENT= 1
                //
                if (log.isRowLevel()) {
                    logRowlevel("START_ELEMENT / " + vReader.getAttributeCount() + " / "
                            + vReader.getNamespaceCount());
                }

                // If we start the xml element named like the return type,
                // we start a new row
                //
                if (log.isRowLevel()) {
                    logRowlevel("vReader.getLocalName = " + vReader.getLocalName());
                }
                if (Const.isEmpty(meta.getOutFieldArgumentName())) {
                    // getOutFieldArgumentName() == null
                    if (oneValueRowProcessing) {
                        WebServiceField field = meta.getFieldOutFromWsName(vReader.getLocalName(),
                                ignoreNamespacePrefix);
                        if (field != null) {
                            outputRowData[outputIndex++] = getValue(vReader.getElementText(), field);
                            putRow(data.outputRowMeta, outputRowData);
                            oneValueRowProcessing = false;
                        } else {
                            if (meta.getOutFieldContainerName().equals(vReader.getLocalName())) {
                                // meta.getOutFieldContainerName() = vReader.getLocalName()
                                if (log.isRowLevel()) {
                                    logRowlevel("OutFieldContainerName = " + meta.getOutFieldContainerName());
                                }
                                oneValueRowProcessing = true;
                            }
                        }
                    }
                } else {
                    // getOutFieldArgumentName() != null
                    if (log.isRowLevel()) {
                        logRowlevel("OutFieldArgumentName = " + meta.getOutFieldArgumentName());
                    }
                    if (meta.getOutFieldArgumentName().equals(vReader.getLocalName())) {
                        if (log.isRowLevel()) {
                            logRowlevel("vReader.getLocalName = " + vReader.getLocalName());
                        }
                        if (log.isRowLevel()) {
                            logRowlevel("OutFieldArgumentName = ");
                        }
                        if (processing) {
                            WebServiceField field = meta.getFieldOutFromWsName(vReader.getLocalName(),
                                    ignoreNamespacePrefix);
                            if (field != null) {
                                int index = data.outputRowMeta.indexOfValue(field.getName());
                                if (index >= 0) {
                                    outputRowData[index] = getValue(vReader.getElementText(), field);
                                }
                            }
                            processing = false;
                        } else {
                            WebServiceField field = meta.getFieldOutFromWsName(vReader.getLocalName(),
                                    ignoreNamespacePrefix);
                            if (meta.getFieldsOut().size() == 1 && field != null) {
                                // This can be either a simple return element, or a complex type...
                                //
                                try {
                                    if (meta.isPassingInputData()) {
                                        for (int i = 0; i < rowMeta.getValueMetaList().size(); i++) {
                                            ValueMetaInterface valueMeta = getInputRowMeta().getValueMeta(i);
                                            outputRowData[outputIndex++] = valueMeta.cloneValueData(rowData[i]);

                                        }
                                    }

                                    outputRowData[outputIndex++] = getValue(vReader.getElementText(), field);
                                    putRow(data.outputRowMeta, outputRowData);
                                } catch (WstxParsingException e) {
                                    throw new KettleStepException("Unable to get value for field ["
                                            + field.getName()
                                            + "].  Verify that this is not a complex data type by looking at the response XML.",
                                            e);
                                }
                            } else {
                                for (WebServiceField curField : meta.getFieldsOut()) {
                                    if (!Const.isEmpty(curField.getName())) {
                                        outputRowData[outputIndex++] = getValue(vReader.getElementText(),
                                                curField);
                                    }
                                }
                                processing = true;
                            }
                        }

                    } else {
                        if (log.isRowLevel()) {
                            logRowlevel("vReader.getLocalName = " + vReader.getLocalName());
                        }
                        if (log.isRowLevel()) {
                            logRowlevel("OutFieldArgumentName = " + meta.getOutFieldArgumentName());
                        }
                    }
                }
                break;

            case XMLStreamConstants.END_ELEMENT:
                // END_ELEMENT= 2
                if (log.isRowLevel()) {
                    logRowlevel("END_ELEMENT");
                }
                // If we end the xml element named as the return type, we
                // finish a row
                if ((meta.getOutFieldArgumentName() == null
                        && meta.getOperationName().equals(vReader.getLocalName()))) {
                    oneValueRowProcessing = false;
                } else if (meta.getOutFieldArgumentName() != null
                        && meta.getOutFieldArgumentName().equals(vReader.getLocalName())) {
                    putRow(data.outputRowMeta, outputRowData);
                    processing = false;
                }
                break;
            case XMLStreamConstants.PROCESSING_INSTRUCTION:
                // PROCESSING_INSTRUCTION= 3
                if (log.isRowLevel()) {
                    logRowlevel("PROCESSING_INSTRUCTION");
                }
                break;
            case XMLStreamConstants.CHARACTERS:
                // CHARACTERS= 4
                if (log.isRowLevel()) {
                    logRowlevel("CHARACTERS");
                }
                break;
            case XMLStreamConstants.COMMENT:
                // COMMENT= 5
                if (log.isRowLevel()) {
                    logRowlevel("COMMENT");
                }
                break;
            case XMLStreamConstants.SPACE:
                // PROCESSING_INSTRUCTION= 6
                if (log.isRowLevel()) {
                    logRowlevel("PROCESSING_INSTRUCTION");
                }
                break;
            case XMLStreamConstants.START_DOCUMENT:
                // START_DOCUMENT= 7
                if (log.isRowLevel()) {
                    logRowlevel("START_DOCUMENT");
                }
                if (log.isRowLevel()) {
                    logRowlevel(vReader.getText());
                }
                break;
            case XMLStreamConstants.END_DOCUMENT:
                // END_DOCUMENT= 8
                if (log.isRowLevel()) {
                    logRowlevel("END_DOCUMENT");
                }
                break;
            case XMLStreamConstants.ENTITY_REFERENCE:
                // ENTITY_REFERENCE= 9
                if (log.isRowLevel()) {
                    logRowlevel("ENTITY_REFERENCE");
                }
                break;
            case XMLStreamConstants.ATTRIBUTE:
                // ATTRIBUTE= 10
                if (log.isRowLevel()) {
                    logRowlevel("ATTRIBUTE");
                }
                break;
            case XMLStreamConstants.DTD:
                // DTD= 11
                if (log.isRowLevel()) {
                    logRowlevel("DTD");
                }
                break;
            case XMLStreamConstants.CDATA:
                // CDATA= 12
                if (log.isRowLevel()) {
                    logRowlevel("CDATA");
                }
                break;
            case XMLStreamConstants.NAMESPACE:
                // NAMESPACE= 13
                if (log.isRowLevel()) {
                    logRowlevel("NAMESPACE");
                }
                break;
            case XMLStreamConstants.NOTATION_DECLARATION:
                // NOTATION_DECLARATION= 14
                if (log.isRowLevel()) {
                    logRowlevel("NOTATION_DECLARATION");
                }
                break;
            case XMLStreamConstants.ENTITY_DECLARATION:
                // ENTITY_DECLARATION= 15
                if (log.isRowLevel()) {
                    logRowlevel("ENTITY_DECLARATION");
                }
                break;
            default:
                break;
            }
        }
    } catch (Exception e) {
        throw new KettleStepException(
                BaseMessages.getString(PKG, "WebServices.ERROR0010.OutputParsingError", response.toString()),
                e);
    }
}

From source file:org.pentaho.platform.dataaccess.datasource.api.AnalysisService.java

private String getSchemaName(String encoding, InputStream inputStream) throws XMLStreamException, IOException {
    String domainId = null;// w  ww  .  jav a 2  s. c om
    XMLStreamReader reader = null;
    try {
        XMLInputFactory factory = XMLInputFactory.newInstance();
        factory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);
        if (StringUtils.isEmpty(encoding)) {
            reader = factory.createXMLStreamReader(inputStream);
        } else {
            reader = factory.createXMLStreamReader(inputStream, encoding);
        }

        while (reader.next() != XMLStreamReader.END_DOCUMENT) {
            if (reader.getEventType() == XMLStreamReader.START_ELEMENT
                    && reader.getLocalName().equalsIgnoreCase("Schema")) {
                domainId = reader.getAttributeValue("", "name");
                return domainId;
            }
        }
    } finally {
        if (reader != null) {
            reader.close();
        }
        inputStream.reset();
    }

    return domainId;
}

From source file:org.restcomm.connect.interpreter.rcml.Parser.java

private void start(final Stack<Tag.Builder> builders, final XMLStreamReader stream) {
    final Tag.Builder builder = Tag.builder();
    // Read the next tag.
    builder.setName(stream.getLocalName());
    // Read the attributes.
    final int limit = stream.getAttributeCount();
    for (int index = 0; index < limit; index++) {
        final String name = stream.getAttributeLocalName(index);
        final String value = stream.getAttributeValue(index).trim();
        final Attribute attribute = new Attribute(name, value);
        builder.addAttribute(attribute);
    }/*from w  w w  .  j a v a  2s .  c  o  m*/
    builders.push(builder);
}

From source file:org.reusables.dbunit.autocomplete.AutoCompletionRules.java

private void parse(final URL rulesFileUrl) {
    InputStream input = null;//ww  w .  j av a 2 s.  com
    XMLStreamReader parser = null;

    try {
        final XMLInputFactory factory = XMLInputFactory.newInstance();

        input = rulesFileUrl.openStream();
        parser = factory.createXMLStreamReader(input);

        for (int event = parser.next(); event != XMLStreamConstants.END_DOCUMENT; event = parser.next()) {
            if (event == XMLStreamConstants.START_ELEMENT && ELEM_RULES.equals(parser.getLocalName())) {
                parseRules(parser);
            } else if (event == XMLStreamConstants.START_ELEMENT && ELEM_TABLE.equals(parser.getLocalName())) {
                parseTable(parser);
            }
        }
    } catch (final XMLStreamException e) {
        throw new DbUnitAutoCompletionException("Error parsing xml stream.", e);
    } catch (final IOException e) {
        throw new DbUnitAutoCompletionException("Error reading stream.", e);
    } finally {
        IOUtils.closeQuietly(input);
        closeParser(parser);
    }
}

From source file:org.reusables.dbunit.autocomplete.AutoCompletionRules.java

private void parseTable(final XMLStreamReader parser) throws XMLStreamException {
    final String name = getName(parser);
    final Map<String, AutoCompletionColumn> columns = addTable(name);
    AutoCompletionColumn currentColumn = null;

    for (int event = parser.next(); event != XMLStreamConstants.END_DOCUMENT; event = parser.next()) {
        if (event == XMLStreamConstants.START_ELEMENT) {
            currentColumn = parseColumn(parser);
            columns.put(currentColumn.getName().toLowerCase(), currentColumn);
        } else if (event == XMLStreamConstants.CHARACTERS || event == XMLStreamConstants.CDATA) {
            parseColumnValue(parser, currentColumn);
        } else if (event == XMLStreamConstants.END_ELEMENT) {
            currentColumn = null;// w  w  w  .jav  a  2 s  . co m
            if (ELEM_TABLE.equals(parser.getLocalName())) {
                return;
            }
        }
    }
}

From source file:org.reusables.dbunit.autocomplete.AutoCompletionRules.java

private AutoCompletionColumn parseColumn(final XMLStreamReader parser) {
    final ColumnType type = ColumnType.valueOf(parser.getLocalName().toUpperCase());
    final AutoCompletionColumn column = new AutoCompletionColumn(type, getName(parser));
    column.setMin(parseNumber(parser, ATTR_MIN));
    column.setMax(parseNumber(parser, ATTR_MAX));
    column.setOptional(parseBoolean(parser, ATTR_OPTIONAL));
    column.setReference(parser.getAttributeValue(null, ATTR_REFERENCE));
    return column;
}