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:StAXStreamTreeViewer.java

private void parseRestOfDocument(XMLStreamReader reader, DefaultMutableTreeNode current)
        throws XMLStreamException {

    while (reader.hasNext()) {
        int type = reader.next();
        switch (type) {
        case XMLStreamConstants.START_ELEMENT:

            DefaultMutableTreeNode element = new DefaultMutableTreeNode(reader.getLocalName());
            current.add(element);/*from  w w  w  .  ja v  a2  s. co m*/
            current = element;

            if (reader.getNamespaceURI() != null) {
                String prefix = reader.getPrefix();
                if (prefix == null) {
                    prefix = "[None]";
                }
                DefaultMutableTreeNode namespace = new DefaultMutableTreeNode(
                        "prefix = '" + prefix + "', URI = '" + reader.getNamespaceURI() + "'");
                current.add(namespace);
            }

            if (reader.getAttributeCount() > 0) {
                for (int i = 0; i < reader.getAttributeCount(); i++) {
                    DefaultMutableTreeNode attribute = new DefaultMutableTreeNode(
                            "Attribute (name = '" + reader.getAttributeLocalName(i) + "', value = '"
                                    + reader.getAttributeValue(i) + "')");
                    String attURI = reader.getAttributeNamespace(i);
                    if (attURI != null) {
                        String attPrefix = reader.getAttributePrefix(i);
                        if (attPrefix == null || attPrefix.equals("")) {
                            attPrefix = "[None]";
                        }
                        DefaultMutableTreeNode attNamespace = new DefaultMutableTreeNode(
                                "prefix=" + attPrefix + ",URI=" + attURI);
                        attribute.add(attNamespace);
                    }
                    current.add(attribute);
                }
            }

            break;
        case XMLStreamConstants.END_ELEMENT:
            current = (DefaultMutableTreeNode) current.getParent();
            break;
        case XMLStreamConstants.CHARACTERS:
            if (!reader.isWhiteSpace()) {
                DefaultMutableTreeNode data = new DefaultMutableTreeNode("CD:" + reader.getText());
                current.add(data);
            }
            break;
        case XMLStreamConstants.DTD:
            DefaultMutableTreeNode dtd = new DefaultMutableTreeNode("DTD:" + reader.getText());
            current.add(dtd);
            break;
        case XMLStreamConstants.SPACE:
            break;
        case XMLStreamConstants.COMMENT:
            DefaultMutableTreeNode comment = new DefaultMutableTreeNode(reader.getText());
            current.add(comment);
            break;
        default:
            System.out.println(type);
        }
    }
}

From source file:de.codesourcery.eve.skills.util.XMLMapper.java

public <T> Collection<T> read(Class<T> clasz, IFieldConverters converters, InputStream instream)
        throws XMLStreamException, IOException, IllegalArgumentException, InstantiationException,
        IllegalAccessException, InvocationTargetException, SecurityException, NoSuchMethodException {

    final Collection<T> result = new ArrayList<T>();

    try {//from   w ww.j  a  va  2  s. com

        final BeanDescription desc = createBeanDescription(clasz);

        /* 
         * Create inverse mapping attribute name -> field. 
         */
        final Map<String, Field> inverseMapping = new HashMap<String, Field>();

        if (!this.propertyNameMappings.isEmpty()) {

            // key = property name  / value = attribute name
            for (Map.Entry<String, String> propToAttribute : this.propertyNameMappings.entrySet()) {
                inverseMapping.put(propToAttribute.getValue(), desc.getFieldByName(propToAttribute.getKey()));
            }

        } else { // create default mappings
            for (Field f : desc.getFields()) {
                inverseMapping.put(f.getName(), f);
            }
        }

        final int fieldCount = desc.getFields().size();

        final XMLInputFactory factory = XMLInputFactory.newInstance();
        final XMLStreamReader parser = factory.createXMLStreamReader(instream);

        boolean inRow = false;

        final Constructor<T> constructor = clasz.getConstructor(new Class<?>[0]);

        for (int event = parser.next(); event != XMLStreamConstants.END_DOCUMENT; event = parser.next()) {
            switch (event) {
            case XMLStreamConstants.START_ELEMENT:
                if ("row".equals(parser.getLocalName())) { // parse row
                    if (inRow) {
                        throw new XMLStreamException("Found nested <row> tag ?", parser.getLocation());
                    }
                    inRow = true;

                    final T bean = constructor.newInstance(new Object[0]);
                    for (int i = 0; i < fieldCount; i++) {
                        final String attrName = parser.getAttributeLocalName(i);
                        final String attrValue = parser.getAttributeValue(i);
                        final Field field = inverseMapping.get(attrName);

                        if (!NIL.equals(attrValue)) {
                            final Object fieldValue = converters.getConverter(field)
                                    .toObject(fromAttributeValue(attrValue), field.getType());
                            field.set(bean, fieldValue);
                        } else {
                            field.set(bean, null);
                        }

                    }
                    result.add(bean);
                }
                break;

            case XMLStreamConstants.END_ELEMENT:
                if ("row".equals(parser.getLocalName())) { // parse row
                    if (!inRow) {
                        throw new XMLStreamException("Found </row> tag without start tag at ",
                                parser.getLocation());
                    }
                    inRow = false;
                }
                break;

            }
        }
    } finally {
        instream.close();
    }

    return result;
}

From source file:com.rapidminer.gui.properties.OperatorPropertyPanel.java

/**
 * Starts a progress thread which parses the parameter descriptions for the provided operator ,
 * cleans the {@link #parameterDescriptionCache}, and stores parsed descriptions in the
 * {@link #parameterDescriptionCache}.//w w  w  .  j  a  va  2s  .  c o  m
 */
private void parseParameterDescriptions(final Operator operator) {
    parameterDescriptionCache.clear();
    URL documentationURL = OperatorDocumentationBrowser.getDocResourcePath(operator);
    if (documentationURL != null) {
        try (InputStream documentationStream = documentationURL.openStream()) {
            XMLStreamReader reader = XML_STREAM_FACTORY.createXMLStreamReader(documentationStream);
            String parameterKey = null;

            // The builder that stores the parameter description text
            StringBuilder parameterTextBuilder = null;
            boolean inParameters = false;
            while (reader.hasNext()) {
                switch (reader.next()) {
                case XMLStreamReader.START_ELEMENT:
                    if (!inParameters && reader.getLocalName().equals(TAG_PARAMETERS)) {
                        inParameters = true;
                    } else {
                        AttributesImpl attributes = new AttributesImpl();
                        for (int i = 0; i < reader.getAttributeCount(); i++) {
                            attributes.addAttribute("", reader.getAttributeLocalName(i),
                                    reader.getAttributeName(i).toString(), reader.getAttributeType(i),
                                    reader.getAttributeValue(i));
                        }

                        // Check if no parameter was found
                        if (reader.getLocalName().equals(TAG_PARAMETER)) {
                            parameterKey = attributes.getValue(ATTRIBUTE_PARAMETER_KEY);

                            // In case a parameter key was found, create a new string
                            // builder
                            if (parameterKey != null) {
                                parameterTextBuilder = new StringBuilder();
                            }
                        }

                        if (parameterTextBuilder != null) {
                            appendParameterStartTag(reader.getLocalName(), attributes, parameterTextBuilder);
                        }
                    }
                    break;
                case XMLStreamReader.END_ELEMENT:
                    // end parsing when end of parameters element is reached
                    if (reader.getLocalName().equals(TAG_PARAMETERS)) {
                        return;
                    }

                    if (parameterTextBuilder != null) {

                        // otherwise add element to description text
                        parameterTextBuilder.append("</");
                        parameterTextBuilder.append(reader.getLocalName());
                        parameterTextBuilder.append(">");

                        // Store description when parameter element ends
                        if (reader.getLocalName().equals(TAG_PARAMETER)) {
                            final String parameterDescription = parameterTextBuilder.toString();
                            final String key = parameterKey;
                            if (!parameterDescriptionCache.containsKey(parameterKey)) {
                                Source xmlSource = new StreamSource(new StringReader(parameterDescription));
                                try {
                                    String desc = OperatorDocToHtmlConverter.applyXSLTTransformation(xmlSource);
                                    parameterDescriptionCache.put(key, StringEscapeUtils.unescapeHtml(desc));
                                } catch (TransformerException e) {
                                    // ignore
                                }
                            }
                        }
                    }
                    break;
                case XMLStreamReader.CHARACTERS:
                    if (parameterTextBuilder != null) {
                        parameterTextBuilder.append(StringEscapeUtils.escapeHtml(reader.getText()));
                    }
                    break;
                default:
                    // ignore other events
                    break;
                }
            }
        } catch (IOException | XMLStreamException e) {
            // ignore
        }
    }
}

From source file:com.clustercontrol.agent.winevent.WinEventMonitor.java

/**
 * XMLStAX???EventLogRecord????/*w w  w .j a v a2 s  .com*/
 * @param eventXmlStream
 * @return EventLogRecord?
 */
private ArrayList<EventLogRecord> parseEventXML(InputStream eventXmlStream) {
    ArrayList<EventLogRecord> eventlogs = new ArrayList<EventLogRecord>();

    try {
        XMLInputFactory xmlif = XMLInputFactory.newInstance();
        /**
         * OpenJDK7/OracleJDK7??"]"?2?????????????????????????????
         * ?XML?????????OpenJDK7/OracleJDK7???????/??????????
         * URL???????????????
         * 
         * URL
         * http://docs.oracle.com/javase/jp/6/api/javax/xml/stream/XMLStreamReader.html#next()
         */
        String xmlCoalescingKey = "javax.xml.stream.isCoalescing";// TODO JRE???????????????????
        if (m_log.isDebugEnabled()) {
            m_log.debug(xmlCoalescingKey + " = true");
        }
        xmlif.setProperty(xmlCoalescingKey, true);
        XMLStreamReader xmlr = xmlif.createXMLStreamReader(eventXmlStream);

        while (xmlr.hasNext()) {
            switch (xmlr.getEventType()) {
            case XMLStreamConstants.START_ELEMENT:
                m_log.trace("EventType : XMLStreamConstants.START_ELEMENT");

                String localName = xmlr.getLocalName();
                m_log.trace("local name : " + localName);

                if ("Event".equals(localName)) {
                    EventLogRecord eventlog = new EventLogRecord();
                    eventlogs.add(eventlog);
                    m_log.debug("create new EventLogRecord");
                } else {
                    String attrLocalName = null;
                    String attrValue = null;

                    if (xmlr.getAttributeCount() != 0) {
                        attrLocalName = xmlr.getAttributeLocalName(0);
                        attrValue = xmlr.getAttributeValue(0);
                        m_log.trace("attribute local name : " + attrLocalName);
                        m_log.trace("attribute local value : " + attrValue);
                    }

                    if ("Provider".equals(localName)) {
                        if ("Name".equals(attrLocalName)) {
                            m_log.trace("target value : " + attrValue);

                            EventLogRecord eventlog = eventlogs.get(eventlogs.size() - 1);
                            eventlog.setProviderName(attrValue);
                            m_log.debug("set ProviderName : " + eventlog.getProviderName());
                        }
                    }
                    // Get-WinEvent/wevtutil.exe
                    else if ("TimeCreated".equals(localName) && "SystemTime".equals(attrLocalName)) {
                        m_log.trace("target value : " + attrValue);

                        // "yyyy-MM-dd'T'HH:mm:ss.SSSSSSSSS'Z'"???S????????????
                        String formatedDateString = attrValue.replaceAll("\\..*Z", "");
                        m_log.trace("formatted target value : " + formatedDateString);
                        DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
                        sdf.setTimeZone(TimeZone.getTimeZone("UTC"));

                        EventLogRecord eventlog = eventlogs.get(eventlogs.size() - 1);
                        ;
                        try {
                            eventlog.setTimeCreated(sdf.parse(formatedDateString));
                        } catch (ParseException e) {
                            // do nothing
                            m_log.error("set TimeCreated Error", e);
                        }
                        m_log.debug("set TimeCreated : " + eventlog.getTimeCreated());
                    }
                    // Get-EventLog
                    if ("TimeGenerated".equals(localName) && "SystemTime".equals(attrLocalName)) {
                        m_log.trace("target value : " + attrValue);
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss'Z'");
                        sdf.setTimeZone(HinemosTime.getTimeZone());

                        EventLogRecord eventlog = eventlogs.get(eventlogs.size() - 1);
                        ;
                        try {
                            eventlog.setTimeCreated(sdf.parse(attrValue));
                        } catch (ParseException e) {
                            // do nothing
                            m_log.error("set TimeCreated Error", e);
                        }
                        m_log.debug("set TimeCreated : " + eventlog.getTimeCreated());
                    } else {
                        targetProperty = localName;
                        m_log.trace("target property : " + targetProperty);
                    }
                }

                break;
            case XMLStreamConstants.SPACE:
            case XMLStreamConstants.CHARACTERS:
                m_log.trace("EventType : XMLStreamConstants.CHARACTERS, length=" + xmlr.getTextLength());
                if (targetProperty != null) {
                    try {
                        EventLogRecord eventlog = eventlogs.get(eventlogs.size() - 1);
                        ;
                        if ("EventID".equals(targetProperty)) {
                            eventlog.setId(Integer.parseInt(new String(xmlr.getTextCharacters(),
                                    xmlr.getTextStart(), xmlr.getTextLength())));
                            m_log.debug("set EventID : " + eventlog.getId());
                        }
                        // Get-WinEvent/wevtutil.exe
                        else if ("Level".equals(targetProperty)) {
                            if (eventlog.getLevel() == WinEventConstant.UNDEFINED) {
                                eventlog.setLevel(Integer.parseInt(new String(xmlr.getTextCharacters(),
                                        xmlr.getTextStart(), xmlr.getTextLength())));
                                m_log.debug("set Level : " + eventlog.getLevel());
                            }
                        } else if ("Task".equals(targetProperty)) {
                            if (eventlog.getTask() == WinEventConstant.UNDEFINED) {
                                eventlog.setTask(Integer.parseInt(new String(xmlr.getTextCharacters(),
                                        xmlr.getTextStart(), xmlr.getTextLength())));
                                m_log.debug("set Task : " + eventlog.getTask());
                            }
                        } else if ("Keywords".equals(targetProperty)) {
                            // TODO ????????0x8080000000000000
                            //eventlog.setKeywords(Long.decode(new String(xmlr.getTextCharacters(), xmlr.getTextStart(), xmlr.getTextLength())));
                            //m_log.debug("set Keywords : " + eventlog.getKeywords());
                        } else if ("EventRecordId".equals(targetProperty)) {
                            eventlog.setRecordId(Long.parseLong(new String(xmlr.getTextCharacters(),
                                    xmlr.getTextStart(), xmlr.getTextLength())));
                            m_log.debug("set RecordId : " + eventlog.getRecordId());
                        } else if ("Channel".equals(targetProperty)) {
                            eventlog.setLogName(new String(xmlr.getTextCharacters(), xmlr.getTextStart(),
                                    xmlr.getTextLength()));
                            m_log.debug("set LogName : " + eventlog.getLogName());
                        } else if ("Computer".equals(targetProperty)) {
                            eventlog.setMachineName(new String(xmlr.getTextCharacters(), xmlr.getTextStart(),
                                    xmlr.getTextLength()));
                            m_log.debug("set MachineName : " + eventlog.getMachineName());
                        } else if ("Message".equals(targetProperty)) {
                            String message = new String(xmlr.getTextCharacters(), xmlr.getTextStart(),
                                    xmlr.getTextLength());
                            message = message.replaceAll(tmpReturnCode, "\r\n");
                            message = message.replaceAll(tmpLtCode, "<");
                            message = message.replaceAll(tmpGtCode, ">");
                            eventlog.setMessage(message);
                            m_log.debug("set Message : " + eventlog.getMessage());
                        } else if ("Data".equals(targetProperty)) {
                            String data = new String(xmlr.getTextCharacters(), xmlr.getTextStart(),
                                    xmlr.getTextLength());
                            eventlog.getData().add(data);
                            m_log.debug("set Data : " + data);
                        } else {
                            m_log.debug("unknown target property : " + targetProperty);
                        }
                    } catch (NumberFormatException e) {
                        m_log.debug("number parse error", e);
                    }
                }
                targetProperty = null;
                break;
            default: // 
                break;
            }
            xmlr.next();
        }
        xmlr.close();
    } catch (XMLStreamException e) {
        m_log.warn("parseEvent() xmlstream error", e);
    }

    return eventlogs;

}

From source file:ddf.security.assertion.impl.SecurityAssertionImpl.java

/**
 * Parses the SecurityToken by wrapping within an AssertionWrapper.
 *
 * @param securityToken SecurityToken//from  www.  j av  a 2s . c  o  m
 */
private void parseToken(SecurityToken securityToken) {
    XMLStreamReader xmlStreamReader = StaxUtils.createXMLStreamReader(securityToken.getToken());

    try {
        AttrStatement attributeStatement = null;
        AuthenticationStatement authenticationStatement = null;
        Attr attribute = null;
        int attrs = 0;
        while (xmlStreamReader.hasNext()) {
            int event = xmlStreamReader.next();
            switch (event) {
            case XMLStreamConstants.START_ELEMENT: {
                String localName = xmlStreamReader.getLocalName();
                switch (localName) {
                case NameID.DEFAULT_ELEMENT_LOCAL_NAME:
                    name = xmlStreamReader.getElementText();
                    for (int i = 0; i < xmlStreamReader.getAttributeCount(); i++) {
                        if (xmlStreamReader.getAttributeLocalName(i).equals(NameID.FORMAT_ATTRIB_NAME)) {
                            nameIDFormat = xmlStreamReader.getAttributeValue(i);
                            break;
                        }
                    }
                    break;
                case AttributeStatement.DEFAULT_ELEMENT_LOCAL_NAME:
                    attributeStatement = new AttrStatement();
                    attributeStatements.add(attributeStatement);
                    break;
                case AuthnStatement.DEFAULT_ELEMENT_LOCAL_NAME:
                    authenticationStatement = new AuthenticationStatement();
                    authenticationStatements.add(authenticationStatement);
                    attrs = xmlStreamReader.getAttributeCount();
                    for (int i = 0; i < attrs; i++) {
                        String name = xmlStreamReader.getAttributeLocalName(i);
                        String value = xmlStreamReader.getAttributeValue(i);
                        if (AuthnStatement.AUTHN_INSTANT_ATTRIB_NAME.equals(name)) {
                            authenticationStatement.setAuthnInstant(DateTime.parse(value));
                        }
                    }
                    break;
                case AuthnContextClassRef.DEFAULT_ELEMENT_LOCAL_NAME:
                    if (authenticationStatement != null) {
                        String classValue = xmlStreamReader.getText();
                        classValue = classValue.trim();
                        AuthenticationContextClassRef authenticationContextClassRef = new AuthenticationContextClassRef();
                        authenticationContextClassRef.setAuthnContextClassRef(classValue);
                        AuthenticationContext authenticationContext = new AuthenticationContext();
                        authenticationContext.setAuthnContextClassRef(authenticationContextClassRef);
                        authenticationStatement.setAuthnContext(authenticationContext);
                    }
                    break;
                case Attribute.DEFAULT_ELEMENT_LOCAL_NAME:
                    attribute = new Attr();
                    if (attributeStatement != null) {
                        attributeStatement.addAttribute(attribute);
                    }
                    attrs = xmlStreamReader.getAttributeCount();
                    for (int i = 0; i < attrs; i++) {
                        String name = xmlStreamReader.getAttributeLocalName(i);
                        String value = xmlStreamReader.getAttributeValue(i);
                        if (Attribute.NAME_ATTTRIB_NAME.equals(name)) {
                            attribute.setName(value);
                        } else if (Attribute.NAME_FORMAT_ATTRIB_NAME.equals(name)) {
                            attribute.setNameFormat(value);
                        }
                    }
                    break;
                case AttributeValue.DEFAULT_ELEMENT_LOCAL_NAME:
                    XSString xsString = new XMLString();
                    xsString.setValue(xmlStreamReader.getElementText());
                    if (attribute != null) {
                        attribute.addAttributeValue(xsString);
                    }
                    break;
                case Issuer.DEFAULT_ELEMENT_LOCAL_NAME:
                    issuer = xmlStreamReader.getElementText();
                    break;
                case Conditions.DEFAULT_ELEMENT_LOCAL_NAME:
                    attrs = xmlStreamReader.getAttributeCount();
                    for (int i = 0; i < attrs; i++) {
                        String name = xmlStreamReader.getAttributeLocalName(i);
                        String value = xmlStreamReader.getAttributeValue(i);
                        if (Conditions.NOT_BEFORE_ATTRIB_NAME.equals(name)) {
                            notBefore = DatatypeConverter.parseDateTime(value).getTime();
                        } else if (Conditions.NOT_ON_OR_AFTER_ATTRIB_NAME.equals(name)) {
                            notOnOrAfter = DatatypeConverter.parseDateTime(value).getTime();
                        }
                    }
                    break;
                case SubjectConfirmation.DEFAULT_ELEMENT_LOCAL_NAME:
                    attrs = xmlStreamReader.getAttributeCount();
                    for (int i = 0; i < attrs; i++) {
                        String name = xmlStreamReader.getAttributeLocalName(i);
                        String value = xmlStreamReader.getAttributeValue(i);
                        if (SubjectConfirmation.METHOD_ATTRIB_NAME.equals(name)) {
                            subjectConfirmations.add(value);
                        }
                    }
                case Assertion.DEFAULT_ELEMENT_LOCAL_NAME:
                    attrs = xmlStreamReader.getAttributeCount();
                    for (int i = 0; i < attrs; i++) {
                        String name = xmlStreamReader.getAttributeLocalName(i);
                        String value = xmlStreamReader.getAttributeValue(i);
                        if (Assertion.VERSION_ATTRIB_NAME.equals(name)) {
                            if ("2.0".equals(value)) {
                                tokenType = "http://docs.oasis-open.org/wss/oasis-wss-saml-token-profile-1.1#SAMLV2.0";
                            } else if ("1.1".equals(value)) {
                                tokenType = "http://docs.oasis-open.org/wss/oasis-wss-saml-token-profile-1.1#SAMLV1.1";
                            }
                        }
                    }
                }
                break;
            }
            case XMLStreamConstants.END_ELEMENT: {
                String localName = xmlStreamReader.getLocalName();
                switch (localName) {
                case AttributeStatement.DEFAULT_ELEMENT_LOCAL_NAME:
                    attributeStatement = null;
                    break;
                case Attribute.DEFAULT_ELEMENT_LOCAL_NAME:
                    attribute = null;
                    break;
                default:
                    break;
                }
                break;
            }
            }
        }
    } catch (XMLStreamException e) {
        LOGGER.error("Unable to parse security token.", e);
    } finally {
        try {
            xmlStreamReader.close();
        } catch (XMLStreamException ignore) {
            //ignore
        }
    }
}

From source file:edu.indiana.d2i.htrc.portal.HTRCPersistenceAPIClient.java

private AlgorithmDetailsBean parseAlgorithmDetailBean(InputStream stream) throws XMLStreamException {
    AlgorithmDetailsBean res = new AlgorithmDetailsBean();
    XMLStreamReader parser = factory.createXMLStreamReader(stream);

    List<AlgorithmDetailsBean.Parameter> parameters = new ArrayList<AlgorithmDetailsBean.Parameter>();
    List<String> authors = new ArrayList<String>();
    while (parser.hasNext()) {
        int event = parser.next();
        if (event == XMLStreamConstants.START_ELEMENT) {
            if (parser.hasName()) {
                // only parse the info tag!
                if (parser.getLocalName().equals(AlgorithmDetailsBean.NAME)) {
                    res.setName(parser.getElementText());
                } else if (parser.getLocalName().equals(AlgorithmDetailsBean.VERSION)) {
                    res.setVersion(parser.getElementText());
                } else if (parser.getLocalName().equals(AlgorithmDetailsBean.DESCRIPTION)) {
                    res.setDescription(parser.getElementText());
                } else if (parser.getLocalName().equals(AlgorithmDetailsBean.SUPPORTURL)) {
                    res.setSupportUrl(parser.getElementText());
                } else if (parser.getLocalName().equals(AlgorithmDetailsBean.PARAMETER)) {
                    AlgorithmDetailsBean.Parameter parameter = new AlgorithmDetailsBean.Parameter();
                    int count = parser.getAttributeCount();
                    for (int i = 0; i < count; i++) {
                        if (parser.getAttributeLocalName(i).equals("required"))
                            parameter.setRequired(Boolean.valueOf(parser.getAttributeValue(i)));
                        if (parser.getAttributeLocalName(i).equals("type"))
                            parameter.setType(parser.getAttributeValue(i));
                        if (parser.getAttributeLocalName(i).equals("name"))
                            parameter.setName(parser.getAttributeValue(i));
                        if (parser.getAttributeLocalName(i).equals("defaultValue"))
                            parameter.setDefaultValue(parser.getAttributeValue(i));
                        if (parser.getAttributeLocalName(i).equals("validation"))
                            parameter.setValidation(parser.getAttributeValue(i));
                        if (parser.getAttributeLocalName(i).equals("validationError"))
                            parameter.setValidationError(parser.getAttributeValue(i));
                        if (parser.getAttributeLocalName(i).equals("readOnly"))
                            parameter.setReadOnly(Boolean.parseBoolean(parser.getAttributeValue(i)));
                    }/*from  w ww.  java 2  s. c  o m*/
                    parser.nextTag();
                    if (parser.getLocalName().equals("label"))
                        parameter.setLabel(parser.getElementText());
                    parser.nextTag();
                    if (parser.getLocalName().equals("description"))
                        parameter.setDescription(parser.getElementText());
                    parameters.add(parameter);
                } else if (parser.getLocalName().equals(AlgorithmDetailsBean.AUTHOR)) {
                    int count = parser.getAttributeCount();
                    for (int i = 0; i < count; i++) {
                        if (parser.getAttributeLocalName(i).equals("name"))
                            authors.add(parser.getAttributeValue(i));
                    }
                }
            }
        }
    }
    res.setParameters(parameters);
    res.setAuthors(authors);
    return res;
}

From source file:net.landora.animeinfo.anidb.AniDBHTTPManager.java

public Anime downloadAnime(int aid) {
    InputStream is = null;//from   ww  w .  j a v a  2 s.  co  m
    try {

        //            URL url = new URL(String.format("%s&request=anime&aid=%d", HTTP_URL, aid));
        //            is = new GZIPInputStream(url.openStream());
        is = new BufferedInputStream(new FileInputStream("/home/bdickie/anidb/http_test.xml"));

        XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(is);

        reader.nextTag();
        reader.require(XMLStreamReader.START_ELEMENT, null, "anime");

        Anime anime = new Anime();

        anime.setAnimeId(Integer.parseInt(reader.getAttributeValue(null, "id")));
        anime.setHentai(Boolean.parseBoolean(reader.getAttributeValue(null, "restricted")));
        anime.setLastLoaded(Calendar.getInstance());

        while (reader.nextTag() != XMLStreamReader.END_ELEMENT) {
            String tagName = reader.getLocalName();
            if (tagName.equals("type")) {
                anime.setType(nextString(reader));
            } else if (tagName.equals("episodecount")) {
                anime.setEpisodeCount(Integer.parseInt(nextString(reader)));
            } else if (tagName.equals("startdate")) {
                //                    anime.setStartDate(nextString(reader));
            } else if (tagName.equals("enddate")) {
                //                    anime.setEndDate(nextString(reader));
            } else if (tagName.equals("description")) {
                anime.setDescription(nextString(reader));
            } else if (tagName.equals("picture")) {
                anime.setPictureFileName(nextString(reader));
            } else if (tagName.equals("titles")) {
                List<AnimeName> names = new ArrayList<AnimeName>();
                while (reader.nextTag() != XMLStreamReader.END_ELEMENT) {
                    reader.require(XMLStreamReader.START_ELEMENT, null, "title");

                    AnimeName name = new AnimeName();
                    for (int i = 0; i < reader.getAttributeCount(); i++) {
                        String aname = reader.getAttributeLocalName(i);
                        if (aname.equals("type")) {
                            name.setType(reader.getAttributeValue(i));
                        } else if (aname.equals("lang")) {
                            name.setLanguage(reader.getAttributeValue(i));
                        }

                    }

                    name.setName(nextString(reader));
                    name.setAnime(anime);
                    names.add(name);
                }

                for (AnimeName name : names) {
                    if (name.getType().equalsIgnoreCase("main")) {
                        anime.setNameMain(name.getName());
                    } else if (name.getType().equalsIgnoreCase("official")
                            && name.getLanguage().equalsIgnoreCase("en")) {
                        anime.setNameEnglish(name.getName());
                    }
                }
                anime.setNames(names);
            } else if (tagName.equals("ratings")) {

                while (reader.nextTag() != XMLStreamReader.END_ELEMENT) {
                    String tagName2 = reader.getLocalName();
                    int count = Integer.parseInt(reader.getAttributeValue(null, "count"));
                    float value = Float.parseFloat(nextString(reader));
                    if (tagName2.equals("permanent")) {
                        anime.setRatingPermanent(value);
                        anime.setRatingPermanentVotes(count);
                    } else if (tagName2.equals("temporary")) {
                        anime.setRatingTemporary(value);
                        anime.setRatingTemporaryVotes(count);
                    }
                }

            } else if (tagName.equals("categories")) {

                while (reader.nextTag() != XMLStreamReader.END_ELEMENT) {
                    reader.require(XMLStreamReader.START_ELEMENT, null, "category");

                    int categoryid = Integer.parseInt(reader.getAttributeValue(null, "id"));
                    int weight = Integer.parseInt(reader.getAttributeValue(null, "weight"));

                    AnimeCategory category = AnimeDBA.getAnimeCategory(categoryid);
                    if (category == null) {
                        return null;
                    }

                    ignoreTag(reader);
                }
            } else {
                ignoreTag(reader);
            }

        }
        reader.close();

        return anime;
    } catch (Exception e) {
        log.error("Error downloading anime: " + aid, e);
        return null;
    } finally {
        if (is != null) {
            IOUtils.closeQuietly(is);
        }
    }
}

From source file:com.ikanow.infinit.e.harvest.extraction.document.file.XmlToMetadataParser.java

/**
 * Parses XML and returns a new feed with the resulting HashMap as Metadata
 * @param reader XMLStreamReader using Stax to avoid out of memory errors
 * @return List of Feeds with their Metadata set
 *//*  w  w w  .jav a  2s .  c  o  m*/
public List<DocumentPojo> parseDocument(XMLStreamReader reader) throws XMLStreamException {
    DocumentPojo doc = new DocumentPojo();
    List<DocumentPojo> docList = new ArrayList<DocumentPojo>();
    boolean justIgnored = false;
    boolean hitIdentifier = false;

    while (reader.hasNext()) {
        int eventCode = reader.next();

        switch (eventCode) {
        case (XMLStreamReader.START_ELEMENT): {
            String tagName = reader.getLocalName();

            if (null == levelOneFields || levelOneFields.size() == 0) {
                levelOneFields = new ArrayList<String>();
                levelOneFields.add(tagName);
                doc = new DocumentPojo();
                sb.delete(0, sb.length());
                justIgnored = false;
            } else if (levelOneFields.contains(tagName)) {
                sb.delete(0, sb.length());
                doc = new DocumentPojo();
                justIgnored = false;
            } else if ((null != ignoreFields) && ignoreFields.contains(tagName)) {
                justIgnored = true;
            } else {
                if (this.bPreserveCase) {
                    sb.append("<").append(tagName).append(">");
                } else {
                    sb.append("<").append(tagName.toLowerCase()).append(">");
                }
                justIgnored = false;
            }
            hitIdentifier = tagName.equalsIgnoreCase(PKElement);

            if (!justIgnored && (null != this.AttributePrefix)) { // otherwise ignore attributes anyway
                int nAttributes = reader.getAttributeCount();
                StringBuffer sb2 = new StringBuffer();
                for (int i = 0; i < nAttributes; ++i) {
                    sb2.setLength(0);
                    sb.append('<');

                    sb2.append(this.AttributePrefix);
                    if (this.bPreserveCase) {
                        sb2.append(reader.getAttributeLocalName(i).toLowerCase());
                    } else {
                        sb2.append(reader.getAttributeLocalName(i));
                    }
                    sb2.append('>');

                    sb.append(sb2);
                    sb.append("<![CDATA[").append(reader.getAttributeValue(i).trim()).append("]]>");
                    sb.append("</").append(sb2);
                }
            }
        }
            break;

        case (XMLStreamReader.CHARACTERS): {
            if (reader.getText().trim().length() > 0 && justIgnored == false)
                sb.append("<![CDATA[").append(reader.getText().trim()).append("]]>");
            if (hitIdentifier) {
                String tValue = reader.getText().trim();
                if (null != XmlSourceName) {
                    if (tValue.length() > 0) {
                        doc.setUrl(XmlSourceName + tValue);
                    }
                }
            }
        }
            break;
        case (XMLStreamReader.END_ELEMENT): {
            hitIdentifier = !reader.getLocalName().equalsIgnoreCase(PKElement);
            if ((null != ignoreFields) && !ignoreFields.contains(reader.getLocalName())) {
                if (levelOneFields.contains(reader.getLocalName())) {
                    JSONObject json;
                    try {
                        json = XML.toJSONObject(sb.toString());
                        for (String names : JSONObject.getNames(json)) {
                            JSONObject rec = null;
                            JSONArray jarray = null;

                            try {
                                jarray = json.getJSONArray(names);
                                doc.addToMetadata(names, handleJsonArray(jarray, false));
                            } catch (JSONException e) {
                                try {
                                    rec = json.getJSONObject(names);
                                    doc.addToMetadata(names, convertJsonObjectToLinkedHashMap(rec));
                                } catch (JSONException e2) {
                                    try {
                                        Object[] val = { json.getString(names) };
                                        doc.addToMetadata(names, val);
                                    } catch (JSONException e1) {
                                        e1.printStackTrace();
                                    }
                                }
                            }
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    sb.delete(0, sb.length());
                    docList.add(doc);
                } else {
                    if (this.bPreserveCase) {
                        sb.append("</").append(reader.getLocalName()).append(">");
                    } else {
                        sb.append("</").append(reader.getLocalName().toLowerCase()).append(">");
                    }

                }
            }
        } // (end case)
            break;
        } // (end switch)
    }
    return docList;
}

From source file:gima.neo4j.testsuite.osmcheck.OSMImporter.java

private Map<String, Object> extractProperties(String name, XMLStreamReader parser) {
    // <node id="269682538" lat="56.0420950" lon="12.9693483" user="sanna" uid="31450" visible="true" version="1" changeset="133823" timestamp="2008-06-11T12:36:28Z"/>
    // <way id="27359054" user="spull" uid="61533" visible="true" version="8" changeset="4707351" timestamp="2010-05-15T15:39:57Z">
    // <relation id="77965" user="Grillo" uid="13957" visible="true" version="24" changeset="5465617" timestamp="2010-08-11T19:25:46Z">
    LinkedHashMap<String, Object> properties = new LinkedHashMap<String, Object>();
    for (int i = 0; i < parser.getAttributeCount(); i++) {
        String prop = parser.getAttributeLocalName(i);
        String value = parser.getAttributeValue(i);
        if (name != null && prop.equals("id")) {
            prop = name + "_osm_id";
            name = null;/* w  w  w  . ja va  2s.co m*/
        }
        if (prop.equals("lat") || prop.equals("lon")) {
            properties.put(prop, Double.parseDouble(value));
        } else if (name != null && prop.equals("version")) {
            properties.put(prop, Integer.parseInt(value));
        } else if (prop.equals("visible")) {
            if (!value.equals("true") && !value.equals("1")) {
                properties.put(prop, false);
            }
        } else if (prop.equals("timestamp")) {
            try {
                Date timestamp = timestampFormat.parse(value);
                properties.put(prop, timestamp.getTime());
            } catch (ParseException e) {
                error("Error parsing timestamp", e);
            }
        } else {
            properties.put(prop, value);
        }
    }
    if (name != null) {
        properties.put("name", name);
    }
    return properties;
}

From source file:gima.neo4j.testsuite.osmcheck.OSMImporter.java

public void importFile(OSMWriter<?> osmWriter, String dataset, boolean allPoints, Charset charset)
        throws IOException, XMLStreamException {
    System.out.println("Importing with osm-writer: " + osmWriter);
    osmWriter.getOrCreateOSMDataset(layerName);
    osm_dataset = osmWriter.getDatasetId();

    long startTime = System.currentTimeMillis();
    long[] times = new long[] { 0L, 0L, 0L, 0L };
    javax.xml.stream.XMLInputFactory factory = javax.xml.stream.XMLInputFactory.newInstance();
    CountedFileReader reader = new CountedFileReader(dataset, charset);
    javax.xml.stream.XMLStreamReader parser = factory.createXMLStreamReader(reader);
    int countXMLTags = 0;
    beginProgressMonitor(100);/*from   w  w w  .  j a va2s .  c o  m*/
    setLogContext(dataset);
    boolean startedWays = false;
    boolean startedRelations = false;
    try {
        ArrayList<String> currentXMLTags = new ArrayList<String>();
        int depth = 0;
        Map<String, Object> wayProperties = null;
        ArrayList<Long> wayNodes = new ArrayList<Long>();
        Map<String, Object> relationProperties = null;
        ArrayList<Map<String, Object>> relationMembers = new ArrayList<Map<String, Object>>();
        LinkedHashMap<String, Object> currentNodeTags = new LinkedHashMap<String, Object>();
        while (true) {
            updateProgressMonitor(reader.getPercentRead());
            incrLogContext();
            int event = parser.next();
            if (event == javax.xml.stream.XMLStreamConstants.END_DOCUMENT) {
                break;
            }
            switch (event) {
            case javax.xml.stream.XMLStreamConstants.START_ELEMENT:
                currentXMLTags.add(depth, parser.getLocalName());
                String tagPath = currentXMLTags.toString();
                if (tagPath.equals("[osm]")) {
                    osmWriter.setDatasetProperties(extractProperties(parser));
                } else if (tagPath.equals("[osm, bounds]")) {
                    osmWriter.addOSMBBox(extractProperties("bbox", parser));
                } else if (tagPath.equals("[osm, node]")) {
                    // <node id="269682538" lat="56.0420950" lon="12.9693483" user="sanna" uid="31450" visible="true" version="1" changeset="133823" timestamp="2008-06-11T12:36:28Z"/>
                    osmWriter.createOSMNode(extractProperties("node", parser));
                } else if (tagPath.equals("[osm, way]")) {
                    // <way id="27359054" user="spull" uid="61533" visible="true" version="8" changeset="4707351" timestamp="2010-05-15T15:39:57Z">
                    if (!startedWays) {
                        startedWays = true;
                        times[0] = System.currentTimeMillis();
                        osmWriter.optimize();
                        times[1] = System.currentTimeMillis();
                    }
                    wayProperties = extractProperties("way", parser);
                    wayNodes.clear();
                } else if (tagPath.equals("[osm, way, nd]")) {
                    Map<String, Object> properties = extractProperties(parser);
                    wayNodes.add(Long.parseLong(properties.get("ref").toString()));
                } else if (tagPath.endsWith("tag]")) {
                    Map<String, Object> properties = extractProperties(parser);
                    currentNodeTags.put(properties.get("k").toString(), properties.get("v").toString());
                } else if (tagPath.equals("[osm, relation]")) {
                    // <relation id="77965" user="Grillo" uid="13957" visible="true" version="24" changeset="5465617" timestamp="2010-08-11T19:25:46Z">
                    if (!startedRelations) {
                        startedRelations = true;
                        times[2] = System.currentTimeMillis();
                        osmWriter.optimize();
                        times[3] = System.currentTimeMillis();
                    }
                    relationProperties = extractProperties("relation", parser);
                    relationMembers.clear();
                } else if (tagPath.equals("[osm, relation, member]")) {
                    relationMembers.add(extractProperties(parser));
                }
                if (startedRelations) {
                    if (countXMLTags < 10) {
                        log("Starting tag at depth " + depth + ": " + currentXMLTags.get(depth) + " - "
                                + currentXMLTags.toString());
                        for (int i = 0; i < parser.getAttributeCount(); i++) {
                            log("\t" + currentXMLTags.toString() + ": " + parser.getAttributeLocalName(i) + "["
                                    + parser.getAttributeNamespace(i) + "," + parser.getAttributePrefix(i) + ","
                                    + parser.getAttributeType(i) + "," + "] = " + parser.getAttributeValue(i));
                        }
                    }
                    countXMLTags++;
                }
                depth++;
                break;
            case javax.xml.stream.XMLStreamConstants.END_ELEMENT:
                if (currentXMLTags.toString().equals("[osm, node]")) {
                    osmWriter.addOSMNodeTags(allPoints, currentNodeTags);
                } else if (currentXMLTags.toString().equals("[osm, way]")) {
                    osmWriter.createOSMWay(wayProperties, wayNodes, currentNodeTags);
                } else if (currentXMLTags.toString().equals("[osm, relation]")) {
                    osmWriter.createOSMRelation(relationProperties, relationMembers, currentNodeTags);
                }
                depth--;
                currentXMLTags.remove(depth);
                // log("Ending tag at depth "+depth+": "+currentTags.get(depth));
                break;
            default:
                break;
            }
        }
    } finally {
        endProgressMonitor();
        parser.close();
        osmWriter.finish();
        this.osm_dataset = osmWriter.getDatasetId();
    }
    describeTimes(startTime, times);
    osmWriter.describeMissing();
    osmWriter.describeLoaded();

    long stopTime = System.currentTimeMillis();
    log("info | Elapsed time in seconds: " + (1.0 * (stopTime - startTime) / 1000.0));
    stats.dumpGeomStats();
    stats.printTagStats();
}