| 1 | # -*- perl -*- | ||||||||||||||
| 2 | |||||||||||||||
| 3 | # | ||||||||||||||
| 4 | # $Id: Base.pm,v 1.14 2005/05/13 09:53:13 eserte Exp $ | ||||||||||||||
| 5 | # Author: Slaven Rezic | ||||||||||||||
| 6 | # | ||||||||||||||
| 7 | # Copyright (C) 2001 Online Office Berlin. All rights reserved. | ||||||||||||||
| 8 | # Copyright (C) 2002 Slaven Rezic. | ||||||||||||||
| 9 | # This is free software; you can redistribute it and/or modify it under the | ||||||||||||||
| 10 | # terms of the GNU General Public License, see the file COPYING. | ||||||||||||||
| 11 | |||||||||||||||
| 12 | # | ||||||||||||||
| 13 | # Mail: slaven@rezic.de | ||||||||||||||
| 14 | # WWW: http://we-framework.sourceforge.net | ||||||||||||||
| 15 | # | ||||||||||||||
| 16 | |||||||||||||||
| 17 | package WE_Content::Base; | ||||||||||||||
| 18 | |||||||||||||||
| 19 | 2 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 20 | 2 | ▶ | use vars qw($VERSION); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 21 | 2 | $VERSION = sprintf("%d.%02d", q$Revision: 1.14 $ =~ /(\d+)\.(\d+)/); | |||||||||||||
| 22 | |||||||||||||||
| 23 | 3 | ▶ | sub new { | ||||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 24 | 3 | my($self, %args) = @_; | |||||||||||||
| 25 | 3 | my $buf = $self->get_string(%args); | |||||||||||||
| 26 | 3 | my $class = $self->guess_class($buf); | |||||||||||||
| 27 | 3 | my %newargs = %args; | |||||||||||||
| 28 | 3 | delete $newargs{$_} for ("-string", "-file"); | |||||||||||||
| 29 | 3 | $newargs{-string} = $buf; | |||||||||||||
| 30 | 3 | $class->new(%newargs); | |||||||||||||
| 31 | } | ||||||||||||||
| 32 | |||||||||||||||
| 33 | 0 | ▶ | sub debug { | ||||||||||||
|
Subroutine
debug: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||
| 34 | 0 | my $self = shift; | |||||||||||||
| 35 | 0 | ▶ | if (@_) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 36 | 0 | $self->{Debug} = $_[0]; | |||||||||||||
| 37 | } | ||||||||||||||
| 38 | 0 | $self->{Debug}; | |||||||||||||
| 39 | } | ||||||||||||||
| 40 | |||||||||||||||
| 41 | 3 | ▶ | sub clone { | ||||||||||||
|
Subroutine
clone: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||
| 42 | 3 | my($self, $as) = @_; | |||||||||||||
| 43 | 3 | require Data::Dumper; | |||||||||||||
| 44 | 3 | my $o; | |||||||||||||
| 45 | 3 | eval Data::Dumper->new([$self],['o'])->Indent(0)->Purity(1)->Dump; | |||||||||||||
| 46 | 3 | ▶ | die $@ if $@; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 47 | 3 | ▶ | if (defined $as) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 48 | 1 | bless $o, $as; | |||||||||||||
| 49 | } | ||||||||||||||
| 50 | 3 | $o; | |||||||||||||
| 51 | } | ||||||||||||||
| 52 | |||||||||||||||
| 53 | 9 | ▶ | sub get_string { | ||||||||||||
|
Subroutine
get_string: called CC=4 SCAR=14.1
Pod
documented
| |||||||||||||||
| 54 | 9 | my($self, %args) = @_; | |||||||||||||
| 55 | 9 | my($buf); | |||||||||||||
| 56 | 9 | ▶ | if ($args{-file}) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 57 | 4 | ▶ | open(F, "< $args{-file}") or die "Can't read $args{-file}: $!"; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 58 | 4 | local $/ = undef; | |||||||||||||
| 59 | 4 | $buf = <F>; | |||||||||||||
| 60 | 4 | close F; | |||||||||||||
| 61 | } elsif ($args{-string}) { | ||||||||||||||
| 62 | 5 | $buf = $args{-string}; | |||||||||||||
| 63 | } else { | ||||||||||||||
| 64 | 0 | die "Either -string or -file must be specified"; | |||||||||||||
| 65 | } | ||||||||||||||
| 66 | 9 | $buf; | |||||||||||||
| 67 | } | ||||||||||||||
| 68 | |||||||||||||||
| 69 | 3 | ▶ | sub guess_class { | ||||||||||||
|
Subroutine
guess_class: called CC=4 SCAR=17.3
Pod
documented
| |||||||||||||||
| 70 | 3 | my($self, $buf) = @_; | |||||||||||||
| 71 | 3 | ▶ | if ($buf =~ /^---( \#YAML:)?/) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 72 | 1 | require WE_Content::YAML; | |||||||||||||
| 73 | 1 | 'WE_Content::YAML'; | |||||||||||||
| 74 | } elsif ($buf =~ /^<\?xml/) { | ||||||||||||||
| 75 | 0 | ▶ | if ($buf =~ /<webeditordoc/) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 76 | 0 | require WE_Content::XMLText; | |||||||||||||
| 77 | 0 | 'WE_Content::XMLText'; | |||||||||||||
| 78 | } else { | ||||||||||||||
| 79 | 0 | require WE_Content::XML; | |||||||||||||
| 80 | 0 | 'WE_Content::XML'; | |||||||||||||
| 81 | } | ||||||||||||||
| 82 | } else { | ||||||||||||||
| 83 | 2 | require WE_Content::PerlDD; | |||||||||||||
| 84 | 2 | 'WE_Content::PerlDD'; | |||||||||||||
| 85 | } | ||||||||||||||
| 86 | } | ||||||||||||||
| 87 | |||||||||||||||
| 88 | 12 | ▶ | sub _by_path { | ||||||||||||
|
Subroutine
_by_path: called CC=13 SCAR=27.1
| |||||||||||||||
| 89 | 12 | my $self = shift; | |||||||||||||
| 90 | 12 | my $path = shift; | |||||||||||||
| 91 | 12 | my $sep = shift; | |||||||||||||
| 92 | 12 | my $do_set; | |||||||||||||
| 93 | my $value; | ||||||||||||||
| 94 | 12 | ▶ | if (@_) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 95 | 0 | $do_set = 1; | |||||||||||||
| 96 | 0 | $value = shift; | |||||||||||||
| 97 | } | ||||||||||||||
| 98 | 12 | my $o = $self->{Object}; | |||||||||||||
| 99 | 12 | ▶ | return undef if !$o; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 100 | 12 | my $o_ref = \$o; | |||||||||||||
| 101 | 12 | my @path; | |||||||||||||
| 102 | 12 | ▶ | $sep = "/" if !defined $sep; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 103 | 12 | my $sep_rx = quotemeta $sep; | |||||||||||||
| 104 | 12 | ▶ | if (UNIVERSAL::isa($path, 'ARRAY')) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 105 | 2 | @path = @$path; | |||||||||||||
| 106 | } else { | ||||||||||||||
| 107 | 10 | $path =~ s|^$sep_rx/+||; | |||||||||||||
| 108 | 10 | @path = split $sep_rx, $path; | |||||||||||||
| 109 | } | ||||||||||||||
| 110 | 12 | foreach my $p (@path) { | |||||||||||||
| 111 | 30 | ▶ | if (UNIVERSAL::isa($o, 'ARRAY')) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 112 | 12 | ▶ | if (defined $o->[$p]) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 113 | 10 | $o_ref = \$o->[$p]; | |||||||||||||
| 114 | 10 | $o = $o->[$p]; | |||||||||||||
| 115 | } else { | ||||||||||||||
| 116 | 2 | ▶ | if ($do_set) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 117 | 0 | die "Can't set $path to $value"; | |||||||||||||
| 118 | } | ||||||||||||||
| 119 | 2 | return undef; | |||||||||||||
| 120 | } | ||||||||||||||
| 121 | } elsif (UNIVERSAL::isa($o, 'HASH')) { | ||||||||||||||
| 122 | 18 | ▶ | if (exists $o->{$p}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 123 | 16 | $o_ref = \$o->{$p}; | |||||||||||||
| 124 | 16 | $o = $o->{$p}; | |||||||||||||
| 125 | } else { | ||||||||||||||
| 126 | 2 | ▶ | if ($do_set) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 127 | 0 | die "Can't set $path to $value"; | |||||||||||||
| 128 | } | ||||||||||||||
| 129 | 2 | return undef; | |||||||||||||
| 130 | } | ||||||||||||||
| 131 | } else { | ||||||||||||||
| 132 | 0 | die "Can't handle " . ref($o) . " ($o) from path @path"; | |||||||||||||
| 133 | } | ||||||||||||||
| 134 | } | ||||||||||||||
| 135 | 8 | ▶ | if ($do_set) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 136 | 0 | $$o_ref = $value; | |||||||||||||
| 137 | } | ||||||||||||||
| 138 | 8 | $o; | |||||||||||||
| 139 | } | ||||||||||||||
| 140 | |||||||||||||||
| 141 | 12 | ▶ | sub by_path { shift->_by_path(@_, "/") } | ||||||||||||
|
Subroutine
by_path: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 142 | 0 | ▶ | sub by_dotted { shift->_by_path(@_, ".") } | ||||||||||||
|
Subroutine
by_dotted: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 143 | 0 | ▶ | sub set_by_path { shift->by_path(shift, "/", @_) } | ||||||||||||
|
Subroutine
set_by_path: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 144 | 0 | ▶ | sub set_by_dotted { shift->by_dotted(shift, ".", @_) } | ||||||||||||
|
Subroutine
set_by_dotted: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 145 | |||||||||||||||
| 146 | # "prototype": new name, "template": old name | ||||||||||||||
| 147 | 0 | ▶ | sub is_prototype { | ||||||||||||
|
Subroutine
is_prototype: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 148 | 0 | $_[0]->{Type} eq 'template'; | |||||||||||||
| 149 | } | ||||||||||||||
| 150 | |||||||||||||||
| 151 | 0 | ▶ | sub serialize_as { | ||||||||||||
|
Subroutine
serialize_as: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||
| 152 | 0 | my $self = shift; | |||||||||||||
| 153 | 0 | my $as = shift; | |||||||||||||
| 154 | 0 | ▶ | if ($as !~ /^WE_Content::/) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 155 | 0 | $as = "WE_Content::$as"; | |||||||||||||
| 156 | } | ||||||||||||||
| 157 | 0 | eval "require $as"; | |||||||||||||
| 158 | 0 | ▶ | die $@ if $@; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 159 | 0 | require Storable; | |||||||||||||
| 160 | 0 | my $clone = Storable::dclone($self); | |||||||||||||
| 161 | 0 | bless $clone, $as; # re-bless | |||||||||||||
| 162 | 0 | $clone->serialize(@_); | |||||||||||||
| 163 | } | ||||||||||||||
| 164 | |||||||||||||||
| 165 | 2 | 1; | |||||||||||||
| 166 | |||||||||||||||
| 167 | __END__ | ||||||||||||||