XML::XBEL - OOP for reading and writing XBEL documents.


XML-XBEL documentation Contained in the XML-XBEL distribution.

Index


Code Index:

NAME

Top

XML::XBEL - OOP for reading and writing XBEL documents.

SYNOPSIS

Top

 # creating an XBEL document

 use XML::XBEL;
 use XML::XBEL::Folder;
 use XML::XBEL::Bookmark;

 my $xbel = XML::XBEL->new();
 $xbel->new_document({title=>"My Bookmarks"});

 $xbel->add_bookmark({href  => "http://foo.com",
	 	      title => "foo",
		      desc  => "bar"});

 my $folder1 = XML::XBEL::Folder->new({title => "comp"});
 my $folder2 = XML::XBEL::Folder->new({title => "lang"});
 my $folder3 = XML::XBEL::Folder->new({title => "perl"});

 my $bm = XML::XBEL::Bookmark->new({"title=>"misc"});
 $bm->href("http://groups.google.com/groups?q=comp.lang.perl.misc");

 $folder3->add_bookmark($bm);
 $folder2->add_folder($folder3);
 $folder1->add_folder($folder2);

 $xbel->add_folder($folder1);

 print $xbel->toString();

 # parsing an XBEL document

 use XML::XBEL;

 my $xbel = XML::XBEL->new();
 $xbel->parse_file($file);

 foreach my $bm ($xbel->bookmarks()) {

     print sprintf("%s points to %s\n",
		   $bm->title(),
		   $bm->href());
 } 

DESCRIPTION

Top

OOP for reading and writing XBEL files.

PACKAGE METHODS

Top

__PACKAGE__->new()

Returns an XML::XBEL object.

OBJECT METHODS

Top

$self->parse_file($file)

Returns true or false.

$self->parse_string($string)

Returns true or false.

$obj->new_document(\%args)

Valid arguments are :

* title

String.

* desc

String.

* info

Hash ref, with the following key/value pairs :

* owner

Array ref.

Returns true or false.

$obj->title($title)

Get/set the title for an XBEL document.

Returns a string when called with no arguments; otherwise returns true or false.

$obj->desc($description)

Get/set the description for an XBEL document.

Returns a string when called with no arguments; otherwise returns true or false.

$obj->info(\%args)

Get/set the metadata for an XBEL document.

Valid args are :

* owner

Array reference

Returns an array reference when called with no arguments; otherwise returns true or false.

$obj->bookmarks($recursive)

Returns a list of child XML::XBEL::Bookmark objects.

Where $recursive is a boolean indicating whether to return all the bookmarks in an XBEL document or only its immediate children.

$obj->folders($recursive)

Returns a list of child XML::XBEL::Folder objects.

Where $recursive is a boolean indicating whether to return all the folders in an XBEL document or only its immediate children.

$obj->aliases($recursive)

Returns a list of child XML::XBEL::Alias objects.

Where $recursive is a boolean indicating whether to return all the aliases in an XBEL document or only its immediate children.

$obj->find_by_id($id)

Returns an XML::XBEL::Bookmark or XML::XBEL::Folder object whose id attribute matches $id.

$obj->find_by_href($href)

Returns a list of XML::XBEL::Bookmark objects whose href attribute matches $href.

$obj->add_bookmark((XML::XBEL::Bookmark || \%args))

Add a new bookmark to an XBEL document.

If passed a hash ref, valid arguments are the same as those defined for the XML::XBEL::Bookmark object constructor.

$obj->add_folder((XML::XBEL::Folder || \%args))

Add a new folder to an XBEL document.

If passed a hash ref, valid arguments are the same as those defined for the XML::XBEL::Folder object constructor.

$obj->add_alias((XML::XBEL::Alias || \%args))

Add a new alias to an XBEL document.

If passed a hash ref, valid arguments are the same as those defined for the XML::XBEL::Alias object constructor.

$obj->add_separator()

Add a new separator to an XBEL document.

$obj->toString($format)

$obj->toFile($filename,$format)

$obj->toFH(\*$fh,$format)

$obj->toSAX(A::SAX::Handler)

Generate SAX events for the XBEL object.

VERSION

Top

1.4

DATE

Top

$Date: 2005/04/02 20:54:52 $

AUTHOR

Top

Aaron Straup Cope <ascope@cpan.org>

SEE ALSO

Top

XML::XBEL::Folder

XML::XBEL::Bookmark

XML::XBEL::Alias

XML::XBEL::Separator

BUGS

Top

It's possible. Please report all bugs via http://rt.cpan.org

LICENSE

Top

Copyright (c) 2004 Aaron Straup Cope. All rights reserved.

This is free software, you may use it and distribute it under the same terms as Perl itself.


XML-XBEL documentation Contained in the XML-XBEL distribution.
use strict;
package XML::XBEL;

use base qw (XML::XBEL::item
	     XML::XBEL::container);

# $Id: XBEL.pm,v 1.9 2005/04/02 20:54:52 asc Exp $

$XML::XBEL::VERSION = '1.4';

use XML::LibXML;

sub new {
    my $pkg  = shift;

    return bless {'__doc'  => undef,
		  '__root' => undef } , $pkg;
}

sub parse_file {
    my $self = shift;
    my $file = shift;

    my $parser = XML::LibXML->new();
    my $doc = $parser->parse_file($file);

    return $self->_parse($doc);
}

sub parse_string {
    my $self = shift;
    my $str  = shift;

    my $parser = XML::LibXML->new();
    my $doc = $parser->parse_string($str);

    return $self->_parse($doc);
}

sub _parse {
    my $self = shift;
    my $doc  = shift;

    $self->{'__doc'}  = $doc;
    $self->{'__root'} = $doc->documentElement();

    return 1;
}

sub new_document {
    my $self = shift;
    my $args = shift;

    my $doc = XML::LibXML::Document->new();

    if ($args->{encoding}) {
	$doc->setEncoding($args->{encoding});
    }

    my $root = XML::LibXML::Element->new("xbel");
    $doc->setDocumentElement($root);

    $self->{'__doc'}  = $doc;
    $self->{'__root'} = $root;

    foreach my $el ("title","desc","info") {

	if (! exists($args->{$el})) {
	    next;
	}

	$self->$el($args->{$el});
    }

    return 1;
}

# Defined in XML::XBEL::item

# Defined in XML::XBEL::item

# Defined in XML::XBEL::info

# Defined in XML::XBEL::container

# Defined in XML::XBEL::container

# Defined in XML::XBEL::container

sub find_by_id {
    my $self = shift;
    my $id   = shift;

    my $node = ($self->{'__root'}->findnodes("//child::*[\@id='$id']"))[0];

    # print sprintf("%s %s\n",$node,$node->nodeName());

    if (! $node) {
	return undef;
    }

    elsif ($node->nodeName() eq "folder") {
	require XML::XBEL::Folder;
	return XML::XBEL::Folder->build_node($node);
    }

    elsif ($node->nodeName() eq "bookmark") {
	require XML::XBEL::Bookmark;
	return XML::XBEL::Bookmark->build_node($node);
    }

    else {
	return undef;
    }
}

sub find_by_href {
    my $self = shift;
    my $href = shift;

    my @nodes = $self->{'__root'}->findnodes("//child::*[name()='bookmark' and \@href='$href']");

    if (! @nodes) {
	return undef;
    }

    require XML::XBEL::Bookmark;
	
    return map { 
	XML::XBEL::Bookmark->build_node($_);
    } @nodes
}

# Defined in XML::XBEL::container

# Defined in XML::XBEL::container

# Defined in XML::XBEL::container

# Defined in XML::XBEL::container

sub toString {
    my $self = shift;
    $self->{'__doc'}->toString(@_);
}

sub toFile {
    my $self = shift;
    $self->{'__doc'}->toString(@_);
}

sub toFH {
    my $self = shift;
    $self->{'__doc'}->toString(@_);
}

sub toSAX {
    my $self    = shift;
    my $handler = shift;

    require XML::LibXML::SAX::Parser;
    my $gen = XML::LibXML::SAX::Parser->new(Handler => $handler);
    $gen->generate($self->{'__doc'});
}

return 1;