Skip to content

Commit

Permalink
Feedparser with reflection.
Browse files Browse the repository at this point in the history
TODO: This needs a special build.xml entry


git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@3832 6c8d7289-2bf4-0310-a012-ef5d649a1542
  • Loading branch information
allo committed Jun 8, 2007
1 parent 8bff810 commit 7b2e1bb
Show file tree
Hide file tree
Showing 6 changed files with 322 additions and 93 deletions.
9 changes: 8 additions & 1 deletion htroot/FeedReader_p.html
Expand Up @@ -20,5 +20,12 @@
#{/items}#
</dl>
::
Error

Error:
#(error)#
You need to install libx
::
Problem with url
#(/error)#
test
#(/page)#
143 changes: 143 additions & 0 deletions htroot/FeedReader_p.java
@@ -0,0 +1,143 @@
//FeedReader_p.java
//------------
// part of YACY
//
// (C) 2007 Alexander Schier
//
// last change: $LastChangedDate: $ by $LastChangedBy: $
// $LastChangedRevision: $
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collection;
import java.util.Iterator;

import de.anomic.data.rssReaderItem;
import de.anomic.http.httpHeader;
import de.anomic.server.serverObjects;
import de.anomic.server.serverSwitch;
import de.anomic.server.servletProperties;

public class FeedReader_p {

Class rssReaderClass;
String url;
Object reader;
public FeedReader_p(String url) throws ClassNotFoundException{
this.url=url;
//reflection
Class[] paramClasses=new Class[1];
Object[] params=new Object[1];
paramClasses[0]=Class.forName("java.lang.String");
rssReaderClass=Class.forName("de.anomic.data.rssReader");
params[0]=url.toString();
Constructor constructor;
try {
constructor = rssReaderClass.getConstructor(paramClasses);
reader=constructor.newInstance(params);
}
catch (SecurityException e) {}
catch (NoSuchMethodException e) {}
catch (IllegalArgumentException e) {}
catch (InstantiationException e) {}
catch (IllegalAccessException e) {}
catch (InvocationTargetException e) {}

}
//this could be in the servlet class(?)
public Object ReflectionWrapper(Class theclass, Object theinstance, String method, Class[] class_array, Object[] object_array){
try {
return theclass.getMethod(method, class_array).invoke(theinstance, object_array);
}
catch (IllegalArgumentException e) {}
catch (SecurityException e) {}
catch (IllegalAccessException e) {}
catch (InvocationTargetException e) {}
catch (NoSuchMethodException e) {}
return null;
}
public String getTitle(){
return (String) ReflectionWrapper(rssReaderClass, reader, "getTitle", null, null);
}
public String getCreator(){
return (String) ReflectionWrapper(rssReaderClass, reader, "getCreator", new Class[0], new Object[0]);
}
public String getDescription(){
return (String) ReflectionWrapper(rssReaderClass, reader, "getDescription", new Class[0], new Object[0]);
}
public Collection getFeedItems(){
return (Collection) ReflectionWrapper(rssReaderClass, reader, "getFeedItems", new Class[0], new Object[0]);
}
public static servletProperties respond(httpHeader header, serverObjects post, serverSwitch env) {
servletProperties prop = new servletProperties();
prop.put("SUPERTEMPLATE", "/env/page.html");

URL url;
prop.put("page", 0);
try {
if(post!=null){
url = new URL((String) post.get("url"));
int maxitems=Integer.parseInt(post.get("max", "0"));
int offset=Integer.parseInt(post.get("offset", "0")); //offset to the first displayed item
FeedReader_p self;
try {
self = new FeedReader_p(url.toString());
} catch (ClassNotFoundException e) {
System.out.println("DEBUG!!!");
prop.put("page", 2);
prop.put("page_error", 0);
return prop;
}

//rssReader reader=new rssReader(url.toString());
prop.put("page_title", self.getTitle());
if(self.getCreator()!=null){
prop.put("page_hasAuthor", 1);
prop.put("page_hasAuthor_author", self.getCreator());
}else
prop.put("page_hasAuthor", 0);
prop.put("page_description", self.getDescription());

Collection feedItems=self.getFeedItems();
if(feedItems!=null && !feedItems.isEmpty()){
Iterator it=feedItems.iterator();
int count=0;
while(it.hasNext() && (maxitems==0 || count<maxitems)){
rssReaderItem item=(rssReaderItem)it.next();
prop.put("page_items_"+count+"_author", item.getCreator());
prop.put("page_items_"+count+"_title", item.getTitle());
prop.put("page_items_"+count+"_link", item.getLink().toString());
prop.putASIS("page_items_"+count+"_description", item.getDescription());
prop.put("page_items_"+count+"_date", item.getDate());
if(! (offset-- >0)) //first check, if there still is an offset. if yes, skip count++, so the next item will overwrite this item. then decrement offset.
count++;
}
prop.put("page_items", count);
}
prop.put("page", 1);
}
} catch (MalformedURLException e) {
prop.put("page", 2);
prop.put("page_error", 1);
}

// return rewrite properties
return prop;
}
}
92 changes: 0 additions & 92 deletions htroot/FeedReader_p.java.disabled

This file was deleted.

73 changes: 73 additions & 0 deletions source/de/anomic/data/rssReader.java
@@ -0,0 +1,73 @@
//rssReader.java
//------------
// part of YACY
//
// (C) 2007 Alexander Schier
//
// last change: $LastChangedDate: $ by $LastChangedBy: $
// $LastChangedRevision: $
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

package de.anomic.data;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collection;
import java.util.Iterator;
import java.util.TreeSet;

import de.nava.informa.core.ChannelIF;
import de.nava.informa.core.ParseException;
import de.nava.informa.impl.basic.ChannelBuilder;
import de.nava.informa.parsers.FeedParser;

public class rssReader {
URL url;
ChannelIF channel;
TreeSet feedItems;
public rssReader(String url) throws MalformedURLException{
this.url=new URL(url);
ChannelBuilder builder=new ChannelBuilder();
try {
channel=FeedParser.parse(builder, url);
Collection oldfeedItems=channel.getItems();
feedItems=new TreeSet(new rssReaderItemComparator());
Iterator it=oldfeedItems.iterator();
int count=0;
while(it.hasNext()){
de.nava.informa.impl.basic.Item item=(de.nava.informa.impl.basic.Item) it.next();
rssReaderItem newItem=new rssReaderItem(count++, item.getLink(), item.getTitle(), item.getDescription(), item.getDate(), item.getCreator());
feedItems.add(newItem);
}
}
catch (IOException e) {}
catch (ParseException e) {}
}
public String getCreator(){
return (channel!=null)? channel.getCreator(): null;
}
public String getTitle(){
return (channel!=null)? channel.getTitle(): null;
}
public String getDescription(){
return (channel!=null)? channel.getDescription(): null;
}
public Collection getFeedItems(){
return feedItems;
}

}
62 changes: 62 additions & 0 deletions source/de/anomic/data/rssReaderItem.java
@@ -0,0 +1,62 @@
//rssReaderItem.java
//------------
// part of YACY
//
// (C) 2007 Alexander Schier
//
// last change: $LastChangedDate: $ by $LastChangedBy: $
// $LastChangedRevision: $
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

package de.anomic.data;

import java.net.URL;
import java.util.Date;


public class rssReaderItem{
String creator, title, description;
Date date;
URL link;
int num;
public rssReaderItem(int num, URL link, String title, String description, Date date, String creator){
this.link=link;
this.title=title;
this.description=description;
this.date=date;
this.creator=creator;
this.num=num;
}
public URL getLink(){
return link;
}
public String getTitle(){
return (title!=null)? title: "";
}
public String getDescription(){
return (description!=null)? description: "";
}
public Date getDate(){
return (date!=null)? date: new Date();
}
public String getCreator(){
return (creator!=null)? creator: "";
}
public int getNum(){
return num;
}

}

0 comments on commit 7b2e1bb

Please sign in to comment.