Example usage for javax.xml.stream XMLStreamReader nextTag

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

Introduction

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

Prototype

public int nextTag() throws XMLStreamException;

Source Link

Document

Skips any white space (isWhiteSpace() returns true), COMMENT, or PROCESSING_INSTRUCTION, until a START_ELEMENT or END_ELEMENT is reached.

Usage

From source file:de.huxhorn.sulky.plist.impl.PropertyListReader.java

private List<?> readArray(XMLStreamReader reader) throws XMLStreamException {
    reader.require(XMLStreamConstants.START_ELEMENT, null, ARRAY_NODE);
    reader.nextTag();
    List<Object> array = new ArrayList<Object>();
    for (;;) {//w ww  .j  a  v  a 2  s.  c o  m
        int type = reader.getEventType();
        if (XMLStreamConstants.END_ELEMENT == type && ARRAY_NODE.equals(reader.getLocalName())) {
            reader.nextTag();
            break;
        }
        array.add(readValue(reader));
    }
    return array;
}

From source file:de.huxhorn.sulky.plist.impl.PropertyListReader.java

private Map<String, ?> readDict(XMLStreamReader reader) throws XMLStreamException {
    reader.require(XMLStreamConstants.START_ELEMENT, null, DICT_NODE);
    reader.nextTag();
    Map<String, Object> map = new HashMap<String, Object>();
    for (;;) {/*from   w  w  w.  ja  va2  s  .c o m*/
        int type = reader.getEventType();
        if (XMLStreamConstants.END_ELEMENT == type && DICT_NODE.equals(reader.getLocalName())) {
            reader.nextTag();
            break;
        }
        String key = StaxUtilities.readSimpleTextNodeIfAvailable(reader, null, KEY_NODE);
        if (key != null) {
            map.put(key, readValue(reader));
        }
    }
    return map;
}

From source file:org.maodian.flyingcat.xmpp.extensions.xep0077.RegistrationCodec.java

@Override
public Object decode(XMLStreamReader xmlsr) {
    try {/*w  w  w .java  2s .c om*/
        xmlsr.require(XMLStreamConstants.START_ELEMENT, InBandRegistration.REGISTER, "query");
        String username = null;
        String password = null;

        while (xmlsr.nextTag() == XMLStreamConstants.START_ELEMENT) {
            if (xmlsr.getName().equals(new QName(InBandRegistration.REGISTER, "username"))) {
                username = xmlsr.getElementText();
            } else if (xmlsr.getName().equals(new QName(InBandRegistration.REGISTER, "password"))) {
                password = xmlsr.getElementText();
            }
        }
        return new Registration(username, password);
    } catch (XMLStreamException e) {
        throw new XmppException(e, StreamError.INVALID_XML);
    }
}

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

public Anime downloadAnime(int aid) {
    InputStream is = null;/*from   www  .  ja  va 2s .  c o 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:de.huxhorn.sulky.plist.impl.PropertyListReader.java

private Object readValue(XMLStreamReader reader) throws XMLStreamException {
    int type = reader.getEventType();
    if (XMLStreamConstants.START_ELEMENT == type && TRUE_NODE.equals(reader.getLocalName())) {
        reader.nextTag();
        reader.require(XMLStreamConstants.END_ELEMENT, null, TRUE_NODE);
        reader.nextTag();//from   w w  w  .  j  av a 2  s.  c o  m
        return Boolean.TRUE;
    }
    if (XMLStreamConstants.START_ELEMENT == type && FALSE_NODE.equals(reader.getLocalName())) {
        reader.nextTag();
        reader.require(XMLStreamConstants.END_ELEMENT, null, FALSE_NODE);
        reader.nextTag();
        return Boolean.FALSE;
    }
    if (XMLStreamConstants.START_ELEMENT == type && REAL_NODE.equals(reader.getLocalName())) {
        String text = StaxUtilities.readSimpleTextNodeIfAvailable(reader, null, REAL_NODE);

        return Double.parseDouble(text);
    }
    if (XMLStreamConstants.START_ELEMENT == type && INTEGER_NODE.equals(reader.getLocalName())) {
        String text = StaxUtilities.readSimpleTextNodeIfAvailable(reader, null, INTEGER_NODE);

        return Long.parseLong(text);
    }
    if (XMLStreamConstants.START_ELEMENT == type && STRING_NODE.equals(reader.getLocalName())) {
        return StaxUtilities.readSimpleTextNodeIfAvailable(reader, null, STRING_NODE);
    }
    if (XMLStreamConstants.START_ELEMENT == type && DATA_NODE.equals(reader.getLocalName())) {
        return readData(reader);
    }
    if (XMLStreamConstants.START_ELEMENT == type && DATE_NODE.equals(reader.getLocalName())) {
        return readDate(reader);
    }
    if (XMLStreamConstants.START_ELEMENT == type && ARRAY_NODE.equals(reader.getLocalName())) {
        return readArray(reader);
    }
    if (XMLStreamConstants.START_ELEMENT == type && DICT_NODE.equals(reader.getLocalName())) {
        return readDict(reader);
    }
    throw new RuntimeException("Unexpected XML-Node: " + reader.getLocalName());
}

From source file:com.pocketsoap.salesforce.soap.ChatterClient.java

private <T> T makeSoapRequest(String serverUrl, RequestEntity req, ResponseParser<T> respParser)
        throws XMLStreamException, IOException {
    PostMethod post = new PostMethod(serverUrl);
    post.addRequestHeader("SOAPAction", "\"\"");
    post.setRequestEntity(req);//from   www . ja  v  a  2  s  . c  om

    HttpClient http = new HttpClient();
    int sc = http.executeMethod(post);
    if (sc != 200 && sc != 500)
        throw new IOException("request to " + serverUrl + " returned unexpected HTTP status code of " + sc
                + ", check configuration.");

    XMLInputFactory f = XMLInputFactory.newInstance();
    f.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);

    XMLStreamReader rdr = f.createXMLStreamReader(post.getResponseBodyAsStream());
    rdr.require(XMLStreamReader.START_DOCUMENT, null, null);
    rdr.nextTag();
    rdr.require(XMLStreamReader.START_ELEMENT, SOAP_NS, "Envelope");
    rdr.nextTag();
    // TODO, should handle a Header appearing in the response.
    rdr.require(XMLStreamReader.START_ELEMENT, SOAP_NS, "Body");
    rdr.nextTag();
    if (rdr.getLocalName().equals("Fault")) {
        throw handleSoapFault(rdr);
    }
    try {
        T response = respParser.parse(rdr);
        while (rdr.hasNext())
            rdr.next();
        return response;
    } finally {
        try {
            rdr.close();
        } finally {
            post.releaseConnection();
        }
    }
}

From source file:com.auditbucket.client.Importer.java

static long processXMLFile(String file, AbRestClient abExporter, XmlMappable mappable, boolean simulateOnly)
        throws ParserConfigurationException, IOException, SAXException, JDOMException, DatagioException {
    try {//  ww w.j  ava  2s. co  m
        long rows = 0;
        StopWatch watch = new StopWatch();
        StreamSource source = new StreamSource(file);
        XMLInputFactory xif = XMLInputFactory.newFactory();
        XMLStreamReader xsr = xif.createXMLStreamReader(source);
        mappable.positionReader(xsr);
        List<CrossReferenceInputBean> referenceInputBeans = new ArrayList<>();

        String docType = mappable.getDataType();
        watch.start();
        try {
            long then = new DateTime().getMillis();
            while (xsr.getLocalName().equals(docType)) {
                XmlMappable row = mappable.newInstance(simulateOnly);
                String json = row.setXMLData(xsr);
                MetaInputBean header = (MetaInputBean) row;
                if (!header.getCrossReferences().isEmpty()) {
                    referenceInputBeans.add(new CrossReferenceInputBean(header.getFortress(),
                            header.getCallerRef(), header.getCrossReferences()));
                    rows = rows + header.getCrossReferences().size();
                }
                LogInputBean logInputBean = new LogInputBean("system", new DateTime(header.getWhen()), json);
                header.setLog(logInputBean);
                //logger.info(json);
                xsr.nextTag();
                writeAudit(abExporter, header, mappable.getClass().getCanonicalName());
                rows++;
                if (rows % 500 == 0 && !simulateOnly)
                    logger.info("Processed {} elapsed seconds {}", rows,
                            new DateTime().getMillis() - then / 1000d);

            }
        } finally {
            abExporter.flush(mappable.getClass().getCanonicalName(), mappable.getABType());
        }
        if (!referenceInputBeans.isEmpty()) {
            logger.debug("Wrote [{}] cross references",
                    writeCrossReferences(abExporter, referenceInputBeans, "Cross References"));
        }
        return endProcess(watch, rows);

    } catch (XMLStreamException | JAXBException e1) {
        throw new IOException(e1);
    }
}

From source file:net.landora.video.info.file.FileInfoManager.java

private synchronized Map<String, FileInfo> parseCacheFile(File file) {
    InputStream is = null;//from  w w  w.j  av  a2 s .com
    try {
        is = new BufferedInputStream(new FileInputStream(file));
        if (COMPRESS_INFO_FILE) {
            is = new GZIPInputStream(is);
        }

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

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

        Map<String, FileInfo> files = new HashMap<String, FileInfo>();

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

            FileInfo info = new FileInfo();
            String filename = reader.getAttributeValue(null, "filename");
            info.setFilename(filename);
            info.setE2dkHash(reader.getAttributeValue(null, "ed2k"));
            info.setFileSize(Long.parseLong(reader.getAttributeValue(null, "length")));
            info.setLastModified(Long.parseLong(reader.getAttributeValue(null, "lastmodified")));
            info.setMetadataSource(reader.getAttributeValue(null, "metadatasource"));
            info.setMetadataId(reader.getAttributeValue(null, "metadataid"));
            info.setVideoId(reader.getAttributeValue(null, "videoid"));

            files.put(filename, info);

            XMLUtilities.ignoreTag(reader);

        }
        reader.close();

        return files;

    } catch (Exception e) {
        log.error("Error parsing file cache.", e);
        return new HashMap<String, FileInfo>();
    } finally {
        if (is != null) {
            IOUtils.closeQuietly(is);
        }
    }
}

From source file:net.landora.animeinfo.mylistreader.ListReader.java

public boolean download(InputStream input) throws Throwable {
    TarInputStream is = null;/* www  .j  a va2s .c  o m*/
    try {
        is = new TarInputStream(new GZIPInputStream(input));

        TarEntry entry;
        while ((entry = is.getNextEntry()) != null) {
            if (!entry.getName().equalsIgnoreCase("mylist.xml")) {
                continue;
            }

            XMLStreamReader reader = XMLInputFactory.newFactory().createXMLStreamReader(is);
            reader.nextTag();
            reader.require(XMLStreamReader.START_ELEMENT, null, "my_anime_list");
            values = new HashMap<String, String>();
            StringBuilder value = new StringBuilder();

            while (reader.nextTag() != XMLStreamReader.END_ELEMENT) {
                reader.require(XMLStreamReader.START_ELEMENT, null, null);
                String tableName = reader.getLocalName();

                values.clear();

                while (reader.nextTag() != XMLStreamReader.END_ELEMENT) {
                    String valueName = reader.getLocalName();

                    value.setLength(0);
                    while (reader.next() != XMLStreamReader.END_ELEMENT) {
                        switch (reader.getEventType()) {
                        case XMLStreamReader.CDATA:
                        case XMLStreamReader.CHARACTERS:
                        case XMLStreamReader.SPACE:
                            value.append(reader.getText());
                        }
                    }
                    reader.require(XMLStreamReader.END_ELEMENT, null, valueName);
                    values.put(valueName, value.toString());

                }
                reader.require(XMLStreamReader.END_ELEMENT, null, tableName);

                handleTable(tableName);

            }
            reader.require(XMLStreamReader.END_ELEMENT, null, "my_anime_list");

            saveLast();
        }
        return true;
    } finally {
        if (is != null) {
            IOUtils.closeQuietly(is);
        } else if (input != null) {
            IOUtils.closeQuietly(input);
        }
    }
}

From source file:net.landora.anidb.mylist.ListReader.java

public boolean download(InputStream input) throws Throwable {
    TarInputStream is = null;/*from www . j a  v a2  s. c o  m*/
    try {
        is = new TarInputStream(new GZIPInputStream(input));

        TarEntry entry;
        while ((entry = is.getNextEntry()) != null) {
            if (!entry.getName().equalsIgnoreCase("mylist.xml")) {
                continue;
            }

            XMLStreamReader reader = XMLInputFactory.newFactory().createXMLStreamReader(is);
            reader.nextTag();
            reader.require(XMLStreamReader.START_ELEMENT, null, "my_anime_list");
            values = new HashMap<>();
            StringBuilder value = new StringBuilder();

            while (reader.nextTag() != XMLStreamReader.END_ELEMENT) {
                reader.require(XMLStreamReader.START_ELEMENT, null, null);
                String tableName = reader.getLocalName();

                values.clear();

                while (reader.nextTag() != XMLStreamReader.END_ELEMENT) {
                    String valueName = reader.getLocalName();

                    value.setLength(0);
                    while (reader.next() != XMLStreamReader.END_ELEMENT) {
                        switch (reader.getEventType()) {
                        case XMLStreamReader.CDATA:
                        case XMLStreamReader.CHARACTERS:
                        case XMLStreamReader.SPACE:
                            value.append(reader.getText());
                        }
                    }
                    reader.require(XMLStreamReader.END_ELEMENT, null, valueName);
                    values.put(valueName, value.toString());

                }
                reader.require(XMLStreamReader.END_ELEMENT, null, tableName);

                handleTable(tableName);

            }
            reader.require(XMLStreamReader.END_ELEMENT, null, "my_anime_list");

            saveLast();
        }
        return true;
    } finally {
        if (is != null) {
            IOUtils.closeQuietly(is);
        } else if (input != null) {
            IOUtils.closeQuietly(input);
        }
    }
}