1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
|
/****************************************
* Nom de fichier: OutilsDOM.java *
* Author: *
* Date : 15 décembre 2006 *
* Rôle: *
* *
* *
*****************************************/
/**
*
* @author Morad KALKOUL
*/
package mkrss;
import java.io.*;
import java.net.*;
import java.util.Vector;
import javax.xml.parsers.*;
import org.w3c.dom.*;
public class OutilsDOM {
public MkTree rssvct = new MkTree();//Déclaration d'un tableau.
public InputStream is ;
BufferedReader entreeClavier;
String rep;
public void OuvrirConnexion(String urlparam)throws Exception {
try
{
URL url = new URL(urlparam);
URLConnection connection = url.openConnection();
connection.connect();
this.is = connection.getInputStream();
}
catch(Exception e)
{
throw new Exception("URL incorrecte ou mal formée.");
}
}//fin de OuvrirConnexion
public Document CreerFabriqueDocument (InputStream isparam)throws Exception {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(isparam);
return doc;
}//Fin CreerFabriqueDocument
public void AfficherDocument(Document document)throws Exception {
Element racine = document.getDocumentElement();
AfficherNode(racine);
}//Fin de AfficherDocument
public void AfficherNode(Node node)throws Exception {
System.out.println(node);
NodeList nodes = node.getChildNodes();
for(int i=0; i<nodes.getLength(); i++){
Node n = nodes.item(i);
AfficherNode(n);
}//fin de for
}//Fin de AfficherNode
public void AfficherDocumentSimple (Document document) throws Exception {
/* Parcours de l'arbre et extraction des informations */;
NodeList nlitem = document.getElementsByTagName("item");
for (int i=0 ; i<nlitem.getLength(); i++)
{
Element item = (Element)nlitem.item(i);
/* Affichage des titres des articles */
for (Node child = item.getFirstChild(); child!=null ; child = child.getNextSibling())
{
if (child.getNodeType() == Node.ELEMENT_NODE && ((Element)child).getTagName().equals("title"))
{
System.out.println(i+1 + "\t" + child.getTextContent());
}
else if (child.getNodeType() == Node.ELEMENT_NODE && ((Element)child).getTagName().equals("description"))
{
System.out.println("\t\t" + child.getTextContent());
}
/*else if (child.getNodeType() == Node.ELEMENT_NODE && ((Element)child).getTagName().equals("link"))
{
System.out.println("\t\t" + child.getTextContent());
}*/
}
}
}//Finde AfficherDocumentSimple
public void TraitementDunFlux(OutilsDOM monoutil, InterfaceGraphique gui)throws Exception {
String atitre = ""; //variablede travail.
String adescription = ""; //variable de travail.
String alink =""; //variable de travail.
String saisie;
//MkItem anode = new MkItem(); //Déclaration d'un élement contenant toutes les infos concernant un titre.
monoutil.rep ="OK";
monoutil.entreeClavier= new BufferedReader(
new InputStreamReader(System.in));
while (! monoutil.rep.equals("fin")){
System.out.println ("Saisissez une url :");
saisie = monoutil.entreeClavier.readLine().toString();
System.out.println ("Merci !");
monoutil.OuvrirConnexion(saisie);
Document envfabricdoc = monoutil.CreerFabriqueDocument(monoutil.is);
/* Parcours de l'arbre et extraction des informations */;
NodeList nlitem = envfabricdoc.getElementsByTagName("item");
for (int i=0 ; i<nlitem.getLength(); i++)
{
Element item = (Element)nlitem.item(i);
/* Affichage des titres et des articles */
/* et remplissage au fur et à mesure du vector */
for (Node child = item.getFirstChild(); child!=null ; child = child.getNextSibling())
{
if (child.getNodeType() == Node.ELEMENT_NODE && ((Element)child).getTagName().equals("title"))
{
atitre = child.getTextContent();
//System.out.println(i+1 + "\t" + atitre);
}
else if (child.getNodeType() == Node.ELEMENT_NODE && ((Element)child).getTagName().equals("description"))
{
adescription = child.getTextContent();
//System.out.println("\t\t" + adescription);
}
else if (child.getNodeType() == Node.ELEMENT_NODE && ((Element)child).getTagName().equals("link"))
{
alink = child.getTextContent();
//System.out.println("\t\t" + alink);
}
} //fin de for
//Initialisation d'un élément à ajouter au vecteur.
MkItem anode = new MkItem(saisie, atitre, adescription,alink, false);
System.out.println(i+1 + "\t" + anode.titre );
System.out.println( "\t");
//ajout de l'élément au vecteur.
monoutil.rssvct.ajouter(anode);
}//fin de for
}
}//FIN DE RecuperationDunFlux
} //FIN DE DECLARATION DE LA CLASSE MONPARSER |
Partager