Search in sources :

Example 21 with DefaultHandler

use of org.xml.sax.helpers.DefaultHandler in project newsrob by marianokamp.

the class GRAnsweredBadRequestException method getStreamIDsFromGR.

/**
     * @param xt
     *            can be null
     */
protected long[] getStreamIDsFromGR(NewsRobHttpClient httpClient, final List<String> tags, String xt, int max) throws IOException, SAXException, ParserConfigurationException, GRTokenExpiredException, GRAnsweredBadRequestException {
    if (max == 0)
        return new long[0];
    final String tagsLabel = String.valueOf(tags);
    Timing t = new Timing("EntriesRetriever.getStreamIDsFromGR(" + tagsLabel + ") (-" + xt + ")", context);
    int currentCapacity = getEntryManager().getArticleCount();
    String url = getGoogleHost() + "/reader/api/0/stream/items/ids";
    url += "?s=" + tags.remove(0);
    for (String tag : tags) url += "&s=" + tag;
    if (xt != null)
        url += "&xt=" + xt;
    url += "&n=" + max;
    try {
        HttpRequestBase req = createGRRequest(httpClient, url);
        HttpResponse response = executeGRRequest(httpClient, req, true);
        throwExceptionWhenNotStatusOK(response);
        final List<Long> unreadIds = new ArrayList<Long>(currentCapacity * 80 / 100);
        SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
        SAXParser parser = saxParserFactory.newSAXParser();
        DefaultHandler handler = new SimpleStringExtractorHandler() {

            String currentName;

            boolean validResponse;

            @Override
            public final void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException {
                super.startElement(uri, localName, name, attributes);
                currentName = attributes.getValue("name");
                if (!validResponse && "/object".equals(getFullyQualifiedPathName()))
                    validResponse = true;
            }

            @Override
            public final void receivedString(String localTagName, String fqn, String value) {
                if ("number".equals(localTagName) && "id".equals(currentName))
                    unreadIds.add(Long.parseLong(value));
            }

            @Override
            public void endDocument() throws SAXException {
                super.endDocument();
                if (!validResponse)
                    throw new RuntimeException("Google Reader response was invalid. Proxy issue?");
            }
        };
        InputStream is = NewsRobHttpClient.getUngzippedContent(response.getEntity(), context);
        parser.parse(is, handler);
        if (NewsRob.isDebuggingEnabled(context))
            PL.log(TAG + ": GR returned number of articles(" + tagsLabel + ") (-" + xt + ")=" + unreadIds.size(), context);
        long[] rv = new long[unreadIds.size()];
        int idx = 0;
        for (Long unreadId : unreadIds) rv[idx++] = unreadId;
        return rv;
    } finally {
        t.stop();
    }
}
Also used : HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) SimpleStringExtractorHandler(com.newsrob.util.SimpleStringExtractorHandler) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) Attributes(org.xml.sax.Attributes) HttpResponse(org.apache.http.HttpResponse) DefaultHandler(org.xml.sax.helpers.DefaultHandler) SAXParser(javax.xml.parsers.SAXParser) Timing(com.newsrob.util.Timing) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 22 with DefaultHandler

use of org.xml.sax.helpers.DefaultHandler in project newsrob by marianokamp.

the class GRAnsweredBadRequestException method updateSubscriptionList.

void updateSubscriptionList(final EntryManager entryManager, final Job job) throws IOException, ParserConfigurationException, SAXException, GRTokenExpiredException {
    if (job.isCancelled())
        return;
    if (entryManager.getLastSyncedSubscriptions() != -1l && System.currentTimeMillis() < entryManager.getLastSyncedSubscriptions() + ONE_DAY_IN_MS) {
        PL.log("Not updating subscription list this time.", context);
        return;
    }
    PL.log("Updating subscription list.", context);
    Timing t = new Timing("UpdateSubscriptionList", context);
    final NewsRobHttpClient httpClient = NewsRobHttpClient.newInstance(false, context);
    try {
        final String url = getGoogleHost() + "/reader/api/0/subscription/list";
        HttpRequestBase req = createGRRequest(httpClient, url);
        HttpResponse response;
        try {
            response = executeGRRequest(httpClient, req, true);
        } catch (GRAnsweredBadRequestException e) {
            throw new IOException("GR: Bad Request.");
        }
        SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
        SAXParser parser = saxParserFactory.newSAXParser();
        final Map<String, String> remoteTitlesAndIds = new HashMap<String, String>(107);
        DefaultHandler handler = new SimpleStringExtractorHandler() {

            private String currentFeedAtomId;

            private String currentString;

            @Override
            public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException {
                if (job.isCancelled())
                    throw new UpdateSubscriptionsCancelledException();
                super.startElement(uri, localName, name, attributes);
                String fqn = getFullyQualifiedPathName();
                if ("/object/list/object".equals(fqn)) {
                    currentFeedAtomId = null;
                } else if ("/object/list/object/string".equals(fqn)) {
                    currentString = attributes.getValue("name");
                }
            }

            @Override
            public void receivedString(String localName, String fqn, String s) {
                if (!"/object/list/object/string".equals(fqn))
                    return;
                if ("id".equals(currentString))
                    currentFeedAtomId = TAG_GR + s;
                else if ("title".equals(currentString)) {
                    if (currentFeedAtomId != null)
                        remoteTitlesAndIds.put(currentFeedAtomId, s);
                // entryManager.updateFeedName(currentFeedAtomId, s);
                }
            }
        };
        parser.parse(NewsRobHttpClient.getUngzippedContent(response.getEntity(), context), handler);
        if (NewsRob.isDebuggingEnabled(context))
            PL.log("Got subscription list with " + remoteTitlesAndIds.size() + " feeds.", context);
        if (job.isCancelled())
            return;
        entryManager.updateFeedNames(remoteTitlesAndIds);
    } finally {
        httpClient.close();
        t.stop();
    }
    entryManager.updateLastSyncedSubscriptions(System.currentTimeMillis());
}
Also used : HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) HashMap(java.util.HashMap) SimpleStringExtractorHandler(com.newsrob.util.SimpleStringExtractorHandler) Attributes(org.xml.sax.Attributes) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) DefaultHandler(org.xml.sax.helpers.DefaultHandler) NewsRobHttpClient(com.newsrob.download.NewsRobHttpClient) SAXParser(javax.xml.parsers.SAXParser) Timing(com.newsrob.util.Timing) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 23 with DefaultHandler

use of org.xml.sax.helpers.DefaultHandler in project newsrob by marianokamp.

the class GRAnsweredBadRequestException method processInputStream.

protected void processInputStream(final Job job, final FetchContext fetchCtx, InputStream is) throws ParserConfigurationException, SAXException, IOException {
    SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
    SAXParser parser = saxParserFactory.newSAXParser();
    DefaultHandler handler = new ReadingListStreamHandler(getEntryManager(), fetchCtx, job);
    parser.parse(is, handler);
}
Also used : SAXParser(javax.xml.parsers.SAXParser) SAXParserFactory(javax.xml.parsers.SAXParserFactory) DefaultHandler(org.xml.sax.helpers.DefaultHandler)

Example 24 with DefaultHandler

use of org.xml.sax.helpers.DefaultHandler in project newsrob by marianokamp.

the class Feed method restoreFeedsIfNeccesary.

public static final boolean restoreFeedsIfNeccesary(Context context) {
    // initial startup?
    final EntryManager em = EntryManager.getInstance(context);
    if (em.getFeedCount() != 0)
        return false;
    SdCardStorageAdapter storageAdapter = new SdCardStorageAdapter(context.getApplicationContext(), false);
    final String fileName = storageAdapter.getAbsolutePathForAsset(FEED_SETTINGS_FILE_NAME);
    if (!new File(fileName).exists()) {
        Log.w("Feed", "No " + fileName + " existing. Not trying to restore feeds.");
        return false;
    }
    Log.i("Feed", "Trying to restore feeds.");
    try {
        SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
        SAXParser parser = saxParserFactory.newSAXParser();
        DefaultHandler handler = new SimpleStringExtractorHandler() {

            @Override
            public final void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException {
                super.startElement(uri, localName, name, attributes);
                if (!"feed".equals(localName))
                    return;
                Feed f = new Feed();
                f.setAtomId(URLDecoder.decode(attributes.getValue("atomId")));
                f.setTitle(URLDecoder.decode(attributes.getValue("title")));
                f.setDownloadPref(Integer.parseInt(attributes.getValue("downloadPref")));
                f.setDisplayPref(Integer.parseInt(attributes.getValue("displayPref")));
                f.setWebScale(Float.parseFloat(attributes.getValue("webScale")));
                f.setFeedScale(Float.parseFloat(attributes.getValue("feedScale")));
                f.setJavaScriptEnabled(Boolean.parseBoolean(attributes.getValue("javaScriptEnabled")));
                try {
                    f.setFitToWidthEnabled(Boolean.parseBoolean(attributes.getValue("fitToWidthEnabled")));
                } catch (RuntimeException rte) {
                // skip as it may be missing. Default is true then.
                }
                f.setNotificationEnabled(Boolean.parseBoolean(attributes.getValue("notificationEnabled")));
                f.setAlternateUrl(attributes.getValue("altUrl"));
                em.insert(f);
            }

            @Override
            public void receivedString(String localTagName, String fullyQualifiedLocalName, String value) {
            }
        };
        parser.parse(new File(fileName), handler);
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    }
    Log.i("Feed", "Restored feeds. Now " + em.getFeedCount() + " feeds in database.");
    return true;
}
Also used : SimpleStringExtractorHandler(com.newsrob.util.SimpleStringExtractorHandler) Attributes(org.xml.sax.Attributes) IOException(java.io.IOException) SdCardStorageAdapter(com.newsrob.storage.SdCardStorageAdapter) DefaultHandler(org.xml.sax.helpers.DefaultHandler) SAXException(org.xml.sax.SAXException) SAXParser(javax.xml.parsers.SAXParser) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) File(java.io.File) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 25 with DefaultHandler

use of org.xml.sax.helpers.DefaultHandler in project BookReader by JustWayward.

the class Utils method domparse.

public static ArrayList<String> domparse(String filePath, String extractPath, String md5) throws IOException {
    final ArrayList<String> listSite = new ArrayList<>();
    listSite.add(md5);
    ///////////////////////////////////////////////////
    try {
        final FileOutputStream fosHTMLMap = new FileOutputStream(extractPath + "/" + md5);
        final FileOutputStream fosListSite = new FileOutputStream(extractPath + "/site_map_" + md5);
        try {
            fosListSite.write((md5 + ";").getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (chm.getResourceAsStream("") != null) {
            SAXParserImpl.newInstance(null).parse(chm.getResourceAsStream(""), new DefaultHandler() {

                class MyUrl {

                    public int status = 0;

                    public String name;

                    public String local;

                    public String toString() {
                        if (status == 1)
                            return "<a href=\"#\">" + name + "</a>";
                        else
                            return "<a href=\"" + local + "\">" + name + "</a>";
                    }
                }

                MyUrl url = new MyUrl();

                HashMap<String, String> myMap = new HashMap<String, String>();

                int count = 0;

                public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
                    if (qName.equals("param")) {
                        count++;
                        for (int i = 0; i < attributes.getLength(); i++) {
                            myMap.put(attributes.getQName(i).toLowerCase(), attributes.getValue(i).toLowerCase());
                        }
                        if (myMap.get("name").equals("name") && myMap.get("value") != null) {
                            url.name = myMap.get("value");
                            url.status = 1;
                        } else if (myMap.get("name").equals("local") && myMap.get("value") != null) {
                            url.local = myMap.get("value");
                            url.status = 2;
                            listSite.add(url.local.replaceAll("%20", " "));
                            try {
                                fosListSite.write((url.local.replaceAll("%20", " ") + ";").getBytes());
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                        if (url.status == 2) {
                            url.status = 0;
                            try {
                                fosHTMLMap.write(url.toString().getBytes());
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    } else {
                        if (url.status == 1) {
                            try {
                                fosHTMLMap.write(url.toString().getBytes());
                                url.status = 0;
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                    if (!qName.equals("object") && !qName.equals("param"))
                        try {
                            fosHTMLMap.write(("<" + qName + ">").getBytes());
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                }

                public void endElement(String uri, String localName, String qName) throws SAXException {
                    if (!qName.equals("object") && !qName.equals("param"))
                        try {
                            fosHTMLMap.write(("</" + qName + ">").getBytes());
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                }
            });
        } else {
            fosHTMLMap.write("<HTML> <BODY> <UL>".getBytes());
            for (String fileName : chm.list()) {
                fileName = fileName.substring(1);
                if (fileName.endsWith(".htm") || fileName.endsWith(".html")) {
                    fosListSite.write((fileName + ";").getBytes());
                    fosHTMLMap.write(("<li><a href=\"" + fileName + "\">" + fileName + "</a></li>").getBytes());
                    listSite.add(fileName);
                }
            }
            fosHTMLMap.write("</UL> </BODY> </HTML>".getBytes());
        }
        fosHTMLMap.close();
        fosListSite.close();
    } catch (SAXException | IOException e) {
        e.printStackTrace();
    }
    return listSite;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Attributes(org.xml.sax.Attributes) IOException(java.io.IOException) DefaultHandler(org.xml.sax.helpers.DefaultHandler) SAXException(org.xml.sax.SAXException) FileOutputStream(java.io.FileOutputStream)

Aggregations

DefaultHandler (org.xml.sax.helpers.DefaultHandler)139 InputStream (java.io.InputStream)61 Metadata (org.apache.tika.metadata.Metadata)59 ParseContext (org.apache.tika.parser.ParseContext)52 Test (org.junit.Test)43 Attributes (org.xml.sax.Attributes)39 SAXParser (javax.xml.parsers.SAXParser)35 SAXException (org.xml.sax.SAXException)34 ByteArrayInputStream (java.io.ByteArrayInputStream)30 SAXParserFactory (javax.xml.parsers.SAXParserFactory)25 IOException (java.io.IOException)24 Parser (org.apache.tika.parser.Parser)22 InputSource (org.xml.sax.InputSource)21 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)20 TikaInputStream (org.apache.tika.io.TikaInputStream)20 ContentHandler (org.xml.sax.ContentHandler)20 File (java.io.File)17 AutoDetectParser (org.apache.tika.parser.AutoDetectParser)17 BodyContentHandler (org.apache.tika.sax.BodyContentHandler)16 FileInputStream (java.io.FileInputStream)14