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.jwebsocket.config.xml.JWebSocketConfigHandler.java

/**
 * private method to read the list of rights configuration
 *
 * @param aStreamReader//from www. ja v a  2  s  . c  om
 *      the stream reader object
 * @return the list of rights configuration
 * @throws XMLStreamException
 *       if there's any exception reading configuration
 */
private List<RightConfig> handleRights(XMLStreamReader aStreamReader) throws XMLStreamException {
    List<RightConfig> lRights = new FastList<RightConfig>();
    while (aStreamReader.hasNext()) {
        aStreamReader.next();
        if (aStreamReader.isStartElement()) {
            String lElementName = aStreamReader.getLocalName();
            if (lElementName.equals(ELEMENT_RIGHT)) {
                RightConfig lRight = (RightConfig) handlerContext.get(lElementName)
                        .processConfig(aStreamReader);
                lRights.add(lRight);
            }
        }
        if (aStreamReader.isEndElement()) {
            String lElementName = aStreamReader.getLocalName();
            if (lElementName.equals(ELEMENT_RIGHTS)) {
                break;
            }
        }
    }
    return lRights;
}

From source file:org.jwebsocket.config.xml.JWebSocketConfigHandler.java

/**
 * private method that reads the config for plugins
 *
 * @param aStreamReader/* w w  w  .ja va2s .c  o  m*/
 *      the stream reader object
 * @return the list of plugin configs
 * @throws XMLStreamException
 *       if exception occurs while reading
 */
protected List<PluginConfig> handlePlugins(XMLStreamReader aStreamReader) throws XMLStreamException {
    List<PluginConfig> lPlugins = new FastList<PluginConfig>();
    while (aStreamReader.hasNext()) {
        aStreamReader.next();
        if (aStreamReader.isStartElement()) {
            String lElementName = aStreamReader.getLocalName();
            if (lElementName.equals(ELEMENT_PLUGIN)) {
                PluginConfig lPlugin = (PluginConfig) handlerContext.get(lElementName)
                        .processConfig(aStreamReader);
                lPlugins.add(lPlugin);
            }
        }
        if (aStreamReader.isEndElement()) {
            String lElementName = aStreamReader.getLocalName();
            if (lElementName.equals(ELEMENT_PLUGINS)) {
                break;
            }
        }
    }
    return lPlugins;
}

From source file:org.jwebsocket.config.xml.JWebSocketConfigHandler.java

/**
 * private method that reads the config for filters
 *
 * @param aStreamReader/*www. j  ava2 s .c  o  m*/
 *      the stream reader object
 * @return the list of filter configs
 * @throws XMLStreamException
 *       if exception occurs while reading
 */
protected List<FilterConfig> handleFilters(XMLStreamReader aStreamReader) throws XMLStreamException {
    List<FilterConfig> lFilters = new FastList<FilterConfig>();
    while (aStreamReader.hasNext()) {
        aStreamReader.next();
        if (aStreamReader.isStartElement()) {
            String lElementName = aStreamReader.getLocalName();
            if (lElementName.equals(ELEMENT_FILTER)) {
                FilterConfig lFilter = (FilterConfig) handlerContext.get(lElementName)
                        .processConfig(aStreamReader);
                lFilters.add(lFilter);
            }
        }
        if (aStreamReader.isEndElement()) {
            String lElementName = aStreamReader.getLocalName();
            if (lElementName.equals(ELEMENT_FILTERS)) {
                break;
            }
        }
    }
    return lFilters;
}

From source file:org.jwebsocket.config.xml.JWebSocketConfigHandler.java

/**
 * private method that reads the config for logging
 *
 * @param aStreamReader//from w  w  w. j av a  2  s. com
 *      the stream reader object
 * @return the list of logging configs
 * @throws XMLStreamException
 *       if exception occurs while reading
 */
private List<LoggingConfig> handleLoggingConfigs(XMLStreamReader aStreamReader) throws XMLStreamException {
    List<LoggingConfig> loggingConfigs = new FastList<LoggingConfig>();
    while (aStreamReader.hasNext()) {
        aStreamReader.next();
        if (aStreamReader.isStartElement()) {
            String lElementName = aStreamReader.getLocalName();
            if (lElementName.equals(ELEMENT_LOG4J)) {
                LoggingConfig loggingConfig = (LoggingConfig) handlerContext.get(lElementName)
                        .processConfig(aStreamReader);
                loggingConfigs.add(loggingConfig);
            }
        }
        if (aStreamReader.isEndElement()) {
            String lElementName = aStreamReader.getLocalName();
            if (lElementName.equals(ELEMENT_LOGGING)) {
                break;
            }
        }
    }
    return loggingConfigs;
}

From source file:org.jwebsocket.config.xml.JWebSocketConfigHandler.java

/**
 * private method that reads the list of server configs
 *
 * @param aStreamReader/* www  .  ja va 2s  .  c o m*/
 *      the stream reader object
 * @return the list of server configs
 * @throws XMLStreamException
 *       if exception occurs reading xml
 */
private List<ServerConfig> handleServers(XMLStreamReader aStreamReader) throws XMLStreamException {
    List<ServerConfig> lServers = new FastList<ServerConfig>();
    while (aStreamReader.hasNext()) {
        aStreamReader.next();
        if (aStreamReader.isStartElement()) {
            String lElementName = aStreamReader.getLocalName();
            if (lElementName.equals(ELEMENT_SERVER)) {
                ServerConfig lServer = (ServerConfig) handlerContext.get(lElementName)
                        .processConfig(aStreamReader);
                lServers.add(lServer);
            }
        }
        if (aStreamReader.isEndElement()) {
            String lElementName = aStreamReader.getLocalName();
            if (lElementName.equals(ELEMENT_SERVERS)) {
                break;
            }
        }
    }
    return lServers;
}

From source file:org.jwebsocket.config.xml.JWebSocketConfigHandler.java

/**
 * private method that reads the list of engines config from the xml file
 *
 * @param aStreamReader/*from w ww.  j  a  va2 s .co  m*/
 *      the stream reader object
 * @return the list of engine configs
 * @throws XMLStreamException
 *       if exception occurs while reading
 */
private List<LibraryConfig> handleLibraries(XMLStreamReader aStreamReader) throws XMLStreamException {
    List<LibraryConfig> lLibraries = new FastList<LibraryConfig>();
    while (aStreamReader.hasNext()) {
        aStreamReader.next();
        if (aStreamReader.isStartElement()) {
            String lElementName = aStreamReader.getLocalName();
            if (lElementName.equals(ELEMENT_LIBRARY)) {
                LibraryConfig lLibrary = (LibraryConfig) handlerContext.get(lElementName)
                        .processConfig(aStreamReader);
                lLibraries.add(lLibrary);
            }
        }
        if (aStreamReader.isEndElement()) {
            String lElementName = aStreamReader.getLocalName();
            if (lElementName.equals(ELEMENT_LIBRARIES)) {
                break;
            }
        }
    }
    return lLibraries;
}

From source file:org.jwebsocket.config.xml.JWebSocketConfigHandler.java

/**
 * private method that reads the list of engines config from the xml file
 *
 * @param aStreamReader/*from   w  ww.j a  v  a  2  s. co  m*/
 *      the stream reader object
 * @return the list of engine configs
 * @throws XMLStreamException
 *       if exception occurs while reading
 */
private List<EngineConfig> handleEngines(XMLStreamReader aStreamReader) throws XMLStreamException {
    List<EngineConfig> lEngines = new FastList<EngineConfig>();
    while (aStreamReader.hasNext()) {
        aStreamReader.next();
        if (aStreamReader.isStartElement()) {
            String lElementName = aStreamReader.getLocalName();
            if (lElementName.equals(ELEMENT_ENGINE)) {
                EngineConfig lEngine = (EngineConfig) handlerContext.get(lElementName)
                        .processConfig(aStreamReader);
                lEngines.add(lEngine);
            }
        }
        if (aStreamReader.isEndElement()) {
            String lElementName = aStreamReader.getLocalName();
            if (lElementName.equals(ELEMENT_ENGINES)) {
                break;
            }
        }
    }
    return lEngines;
}

From source file:org.jwebsocket.config.xml.JWebSocketConfigHandler.java

/**
 * Read the map of plug-in specific settings
 * @param aStreamReader// www .j a va 2s.  co m
 *        the stream reader object
 * @return the list of domains for the engine
 * @throws XMLStreamException
 *         in case of stream exception
 */
public static Map<String, Object> getSettings(XMLStreamReader aStreamReader) throws XMLStreamException {

    Map<String, Object> lSettings = new FastMap<String, Object>();
    while (aStreamReader.hasNext()) {
        aStreamReader.next();
        if (aStreamReader.isStartElement()) {
            String lElementName = aStreamReader.getLocalName();
            if (lElementName.equals(SETTING)) {
                String lKey = aStreamReader.getAttributeValue(null, "key");
                String lType = aStreamReader.getAttributeValue(null, "type");

                aStreamReader.next();
                String lValue = aStreamReader.getText();

                if (lKey != null && lValue != null) {
                    if ("json".equalsIgnoreCase(lType)) {
                        JSONObject lJSON = null;
                        try {
                            lJSON = new JSONObject(lValue);
                        } catch (JSONException lEx) {
                            // TODO: handle invalid JSON code in settings properly!
                        }
                        lSettings.put(lKey, lJSON);
                    } else {
                        lSettings.put(lKey, lValue);
                    }
                }
            }
        }

        if (aStreamReader.isEndElement()) {
            String lElementName = aStreamReader.getLocalName();
            if (lElementName.equals(SETTINGS)) {
                break;
            }
        }
    }

    return lSettings;
}

From source file:org.mule.module.xml.util.XMLUtils.java

private static void writeStartElement(XMLStreamReader reader, XMLStreamWriter writer)
        throws XMLStreamException {
    String local = reader.getLocalName();
    String uri = reader.getNamespaceURI();
    String prefix = reader.getPrefix();
    if (prefix == null) {
        prefix = "";
    }//w  w w  .  j  a  v a  2s.c o m

    //        System.out.println("STAXUTILS:writeStartElement : node name : " + local +  " namespace URI" + uri);
    boolean writeElementNS = false;
    if (uri != null) {
        String boundPrefix = writer.getPrefix(uri);
        if (boundPrefix == null || !prefix.equals(boundPrefix)) {
            writeElementNS = true;
        }
    }

    // Write out the element name
    if (uri != null) {
        if (prefix.length() == 0 && StringUtils.isEmpty(uri)) {
            writer.writeStartElement(local);
            writer.setDefaultNamespace(uri);

        } else {
            writer.writeStartElement(prefix, local, uri);
            writer.setPrefix(prefix, uri);
        }
    } else {
        writer.writeStartElement(local);
    }

    // Write out the namespaces
    for (int i = 0; i < reader.getNamespaceCount(); i++) {
        String nsURI = reader.getNamespaceURI(i);
        String nsPrefix = reader.getNamespacePrefix(i);
        if (nsPrefix == null) {
            nsPrefix = "";
        }

        if (nsPrefix.length() == 0) {
            writer.writeDefaultNamespace(nsURI);
        } else {
            writer.writeNamespace(nsPrefix, nsURI);
        }

        if (nsURI.equals(uri) && nsPrefix.equals(prefix)) {
            writeElementNS = false;
        }
    }

    // Check if the namespace still needs to be written.
    // We need this check because namespace writing works
    // different on Woodstox and the RI.
    if (writeElementNS) {
        if (prefix.length() == 0) {
            writer.writeDefaultNamespace(uri);
        } else {
            writer.writeNamespace(prefix, uri);
        }
    }

    // Write out attributes
    for (int i = 0; i < reader.getAttributeCount(); i++) {
        String ns = reader.getAttributeNamespace(i);
        String nsPrefix = reader.getAttributePrefix(i);
        if (ns == null || ns.length() == 0) {
            writer.writeAttribute(reader.getAttributeLocalName(i), reader.getAttributeValue(i));
        } else if (nsPrefix == null || nsPrefix.length() == 0) {
            writer.writeAttribute(reader.getAttributeNamespace(i), reader.getAttributeLocalName(i),
                    reader.getAttributeValue(i));
        } else {
            writer.writeAttribute(reader.getAttributePrefix(i), reader.getAttributeNamespace(i),
                    reader.getAttributeLocalName(i), reader.getAttributeValue(i));
        }

    }
}

From source file:org.mule.modules.jive.api.xml.XmlMapper.java

/**Maps an xml from a {@link Reader} to a {@link Map}.
 * @param reader The {@link Reader} with the xml data
 * @return The map with the entity data/*from   ww w.  j a  v  a 2 s.  c  o  m*/
 * */
@SuppressWarnings("unchecked")
public final Map<String, Object> xml2map(final Reader reader) {
    final Map<String, Object> ret = new HashMap<String, Object>();
    final Stack<Map<String, Object>> maps = new Stack<Map<String, Object>>();
    Map<String, Object> current = ret;

    try {
        final XMLStreamReader r = xmlInputFactory.createXMLStreamReader(reader);
        StringBuilder lastText = new StringBuilder();
        String currentElement = null;
        int returnCount = 0;
        while (r.hasNext()) {
            final int eventType = r.next();
            if (eventType == CHARACTERS || eventType == CDATA || eventType == SPACE
                    || eventType == ENTITY_REFERENCE) {
                lastText.append(r.getText());
            } else if (eventType == END_DOCUMENT) {
                break;
            } else if (eventType == START_ELEMENT) {
                if (currentElement != null) {
                    maps.push(current);
                    final Map<String, Object> map = new HashMap<String, Object>();
                    if (StringUtils.startsWith(currentElement, "return")) {
                        currentElement = currentElement + "--" + String.valueOf(returnCount);
                        returnCount++;
                    }
                    current.put(currentElement, map);
                    current = map;
                }
                currentElement = r.getLocalName();
            } else if (eventType == END_ELEMENT) {
                if (currentElement == null) {
                    current = maps.pop();
                } else {
                    current.put(currentElement, lastText.toString().trim());
                    currentElement = null;
                    lastText = new StringBuilder();
                }
            } else {
                throw new XMLStreamException("Unexpected event type " + eventType);
            }
        }

        final Object obj = ret.get(ret.keySet().iterator().next());
        if (obj instanceof String) {
            Map<String, Object> responseTag = new HashMap<String, Object>();
            responseTag.put("response", ret.keySet().iterator().next().toString());
            return responseTag;
        } else {
            final Map<String, Object> returnXMLElement = (Map<String, Object>) ret
                    .get(ret.keySet().iterator().next());

            if (returnXMLElement.keySet().contains("return--1")) {
                return returnXMLElement;
            }
            return (Map<String, Object>) returnXMLElement.get("return--0");
        }

    } catch (XMLStreamException e) {
        throw new UnhandledException(e);
    }
}