|| tag.equals (HTML.Tag.TD)) // Tag
{
attribute = (String)att.getAttribute (HTML.Attribute.BACKGROUND);
if (attribute != null)
addURL (attribute);
}
}
// Methode appelee quand un tag simple est lu
public void handleSimpleTag (HTML.Tag tag, MutableAttributeSet att, int pos)
{
String attribute;
if ( tag.equals (HTML.Tag.FRAME) // Tag
|| tag.equals (HTML.Tag.IMG)) // Tag
{
attribute = (String)att.getAttribute (HTML.Attribute.SRC);
if (attribute != null)
addURL (attribute);
}
else if (tag.equals (HTML.Tag.AREA)) // Tag
{
attribute = (String)att.getAttribute (HTML.Attribute.HREF);
if (attribute != null)
addURL (attribute);
}
}
// HTMLEditorKit.ParserCallback definit aussi d'autres methodes
// qu'il est inutile d'outrepasser ici (tag de fin, commentaires,
// corps de texte,...)
}
// Methode main () d'exemple de mise en oeuvre, prend en argument
// un nom de fichier HTML sous forme d'URL, par exemple :
// java HTMLDocumentLinks file:/disk/dir1/index.html
public static void main (String args [])
{
try
{
// Ouverture du fichier contenu dans l'argument 0
URL fileURL = new URL (args [0]);
Reader urlReader = new BufferedReader (
new InputStreamReader (fileURL.openStream ()));
// Creation d'une instance de parser
HTMLDocumentLinks doc = new HTMLDocumentLinks (fileURL);
// Parsing du fichier HTML avec Swing
new HTMLEditorKit ().read (urlReader, doc, 0);
urlReader.close ();
// Listing des ancres et des URLs trouvees dans le fichier
System.out.println ("Liste des ancres :");
for (Enumeration e = doc.getAnchors ().elements ();
e.hasMoreElements (); )
System.out.println (e.nextElement ());
System.out.println ("\nListe des URLs :");
for (Enumeration e = doc.getURLs ().elements ();
e.hasMoreElements (); )
System.out.println (e.nextElement ());
}
catch (IOException e)
{
System.out.println ("Probleme d'acces a l'URL : " + args [0]);
}
catch (BadLocationException e)
{ }
}
}
|