| 1 | #!/usr/bin/perl -w | ||||||||||||||
| 2 | # | ||||||||||||||
| 3 | # $Id: String.pm,v 1.1.1.1 2003/10/28 16:04:38 andy Exp $ | ||||||||||||||
| 4 | # | ||||||||||||||
| 5 | # This code is copyright 1999-2000 by Scott Guelich <scott@scripted.com> | ||||||||||||||
| 6 | # and is distributed according to the same conditions as Perl itself | ||||||||||||||
| 7 | # Please visit http://www.scripted.com/wddx/ for more information | ||||||||||||||
| 8 | # | ||||||||||||||
| 9 | |||||||||||||||
| 10 | package WDDX::String; | ||||||||||||||
| 11 | |||||||||||||||
| 12 | # Auto-inserted by build scripts | ||||||||||||||
| 13 | $VERSION = "1.01"; | ||||||||||||||
| 14 | |||||||||||||||
| 15 | 1 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 16 | 1 | ▶ | use Carp; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 17 | |||||||||||||||
| 18 | require WDDX; | ||||||||||||||
| 19 | |||||||||||||||
| 20 | { my $i_hate_the_w_flag_sometimes = [ | ||||||||||||||
| 21 | $WDDX::PACKET_HEADER, | ||||||||||||||
| 22 | $WDDX::PACKET_FOOTER, | ||||||||||||||
| 23 | $WDDX::String::VERSION | ||||||||||||||
| 24 | ] } | ||||||||||||||
| 25 | |||||||||||||||
| 26 | 1; | ||||||||||||||
| 27 | |||||||||||||||
| 28 | |||||||||||||||
| 29 | #/----------------------------------------------------------------------- | ||||||||||||||
| 30 | # Public Methods | ||||||||||||||
| 31 | # | ||||||||||||||
| 32 | |||||||||||||||
| 33 | 0 | ▶ | sub new { | ||||||||||||
|
Subroutine
new: not called CC=3 SCAR=24.8
| |||||||||||||||
| 34 | 0 | my( $class, $value ) = @_; | |||||||||||||
| 35 | |||||||||||||||
| 36 | 0 | ▶ | croak "You must supply a value when creating a new $class object\n" | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 37 | unless defined $value; | ||||||||||||||
| 38 | |||||||||||||||
| 39 | 0 | ▶ | croak "WDDX strings may not contain null characters (\\0)\n" | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 40 | if $value =~ /\0/; | ||||||||||||||
| 41 | |||||||||||||||
| 42 | 0 | my $self = { | |||||||||||||
| 43 | value => $value, | ||||||||||||||
| 44 | }; | ||||||||||||||
| 45 | |||||||||||||||
| 46 | 0 | bless $self, $class; | |||||||||||||
| 47 | 0 | return $self; | |||||||||||||
| 48 | } | ||||||||||||||
| 49 | |||||||||||||||
| 50 | |||||||||||||||
| 51 | 0 | ▶ | sub type { | ||||||||||||
|
Subroutine
type: not called CC=1 SCAR=6.9
| |||||||||||||||
| 52 | 0 | return "string"; | |||||||||||||
| 53 | } | ||||||||||||||
| 54 | |||||||||||||||
| 55 | |||||||||||||||
| 56 | 0 | ▶ | sub as_packet { | ||||||||||||
|
Subroutine
as_packet: not called CC=1 SCAR=6.9
| |||||||||||||||
| 57 | 0 | my( $self ) = @_; | |||||||||||||
| 58 | 0 | my $output = $WDDX::PACKET_HEADER . | |||||||||||||
| 59 | $self->_serialize . | ||||||||||||||
| 60 | $WDDX::PACKET_FOOTER; | ||||||||||||||
| 61 | } | ||||||||||||||
| 62 | |||||||||||||||
| 63 | |||||||||||||||
| 64 | 0 | ▶ | sub as_scalar { | ||||||||||||
|
Subroutine
as_scalar: not called CC=1 SCAR=6.9
| |||||||||||||||
| 65 | 0 | my( $self ) = @_; | |||||||||||||
| 66 | 0 | return $self->_deserialize; | |||||||||||||
| 67 | } | ||||||||||||||
| 68 | |||||||||||||||
| 69 | |||||||||||||||
| 70 | 0 | ▶ | sub as_javascript { | ||||||||||||
|
Subroutine
as_javascript: not called CC=1 SCAR=6.9
| |||||||||||||||
| 71 | 0 | my( $self, $js_var ) = @_; | |||||||||||||
| 72 | 0 | local $_ = $self->{value}; | |||||||||||||
| 73 | |||||||||||||||
| 74 | # Escape for JavaScript... forget anything? | ||||||||||||||
| 75 | 0 | s/\\/\\\\/g; | |||||||||||||
| 76 | 0 | s/\n/\\n/g; | |||||||||||||
| 77 | 0 | s/\r/\\r/g; | |||||||||||||
| 78 | 0 | s/\t/\\t/g; | |||||||||||||
| 79 | 0 | s/"/\\"/g; | |||||||||||||
| 80 | |||||||||||||||
| 81 | 0 | return "$js_var=\"$_\";"; | |||||||||||||
| 82 | } | ||||||||||||||
| 83 | |||||||||||||||
| 84 | |||||||||||||||
| 85 | #/----------------------------------------------------------------------- | ||||||||||||||
| 86 | # Private Methods | ||||||||||||||
| 87 | # | ||||||||||||||
| 88 | |||||||||||||||
| 89 | 0 | ▶ | sub is_parser { | ||||||||||||
|
Subroutine
is_parser: not called CC=1 SCAR=6.9
| |||||||||||||||
| 90 | 0 | return 0; | |||||||||||||
| 91 | } | ||||||||||||||
| 92 | |||||||||||||||
| 93 | |||||||||||||||
| 94 | 0 | ▶ | sub _serialize { | ||||||||||||
|
Subroutine
_serialize: not called CC=1 SCAR=6.9
| |||||||||||||||
| 95 | 0 | my( $self ) = @_; | |||||||||||||
| 96 | 0 | my $val = $self->xml_encode; | |||||||||||||
| 97 | 0 | my $output = "<string>$val</string>"; | |||||||||||||
| 98 | |||||||||||||||
| 99 | 0 | return $output; | |||||||||||||
| 100 | } | ||||||||||||||
| 101 | |||||||||||||||
| 102 | |||||||||||||||
| 103 | 0 | ▶ | sub _deserialize { | ||||||||||||
|
Subroutine
_deserialize: not called CC=1 SCAR=6.9
| |||||||||||||||
| 104 | 0 | my( $self ) = @_; | |||||||||||||
| 105 | 0 | return $self->{value}; | |||||||||||||
| 106 | } | ||||||||||||||
| 107 | |||||||||||||||
| 108 | |||||||||||||||
| 109 | 0 | ▶ | sub xml_encode { | ||||||||||||
|
Subroutine
xml_encode: not called CC=1 SCAR=6.9
| |||||||||||||||
| 110 | 0 | my $self = shift; | |||||||||||||
| 111 | 0 | local $_ = $self->{value}; | |||||||||||||
| 112 | |||||||||||||||
| 113 | 0 | s/&/&/g; | |||||||||||||
| 114 | 0 | s/</</g; | |||||||||||||
| 115 | 0 | s/>/>/g; | |||||||||||||
| 116 | 0 | s/'/'/g; | |||||||||||||
| 117 | 0 | s/"/"/g; | |||||||||||||
| 118 | 0 | s|([\000-\037])|<char code='@{[ sprintf "%02X", ord($1) ]}'/>|g; | |||||||||||||
| 119 | 0 | return $_; | |||||||||||||
| 120 | } | ||||||||||||||
| 121 | |||||||||||||||
| 122 | #/----------------------------------------------------------------------- | ||||||||||||||
| 123 | # Parsing Code | ||||||||||||||
| 124 | # | ||||||||||||||
| 125 | |||||||||||||||
| 126 | package WDDX::String::Parser; | ||||||||||||||
| 127 | |||||||||||||||
| 128 | |||||||||||||||
| 129 | 0 | ▶ | sub new { | ||||||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
| |||||||||||||||
| 130 | 0 | return bless { value => "" }, shift; | |||||||||||||
| 131 | } | ||||||||||||||
| 132 | |||||||||||||||
| 133 | |||||||||||||||
| 134 | 0 | ▶ | sub start_tag { | ||||||||||||
|
Subroutine
start_tag: not called CC=3 SCAR=24.8
| |||||||||||||||
| 135 | 0 | my( $self, $element, $attribs ) = @_; | |||||||||||||
| 136 | |||||||||||||||
| 137 | 0 | ▶ | if ( $element eq "char" ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 138 | 0 | $self->append_data( $self->char_decode( $attribs->{code} ) ); | |||||||||||||
| 139 | } | ||||||||||||||
| 140 | elsif ( $element ne "string" ) { | ||||||||||||||
| 141 | 0 | die "<$element> not allowed within <string> element\n"; | |||||||||||||
| 142 | } | ||||||||||||||
| 143 | |||||||||||||||
| 144 | 0 | return $self; | |||||||||||||
| 145 | } | ||||||||||||||
| 146 | |||||||||||||||
| 147 | |||||||||||||||
| 148 | 0 | ▶ | sub end_tag { | ||||||||||||
|
Subroutine
end_tag: not called CC=3 SCAR=24.8
| |||||||||||||||
| 149 | 0 | my( $self, $element ) = @_; | |||||||||||||
| 150 | |||||||||||||||
| 151 | 0 | ▶ | if ( $element eq "string" ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 152 | 0 | $self = new WDDX::String( $self->{value} ); | |||||||||||||
| 153 | } | ||||||||||||||
| 154 | elsif ( $element ne "char" ) { | ||||||||||||||
| 155 | 0 | die "</$element> not allowed within <string> element\n"; | |||||||||||||
| 156 | } | ||||||||||||||
| 157 | 0 | return $self; | |||||||||||||
| 158 | } | ||||||||||||||
| 159 | |||||||||||||||
| 160 | |||||||||||||||
| 161 | 0 | ▶ | sub append_data { | ||||||||||||
|
Subroutine
append_data: not called CC=1 SCAR=6.9
| |||||||||||||||
| 162 | 0 | my( $self, $data ) = @_; | |||||||||||||
| 163 | 0 | $self->{value} .= $data; | |||||||||||||
| 164 | } | ||||||||||||||
| 165 | |||||||||||||||
| 166 | |||||||||||||||
| 167 | 0 | ▶ | sub char_decode { | ||||||||||||
|
Subroutine
char_decode: not called CC=2 SCAR=17.9
| |||||||||||||||
| 168 | 0 | my( $self, $code ) = @_; | |||||||||||||
| 169 | |||||||||||||||
| 170 | 0 | ▶ | die "Invalid character code\n" unless $code =~ /^[01][0-9a-f]$/i; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 171 | 0 | return chr hex $code; | |||||||||||||
| 172 | } | ||||||||||||||
| 173 | |||||||||||||||
| 174 | |||||||||||||||
| 175 | 0 | ▶ | sub is_parser { | ||||||||||||
|
Subroutine
is_parser: not called CC=1 SCAR=6.9
| |||||||||||||||
| 176 | 0 | return 1; | |||||||||||||
| 177 | } | ||||||||||||||
| 178 | |||||||||||||||