Search in sources :

Example 81 with XmlPullParserException

use of org.xmlpull.v1.XmlPullParserException in project j2objc by google.

the class DocumentBuilderImpl method parse.

@Override
public Document parse(InputSource source) throws SAXException, IOException {
    if (source == null) {
        throw new IllegalArgumentException("source == null");
    }
    String namespaceURI = null;
    String qualifiedName = null;
    DocumentType doctype = null;
    String inputEncoding = source.getEncoding();
    String systemId = source.getSystemId();
    DocumentImpl document = new DocumentImpl(dom, namespaceURI, qualifiedName, doctype, inputEncoding);
    document.setDocumentURI(systemId);
    KXmlParser parser = new KXmlParser();
    try {
        parser.keepNamespaceAttributes();
        parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, namespaceAware);
        if (source.getByteStream() != null) {
            parser.setInput(source.getByteStream(), inputEncoding);
        } else if (source.getCharacterStream() != null) {
            parser.setInput(source.getCharacterStream());
        } else if (systemId != null) {
            URL url = new URL(systemId);
            URLConnection urlConnection = url.openConnection();
            urlConnection.connect();
            // TODO: if null, extract the inputEncoding from the Content-Type header?
            parser.setInput(urlConnection.getInputStream(), inputEncoding);
        } else {
            throw new SAXParseException("InputSource needs a stream, reader or URI", null);
        }
        if (parser.nextToken() == XmlPullParser.END_DOCUMENT) {
            throw new SAXParseException("Unexpected end of document", null);
        }
        parse(parser, document, document, XmlPullParser.END_DOCUMENT);
        parser.require(XmlPullParser.END_DOCUMENT, null, null);
    } catch (XmlPullParserException ex) {
        if (ex.getDetail() instanceof IOException) {
            throw (IOException) ex.getDetail();
        }
        if (ex.getDetail() instanceof RuntimeException) {
            throw (RuntimeException) ex.getDetail();
        }
        LocatorImpl locator = new LocatorImpl();
        locator.setPublicId(source.getPublicId());
        locator.setSystemId(systemId);
        locator.setLineNumber(ex.getLineNumber());
        locator.setColumnNumber(ex.getColumnNumber());
        SAXParseException newEx = new SAXParseException(ex.getMessage(), locator);
        if (errorHandler != null) {
            errorHandler.error(newEx);
        }
        throw newEx;
    } finally {
        IoUtils.closeQuietly(parser);
    }
    return document;
}
Also used : KXmlParser(org.kxml2.io.KXmlParser) SAXParseException(org.xml.sax.SAXParseException) DocumentType(org.w3c.dom.DocumentType) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) LocatorImpl(org.xml.sax.helpers.LocatorImpl) IOException(java.io.IOException) DocumentImpl(org.apache.harmony.xml.dom.DocumentImpl) URL(java.net.URL) URLConnection(java.net.URLConnection)

Example 82 with XmlPullParserException

use of org.xmlpull.v1.XmlPullParserException in project AndroidSDK-RecipeBook by gabu.

the class Recipe078 method onCreate.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    // 名古屋の明日の天気情報を取得できるURLです。
    String uri = "http://weather.livedoor.com/forecast/" + "webservice/rest/v1?city=38&day=tomorrow";
    // HTTPクライアントを作って
    HttpClient client = new DefaultHttpClient();
    // GETオブジェクトを作って
    HttpGet get = new HttpGet();
    try {
        // URIをセット
        get.setURI(new URI(uri));
        // GETリクエストを実行してレスポンスを取得
        HttpResponse res = client.execute(get);
        // レスポンスからInputStreamを取得
        InputStream in = res.getEntity().getContent();
        // XMLプルパーサを生成して
        XmlPullParser parser = Xml.newPullParser();
        // InputStreamをセット
        parser.setInput(in, "UTF-8");
        int eventType = parser.getEventType();
        // イベントタイプがEND_DOCUMENTになるまでループ
        // けど、以下の必要な値が全て取得できたらbreakする。
        String title = "";
        String telop = "";
        String description = "";
        while (eventType != XmlPullParser.END_DOCUMENT) {
            switch(eventType) {
                case XmlPullParser.START_TAG:
                    String tag = parser.getName();
                    if ("title".equals(tag)) {
                        // タイトルを取得
                        title = parser.nextText();
                    } else if ("telop".equals(tag)) {
                        // 天気を取得
                        telop = parser.nextText();
                    } else if ("description".equals(tag)) {
                        // 天気概況文を取得
                        description = parser.nextText();
                    }
                    break;
            }
            if (!("".equals(title) || "".equals(telop) || "".equals(description))) {
                // 必要な値が取得できたらループを抜ける
                break;
            }
            // パーサを次のイベントまで進める
            eventType = parser.next();
        }
        // TextViewに表示!
        TextView v = (TextView) findViewById(R.id.text);
        v.setText(title + "\n\n" + telop + "\n\n" + description);
    } catch (URISyntaxException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (XmlPullParserException e) {
        e.printStackTrace();
    }
}
Also used : InputStream(java.io.InputStream) HttpGet(org.apache.http.client.methods.HttpGet) XmlPullParser(org.xmlpull.v1.XmlPullParser) HttpResponse(org.apache.http.HttpResponse) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) URI(java.net.URI) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) ClientProtocolException(org.apache.http.client.ClientProtocolException) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) TextView(android.widget.TextView) XmlPullParserException(org.xmlpull.v1.XmlPullParserException)

Example 83 with XmlPullParserException

use of org.xmlpull.v1.XmlPullParserException in project ExoPlayer by google.

the class DashManifestParser method parseMediaPresentationDescription.

protected DashManifest parseMediaPresentationDescription(XmlPullParser xpp, String baseUrl) throws XmlPullParserException, IOException {
    long availabilityStartTime = parseDateTime(xpp, "availabilityStartTime", C.TIME_UNSET);
    long durationMs = parseDuration(xpp, "mediaPresentationDuration", C.TIME_UNSET);
    long minBufferTimeMs = parseDuration(xpp, "minBufferTime", C.TIME_UNSET);
    String typeString = xpp.getAttributeValue(null, "type");
    boolean dynamic = typeString != null && typeString.equals("dynamic");
    long minUpdateTimeMs = dynamic ? parseDuration(xpp, "minimumUpdatePeriod", C.TIME_UNSET) : C.TIME_UNSET;
    long timeShiftBufferDepthMs = dynamic ? parseDuration(xpp, "timeShiftBufferDepth", C.TIME_UNSET) : C.TIME_UNSET;
    long suggestedPresentationDelayMs = dynamic ? parseDuration(xpp, "suggestedPresentationDelay", C.TIME_UNSET) : C.TIME_UNSET;
    UtcTimingElement utcTiming = null;
    Uri location = null;
    List<Period> periods = new ArrayList<>();
    long nextPeriodStartMs = dynamic ? C.TIME_UNSET : 0;
    boolean seenEarlyAccessPeriod = false;
    boolean seenFirstBaseUrl = false;
    do {
        xpp.next();
        if (XmlPullParserUtil.isStartTag(xpp, "BaseURL")) {
            if (!seenFirstBaseUrl) {
                baseUrl = parseBaseUrl(xpp, baseUrl);
                seenFirstBaseUrl = true;
            }
        } else if (XmlPullParserUtil.isStartTag(xpp, "UTCTiming")) {
            utcTiming = parseUtcTiming(xpp);
        } else if (XmlPullParserUtil.isStartTag(xpp, "Location")) {
            location = Uri.parse(xpp.nextText());
        } else if (XmlPullParserUtil.isStartTag(xpp, "Period") && !seenEarlyAccessPeriod) {
            Pair<Period, Long> periodWithDurationMs = parsePeriod(xpp, baseUrl, nextPeriodStartMs);
            Period period = periodWithDurationMs.first;
            if (period.startMs == C.TIME_UNSET) {
                if (dynamic) {
                    // This is an early access period. Ignore it. All subsequent periods must also be
                    // early access.
                    seenEarlyAccessPeriod = true;
                } else {
                    throw new ParserException("Unable to determine start of period " + periods.size());
                }
            } else {
                long periodDurationMs = periodWithDurationMs.second;
                nextPeriodStartMs = periodDurationMs == C.TIME_UNSET ? C.TIME_UNSET : (period.startMs + periodDurationMs);
                periods.add(period);
            }
        }
    } while (!XmlPullParserUtil.isEndTag(xpp, "MPD"));
    if (durationMs == C.TIME_UNSET) {
        if (nextPeriodStartMs != C.TIME_UNSET) {
            // If we know the end time of the final period, we can use it as the duration.
            durationMs = nextPeriodStartMs;
        } else if (!dynamic) {
            throw new ParserException("Unable to determine duration of static manifest.");
        }
    }
    if (periods.isEmpty()) {
        throw new ParserException("No periods found.");
    }
    return buildMediaPresentationDescription(availabilityStartTime, durationMs, minBufferTimeMs, dynamic, minUpdateTimeMs, timeShiftBufferDepthMs, suggestedPresentationDelayMs, utcTiming, location, periods);
}
Also used : ParserException(com.google.android.exoplayer2.ParserException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) ArrayList(java.util.ArrayList) Uri(android.net.Uri) Pair(android.util.Pair)

Example 84 with XmlPullParserException

use of org.xmlpull.v1.XmlPullParserException in project ExoPlayer by google.

the class SsManifestParser method parse.

@Override
public SsManifest parse(Uri uri, InputStream inputStream) throws IOException {
    try {
        XmlPullParser xmlParser = xmlParserFactory.newPullParser();
        xmlParser.setInput(inputStream, null);
        SmoothStreamingMediaParser smoothStreamingMediaParser = new SmoothStreamingMediaParser(null, uri.toString());
        return (SsManifest) smoothStreamingMediaParser.parse(xmlParser);
    } catch (XmlPullParserException e) {
        throw new ParserException(e);
    }
}
Also used : ParserException(com.google.android.exoplayer2.ParserException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) XmlPullParser(org.xmlpull.v1.XmlPullParser) XmlPullParserException(org.xmlpull.v1.XmlPullParserException)

Example 85 with XmlPullParserException

use of org.xmlpull.v1.XmlPullParserException in project ExoPlayer by google.

the class TtmlDecoder method decode.

@Override
protected TtmlSubtitle decode(byte[] bytes, int length) throws SubtitleDecoderException {
    try {
        XmlPullParser xmlParser = xmlParserFactory.newPullParser();
        Map<String, TtmlStyle> globalStyles = new HashMap<>();
        Map<String, TtmlRegion> regionMap = new HashMap<>();
        regionMap.put(TtmlNode.ANONYMOUS_REGION_ID, new TtmlRegion());
        ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes, 0, length);
        xmlParser.setInput(inputStream, null);
        TtmlSubtitle ttmlSubtitle = null;
        LinkedList<TtmlNode> nodeStack = new LinkedList<>();
        int unsupportedNodeDepth = 0;
        int eventType = xmlParser.getEventType();
        FrameAndTickRate frameAndTickRate = DEFAULT_FRAME_AND_TICK_RATE;
        while (eventType != XmlPullParser.END_DOCUMENT) {
            TtmlNode parent = nodeStack.peekLast();
            if (unsupportedNodeDepth == 0) {
                String name = xmlParser.getName();
                if (eventType == XmlPullParser.START_TAG) {
                    if (TtmlNode.TAG_TT.equals(name)) {
                        frameAndTickRate = parseFrameAndTickRates(xmlParser);
                    }
                    if (!isSupportedTag(name)) {
                        Log.i(TAG, "Ignoring unsupported tag: " + xmlParser.getName());
                        unsupportedNodeDepth++;
                    } else if (TtmlNode.TAG_HEAD.equals(name)) {
                        parseHeader(xmlParser, globalStyles, regionMap);
                    } else {
                        try {
                            TtmlNode node = parseNode(xmlParser, parent, regionMap, frameAndTickRate);
                            nodeStack.addLast(node);
                            if (parent != null) {
                                parent.addChild(node);
                            }
                        } catch (SubtitleDecoderException e) {
                            Log.w(TAG, "Suppressing parser error", e);
                            // Treat the node (and by extension, all of its children) as unsupported.
                            unsupportedNodeDepth++;
                        }
                    }
                } else if (eventType == XmlPullParser.TEXT) {
                    parent.addChild(TtmlNode.buildTextNode(xmlParser.getText()));
                } else if (eventType == XmlPullParser.END_TAG) {
                    if (xmlParser.getName().equals(TtmlNode.TAG_TT)) {
                        ttmlSubtitle = new TtmlSubtitle(nodeStack.getLast(), globalStyles, regionMap);
                    }
                    nodeStack.removeLast();
                }
            } else {
                if (eventType == XmlPullParser.START_TAG) {
                    unsupportedNodeDepth++;
                } else if (eventType == XmlPullParser.END_TAG) {
                    unsupportedNodeDepth--;
                }
            }
            xmlParser.next();
            eventType = xmlParser.getEventType();
        }
        return ttmlSubtitle;
    } catch (XmlPullParserException xppe) {
        throw new SubtitleDecoderException("Unable to decode source", xppe);
    } catch (IOException e) {
        throw new IllegalStateException("Unexpected error when reading input.", e);
    }
}
Also used : HashMap(java.util.HashMap) XmlPullParser(org.xmlpull.v1.XmlPullParser) IOException(java.io.IOException) LinkedList(java.util.LinkedList) SubtitleDecoderException(com.google.android.exoplayer2.text.SubtitleDecoderException) ByteArrayInputStream(java.io.ByteArrayInputStream) XmlPullParserException(org.xmlpull.v1.XmlPullParserException)

Aggregations

XmlPullParserException (org.xmlpull.v1.XmlPullParserException)1071 IOException (java.io.IOException)630 XmlPullParser (org.xmlpull.v1.XmlPullParser)440 FileNotFoundException (java.io.FileNotFoundException)187 XmlResourceParser (android.content.res.XmlResourceParser)186 FileInputStream (java.io.FileInputStream)182 AttributeSet (android.util.AttributeSet)159 TypedArray (android.content.res.TypedArray)156 Resources (android.content.res.Resources)101 File (java.io.File)101 ArrayList (java.util.ArrayList)99 PackageManager (android.content.pm.PackageManager)62 HashMap (java.util.HashMap)58 ComponentName (android.content.ComponentName)57 InputStream (java.io.InputStream)57 Intent (android.content.Intent)54 XmlSerializer (org.xmlpull.v1.XmlSerializer)54 AtomicFile (android.util.AtomicFile)50 BufferedInputStream (java.io.BufferedInputStream)44 TypedValue (android.util.TypedValue)43