# ResultSet.pm - Will pull settings from an XML config file into a format usable by the system. # Created by James A. Pattie. Copyright (c) 2001-2002, Xperience, Inc. package DBIWrapper::ResultSet; use strict; use XML::LibXML; use vars qw ($AUTOLOAD @ISA @EXPORT $VERSION); require Exporter; @ISA = qw(Exporter AutoLoader); @EXPORT = qw(); $VERSION = "1.1"; # new sub new { my $that = shift; my $class = ref($that) || $that; my $self = bless {}, $class; my $errStr = "ResultSet->new() - Error:"; $self->{version} = "1.2"; $self->{resultFile} = ""; $self->{select} = 0; # indicates if a \n) if ($self->{select}); $result .= <<"END_OF_XML"; END_OF_XML for (my $i=0; $i < $self->{numRows}; $i++) { if ($self->{columns}) { $result .= " \n"; foreach my $column (@{$self->{columnNames}}) { my $data = $self->{rows}->[$i]->{$column}; my $value = $self->encodeEntities(string => $data); my $name = $self->fixupAttributes($column); if ($self->{columns} == 1) { $result .= " \n"; } elsif ($self->{columns} == 2) { $result .= " <$name>\n"; } } $result .= " \n"; } else { $result .= " {columnNames}}) { my $value = $self->encodeEntities(string => $self->{rows}->[$i]->{$column}); my $name = $self->fixupAttributes($column); $result .= " $name=\"$value\""; } $result .= "/>\n"; } } $result .= <<"END_OF_XML"; END_OF_XML } else { $result .= "ResultSet not valid!\n\n"; $result .= join("\n", @{$valid[1]}) . "\n"; die $result; } return $result; } # string encodeEntities(string) # requires: string - string to encode # optional: # returns: string that has been encoded. # summary: replaces all special characters with their XML entity equivalent. " => " sub encodeEntities { my $self = shift; my %args = ( string => "", @_ ); my $string = $args{string}; my @entities = ('&', '"', '<', '>', '\n'); my %entities = ('&' => '&', '"' => '"', '<' => '<', '>' => '>', '\n' => '\\n'); return $string if (length $string == 0); foreach my $entity (@entities) { $string =~ s/$entity/$entities{$entity}/g; } return $string; } =item scalar fixupAttributes(string) Attempts to make sure that the given string can be a valid attribute in an xml document. Converts (, ), -, \, /, =, >, <, & to _ Deletes ', ", \n =cut sub fixupAttributes { my $self = shift; my $string = ""; if (scalar @_ == 1) { $string = shift; } else { my %args = ( string => "", @_ ); $string = $args{string}; } my @replace = ('&', '<', '>', '-', '\\(', '\\)', '\\\\', '/', '='); my @delete = ("'", '"', '\n'); return $string if (length $string == 0); foreach my $entity (@replace) { $string =~ s/$entity/_/g; } foreach my $entity (@delete) { $string =~ s/$entity//g; } return $string; } 1; __END__ =head1 NAME ResultSet - The XML Database ResultSet Module. =head1 SYNOPSIS use DBIWrapper::ResultSet; my $obj = DBIWrapper::ResultSet->new(); =head1 DESCRIPTION ResultSet will contain the parsed XML file generated by readXML(). It provides a method to validate that it is complete and also a method to generate a valid XML file from the data stored in the data hash. =head1 FUNCTIONS scalar new() Creates a new instance of the DBIWrapper::ResultSet object. array isValid(void) Determines if the data structure is complete and usable for generating an XML file from. Returns an array. The first index is boolean (1 or 0 to indicate if the object is valid or not). The second index is an array of error messages that were generated based upon the errors found. string displayData(style) Outputs the contents of the perl data structure after validating it. If style = 'text' then formats for the console, else if style = 'html' we generate valid html output and format in a table. The content is suitable to place straight into the section of your HTML page. string generateXML(void) Creates an XML file based upon the info stored in the ResultSet. It first calls isValid() to make sure this is possible. If not then we die with an informative error message. =head1 VARIABLES version - version of the XML file parsed resultFile - the name of the file parsed or the string of XML sql - the SELECT statement issued which generated the XML plug - the options that were plugged into the SQL statement result - Ok or Error. Indicates the status returned by the database error - The error string returned if result = Error numRows - the number of rows returned from the database columns - 0 or 1. Indicates if we had children for each or if the data returned was stored by the name given from the database in the with no children tags. rows - the array of row entries returned. Each row is a hash of column name = column value attributes. columnNames - The names of the columns used in the XML file. errorCodes - hash of error codes to messages returned by isValid(). NOTE: All data fields are accessible by specifying the object and pointing to the data member to be modified on the left-hand side of the assignment. Ex. $obj->variable($newValue); or $value = $obj->variable; =head1 AUTHOR Xperience, Inc. (mailto:admin at pcxperience.com) =head1 SEE ALSO perl(1), DBIWrapper(3) =cut