4040
4141
4242public final class RSSFeed implements Serializable {
43- private static final long serialVersionUID = 2L ;//publisher, changed value to 2
43+ private static final long serialVersionUID = 2L ;//publisher, changed value to 2
4444
4545 private final static boolean debug = false ;//BuildConfig.DEBUG;//should be false in prod environment
4646
4747 private String _title = "<untitled>" ;
48- private String _language = null ;
49- private Date _pubdate ;
48+ private String _language = null ;
49+ private Date _pubdate ;
5050 private String _publisherurl = null ;//publisher
5151
5252 private String etag ;
5353 private String lastmod ;
5454 private final String feed_id ;
5555
56- private long time ; // new field
57- private final List <RSSItem > _itemlist ;
58- private final static String tagz = "RSSFeed" ;
59-
60- private RSSFeed (String feed_id ) {//do not use constructor from other classes, instead please use getInstance()
61- _itemlist = new Vector <>(0 );
62- _pubdate = new Date (); // default date
56+ private long time ; // new field
57+ private final List <RSSItem > _itemlist ;
58+ private final static String tagz = "RSSFeed" ;
59+
60+ private RSSFeed (String feed_id ) {//do not use constructor from other classes, instead please use getInstance()
61+ _itemlist = new Vector <>(0 );
62+ _pubdate = new Date (); // default date
6363 this .feed_id = feed_id ;
64- }
64+ }
6565
6666 public static RSSFeed getInstance (File cachedir , String feed_id ) {
6767 File feedFile = new File (cachedir , feed_id .concat (".cache" ));
@@ -75,40 +75,40 @@ public static RSSFeed getInstance(File cachedir, String feed_id) {
7575 return new RSSFeed (feed_id );
7676 }
7777
78- public RSSItem getItem (int location ) {
79- return _itemlist .get (location );
80- }
81-
82- public List <RSSItem > getAllItems () {
83- return _itemlist ;
84- }
85-
86- public int size () {
87- return _itemlist .size ();
88- }
89-
90- private void setTitle (String title ) {
91- _title = title .trim ();
92- }
93- private void setLanguage (String lang ) {
94- _language = lang .trim ();
95- }
78+ public RSSItem getItem (int location ) {
79+ return _itemlist .get (location );
80+ }
81+
82+ public List <RSSItem > getAllItems () {
83+ return _itemlist ;
84+ }
85+
86+ public int size () {
87+ return _itemlist .size ();
88+ }
89+
90+ private void setTitle (String title ) {
91+ _title = title .trim ();
92+ }
93+ private void setLanguage (String lang ) {
94+ _language = lang .trim ();
95+ }
9696 void setPublisherLink (String link ) {//publisher
9797 _publisherurl = link ;
9898 }
9999
100- private void setPubDate (String pubdate ) {
100+ private void setPubDate (String pubdate ) {
101101 _pubdate = DateParser .parseDate (pubdate );
102- }
103-
104- public String getTitle () {
105- return _title ;
106- }
107- public String getLanguage (String default_lang ) {
108- if (_language == null )
109- return default_lang ;
110- return _language ;
111- }
102+ }
103+
104+ public String getTitle () {
105+ return _title ;
106+ }
107+ public String getLanguage (String default_lang ) {
108+ if (_language == null )
109+ return default_lang ;
110+ return _language ;
111+ }
112112 public String getPublisherLink () {//publisher, note that it defaults to feed_url
113113 return _publisherurl ;
114114 }
@@ -133,32 +133,32 @@ public void serialize(Context context, String etag, String lastmod) throws IOExc
133133 this .etag = etag ;
134134 this .lastmod = lastmod ;
135135
136- File feedFile = new File (context .getCacheDir (), feed_id .concat (".cache" ));
136+ File feedFile = new File (context .getCacheDir (), feed_id .concat (".cache" ));
137137// feedFile.deleteOnExit(); // delete the file when exiting
138- FileOutputStream fos = new FileOutputStream (feedFile );
139- ObjectOutputStream os = new ObjectOutputStream (fos );
140- os .writeObject (this );
141- os .close ();
138+ FileOutputStream fos = new FileOutputStream (feedFile );
139+ ObjectOutputStream os = new ObjectOutputStream (fos );
140+ os .writeObject (this );
141+ os .close ();
142142 }
143-
143+
144144 public boolean isFileUpdated (long age ) {
145145 return time != 0 && System .currentTimeMillis () - time <= age ;
146146 }
147-
148- // doProcessStream()
147+
148+ // doProcessStream()
149149// in positive case it returns the number of collected items
150150// in negative case it returns an error code < 0
151151 private final static int zNullStream = -1 ;
152- // final static int zInvalidResponseCode = -2; removed
152+ // final static int zInvalidResponseCode = -2; removed
153153 private final static int zMissingRSSTag = -3 ;
154154 public final static int zRedirectFeed = -4 ;
155155 private final static int zEmptyBody = -5 ;
156156
157157 private final static String encoding_element = "encoding=\" " ;
158158
159- //use doProcessStream() only for xml content (not suitable for html content!)
160- public RSSFeedResult doProcessStream (InputStream is , String encoding , int max_titles , String feed_url , String title ) throws IOException , UnsupportedCharsetException {
161- if (is == null ) return new RSSFeedResult (zNullStream );
159+ //use doProcessStream() only for xml content (not suitable for html content!)
160+ public RSSFeedResult doProcessStream (InputStream is , String encoding , int max_titles , String feed_url , String title ) throws IOException , UnsupportedCharsetException {
161+ if (is == null ) return new RSSFeedResult (zNullStream );
162162 BufferedReader stream ;//stream handling modified to support russian web sites with encoding windows-1251
163163 byte [] body = null ;
164164 if (encoding .length () > 0 )
@@ -179,20 +179,20 @@ public RSSFeedResult doProcessStream(InputStream is, String encoding, int max_ti
179179 _title = title ;//set title here and not during web data retrieval
180180 _publisherurl = feed_url ;
181181 String newfeedurl = null ;
182- PriorityQueue <RSSItem > result = new PriorityQueue <>();
182+ PriorityQueue <RSSItem > result = new PriorityQueue <>();
183183 time = System .currentTimeMillis ();
184- RSSItem item = null ;
185- int rssstate = 0 ; // 0: init, 1: rss found, 2: channel found
184+ RSSItem item = null ;
185+ int rssstate = 0 ; // 0: init, 1: rss found, 2: channel found
186186 if (debug ) Log .d (tagz , "doProcessStream, encoding=" +encoding );
187- int state = 0 ; // idle
188- int deepness = 0 ;
189- String st ;
190- StringBuilder content = new StringBuilder (512 ), econtent = new StringBuilder (512 );
191- StringBuilder tag = new StringBuilder ();
187+ int state = 0 ; // idle
188+ int deepness = 0 ;
189+ String st ;
190+ StringBuilder content = new StringBuilder (512 ), econtent = new StringBuilder (512 );
191+ StringBuilder tag = new StringBuilder ();
192192// String stag;
193- long timer = System .nanoTime ();
193+ long timer = System .nanoTime ();
194194 boolean rdf = false ;
195- try {
195+ try {
196196 main :
197197 while ((st = stream .readLine ()) != null ) {
198198//---begin---
@@ -203,10 +203,13 @@ public RSSFeedResult doProcessStream(InputStream is, String encoding, int max_ti
203203 if (pos != -1 ) {
204204 int end = st .indexOf ("\" " , pos + encoding_element .length ());
205205 encoding = st .substring (pos + encoding_element .length (), end );//now encoding is defined
206- stream .close (); //close stream before re-opening it
207- stream = new BufferedReader (new InputStreamReader (new ByteArrayInputStream (body ), encoding ));//re-open stream with proper encoding, beware of loops!!
208- if (debug )
209- Log .d (tagz , "encoding: " + encoding );
206+ if (!encoding .isEmpty ()) {//change encoding
207+ stream .close (); //close stream before re-opening it
208+ stream = new BufferedReader (new InputStreamReader (new ByteArrayInputStream (body ), encoding ));//re-open stream with proper encoding, beware of loops!!
209+ if (debug )
210+ Log .d (tagz , "encoding: " + encoding );
211+ continue ;
212+ }
210213 }
211214 } else if ((xml = st .indexOf ("<rss" )) != -1 )//check basic rss ?
212215 rssstate = 1 ; // rss found, looking for channel
@@ -312,7 +315,7 @@ else if ((xml = st.indexOf("<feed")) != -1) { //check basic feed ?
312315 int gt = st .indexOf (">" , i ); // uu
313316 if (gt == -1 ) continue main ; // uu
314317 i = gt ; // uu
315- //assert: now it is true that (st.charAt(i) == '>')
318+ //assert: now it is true that (st.charAt(i) == '>')
316319
317320 if (deepness == 0 )
318321 state = 0 ; // idle
@@ -439,17 +442,17 @@ else if ((xml = st.indexOf("<feed")) != -1) { //check basic feed ?
439442 if (item != null )
440443 result .add (item );
441444 _itemlist .clear ();
442- while ((result .size () > 0 ) && (_itemlist .size () < max_titles ))
445+ while ((! result .isEmpty () ) && (_itemlist .size () < max_titles ))
443446 _itemlist .add (result .poll ());
444447 if (debug ) Log .d (tagz , "number of parsed items: " + _itemlist .size ());
445448 return new RSSFeedResult (_itemlist .size ());
446449 } else if (newfeedurl != null )
447450 return new RSSFeedResult (zRedirectFeed , newfeedurl );
448451 else return new RSSFeedResult (zMissingRSSTag );
449452 } finally {
450- stream .close ();
453+ stream .close ();
451454 }
452- } // end of doProcessStream()
453-
455+ } // end of doProcessStream()
456+
454457
455458}
0 commit comments