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;
}
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();
}
}));
}
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);
}
}
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);
}
}
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;
}
Aggregations