LRG
LRG
Toolbar
Package variables
No package variables defined.
Synopsis
No synopsis!
Description
No description!
Methods
addEmptyNode | No description | Code |
addExisting() | No description | Code |
addNode | No description | Code |
addNodeMulti() | No description | Code |
content() | No description | Code |
countNodes() | No description | Code |
data | No description | Code |
date() | No description | Code |
empty | No description | Code |
findNode | No description | Code |
findNodeMulti | No description | Code |
findOrAdd() | No description | Code |
moveTo() | No description | Code |
name | No description | Code |
new | No description | Code |
newFromFile | No description | Code |
parent | No description | Code |
pfetch() | No description | Code |
position() | No description | Code |
printAll | No description | Code |
printNode | No description | Code |
requestContent() | No description | Code |
xml() | No description | Code |
Methods description
None available.
Methods code
sub addEmptyNode
{ my $self = shift;
my $name = shift;
my $data = shift if @_;
if(scalar keys %{$data}) {
push @{$self->{'nodes'}}, LRG::Node::newEmpty($name, $self->{'xml'}, $data);
}
else {
push @{$self->{'nodes'}}, LRG::Node::newEmpty($name, $self->{'xml'});
}
(@{$self->{'nodes'}})[-1]->{'parent'} = $self;
return (@{$self->{'nodes'}})[-1];
}
} |
sub addExisting()
{ my $self = shift;
my $new_node = shift;
$new_node->{'parent'} = $self;
$new_node->xml($self->xml) if defined $self->xml;
push @{$self->{'nodes'}}, $new_node;
}
} |
sub addNode
{ my $self = shift;
my $name = shift;
if($name =~ /\//) {
return $self->addNodeMulti($name);
}
my $data = shift if @_;
if(scalar keys %{$data}) {
push @{$self->{'nodes'}}, LRG::Node::new($name, $self->{'xml'}, $data);
}
else {
push @{$self->{'nodes'}}, LRG::Node::new($name, $self->{'xml'});
}
(@{$self->{'nodes'}})[-1]->{'parent'} = $self;
return (@{$self->{'nodes'}})[-1]; } |
sub addNodeMulti()
{ my $self = shift;
my $name = shift;
my @levels = split /\s*\/\s*/, $name;
if(scalar @levels == 1) {
return $self->addNode($name);
}
else {
my $current = $self;
while(@levels) {
my $level = shift @levels;
if(scalar @levels >= 1) {
$current = $current->addNode($level);
}
else {
return $current->addNode($level);
}
}
}
}
} |
sub content()
{ my $self = shift;
$self->{'content'} = shift if @_;
return $self->{'content'};
}
} |
sub countNodes()
{ my $self = shift @_;
return scalar @{$self->parent->{'nodes'}};
}
} |
sub data
{ my $self = shift;
$self->{'data'} = shift if @_;
return $self->{'data'};
}
} |
sub date()
{ my @time = localtime(time());
$time[4]++;
for my $i(0..4) {
$time[$i] = "0".$time[$i] if $time[$i] < 10;
}
my $time = ($time[5] + 1900)."-".$time[4]."-".$time[3];
return $time;
}
} |
sub empty
{ my $self = shift;
$self->{'empty'} = shift if @_;
return $self->{'empty'};
}
} |
sub findNode
{ my $self = shift;
my $name = shift;
my $data = shift if @_;
return $self->findNodeMulti($name, $data) if $name =~ /\//;
my $found;
my $match;
foreach my $node(@{$self->{'nodes'}}) {
if(defined $node->name && defined $name && $node->name eq $name) {
$match = 1;
if(scalar keys %$data && scalar keys %{$node->data}) {
$match = 0;
my $needed = scalar keys %$data;
foreach my $key(keys %$data) {
next unless defined $node->data->{$key};
$match++ if $node->data->{$key} eq $data->{$key};
}
$match = ($match == $needed ? 1 : 0);
}
if($match) {
$found = $node;
last;
}
}
last if defined $found;
$found = $node->findNode($name, $data);
}
return $found;
}
} |
sub findNodeMulti
{ my $self = shift;
my $name = shift;
my $data = shift if @_;
my @levels = split /\s*\/\s*/, $name;
if(scalar @levels == 1) {
return $self->findNode($name, $data);
}
else {
my $current = $self;
while(@levels) {
my $level = shift @levels;
if(scalar @levels >= 1) {
$current = $current->findNode($level);
}
else {
$current = $current->findNode($level, $data);
}
}
return $current;
}
}
} |
sub findOrAdd()
{ my $self = shift;
my $name = shift;
my $find = $self->findNode($name);
if(defined $find) {
return $find;
}
else {
return $self->addNode($name);
}
}
} |
sub moveTo()
{ my $self = shift;
my $num = (@_ ? shift : 1);
my $pos = $self->position();
my @nodes = @{$self->parent->{'nodes'}};
my $last_index = $#nodes;
my @before = ($pos - 1 >= 0 ? @nodes[0..($pos-1)] : ());
my @after = ($pos + 1 <= $last_index ? @nodes[($pos+1)..$last_index] : ());
@nodes = (@before, @after);
$last_index = $#nodes;
@before = ($num - 1 >= 0 ? @nodes[0..($num-1)] : ());
@after = ($num <= $last_index ? @nodes[$num..$last_index] : ());
@nodes = (@before, $self, @after);
$self->parent->{'nodes'} =\@ nodes;
}
} |
sub name
{ my $self = shift;
$self->{'name'} = shift if @_;
return $self->{'name'};
}
} |
sub new
{ my $file = shift if @_;
my $lrg;
if(defined $file) {
my $output = new IO::File(">$file") or die "Could not write to file $file\n";
$lrg->{'xml'} = new XML::Writer(OUTPUT => $output, DATA_INDENT => 2, DATA_MODE => 1);
}
else {
$lrg->{'xml'} = new XML::Writer(DATA_INDENT => 2, DATA_MODE => 1);
}
$lrg->{'nodes'} = ();
$lrg->{'name'} = 'LRG_ROOT_NODE';
bless $lrg, 'LRG::LRG';
return $lrg;
}
} |
sub newFromFile
{ my $file = shift;
my $outfile = shift if @_;
my $lrg = LRG::LRG::new(defined $outfile ? $outfile : undef);
my $current = $lrg;
my $name;
open IN, $file or die("Could not read from file $file\n");
my $xml_string = '';
while(<IN>) {
chomp;
s/^\s+//g;
s/\s+$//g;
next if /^\<\?/;
$xml_string .= $_;
}
close IN;
$xml_string =~ s/\r+//g;
$xml_string =~ s/\n+//g;
my $prev_end = 0;
while($xml_string =~ m/<.+?>/g) {
# get the matched string and details about it my $string = $&; my $length = length($string);
my $pos = pos($xml_string);
my $start = $pos - $length;
my $end = $pos;
if($prev_end >= 1 && $start - $prev_end > 1) {
my $temp = substr($xml_string, $prev_end, ($start - $prev_end));
if($temp =~ /\w+/) {
$current->content($temp);
}
}
$prev_end = $end;
$string =~ s/\<|\>//g;
if($string =~ /^\//) {
$current = $current->parent;
}
else {
my @split = split /\s+/, $string;
$name = shift @split;
if(scalar @split) {
$string = join " ", @split;
$string =~ s/\s*\=\s*/\=/;
@split = split /\s+|\=/, $string;
my %data = ();
while(@split) {
my $key = shift @split;
my $val = shift @split;
$val =~ s/\"|\'//g;
$val =~ s/\/$//g;
$data{$key} = $val;
}
if($string =~ /\/$/) {
$current = $current->addEmptyNode($name,\% data);
$current = $current->parent;
}
else {
$current = $current->addNode($name,\% data);
}
}
else {
if($string =~ /\/$/) {
$current = $current->addEmptyNode($name);
$current = $current->parent;
}
else {
$current = $current->addNode($name);
}
}
}
}
return $lrg;
}
} |
sub parent
{ my $self = shift;
return $self->{'parent'} if defined $self->{'parent'};
}
} |
sub pfetch()
{ my $self = shift;
my $id = shift;
my $sequence;
open IN, "pfetch $id |";
while(<IN>) {
next if /^\>/;
chomp;
$sequence .= $_;
}
close IN;
return $sequence;
}
package LRG::Node;
our @ISA = "LRG::LRG"; } |
sub position()
{ my $self = shift;
my $to = shift if @_;
$self->moveTo($to) if defined $to;
my $pos;
for my $i(0..(scalar @{$self->parent->{'nodes'}} - 1)) {
if($self eq $self->parent->{'nodes'}->[$i]) {
$pos = $i;
last;
}
}
return $pos;
}
} |
sub printAll
{ my $self = shift;
$self->{'xml'}->xmlDecl('UTF-8');
$self->{'xml'}->pi('xml-stylesheet', 'type="text/xsl href="lrg2html.xsl"');
foreach my $node(@{$self->{'nodes'}}) {
$node->printNode();
}
$self->{'xml'}->end();
}
} |
sub printNode
{ my $self = shift;
if($self->empty) {
if(scalar keys %{$self->data}) {
$self->{'xml'}->emptyTag($self->name, %{$self->data});
}
else {
$self->{'xml'}->emptyTag($self->name);
}
}
elsif(scalar keys %{$self->data}) {
$self->{'xml'}->startTag($self->name, %{$self->data});
}
else {
$self->{'xml'}->startTag($self->name);
}
if(defined $self->{'content'}) {
$self->{'xml'}->characters($self->content);
}
foreach my $node(@{$self->{'nodes'}}) {
$node->printNode();
}
$self->{'xml'}->endTag() unless $self->{'empty'};
}
} |
sub requestContent()
{ my $self = shift;
my $input;
foreach my $item(@_) {
print "Input $item\> ";
$input = <STDIN>;
chomp $input;
$self->addNode($item)->content($input);
}
}
} |
sub xml()
{ my $self = shift;
my $xml = shift if @_;
if(defined $xml) {
$self->{'xml'} = $xml;
foreach my $node(@{$self->{'nodes'}}) {
$node->xml($xml);
}
}
return $self->{'xml'};
}
} |
General documentation
No general documentation available.