Back to project page SimpleReader.
The source code is released under:
Apache License
If you think the Android project SimpleReader listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.dreamteam.app.commons; /*w w w . ja v a 2 s . c om*/ import info.monitorenter.cpdetector.io.CodepageDetectorProxy; import info.monitorenter.cpdetector.io.JChardetFacade; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.URL; import java.nio.charset.Charset; import java.util.ArrayList; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.Attributes; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.XMLReader; import org.xml.sax.helpers.DefaultHandler; import com.dreamteam.app.entity.FeedItem; import com.dreamteam.app.entity.ItemListEntity; import com.dreamteam.app.utils.DateUtils; import com.dreamteam.app.utils.HttpUtils; public class ItemListEntityParser extends DefaultHandler { private ItemListEntity itemListEntity; private FeedItem feedItem; private ArrayList<FeedItem> items = new ArrayList<FeedItem>(); private StringBuffer sb = new StringBuffer(); private boolean isFeedTitle = true; private boolean isFeedDesc = true; private boolean isFeedLink = true; @Override public void startDocument() throws SAXException { itemListEntity = new ItemListEntity(); } @Override public void endDocument() throws SAXException { itemListEntity.setItemList(items); } @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { sb.setLength(0); if (qName.equalsIgnoreCase("item")) { feedItem = new FeedItem(); items.add(feedItem); isFeedTitle = false; isFeedDesc = false; isFeedLink = false; } } @Override public void endElement(String uri, String localName, String qName) throws SAXException { String content = sb.toString(); if (!isFeedLink && qName.equalsIgnoreCase("link")) { feedItem.setLink(content); } if (!isFeedTitle && qName.equalsIgnoreCase("title")) { feedItem.setTitle(content); return; } if (!isFeedDesc && (qName.equalsIgnoreCase("description") || qName .equalsIgnoreCase("content:encoded"))) { feedItem.setContent(content); ArrayList<String> srcs = HtmlFilter.getImageSrcs(content); if (!srcs.isEmpty()) feedItem.setFirstImageUrl(srcs.get(0)); feedItem.setImageUrls(srcs); isFeedDesc = false; return; } if (qName.equalsIgnoreCase("pubDate")) { content = DateUtils.rfcNormalDate(content); if (feedItem != null) feedItem.setPubdate(content); } } @Override public void characters(char[] ch, int start, int length) throws SAXException { sb.append(ch, start, length); } public ItemListEntity parse(InputStream source) { SAXParserFactory saxpf = SAXParserFactory.newInstance(); SAXParser saxp = null; InputStream inputStream = null; InputSource inputSource = null; try { inputSource = new InputSource(source); saxp = saxpf.newSAXParser(); XMLReader xmlr = saxp.getXMLReader(); xmlr.setContentHandler(this); xmlr.parse(inputSource); return itemListEntity; } catch (ParserConfigurationException e) { e.printStackTrace(); return null; } catch (SAXException e) { e.printStackTrace(); return null; } catch (FileNotFoundException e) { e.printStackTrace(); return null; } catch (IOException e) { e.printStackTrace(); return null; } catch (Exception e) { e.printStackTrace(); return null; } finally { if (inputStream != null) { try { inputStream.close(); inputStream = null; } catch (IOException e) { e.printStackTrace(); } } } } public ItemListEntity parse(String url) { // ??????????? CodepageDetectorProxy detector = CodepageDetectorProxy.getInstance(); // ?????????????? detector.add(JChardetFacade.getInstance()); // ?????????? Charset charset = null; try { charset = detector.detectCodepage(new URL(url)); } catch (Exception e1) { e1.printStackTrace(); } // ????????? String encodingName = charset.name(); SAXParserFactory saxpf = SAXParserFactory.newInstance(); SAXParser saxp = null; InputStream inputStream = null; InputSource inputSource = null; try { inputStream = HttpUtils.getInputStream(url); if ("GBK".equals(encodingName)) { // ??InputStreamReader???????? InputStreamReader streamReader = new InputStreamReader(inputStream, "GBK"); inputSource = new InputSource(streamReader); } else { inputSource = new InputSource(inputStream); } saxp = saxpf.newSAXParser(); XMLReader xmlr = saxp.getXMLReader(); xmlr.setContentHandler(this); xmlr.parse(inputSource); return itemListEntity; } catch (ParserConfigurationException e) { e.printStackTrace(); return null; } catch (SAXException e) { e.printStackTrace(); return null; } catch (FileNotFoundException e) { e.printStackTrace(); return null; } catch (IOException e) { e.printStackTrace(); return null; } catch (Exception e) { e.printStackTrace(); return null; } finally { if (inputStream != null) { try { inputStream.close(); inputStream = null; } catch (IOException e) { e.printStackTrace(); } } } } }