 |
|
|
OperaBookmarks.java
|
/*
* Author: Havard Rast Blok
* E-mail: 
* Web : www.rememberjava.com
*/
import java.io.*;
import javax.swing.*;
import javax.swing.tree.*;
/**
* Class for parsing and showing the Opera bookmarks file
*/
public class OperaBookmarks extends JFrame
{
String folderMark="#FOLDER";
String urlMark="#URL";
String endMark="-";
String urlTag="URL=";
String nameTag="NAME=";
public OperaBookmarks(String fileName)
{
DefaultMutableTreeNode root;
DefaultTreeModel treeModel;
JTree tree;
BufferedReader in;
root = new DefaultMutableTreeNode("Bookmarks");
try
{
//open file for reading
in = new BufferedReader( new FileReader( fileName ) );
//traverse the file
addNode( in, root );
in.close();
}
catch( IOException e )
{
System.out.println("Error reading file: "+e);
}
treeModel = new DefaultTreeModel(root);
tree = new JTree( treeModel );
getContentPane().add( tree );
setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
pack();
show();
}
private void addNode( BufferedReader in, DefaultMutableTreeNode root ) throws IOException
{
DefaultMutableTreeNode node;
String name, link;
String line;
boolean folder, url, done;
int idx;
//read and parse the file
folder = url = done = false;
name = "";
while( (line = in.readLine()) != null && !done )
{
if( line.indexOf( folderMark ) != -1 )
{
folder = true;
}
else if( line.indexOf( urlMark ) != -1 )
{
url = true;
}
else if( line.indexOf( endMark ) == 0 )
{
//backtrack to last recursive call
done = true;
}
else if( (idx = line.indexOf( nameTag )) != -1 )
{
//get the name of this record
name = line.substring( nameTag.length() + idx );
if( folder )
{
//if folder, make new node in three
node = new DefaultMutableTreeNode( name );
root.add( node );
//RECURSIVE CALL
addNode( in, node );
folder = false;
}
}
else if( (idx = line.indexOf( urlTag )) != -1 && url)
{
//get the url of this record
link = line.substring( urlTag.length() + idx );
//add record to tree
node = new DefaultMutableTreeNode( name+" ("+link+")" );
root.add( node );
url = false;
}
}
}
public static void main( String args[] )
{
if( args.length < 1 )
{
System.out.println("Usage: java OperaBookmarks <Opera bookmarks file>");
System.exit(1);
}
new OperaBookmarks(args[0]);
}
}
|
|
|
 |