Browse Source

feat: Ajout infos lors de l'affichage RSSData

Martin Passard 6 years ago
parent
commit
65b73fb8eb
4 changed files with 64 additions and 60 deletions
  1. 1 1
      src/rss/RSSChecker.java
  2. 60 28
      src/rss/RssData.java
  3. 3 0
      src/rss/RssDataRemainder.java
  4. 0 31
      src/rss/RssDataRemainderTest.java

+ 1 - 1
src/rss/RSSChecker.java

@@ -107,7 +107,7 @@ public class RSSChecker implements Runnable {
 								b.sendMessageOnAllChannels("Nouveautée sur planet.ffdn.org:");
 								
 							}
-							RssData rs = new RssData(article, date);
+							RssData rs = new RssData(article);
 							remainder.push(rs);
 							afficheArticle(rs);
 							lastarticle = date;

+ 60 - 28
src/rss/RssData.java

@@ -1,9 +1,11 @@
 package rss;
 
+import java.text.ParseException;
 import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
 
+import org.w3c.dom.DOMException;
 import org.w3c.dom.NamedNodeMap;
 import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
@@ -22,49 +24,68 @@ public class RssData implements AffichableSurIRC{
 	private String date;
 	private String titre;
 	private String lien;
-	
-	public RssData(String auteur, String date, String titre, String lien) {
+	private String source;
+
+	public RssData(String auteur, String date, String titre, String lien, String source) {
 		super();
 		this.auteur = auteur;
 		this.date = date;
 		this.titre = titre;
 		this.lien = lien;
+		this.source = source;
 	}
-	
+
 	/**
 	 * Transforme un Noeud correspondant à un <entry> en un RSSData
 	 * @param item Noeud w3c 
 	 */
-	public RssData(Node item) {
+	public RssData(Node item, Date date) {
 		this();
 		NodeList childs = item.getChildNodes();
 		int length  = childs.getLength();
 		for(int i = 0; i<length;++i) {
 			Node actualchild = childs.item(i);
-		
-		String balise = actualchild.getNodeName();
-		if(balise.equalsIgnoreCase("title")) {
-			setTitre(actualchild.getTextContent());
-		}else if(balise.equalsIgnoreCase("author")) {
-			NodeList authlist =actualchild.getChildNodes();
-			for(int j = 0;j<authlist.getLength();j++) {
-				Node bn = authlist.item(j);
-				if(bn.getNodeName().equalsIgnoreCase("name")) {
-					String a = ""+bn.getTextContent();
-					setAuteur(a);
+
+			String balise = actualchild.getNodeName();
+			if(balise.equalsIgnoreCase("title")) {
+				setTitre(actualchild.getTextContent());
+			}else if(balise.equalsIgnoreCase("author")) {
+				NodeList authlist =actualchild.getChildNodes();
+				for(int j = 0;j<authlist.getLength();j++) {
+					Node bn = authlist.item(j);
+					if(bn.getNodeName().equalsIgnoreCase("name")) {
+						String a = ""+bn.getTextContent();
+						setAuteur(a);
+					}
+				}
+			}else if(balise.equalsIgnoreCase("link")) {
+				NamedNodeMap nmp = actualchild.getAttributes();
+				String link = nmp.getNamedItem("href").getTextContent();
+				if(link!=null) setLien(link);
+			}else if(balise.equalsIgnoreCase("source")) {
+				NodeList nl = actualchild.getChildNodes();
+				for(int j = 0; j<nl.getLength();++j) {
+					Node node = nl.item(j);
+					if(node.getNodeName().equalsIgnoreCase("title")) {
+						setSource(node.getTextContent());
+					}
+				}
+			}else if(balise.equalsIgnoreCase("updated")) {
+				try {
+					Date d = RSSChecker.DATE_FORMATIN.parse(actualchild.getTextContent());
+					setDate(Main.DATE_FORMAT_OUT.format(d));
+				} catch (DOMException | ParseException e) {
+					e.printStackTrace();
 				}
 			}
-		}else if(balise.equalsIgnoreCase("link")) {
-			NamedNodeMap nmp = actualchild.getAttributes();
-			String link = nmp.getNamedItem("href").getTextContent();
-			if(link!=null) setLien(link);
 		}
+		if(date != null) {
+			this.setDate(Main.DATE_FORMAT_OUT.format(date));
 		}
 	}
-	
-	public RssData(Node item, Date date) {
-		this(item);
-		this.setDate(Main.DATE_FORMAT_OUT.format(date));
+
+	public RssData(Node item) {
+		this(item,null);
 	}
 
 
@@ -74,7 +95,7 @@ public class RssData implements AffichableSurIRC{
 		this.titre="";
 		this.lien="";
 	}
-	
+
 	public String getAuteur() {
 		return auteur;
 	}
@@ -93,7 +114,15 @@ public class RssData implements AffichableSurIRC{
 	public void setDate(String date) {
 		this.date = date;
 	}
+	
+
+	public String getSource() {
+		return source;
+	}
 
+	public void setSource(String source) {
+		this.source = source;
+	}
 
 	public String getTitre() {
 		return titre;
@@ -115,7 +144,7 @@ public class RssData implements AffichableSurIRC{
 	}
 
 	public String toString() {
-		return "Article par "+auteur+" : "+titre+" le "+date+" lien: "+lien;
+		return "Article par "+auteur+" : "+titre+" le "+date+" lien: "+lien+" via "+source;
 	}
 
 
@@ -129,13 +158,16 @@ public class RssData implements AffichableSurIRC{
 		if(!(date == null || date.equals(""))) {
 			auth += "le "+date;
 		}
+		if(!(source == null || source.equals(""))) {
+			auth += " via "+source;
+		}
 		if(!auth.equals("")) res.add(auth);
 		if(lien != null) {
-		res.add("Plus d'infos sur "+lien);
+			res.add("Plus d'infos sur "+lien);
 		}
 		return res;
 	}
-	
-	
+
+
 
 }

+ 3 - 0
src/rss/RssDataRemainder.java

@@ -102,6 +102,9 @@ public class RssDataRemainder implements AffichableSurIRC{
 			if(data[i].getAuteur()!= null && !data[i].getAuteur().equals("")) {
 				ligne +=" par "+data[i].getAuteur();
 			}
+			if(data[i].getSource()!= null && !data[i].getSource().equals("")) {
+				ligne +=" via "+data[i].getSource();
+			}
 			res.add(ligne);
 		}
 		return res;

+ 0 - 31
src/rss/RssDataRemainderTest.java

@@ -1,31 +0,0 @@
-package rss;
-
-import java.util.Date;
-import java.util.List;
-
-public class RssDataRemainderTest {
-
-	public static void main(String[] args) {
-		RssDataRemainder rd = new RssDataRemainder(3);
-		RssData d1  =  new RssData("adrien",new Date().toString(), "A bit blog", "www.irc.to");
-		RssData d2  =  new RssData("MArmat",new Date().toString(), "UneFede", "www.marmat.ovh");
-		RssData d3  =  new RssData("quota",new Date().toString(), "Café", "www.ca.fe");
-		RssData d4  =  new RssData("zorun",new Date().toString(), "ftth", "www.fible.ffdn.org");
-		rd.push(d1);
-		printList(rd.toStringIRC());
-		rd.push(d2);
-		rd.push(d3);
-		printList(rd.toStringIRC());
-		rd.push(d4);
-		printList(rd.toStringIRC());
-		rd.push(d4);
-		printList(rd.toStringIRC());
-		
-	}
-
-	public static void printList(List<String> data) {
-		for(String s : data) {
-			System.out.println(s);
-		}
-	}
-}