get rid of xul xml parser

This commit is contained in:
Christian Schabesberger 2017-03-01 16:40:04 +01:00
parent 7b6dae20be
commit 3404231457
1 changed files with 31 additions and 51 deletions

View File

@ -1,17 +1,21 @@
package org.schabi.newpipe.extractor; package org.schabi.newpipe.extractor;
import android.util.Xml;
import org.schabi.newpipe.extractor.exceptions.ParsingException; import org.schabi.newpipe.extractor.exceptions.ParsingException;
import org.schabi.newpipe.extractor.exceptions.ReCaptchaException; import org.schabi.newpipe.extractor.exceptions.ReCaptchaException;
import org.schabi.newpipe.extractor.stream_info.AudioStream; import org.schabi.newpipe.extractor.stream_info.AudioStream;
import org.xmlpull.v1.XmlPullParser; import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.StringReader; import java.io.InputStream;
import java.util.List; import java.util.List;
import java.util.Vector; import java.util.Vector;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
/** /**
* Created by Christian Schabesberger on 02.02.16. * Created by Christian Schabesberger on 02.02.16.
* *
@ -55,57 +59,33 @@ public class DashMpdParser {
throw new ReCaptchaException("reCaptcha Challenge needed"); throw new ReCaptchaException("reCaptcha Challenge needed");
} }
Vector<AudioStream> audioStreams = new Vector<>(); Vector<AudioStream> audioStreams = new Vector<>();
try { try {
XmlPullParser parser = Xml.newPullParser(); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
parser.setInput(new StringReader(dashDoc)); DocumentBuilder builder = factory.newDocumentBuilder();
String tagName = ""; InputStream stream = new ByteArrayInputStream(dashDoc.getBytes());
String currentMimeType = "";
int currentBandwidth = -1;
int currentSamplingRate = -1;
boolean currentTagIsBaseUrl = false;
for(int eventType = parser.getEventType();
eventType != XmlPullParser.END_DOCUMENT;
eventType = parser.next() ) {
switch(eventType) {
case XmlPullParser.START_TAG:
tagName = parser.getName();
if(tagName.equals("AdaptationSet")) {
currentMimeType = parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "mimeType");
} else if(tagName.equals("Representation") && currentMimeType.contains("audio")) {
currentBandwidth = Integer.parseInt(
parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "bandwidth"));
currentSamplingRate = Integer.parseInt(
parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "audioSamplingRate"));
} else if(tagName.equals("BaseURL")) {
currentTagIsBaseUrl = true;
}
break;
case XmlPullParser.TEXT: Document doc = builder.parse(stream);
// actual stream tag NodeList adaptationSetList = doc.getElementsByTagName("AdaptationSet");
if(currentTagIsBaseUrl && for(int i = 0; i < adaptationSetList.getLength(); i++) {
(currentMimeType.contains("audio"))) { Element adaptationSet = (Element) adaptationSetList.item(i);
int format = -1; String memeType = adaptationSet.getAttribute("mimeType");
if(currentMimeType.equals(MediaFormat.WEBMA.mimeType)) { if(memeType.contains("audio")) {
format = MediaFormat.WEBMA.id; Element representation = (Element) adaptationSet.getElementsByTagName("Representation").item(0);
} else if(currentMimeType.equals(MediaFormat.M4A.mimeType)) { String url = representation.getElementsByTagName("BaseURL").item(0).getTextContent();
format = MediaFormat.M4A.id; int bandwidth = Integer.parseInt(representation.getAttribute("bandwidth"));
} int samplingRate = Integer.parseInt(representation.getAttribute("audioSamplingRate"));
audioStreams.add(new AudioStream(parser.getText(), int format = -1;
format, currentBandwidth, currentSamplingRate)); if(memeType.equals(MediaFormat.WEBMA.mimeType)) {
} format = MediaFormat.WEBMA.id;
break; } else if(memeType.equals(MediaFormat.M4A.mimeType)) {
format = MediaFormat.M4A.id;
case XmlPullParser.END_TAG: }
if(tagName.equals("AdaptationSet")) { audioStreams.add(new AudioStream(url, format, bandwidth, samplingRate));
currentMimeType = "";
} else if(tagName.equals("BaseURL")) {
currentTagIsBaseUrl = false;
}
break;
} }
} }
} catch(Exception e) { }
catch(Exception e) {
throw new DashMpdParsingException("Could not parse Dash mpd", e); throw new DashMpdParsingException("Could not parse Dash mpd", e);
} }
return audioStreams; return audioStreams;