From: <pau...@us...> - 2007-01-03 19:48:47
|
Revision: 935 http://svn.sourceforge.net/everydevel/?rev=935&view=rev Author: paul_the_nomad Date: 2007-01-03 11:48:46 -0800 (Wed, 03 Jan 2007) Log Message: ----------- Move toXML out of node hierarchy to XML/Node.pm Modified Paths: -------------- trunk/ebase/lib/Everything/Node/Test/node.pm trunk/ebase/lib/Everything/Node/node.pm trunk/ebase/lib/Everything/Node.pm trunk/ebase/lib/Everything/Nodeball.pm trunk/ebase/lib/Everything/Test/Node.pm trunk/ebase/lib/Everything/XML/Node.pm trunk/ebase/lib/Everything/XML/Test/Node.pm Property Changed: ---------------- trunk/ebase/ Property changes on: trunk/ebase ___________________________________________________________________ Name: svk:merge - 16c2b9cb-492b-4d64-9535-64d4e875048d:/wip/ebase:937 a6810612-c0f9-0310-9d3e-a9e4af8c5745:/ebase/offline:17930 + 16c2b9cb-492b-4d64-9535-64d4e875048d:/wip/ebase:938 a6810612-c0f9-0310-9d3e-a9e4af8c5745:/ebase/offline:17930 Modified: trunk/ebase/lib/Everything/Node/Test/node.pm =================================================================== --- trunk/ebase/lib/Everything/Node/Test/node.pm 2007-01-03 19:48:10 UTC (rev 934) +++ trunk/ebase/lib/Everything/Node/Test/node.pm 2007-01-03 19:48:46 UTC (rev 935) @@ -38,12 +38,15 @@ push @{ $self->{errors} }, [@_]; } ); + *Everything::Node::node::DB = \$mock; my $module = $self->node_class(); my %import; my $mockimport = sub { + return unless (caller)[0] eq $module; # don't report imports + # from other packages $import{ +shift } = { map { $_ => 1 } @_[ 1 .. $#_ ] }; }; @@ -62,20 +65,10 @@ sub setup_imports { - return qw( DBI Everything ); + my $self = shift; + (); } -sub test_imports :Test(startup => 1) { - my ( $self) = @_; - my $imports = $self->{imports}; - is_deeply( - $$imports{Everything}, - { '$DB' => 1}, - '...imports $DB from Everything' - ); - -} - sub make_base_test_db { my $self = shift; @@ -602,6 +595,9 @@ $node->{type}{maxrevisions} = 0; + $node->fake_module('Everything::XML::Node', new => sub { $node }); + $node->set_true('toXML'); + my $result = $node->logRevision( 'user' ); is( $result, 0, 'logRevisions() should return 0 if lacking max revisons' ); @@ -681,6 +677,9 @@ $node->set_true( 'setVars' ); + $node->fake_module('Everything::XML::Node', new => sub { $node }); + $node->set_true('toXML'); + my $position = \$db->{workspace}{nodes}{13}; $$position = 4; my $result = $node->undo( 'user', 1, 1 ); Modified: trunk/ebase/lib/Everything/Node/node.pm =================================================================== --- trunk/ebase/lib/Everything/Node/node.pm 2007-01-03 19:48:10 UTC (rev 934) +++ trunk/ebase/lib/Everything/Node/node.pm 2007-01-03 19:48:46 UTC (rev 935) @@ -15,7 +15,9 @@ use DBI; use Everything qw/$DB/; +use Everything::XML 'xml2node'; use Everything::NodeBase; +use Everything::XML::Node; use Scalar::Util 'reftype'; @@ -729,8 +731,8 @@ } my $data = $workspace - ? $this->toXML() - : $this->{DB}->getNode( $this->getId, 'force' )->toXML(); + ? Everything::XML::Node->new( node => $this, nodebase => $this->{DB} )->toXML + : Everything::XML::Node->new( node => $this->{DB}->getNode( $this->getId, 'force'), nodebase => $this->{DB} )->toXML(); my $rev_id = $DB->sqlSelect( 'max(revision_id)+1', 'revision', @@ -870,7 +872,7 @@ # prepare the redo/undo (inverse of what's being called) - $REVISION->{xml} = $this->toXML(); + $REVISION->{xml} = Everything::XML::Node->new(node => $this, nodebase => $this->{DB} )->toXML(); $REVISION->{revision_id} = -$revision_id; my ($NEWNODE) = @{ xml2node($xml) }; Modified: trunk/ebase/lib/Everything/Node.pm =================================================================== --- trunk/ebase/lib/Everything/Node.pm 2007-01-03 19:48:10 UTC (rev 934) +++ trunk/ebase/lib/Everything/Node.pm 2007-01-03 19:48:46 UTC (rev 935) @@ -1166,64 +1166,7 @@ =cut -=head2 C<toXML> -This returns a string that contains an XML representation for this node. -Basically a way to export this node. - -We use the XML::Generator to create the XML because the XML::DOM API is not -very friendly for creating XML documents as it is for reading them. - -Returns the XML string. - -=cut - -sub toXML -{ - my ($this) = @_; - my $DOC = new XML::DOM::Document(); - my $NODE; - my $exportFields = $this->getNodeKeys(1); - my $tag; - my @fields; - my @rawFields; - - push @rawFields, keys %$exportFields; - - # This is used to determine if our parser can read in a particular - # export. If the parser is upgraded/modified, this should be bumped - # so that older versions of this code will know that it may have - # problems reading in XML that generated by a newer version. - my $XMLVERSION = "0.5"; - - $NODE = new XML::DOM::Element( $DOC, "NODE" ); - - $NODE->setAttribute( "export_version", $XMLVERSION ); - $NODE->setAttribute( "nodetype", $$this{type}{title} ); - $NODE->setAttribute( "title", $$this{title} ); - - # Sort them so that the exported XML has some order to it. - @fields = sort { $a cmp $b } @rawFields; - - foreach my $field (@fields) - { - $NODE->appendChild( new XML::DOM::Text( $DOC, "\n " ) ); - - $tag = $this->fieldToXML( $DOC, $field, " " ); - $NODE->appendChild($tag); - } - - $NODE->appendChild( new XML::DOM::Text( $DOC, "\n" ) ); - - $DOC->appendChild($NODE); - - # Return the structure as a string - return $DOC->toString(); -} - -=cut - - =head2 C<existingNodeMatches> Mainly used for importing purposes to see of a node matching this one already Modified: trunk/ebase/lib/Everything/Nodeball.pm =================================================================== --- trunk/ebase/lib/Everything/Nodeball.pm 2007-01-03 19:48:10 UTC (rev 934) +++ trunk/ebase/lib/Everything/Nodeball.pm 2007-01-03 19:48:46 UTC (rev 935) @@ -575,7 +575,8 @@ # a new node to the file. open( FILE, ">>" . $file ) or die "couldn't create $file in $dir - do we have permission?"; - print FILE $NODE->toXML(); + my $xmlifier = Everything::XML::Node->new(node => $NODE, nodebase => $DB ); + print FILE $xmlifier->toXML(); close(FILE); Modified: trunk/ebase/lib/Everything/Test/Node.pm =================================================================== --- trunk/ebase/lib/Everything/Test/Node.pm 2007-01-03 19:48:10 UTC (rev 934) +++ trunk/ebase/lib/Everything/Test/Node.pm 2007-01-03 19:48:46 UTC (rev 935) @@ -272,34 +272,7 @@ } -sub test_to_xml : Test(5) { - my $self = shift; - can_ok( $self->{class}, 'toXML' ) || return; - my $instance = Test::MockObject::Extends->new($self->{instance}); - $instance->set_always(getNodeKeys => { key1 => 'value1', key2 => 'value2'} ); - $instance->set_always(fieldToXML => 'a tag' ); - - my $mock = $self->{mock}; - $mock->fake_module('XML::DOM::Document'); - $mock->fake_module('XML::DOM::Text'); - $mock->fake_new('XML::DOM::Document'); - $mock->fake_new('XML::DOM::Text'); - $mock->fake_new('XML::DOM::Element'); - $mock->fake_new('XML::DOM::Element'); - - $mock->set_true('-setAttribute', '-appendChild'); - $mock->set_always('toString', 'a string of xml'); - is ($instance->toXML, 'a string of xml', '...should return XML.'); - my ($method, $args) = $instance->next_call; - - is($method, 'getNodeKeys', '...should get exportable keys from node object.'); - - ($method, $args) = $instance->next_call; - is ($method, 'fieldToXML', '...asks for field in XML.'); - is_deeply ($args, [$instance, $mock, 'key1', ' '], '...with arguments.'); -} - sub test_existing_node_matches : Test(1) { my $self = shift; can_ok( $self->{class}, 'existingNodeMatches' ) || return; Modified: trunk/ebase/lib/Everything/XML/Node.pm =================================================================== --- trunk/ebase/lib/Everything/XML/Node.pm 2007-01-03 19:48:10 UTC (rev 934) +++ trunk/ebase/lib/Everything/XML/Node.pm 2007-01-03 19:48:46 UTC (rev 935) @@ -1,3 +1,9 @@ +=head1 Everything::XML::Node + +A package to turn nodes into XML for exporting to Nodeballs and revisions. + +=cut + package Everything::XML::Node; { @@ -19,10 +25,7 @@ =head2 C<fieldToXML_vars> This is called when the node is being exported to XML and the field we -are creating is a var field. The base node knows how to export fields -to XML, but if the node contains some more complex data structures, -that nodetype needs to export that data structure itself. In this -case, we have a settings field (hash) that needs to get exported. +are creating is a var field. =over 4 @@ -60,7 +63,7 @@ foreach my $var (@vars) { $VARS->appendChild( XML::DOM::Text->new( $DOC, $indentchild ) ); - my $tag = genBasicTag( $DOC, "var", $var, $$vars{$var} ); + my $tag = $this->genBasicTag( $DOC, "var", $var, $$vars{$var} ); $VARS->appendChild($tag); } @@ -310,4 +313,59 @@ return $str; } + +=head2 C<toXML> + +This returns a string that contains an XML representation for this node. +A way to export this node. + +Returns the XML string. + +=cut + +sub toXML +{ + my ($this) = @_; + my $DOC = new XML::DOM::Document(); + my $NODE; + my $enode = $this->get_node; + my $exportFields = $enode->getNodeKeys(1); + my $tag; + my @fields; + my @rawFields; + + push @rawFields, keys %$exportFields; + + # This is used to determine if our parser can read in a particular + # export. If the parser is upgraded/modified, this should be bumped + # so that older versions of this code will know that it may have + # problems reading in XML that generated by a newer version. + my $XMLVERSION = "0.5"; + + $NODE = new XML::DOM::Element( $DOC, "NODE" ); + + $NODE->setAttribute( "export_version", $XMLVERSION ); + $NODE->setAttribute( "nodetype", $$enode{type}{title} ); + $NODE->setAttribute( "title", $$enode{title} ); + + # Sort them so that the exported XML has some order to it. + @fields = sort { $a cmp $b } @rawFields; + + foreach my $field (@fields) + { + $NODE->appendChild( new XML::DOM::Text( $DOC, "\n " ) ); + + $tag = $this->fieldToXML( $DOC, $field, " " ); + $NODE->appendChild($tag); + } + + $NODE->appendChild( new XML::DOM::Text( $DOC, "\n" ) ); + + $DOC->appendChild($NODE); + + # Return the structure as a string + return $DOC->toString(); +} + + 1; Modified: trunk/ebase/lib/Everything/XML/Test/Node.pm =================================================================== --- trunk/ebase/lib/Everything/XML/Test/Node.pm 2007-01-03 19:48:10 UTC (rev 934) +++ trunk/ebase/lib/Everything/XML/Test/Node.pm 2007-01-03 19:48:46 UTC (rev 935) @@ -78,7 +78,7 @@ my @tags; *Everything::XML::Node::genBasicTag = sub { - push @tags, join( ' ', @_[ 1 .. 3 ] ); + push @tags, join( ' ', @_[ 2 .. 4 ] ); }; $mock->set_always( getVars => { a => 1, b => 1, c => 1 } ) @@ -290,4 +290,43 @@ ); } +sub test_to_xml : Test(4) { + my $self = shift; + can_ok( $self->{class}, 'toXML' ) || return; + my $instance = $self->{instance}; + my $mock = Test::MockObject->new; + + $instance->set_node($mock); + + my @fieldtoxml_args = (); + no strict 'refs'; + local *{ $self->{class} . '::fieldToXML'}; + *{ $self->{class} . '::fieldToXML'} = + sub { + push @fieldtoxml_args, [@_]; + return 'a tag'; + }; + use strict 'refs'; + + $mock->set_always(getNodeKeys => { key1 => 'value1', key2 => 'value2'} ); + $mock->fake_module('XML::DOM::Document'); + $mock->fake_module('XML::DOM::Text'); + $mock->fake_new('XML::DOM::Document'); + $mock->fake_new('XML::DOM::Text'); + $mock->fake_new('XML::DOM::Element'); + $mock->fake_new('XML::DOM::Element'); + + $mock->set_true('-setAttribute', '-appendChild'); + $mock->set_always('toString', 'a string of xml'); + + is ($instance->toXML, 'a string of xml', '...should return XML.'); + + my ($method, $args) = $mock->next_call( ); + + is($method, 'getNodeKeys', '...should get exportable keys from node object.'); + + is_deeply ( $fieldtoxml_args[0], [$instance, $mock, 'key1', ' '], '...calls fieldToXML with arguments.'); + +} + 1; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |