Example usage for android.util Xml findEncodingByName

List of usage examples for android.util Xml findEncodingByName

Introduction

In this page you can find the example usage for android.util Xml findEncodingByName.

Prototype

public static Encoding findEncodingByName(String encodingName) throws UnsupportedEncodingException 

Source Link

Document

Finds an encoding by name.

Usage

From source file:org.m2x.rssreader.service.FetcherService.java

private int refreshFeed(String feedId) {
    RssAtomParser handler = null;//from w  w  w.ja  va 2 s  . c  o m

    ContentResolver cr = getContentResolver();
    Cursor cursor = cr.query(FeedColumns.CONTENT_URI(feedId), null, null, null, null);

    if (!cursor.moveToFirst()) {
        cursor.close();
        return 0;
    }

    int urlPosition = cursor.getColumnIndex(FeedColumns.URL);
    int titlePosition = cursor.getColumnIndex(FeedColumns.NAME);
    int fetchmodePosition = cursor.getColumnIndex(FeedColumns.FETCH_MODE);
    int realLastUpdatePosition = cursor.getColumnIndex(FeedColumns.REAL_LAST_UPDATE);
    int iconPosition = cursor.getColumnIndex(FeedColumns.ICON);
    int retrieveFullTextPosition = cursor.getColumnIndex(FeedColumns.RETRIEVE_FULLTEXT);

    HttpURLConnection connection = null;
    try {
        String feedUrl = cursor.getString(urlPosition);
        connection = NetworkUtils.setupConnection(feedUrl);
        String contentType = connection.getContentType();
        int fetchMode = cursor.getInt(fetchmodePosition);

        handler = new RssAtomParser(new Date(cursor.getLong(realLastUpdatePosition)), feedId,
                cursor.getString(titlePosition), feedUrl, cursor.getInt(retrieveFullTextPosition) == 1);
        handler.setFetchImages(PrefUtils.getBoolean(PrefUtils.FETCH_PICTURES, true));

        if (fetchMode == 0) {
            if (contentType != null && contentType.startsWith(CONTENT_TYPE_TEXT_HTML)) {
                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(NetworkUtils.getConnectionInputStream(connection)));

                String line;
                int posStart = -1;

                while ((line = reader.readLine()) != null) {
                    if (line.contains(HTML_BODY)) {
                        break;
                    } else {
                        Matcher matcher = FEED_LINK_PATTERN.matcher(line);

                        if (matcher.find()) { // not "while" as only one link is needed
                            line = matcher.group();
                            posStart = line.indexOf(HREF);

                            if (posStart > -1) {
                                String url = line.substring(posStart + 6, line.indexOf('"', posStart + 10))
                                        .replace("&amp", "&");

                                ContentValues values = new ContentValues();

                                if (url.startsWith("/")) {
                                    int index = feedUrl.indexOf('/', 8);

                                    if (index > -1) {
                                        url = feedUrl.substring(0, index) + url;
                                    } else {
                                        url = feedUrl + url;
                                    }
                                } else if (!url.startsWith(Constants.HTTP_SCHEME)
                                        && !url.startsWith(Constants.HTTPS_SCHEME)) {
                                    url = feedUrl + '/' + url;
                                }
                                values.put(FeedColumns.URL, url);
                                cr.update(FeedColumns.CONTENT_URI(feedId), values, null, null);
                                connection.disconnect();
                                connection = NetworkUtils.setupConnection(url);
                                contentType = connection.getContentType();
                                break;
                            }
                        }
                    }
                }
                // this indicates a badly configured feed
                if (posStart == -1) {
                    connection.disconnect();
                    connection = NetworkUtils.setupConnection(feedUrl);
                    contentType = connection.getContentType();
                }
            }

            if (contentType != null) {
                int index = contentType.indexOf(CHARSET);

                if (index > -1) {
                    int index2 = contentType.indexOf(';', index);

                    try {
                        Xml.findEncodingByName(index2 > -1 ? contentType.substring(index + 8, index2)
                                : contentType.substring(index + 8));
                        fetchMode = FETCHMODE_DIRECT;
                    } catch (UnsupportedEncodingException usee) {
                        fetchMode = FETCHMODE_REENCODE;
                    }
                } else {
                    fetchMode = FETCHMODE_REENCODE;
                }

            } else {
                BufferedReader bufferedReader = new BufferedReader(
                        new InputStreamReader(NetworkUtils.getConnectionInputStream(connection)));

                char[] chars = new char[20];

                int length = bufferedReader.read(chars);

                String xmlDescription = new String(chars, 0, length);

                connection.disconnect();
                connection = NetworkUtils.setupConnection(connection.getURL());

                int start = xmlDescription != null ? xmlDescription.indexOf(ENCODING) : -1;

                if (start > -1) {
                    try {
                        Xml.findEncodingByName(
                                xmlDescription.substring(start + 10, xmlDescription.indexOf('"', start + 11)));
                        fetchMode = FETCHMODE_DIRECT;
                    } catch (UnsupportedEncodingException usee) {
                        fetchMode = FETCHMODE_REENCODE;
                    }
                } else {
                    // absolutely no encoding information found
                    fetchMode = FETCHMODE_DIRECT;
                }
            }

            ContentValues values = new ContentValues();
            values.put(FeedColumns.FETCH_MODE, fetchMode);
            cr.update(FeedColumns.CONTENT_URI(feedId), values, null, null);
        }

        switch (fetchMode) {
        default:
        case FETCHMODE_DIRECT: {
            if (contentType != null) {
                int index = contentType.indexOf(CHARSET);

                int index2 = contentType.indexOf(';', index);

                InputStream inputStream = NetworkUtils.getConnectionInputStream(connection);
                Xml.parse(inputStream,
                        Xml.findEncodingByName(index2 > -1 ? contentType.substring(index + 8, index2)
                                : contentType.substring(index + 8)),
                        handler);
            } else {
                InputStreamReader reader = new InputStreamReader(
                        NetworkUtils.getConnectionInputStream(connection));
                Xml.parse(reader, handler);
            }
            break;
        }
        case FETCHMODE_REENCODE: {
            ByteArrayOutputStream ouputStream = new ByteArrayOutputStream();
            InputStream inputStream = NetworkUtils.getConnectionInputStream(connection);

            byte[] byteBuffer = new byte[4096];

            int n;
            while ((n = inputStream.read(byteBuffer)) > 0) {
                ouputStream.write(byteBuffer, 0, n);
            }

            String xmlText = ouputStream.toString();

            int start = xmlText != null ? xmlText.indexOf(ENCODING) : -1;

            if (start > -1) {
                Xml.parse(new StringReader(new String(ouputStream.toByteArray(),
                        xmlText.substring(start + 10, xmlText.indexOf('"', start + 11)))), handler);
            } else {
                // use content type
                if (contentType != null) {
                    int index = contentType.indexOf(CHARSET);

                    if (index > -1) {
                        int index2 = contentType.indexOf(';', index);

                        try {
                            StringReader reader = new StringReader(new String(ouputStream.toByteArray(),
                                    index2 > -1 ? contentType.substring(index + 8, index2)
                                            : contentType.substring(index + 8)));
                            Xml.parse(reader, handler);
                        } catch (Exception ignored) {
                        }
                    } else {
                        StringReader reader = new StringReader(new String(ouputStream.toByteArray()));
                        Xml.parse(reader, handler);
                    }
                }
            }
            break;
        }
        }

        connection.disconnect();
    } catch (FileNotFoundException e) {
        if (handler == null || (handler != null && !handler.isDone() && !handler.isCancelled())) {
            ContentValues values = new ContentValues();

            // resets the fetchmode to determine it again later
            values.put(FeedColumns.FETCH_MODE, 0);

            values.put(FeedColumns.ERROR, getString(R.string.error_feed_error));
            cr.update(FeedColumns.CONTENT_URI(feedId), values, null, null);
        }
    } catch (Throwable e) {
        if (handler == null || (handler != null && !handler.isDone() && !handler.isCancelled())) {
            ContentValues values = new ContentValues();

            // resets the fetchmode to determine it again later
            values.put(FeedColumns.FETCH_MODE, 0);

            values.put(FeedColumns.ERROR,
                    e.getMessage() != null ? e.getMessage() : getString(R.string.error_feed_process));
            cr.update(FeedColumns.CONTENT_URI(feedId), values, null, null);
        }
    } finally {

        /* check and optionally find favicon */
        try {
            if (handler != null && cursor.getBlob(iconPosition) == null) {
                String feedLink = handler.getFeedLink();
                if (feedLink != null) {
                    NetworkUtils.retrieveFavicon(this, new URL(feedLink), feedId);
                } else {
                    NetworkUtils.retrieveFavicon(this, connection.getURL(), feedId);
                }
            }
        } catch (Throwable ignored) {
        }

        if (connection != null) {
            connection.disconnect();
        }
    }

    cursor.close();

    return handler != null ? handler.getNewCount() : 0;
}

From source file:nl.privacybarometer.privacyvandaag.service.FetcherService.java

private int refreshFeed(String feedId, long keepDateBorderTime) {
    RssAtomParser handler = null;//from w w w .ja v  a 2  s . c  om

    ContentResolver cr = getContentResolver();
    Cursor cursor = cr.query(FeedColumns.CONTENT_URI(feedId), null, null, null, null);

    if (cursor.moveToFirst()) {
        int urlPosition = cursor.getColumnIndex(FeedColumns.URL);
        int idPosition = cursor.getColumnIndex(FeedColumns._ID);
        int titlePosition = cursor.getColumnIndex(FeedColumns.NAME);
        int fetchModePosition = cursor.getColumnIndex(FeedColumns.FETCH_MODE);
        int realLastUpdatePosition = cursor.getColumnIndex(FeedColumns.REAL_LAST_UPDATE);
        int iconPosition = cursor.getColumnIndex(FeedColumns.ICON);
        int retrieveFullscreenPosition = cursor.getColumnIndex(FeedColumns.RETRIEVE_FULLTEXT);

        /* ModPrivacyVandaag: if Fetchmode = 99, do not refresh this feed. */
        int fetchMode = cursor.getInt(fetchModePosition);
        if (fetchMode == FETCHMODE_DO_NOT_FETCH) {
            cursor.close();
            return 0;
        }
        // end of this added block of code; commented out initialize of fetchmode on line 520
        String id = cursor.getString(idPosition);
        HttpURLConnection connection = null;
        try {
            String feedUrl = cursor.getString(urlPosition);
            connection = NetworkUtils.setupConnection(feedUrl);
            String contentType = connection.getContentType();
            handler = new RssAtomParser(new Date(cursor.getLong(realLastUpdatePosition)), keepDateBorderTime,
                    id, cursor.getString(titlePosition), feedUrl,
                    cursor.getInt(retrieveFullscreenPosition) == 1);
            handler.setFetchImages(NetworkUtils.needDownloadPictures());
            // Log.e (TAG,"feedUrl = "+feedUrl);

            if (fetchMode == 0) {
                if (contentType != null && contentType.startsWith(CONTENT_TYPE_TEXT_HTML)) {
                    BufferedReader reader = new BufferedReader(
                            new InputStreamReader(connection.getInputStream()));

                    String line;
                    int posStart = -1;

                    while ((line = reader.readLine()) != null) {
                        if (line.contains(HTML_BODY)) {
                            break;
                        } else {
                            Matcher matcher = FEED_LINK_PATTERN.matcher(line);

                            if (matcher.find()) { // not "while" as only one link is needed
                                line = matcher.group();
                                posStart = line.indexOf(HREF);

                                if (posStart > -1) {
                                    String url = line.substring(posStart + 6, line.indexOf('"', posStart + 10))
                                            .replace(Constants.AMP_SG, Constants.AMP);

                                    ContentValues values = new ContentValues();

                                    if (url.startsWith(Constants.SLASH)) {
                                        int index = feedUrl.indexOf('/', 8);

                                        if (index > -1) {
                                            url = feedUrl.substring(0, index) + url;
                                        } else {
                                            url = feedUrl + url;
                                        }
                                    } else if (!url.startsWith(Constants.HTTP_SCHEME)
                                            && !url.startsWith(Constants.HTTPS_SCHEME)) {
                                        url = feedUrl + '/' + url;
                                    }
                                    values.put(FeedColumns.URL, url);
                                    cr.update(FeedColumns.CONTENT_URI(id), values, null, null);
                                    connection.disconnect();
                                    connection = NetworkUtils.setupConnection(url);
                                    contentType = connection.getContentType();
                                    break;
                                }
                            }
                        }
                    }
                    // this indicates a badly configured feed
                    if (posStart == -1) {
                        connection.disconnect();
                        connection = NetworkUtils.setupConnection(feedUrl);
                        contentType = connection.getContentType();
                    }
                }

                if (contentType != null) {
                    int index = contentType.indexOf(CHARSET);
                    if (index > -1) {
                        int index2 = contentType.indexOf(';', index);

                        try {
                            Xml.findEncodingByName(index2 > -1 ? contentType.substring(index + 8, index2)
                                    : contentType.substring(index + 8));
                            fetchMode = FETCHMODE_DIRECT;
                        } catch (UnsupportedEncodingException ignored) {
                            fetchMode = FETCHMODE_REENCODE;
                        }
                    } else {
                        fetchMode = FETCHMODE_REENCODE;
                    }

                } else {
                    BufferedReader bufferedReader = new BufferedReader(
                            new InputStreamReader(connection.getInputStream()));

                    char[] chars = new char[20];

                    int length = bufferedReader.read(chars);

                    String xmlDescription = new String(chars, 0, length);

                    connection.disconnect();
                    connection = NetworkUtils.setupConnection(connection.getURL());

                    int start = xmlDescription.indexOf(ENCODING);

                    if (start > -1) {
                        try {
                            Xml.findEncodingByName(xmlDescription.substring(start + 10,
                                    xmlDescription.indexOf('"', start + 11)));
                            fetchMode = FETCHMODE_DIRECT;
                        } catch (UnsupportedEncodingException ignored) {
                            fetchMode = FETCHMODE_REENCODE;
                        }
                    } else {
                        // absolutely no encoding information found
                        fetchMode = FETCHMODE_DIRECT;
                    }
                }

                ContentValues values = new ContentValues();
                values.put(FeedColumns.FETCH_MODE, fetchMode);
                cr.update(FeedColumns.CONTENT_URI(id), values, null, null);
            }

            switch (fetchMode) {
            default:
            case FETCHMODE_DIRECT: {
                if (contentType != null) {
                    int index = contentType.indexOf(CHARSET);
                    int index2 = contentType.indexOf(';', index);

                    InputStream inputStream = connection.getInputStream();
                    Xml.parse(inputStream,
                            Xml.findEncodingByName(index2 > -1 ? contentType.substring(index + 8, index2)
                                    : contentType.substring(index + 8)),
                            handler);
                } else {
                    InputStreamReader reader = new InputStreamReader(connection.getInputStream());
                    Xml.parse(reader, handler);
                }
                break;
            }
            case FETCHMODE_REENCODE: {
                ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                InputStream inputStream = connection.getInputStream();

                byte[] byteBuffer = new byte[4096];

                int n;
                while ((n = inputStream.read(byteBuffer)) > 0) {
                    outputStream.write(byteBuffer, 0, n);
                }

                String xmlText = outputStream.toString();

                int start = xmlText != null ? xmlText.indexOf(ENCODING) : -1;

                if (start > -1) {
                    Xml.parse(new StringReader(new String(outputStream.toByteArray(),
                            xmlText.substring(start + 10, xmlText.indexOf('"', start + 11)))), handler);
                } else {
                    // use content type
                    if (contentType != null) {
                        int index = contentType.indexOf(CHARSET);
                        if (index > -1) {
                            int index2 = contentType.indexOf(';', index);

                            try {
                                StringReader reader = new StringReader(new String(outputStream.toByteArray(),
                                        index2 > -1 ? contentType.substring(index + 8, index2)
                                                : contentType.substring(index + 8)));
                                Xml.parse(reader, handler);
                            } catch (Exception e) {
                                Log.e("Privacy Vandaag: ", "Error reading string " + e.getMessage());
                            }
                        } else {
                            StringReader reader = new StringReader(xmlText);
                            Xml.parse(reader, handler);
                        }
                    }
                }
                break;
            }
            }

            connection.disconnect();
        } catch (FileNotFoundException e) {
            if (handler == null || (!handler.isDone() && !handler.isCancelled())) {
                ContentValues values = new ContentValues();

                // resets the fetch mode to determine it again later
                values.put(FeedColumns.FETCH_MODE, 0);

                values.put(FeedColumns.ERROR, getString(R.string.error_feed_error));
                cr.update(FeedColumns.CONTENT_URI(id), values, null, null);
            }
        } catch (Throwable e) {
            if (handler == null || (!handler.isDone() && !handler.isCancelled())) {
                ContentValues values = new ContentValues();

                // resets the fetch mode to determine it again later
                values.put(FeedColumns.FETCH_MODE, 0);

                values.put(FeedColumns.ERROR,
                        e.getMessage() != null ? e.getMessage() : getString(R.string.error_feed_process));
                cr.update(FeedColumns.CONTENT_URI(id), values, null, null);
                handler = null; // If an error has occurred, reset the new articles counter for this feed to avoid notifications.
            }
        } finally {
            /* check and optionally find favicon */
            /* No longer needed, because the icons of the feeds are included in the package */
            /*
                try {
                    if (handler != null && cursor.getBlob(iconPosition) == null) {
                        String feedLink = handler.getFeedLink();
                        if (feedLink != null) {
                            NetworkUtils.retrieveFavicon(this, new URL(feedLink), id);
                        } else {
                            NetworkUtils.retrieveFavicon(this, connection.getURL(), id);
                        }
                    }
                } catch (Throwable ignored) {
                }
            */
            if (connection != null) {
                connection.disconnect();
            }
        }
    }

    cursor.close();

    int newArticles = (handler != null) ? handler.getNewCount() : 0;
    //Log.e(TAG, "Test notification is gegeven voor feedID " + feedId);
    //if (newArticles == 0 ) newArticles =2;      // ONLY FOR TESTING !!!!

    // Check of meldingen voor deze feed aanstaat, anders newArticles op 0 zetten
    if (newArticles > 0) {
        boolean notifyFeed = true;
        switch (Integer.parseInt(feedId)) {
        case 1: // feedID Privacy Barometer
            notifyFeed = PrefUtils.getBoolean(PrefUtils.NOTIFY_PRIVACYBAROMETER, true);
            break;
        case 2: // feedID Bits of Freedom
            notifyFeed = PrefUtils.getBoolean(PrefUtils.NOTIFY_BITSOFFREEDOM, true);
            break;
        case 3: // feedID Privacy First
            notifyFeed = PrefUtils.getBoolean(PrefUtils.NOTIFY_PRIVACYFIRST, true);
            break;
        case 4: // feedID Autoriteit Persoonsgegevens
            notifyFeed = PrefUtils.getBoolean(PrefUtils.NOTIFY_AUTORITEITPERSOONSGEGEVENS, true);
        }
        if (!notifyFeed)
            newArticles = 0; // geen melding als de meldingen voor deze feed uitstaan.
    }
    //Log.e(TAG, "Nieuwe artikelen is " + newArticles);

    return newArticles;
}