Search in sources :

Example 11 with XMLStreamReader

use of javax.xml.stream.XMLStreamReader in project libresonic by Libresonic.

the class ITunesParser method parseTracks.

private Map<String, File> parseTracks(List<ITunesPlaylist> playlists) throws Exception {
    Map<String, File> result = new HashMap<String, File>();
    SortedSet<String> trackIds = new TreeSet<String>();
    for (ITunesPlaylist playlist : playlists) {
        trackIds.addAll(playlist.trackIds);
    }
    InputStream in = new FileInputStream(iTunesXml);
    try {
        XMLStreamReader streamReader = inputFactory.createXMLStreamReader(in);
        String trackId = null;
        while (streamReader.hasNext()) {
            int code = streamReader.next();
            if (code == XMLStreamReader.START_ELEMENT) {
                String key = readKey(streamReader);
                if ("Track ID".equals(key)) {
                    trackId = readNextTag(streamReader);
                } else if (trackId != null && trackIds.contains(trackId) && "Location".equals(key)) {
                    String location = readNextTag(streamReader);
                    File file = new File(StringUtil.urlDecode(new URL(location).getFile()));
                    result.put(trackId, file);
                }
            }
        }
    } finally {
        IOUtils.closeQuietly(in);
    }
    return result;
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) File(java.io.File) FileInputStream(java.io.FileInputStream) URL(java.net.URL)

Example 12 with XMLStreamReader

use of javax.xml.stream.XMLStreamReader in project libresonic by Libresonic.

the class ITunesParser method parsePlaylists.

private List<ITunesPlaylist> parsePlaylists() throws Exception {
    List<ITunesPlaylist> playlists = new ArrayList<ITunesPlaylist>();
    InputStream in = new FileInputStream(iTunesXml);
    try {
        ITunesPlaylist playlist = null;
        XMLStreamReader streamReader = inputFactory.createXMLStreamReader(in);
        while (streamReader.hasNext()) {
            int code = streamReader.next();
            if (code == XMLStreamReader.START_ELEMENT) {
                String key = readKey(streamReader);
                if ("Playlist ID".equals(key)) {
                    playlist = new ITunesPlaylist(readNextTag(streamReader));
                    playlists.add(playlist);
                }
                if (playlist != null) {
                    if ("Name".equals(key)) {
                        playlist.name = readNextTag(streamReader);
                    } else if ("Smart Info".equals(key)) {
                        playlist.smart = true;
                    } else if ("Visible".equals(key)) {
                        playlist.visible = false;
                    } else if ("Distinguished Kind".equals(key)) {
                        playlist.distinguishedKind = readNextTag(streamReader);
                    } else if ("Track ID".equals(key)) {
                        playlist.trackIds.add(readNextTag(streamReader));
                    }
                }
            }
        }
    } finally {
        IOUtils.closeQuietly(in);
    }
    return Lists.newArrayList(Iterables.filter(playlists, new Predicate<ITunesPlaylist>() {

        @Override
        public boolean apply(ITunesPlaylist input) {
            return input.isIncluded();
        }
    }));
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileInputStream(java.io.FileInputStream) Predicate(com.google.common.base.Predicate)

Example 13 with XMLStreamReader

use of javax.xml.stream.XMLStreamReader in project lucene-solr by apache.

the class XPathRecordReader method streamRecords.

/** 
   * Creates an XML stream reader on top of whatever reader has been
   * configured. Then calls parse() with a handler which is
   * invoked forEach record emitted.
   *
   * @param r the stream reader
   * @param handler The callback instance
   */
public void streamRecords(Reader r, Handler handler) {
    try {
        XMLStreamReader parser = factory.createXMLStreamReader(r);
        rootNode.parse(parser, handler, new HashMap<>(), new Stack<>(), false);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) IOException(java.io.IOException) XMLStreamException(javax.xml.stream.XMLStreamException)

Example 14 with XMLStreamReader

use of javax.xml.stream.XMLStreamReader in project lucene-solr by apache.

the class DocumentAnalysisRequestHandler method resolveAnalysisRequest.

//================================================ Helper Methods ==================================================
/**
   * Resolves the {@link DocumentAnalysisRequest} from the given solr request.
   *
   * @param req The solr request.
   *
   * @return The resolved document analysis request.
   *
   * @throws IOException        Thrown when reading/parsing the content stream of the request fails.
   * @throws XMLStreamException Thrown when reading/parsing the content stream of the request fails.
   */
DocumentAnalysisRequest resolveAnalysisRequest(SolrQueryRequest req) throws IOException, XMLStreamException {
    DocumentAnalysisRequest request = new DocumentAnalysisRequest();
    SolrParams params = req.getParams();
    String query = params.get(AnalysisParams.QUERY, params.get(CommonParams.Q, null));
    request.setQuery(query);
    boolean showMatch = params.getBool(AnalysisParams.SHOW_MATCH, false);
    request.setShowMatch(showMatch);
    ContentStream stream = extractSingleContentStream(req);
    InputStream is = null;
    XMLStreamReader parser = null;
    try {
        is = stream.getStream();
        final String charset = ContentStreamBase.getCharsetFromContentType(stream.getContentType());
        parser = (charset == null) ? inputFactory.createXMLStreamReader(is) : inputFactory.createXMLStreamReader(is, charset);
        while (true) {
            int event = parser.next();
            switch(event) {
                case XMLStreamConstants.END_DOCUMENT:
                    {
                        parser.close();
                        return request;
                    }
                case XMLStreamConstants.START_ELEMENT:
                    {
                        String currTag = parser.getLocalName();
                        if ("doc".equals(currTag)) {
                            log.trace("Reading doc...");
                            SolrInputDocument document = readDocument(parser, req.getSchema());
                            request.addDocument(document);
                        }
                        break;
                    }
            }
        }
    } finally {
        if (parser != null)
            parser.close();
        IOUtils.closeQuietly(is);
    }
}
Also used : ContentStream(org.apache.solr.common.util.ContentStream) SolrInputDocument(org.apache.solr.common.SolrInputDocument) XMLStreamReader(javax.xml.stream.XMLStreamReader) InputStream(java.io.InputStream) SolrParams(org.apache.solr.common.params.SolrParams) DocumentAnalysisRequest(org.apache.solr.client.solrj.request.DocumentAnalysisRequest)

Example 15 with XMLStreamReader

use of javax.xml.stream.XMLStreamReader in project jackrabbit-oak by apache.

the class WikipediaImport method importWikipedia.

public int importWikipedia(Session session) throws Exception {
    long start = System.currentTimeMillis();
    int count = 0;
    int code = 0;
    if (doReport) {
        System.out.format("Importing %s...%n", dump);
    }
    String type = "nt:unstructured";
    if (session.getWorkspace().getNodeTypeManager().hasNodeType("oak:Unstructured")) {
        type = "oak:Unstructured";
    }
    Node wikipedia = session.getRootNode().addNode("wikipedia", type);
    int levels = 0;
    if (!flat) {
        // estimate that the average XML size of a page is about 1kB
        for (long pages = dump.length() / 1024; pages > 256; pages /= 256) {
            levels++;
        }
    }
    String title = null;
    String text = null;
    XMLInputFactory factory = XMLInputFactory.newInstance();
    StreamSource source;
    if (dump.getName().endsWith(".xml")) {
        source = new StreamSource(dump);
    } else {
        CompressorStreamFactory csf = new CompressorStreamFactory();
        source = new StreamSource(csf.createCompressorInputStream(new BufferedInputStream(new FileInputStream(dump))));
    }
    haltImport = false;
    XMLStreamReader reader = factory.createXMLStreamReader(source);
    while (reader.hasNext() && !haltImport) {
        switch(reader.next()) {
            case XMLStreamConstants.START_ELEMENT:
                if ("title".equals(reader.getLocalName())) {
                    title = reader.getElementText();
                } else if ("text".equals(reader.getLocalName())) {
                    text = reader.getElementText();
                }
                break;
            case XMLStreamConstants.END_ELEMENT:
                if ("page".equals(reader.getLocalName())) {
                    String name = Text.escapeIllegalJcrChars(title);
                    Node parent = wikipedia;
                    if (levels > 0) {
                        int n = name.length();
                        for (int i = 0; i < levels; i++) {
                            int hash = name.substring(min(i, n)).hashCode();
                            parent = JcrUtils.getOrAddNode(parent, String.format("%02x", hash & 0xff));
                        }
                    }
                    Node page = parent.addNode(name);
                    page.setProperty("title", title);
                    page.setProperty("text", text);
                    code += title.hashCode();
                    code += text.hashCode();
                    count++;
                    if (count % 1000 == 0) {
                        batchDone(session, start, count);
                    }
                    pageAdded(title, text);
                }
                break;
        }
    }
    session.save();
    if (doReport) {
        long millis = System.currentTimeMillis() - start;
        System.out.format("Imported %d pages in %d seconds (%.2fms/page)%n", count, millis / 1000, (double) millis / count);
    }
    return code;
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) BufferedInputStream(java.io.BufferedInputStream) Node(javax.jcr.Node) StreamSource(javax.xml.transform.stream.StreamSource) CompressorStreamFactory(org.apache.commons.compress.compressors.CompressorStreamFactory) XMLInputFactory(javax.xml.stream.XMLInputFactory) FileInputStream(java.io.FileInputStream)

Aggregations

XMLStreamReader (javax.xml.stream.XMLStreamReader)1074 Test (org.junit.Test)486 InputStream (java.io.InputStream)451 ByteArrayInputStream (java.io.ByteArrayInputStream)379 ByteArrayOutputStream (java.io.ByteArrayOutputStream)334 Document (org.w3c.dom.Document)311 XMLStreamException (javax.xml.stream.XMLStreamException)288 ArrayList (java.util.ArrayList)270 XMLSecurityProperties (org.apache.xml.security.stax.ext.XMLSecurityProperties)242 XMLInputFactory (javax.xml.stream.XMLInputFactory)211 QName (javax.xml.namespace.QName)208 DOMSource (javax.xml.transform.dom.DOMSource)206 StringReader (java.io.StringReader)196 SecretKey (javax.crypto.SecretKey)188 StreamResult (javax.xml.transform.stream.StreamResult)183 DocumentBuilder (javax.xml.parsers.DocumentBuilder)178 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)160 InboundXMLSec (org.apache.xml.security.stax.ext.InboundXMLSec)155 IOException (java.io.IOException)144 Key (java.security.Key)103