3 # This file is part of Stem.
4 # Copyright (C) 1999, 2000, 2001 Stem Systems, Inc.
6 # Stem is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # Stem is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with Stem; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 # For a license to use the Stem under conditions other than those
21 # described here, to purchase support for this software, or to purchase a
22 # commercial warranty contract, please contact Stem Systems at:
24 # Stem Systems, Inc. 781-643-7504
25 # 79 Everett St. info@stemsystems.com
39 'default' => 'Data::Dumper',
41 This is the name of the Codec:: subclass that will be used in this cell
48 If an object is passed in, the filter will use it for callbacks
53 'name' => 'encode_method',
54 'default' => 'encoded_data',
60 'name' => 'decode_method',
61 'default' => 'decoded_data',
70 # This POD section is autoegenerated. Any edits to it will be lost.
72 =head2 Constructor Attributes for Class Stem::Codec::Data::Dumper
77 =item * Attribute - B<object>
83 If an object is passed in, the filter will use it for callbacks
86 =item Its B<type> is: object
90 =item * Attribute - B<encode_method>
95 =item It B<defaults> to: encoded_data
99 =item * Attribute - B<decode_method>
104 =item It B<defaults> to: decoded_data
112 # End of autogenerated POD
119 my( $class ) = shift ;
121 my $self = Stem::Class::parse_args( $attr_spec, @_ ) ;
122 return $self unless ref $self ;
124 my $err = $self->load_codec() ;
125 return $err if $err ;
134 my $codec = $self->{codec} ;
136 return if $loaded_codecs{ $codec } ;
138 my $codec_class = "Stem::Codec::$codec" ;
140 eval "require $codec_class" ;
142 return "Can't load Stem codec '$codec_class' $@" if $@ ;
144 $loaded_codecs{ $codec } = {
146 encoder => $codec_class->make_encoder(),
147 decoder => $codec_class->make_decoder(),
159 my $encoder = $loaded_codecs{ $self->{codec} }{encoder} ;
161 # make sure scalars and scalar refs have a ref taken to them as codecs
162 # always take a ref. we do ref on scalar refs so we can tell at decode
163 # time that REF is a scalar ref but SCALAR is a plain scalar
165 #print "IN $_[0] REF ", ref $_[0], "\n" ;
167 my $data_ref = ( ! ref $_[0] || ref $_[0] eq 'SCALAR' ) ?
170 #print "DATA REF $data_ref\n" ;
172 my $encoded_text = $encoder->( $data_ref ) ;
174 if ( my $obj = $self->{'object'} ) {
176 my $method = $self->{'encode_method'} ;
177 $obj->$method( $encoded_text ) ;
180 return $encoded_text ;
187 my $decoder = $loaded_codecs{ $self->{codec} }{decoder} ;
189 my $decoded_data = $decoder->( $_[0] ) ;
191 $decoded_data = ${$decoded_data} if
192 ref $decoded_data eq 'SCALAR' ||
193 ref $decoded_data eq 'REF' ;
195 if ( my $obj = $self->{'object'} ) {
197 my $method = $self->{'decode_method'} ;
198 $obj->$method( $decoded_data ) ;
201 return( $decoded_data ) ;