| 1 | package Boulder::String; | ||
| 2 | use Boulder::Stream; | ||
| 3 | @ISA = 'Boulder::Stream'; | ||
| 4 | |||
| 5 | $DATE="4 Dec 2000"; | ||
| 6 | $VERSION=1.01; | ||
| 7 | |||
| 8 | =head1 NAME | ||
| 9 | |||
| 10 | Boulder::String - Read and write tag/value data from a string. | ||
| 11 | |||
| 12 | =head1 SYNOPSIS | ||
| 13 | |||
| 14 | #!/bin/perl | ||
| 15 | # Read a B<Stone> from stdin and create a string that can be | ||
| 16 | # passed to a dumb sub, which doesn't know about Stones. | ||
| 17 | use Boulder::Stream; | ||
| 18 | use Boulder::String; | ||
| 19 | |||
| 20 | my $stream = Boulder::Stream->newFh; | ||
| 21 | |||
| 22 | # read a stone from stdin | ||
| 23 | my $record = <$stream> ); | ||
| 24 | |||
| 25 | print $stream $record; | ||
| 26 | } | ||
| 27 | |||
| 28 | =head1 DESCRIPTION | ||
| 29 | |||
| 30 | Boulder::String provides access to L<Boulder> IO | ||
| 31 | hierarchical tag/value data. | ||
| 32 | Stone objects printed to the tied string | ||
| 33 | are appended to the string in L<Boulder> format. | ||
| 34 | |||
| 35 | =head1 Boulder::Stream METHODS | ||
| 36 | |||
| 37 | =head2 $stream = Boulder::Stream->new($in_string,\$out_string); | ||
| 38 | |||
| 39 | The B<new()> method creates a new B<Boulder::String> object. You must | ||
| 40 | provide an input string and a reference to an output string. | ||
| 41 | The input string may be empty. | ||
| 42 | |||
| 43 | =head2 $stream->write_record($stone) | ||
| 44 | |||
| 45 | Write the passed stone in L<Boulder> IO format into $out_string. | ||
| 46 | |||
| 47 | =head1 AUTHOR | ||
| 48 | |||
| 49 | Lincoln D. Stein <lstein@cshl.org>, Cold Spring Harbor Laboratory, | ||
| 50 | Cold Spring Harbor, NY. This module can be used and distributed on | ||
| 51 | the same terms as Perl itself. | ||
| 52 | |||
| 53 | Patches and bug fixes contributed by Bernhard Schmalhofer | ||
| 54 | <bernhard@biomax.de>. | ||
| 55 | |||
| 56 | =head1 SEE ALSO | ||
| 57 | |||
| 58 | L<Boulder>, L<Boulder::Stream>, | ||
| 59 | L<Boulder::Blast>, L<Boulder::Genbank>, L<Boulder::Medline>, L<Boulder::Unigene>, | ||
| 60 | L<Boulder::Omim>, L<Boulder::SwissProt> | ||
| 61 | |||
| 62 | =cut | ||
| 63 | |||
| 64 | # Override Stream.pm to allow the input and output to be | ||
| 65 | # strings. If outString is not defined, then we fall back | ||
| 66 | # to Boulder::Stream behavior, otherwise we append to the | ||
| 67 | # indicated string. | ||
| 68 | sub new { | ||
| 69 | my($package,$inString,$outStringRef) = @_; | ||
| 70 | die "Usage: Boulder::String::new(\$inString,\\\$outString)\n" | ||
| 71 | unless defined($inString) && !ref($inString); | ||
| 72 | die "Usage: Boulder::String::new(\$inString,\\\$outString)\n" | ||
| 73 | if defined($outStringRef) && (ref($outStringRef) ne 'SCALAR'); | ||
| 74 | |||
| 75 | return bless { | ||
| 76 | 'IN'=>undef, | ||
| 77 | 'OUT'=>undef, | ||
| 78 | 'INSTRING'=>$inString, | ||
| 79 | 'OUTSTRING'=>$outStringRef, | ||
| 80 | 'delim'=>'=', | ||
| 81 | 'record_stop'=>"=\n", | ||
| 82 | 'line_end'=>"\n", | ||
| 83 | 'subrec_start'=>"\{", | ||
| 84 | 'subrec_end'=>"\}", | ||
| 85 | 'binary'=>'true', | ||
| 86 | 'passthru'=>undef | ||
| 87 | },$package; | ||
| 88 | } | ||
| 89 | |||
| 90 | # Write out the specified Stone record. | ||
| 91 | sub write_record { | ||
| 92 | my($self,$stone)=@_; | ||
| 93 | my $out = $self->{OUTSTRING}; | ||
| 94 | return unless $out; | ||
| 95 | |||
| 96 | $self->{'WRITE'}++; | ||
| 97 | |||
| 98 | # Write out a Stone record in boulder format. | ||
| 99 | my ($key,$value,@value); | ||
| 100 | foreach $key ($stone->tags) { | ||
| 101 | @value = $stone->get($key); | ||
| 102 | $key = $self->escapekey($key); | ||
| 103 | foreach $value (@value) { | ||
| 104 | next unless ref $value; | ||
| 105 | if (exists $value->{'.name'}) { | ||
| 106 | $value = $self->escapeval($value); | ||
| 107 | $$out .= "$key$self->{delim}$value\n"; | ||
| 108 | } else { | ||
| 109 | $$out .= "$key$self->{delim}$self->{subrec_start}\n"; | ||
| 110 | _write_nested($self,1,$value); | ||
| 111 | } | ||
| 112 | } | ||
| 113 | } | ||
| 114 | ${$self->{OUTSTRING}} .= "$self->{delim}\n"; | ||
| 115 | } | ||
| 116 | |||
| 117 | #-------------------------------------- | ||
| 118 | # Internal (private) procedures. | ||
| 119 | #-------------------------------------- | ||
| 120 | # This finds an array of key/value pairs and | ||
| 121 | # stashes it where we can find it. | ||
| 122 | sub read_next_rec { | ||
| 123 | my($self) = @_; | ||
| 124 | unless (defined($self->{RECORDS})) { | ||
| 125 | $self->{RECORDS} = [split("\n$self->{record_stop}",$self->{INSTRING})]; | ||
| 126 | } | ||
| 127 | my($nextrec) = shift(@{$self->{RECORDS}}); | ||
| 128 | $self->{PAIRS}=[grep($_,split($self->{'line_end'},$nextrec))]; | ||
| 129 | } | ||
| 130 | |||
| 131 | # This returns TRUE when we've reached the end | ||
| 132 | # of the input stream | ||
| 133 | sub done { | ||
| 134 | my $self = shift; | ||
| 135 | return undef if @{$self->{PAIRS}}; | ||
| 136 | return undef unless ref($self->{RECORDS}); | ||
| 137 | return !scalar(@{$self->{RECORDS}}); | ||
| 138 | } | ||
| 139 | |||
| 140 | sub _write_nested { | ||
| 141 | my($self,$level,$stone) = @_; | ||
| 142 | my $indent = ' ' x $level; | ||
| 143 | my($key,$value,@value); | ||
| 144 | my $out = $self->{OUTSTRING}; | ||
| 145 | return unless ref($out); | ||
| 146 | |||
| 147 | foreach $key ($stone->tags) { | ||
| 148 | @value = $stone->get($key); | ||
| 149 | $key = $self->escapekey($key); | ||
| 150 | foreach $value (@value) { | ||
| 151 | next unless ref $value; | ||
| 152 | if (exists $value->{'.name'}) { | ||
| 153 | $value = $self->escapeval($value); | ||
| 154 | $$out .= "$indent$key$self->{delim}$value\n"; | ||
| 155 | } else { | ||
| 156 | $$out .= "$indent$key$self->{delim}$self->{subrec_start}\n"; | ||
| 157 | _write_nested($self,$level+1,$value); | ||
| 158 | } | ||
| 159 | } | ||
| 160 | } | ||
| 161 | |||
| 162 | $$out .= (' ' x ($level-1)) . "$self->{'subrec_end'}\n"; | ||
| 163 | } | ||
| 164 | |||
| 165 | sub DESTROY { | ||
| 166 | my $self = shift; | ||
| 167 | $out=$self->{OUTSTRING}; | ||
| 168 | if (ref($out) && !$self->{WRITE} && $self->{INVOKED} && | ||
| 169 | !$self->{LEVEL} && $self->{'passthru'} && $self->{PASSED}) { | ||
| 170 | $$out .= "$self->{'delim'}\n"; | ||
| 171 | } | ||
| 172 | } | ||
| 173 | |||
| 174 | |||
| 175 | 1; | ||
| 176 |