| 1 | package PITA::XML; | ||
| 2 | |||
| 3 | # See POD at end for docs. | ||
| 4 | |||
| 5 | use 5.006; | ||
| 6 | use strict; | ||
| 7 | use Carp (); | ||
| 8 | use Params::Util (); | ||
| 9 | use IO::File (); | ||
| 10 | use IO::String (); | ||
| 11 | BEGIN { | ||
| 12 | # Temporary Hack: | ||
| 13 | # IO::String looks like a duck and quacks liks a duck, but we need it | ||
| 14 | # to be a real duck. So lets make it a duck (if it didn't turn into a | ||
| 15 | # real duck while we weren't looking.) | ||
| 16 | unless ( @IO::String::ISA ) { | ||
| 17 | @IO::String::ISA = qw{ IO::Handle IO::Seekable }; | ||
| 18 | } | ||
| 19 | } | ||
| 20 | use File::ShareDir (); | ||
| 21 | use XML::SAX::ParserFactory (); | ||
| 22 | |||
| 23 | # Optionally load the schema validator | ||
| 24 | BEGIN { | ||
| 25 | local $@; | ||
| 26 | eval { | ||
| 27 | require XML::Validator::Schema; | ||
| 28 | }; | ||
| 29 | } | ||
| 30 | |||
| 31 | use vars qw{$VERSION}; | ||
| 32 | BEGIN { | ||
| 33 | $VERSION = '0.52'; | ||
| 34 | } | ||
| 35 | |||
| 36 | # The XML Schema File | ||
| 37 | # Locate the Schema at use-time (instead of compile-time) and | ||
| 38 | # allow the specification of a custom schema. | ||
| 39 | use vars qw{$SCHEMA}; | ||
| 40 | $SCHEMA ||= File::ShareDir::dist_file('PITA-XML', 'pita-xml.xsd'); | ||
| 41 | |||
| 42 | # While in development, use a version-specific namespace. | ||
| 43 | # In theory, this ensures documents are only truly valid with the | ||
| 44 | # version they were created with. | ||
| 45 | use constant XMLNS => "http://ali.as/xml/schema/pita-xml/$VERSION"; | ||
| 46 | |||
| 47 | # The list of core schemes | ||
| 48 | use vars qw{%SCHEMES}; | ||
| 49 | BEGIN { | ||
| 50 | %SCHEMES = ( | ||
| 51 | 'perl5' => 1, | ||
| 52 | 'perl5.make' => 1, | ||
| 53 | 'perl5.build' => 1, | ||
| 54 | 'perl6' => 1, | ||
| 55 | ); | ||
| 56 | } | ||
| 57 | |||
| 58 | # Load the various classes | ||
| 59 | use PITA::XML::Storable (); | ||
| 60 | use PITA::XML::Command (); | ||
| 61 | use PITA::XML::Test (); | ||
| 62 | use PITA::XML::Request (); | ||
| 63 | use PITA::XML::Platform (); | ||
| 64 | use PITA::XML::File (); | ||
| 65 | use PITA::XML::Guest (); | ||
| 66 | use PITA::XML::Install (); | ||
| 67 | use PITA::XML::Report (); | ||
| 68 | use PITA::XML::SAXParser (); | ||
| 69 | use PITA::XML::SAXDriver (); | ||
| 70 | |||
| 71 | |||
| 72 | |||
| 73 | |||
| 74 | |||
| 75 | ##################################################################### | ||
| 76 | # Main Methods | ||
| 77 | |||
| 78 | sub validate { | ||
| 79 | my $class = shift; | ||
| 80 | my $fh = $class->_FH(shift); | ||
| 81 | |||
| 82 | # Make schema validation dependant on module availability | ||
| 83 | $XML::Validator::Schema::VERSION or return 1; | ||
| 84 | |||
| 85 | # Create the validator | ||
| 86 | my $parser = XML::SAX::ParserFactory->parser( | ||
| 87 | Handler => XML::Validator::Schema->new( | ||
| 88 | file => $SCHEMA, | ||
| 89 | ), | ||
| 90 | ); | ||
| 91 | |||
| 92 | # Validate the document | ||
| 93 | $parser->parse_file( $fh ); | ||
| 94 | |||
| 95 | 1; | ||
| 96 | } | ||
| 97 | |||
| 98 | |||
| 99 | |||
| 100 | |||
| 101 | ##################################################################### | ||
| 102 | # Support Methods | ||
| 103 | |||
| 104 | sub _FH { | ||
| 105 | my $class = shift; | ||
| 106 | my $file = shift; | ||
| 107 | if ( Params::Util::_SCALAR($file) ) { | ||
| 108 | $file = IO::String->new( $file ); | ||
| 109 | } | ||
| 110 | if ( Params::Util::_INSTANCE($file, 'IO::Handle') ) { | ||
| 111 | if ( $file->can('seek') ) { | ||
| 112 | # Reset the file handle | ||
| 113 | $file->seek( 0, 0 ) or Carp::croak( | ||
| 114 | 'Failed to reset file handle (seek to 0)', | ||
| 115 | ); | ||
| 116 | return $file; | ||
| 117 | } | ||
| 118 | Carp::croak('IO::Handle is not seekable'); | ||
| 119 | } | ||
| 120 | unless ( defined $file and ! ref $file and length $file ) { | ||
| 121 | Carp::croak('Did not provide a file name or handle'); | ||
| 122 | } | ||
| 123 | unless ( $file and -f $file and -r _ ) { | ||
| 124 | Carp::croak('Did not provide a readable file name'); | ||
| 125 | } | ||
| 126 | my $fh = IO::File->new( $file ); | ||
| 127 | unless ( $fh ) { | ||
| 128 | Carp::croak("Failed to open PITA::XML file '$file'"); | ||
| 129 | } | ||
| 130 | $fh; | ||
| 131 | } | ||
| 132 | |||
| 133 | sub _OUTPUT { | ||
| 134 | my ($class, $object, $name) = @_; | ||
| 135 | |||
| 136 | # If provided as a param, clean it up | ||
| 137 | if ( exists $object->{$name} ) { | ||
| 138 | # Convert from array to scalar ref | ||
| 139 | if ( Params::Util::_ARRAY0($object->{$name}) ) { | ||
| 140 | # Clean up newlines and merge into SCALAR | ||
| 141 | my $param = $object->{$name}; | ||
| 142 | foreach my $i ( 0 .. $#$param ) { | ||
| 143 | $param->[$i] =~ s/[\012\015]+$/\n/; | ||
| 144 | } | ||
| 145 | $param = join '', @$param; | ||
| 146 | $object->{$name} = \$param; | ||
| 147 | } | ||
| 148 | } | ||
| 149 | |||
| 150 | # Check for scalarness | ||
| 151 | Params::Util::_SCALAR0($object->$name()) ? 1 : undef; | ||
| 152 | } | ||
| 153 | |||
| 154 | sub _SCHEME { | ||
| 155 | my $class = shift; | ||
| 156 | my $string = Params::Util::_STRING(shift) or return undef; | ||
| 157 | ($SCHEMES{$string} or $string =~ /x_/) ? $string : undef; | ||
| 158 | } | ||
| 159 | |||
| 160 | sub _MD5SUM { | ||
| 161 | my $class = shift; | ||
| 162 | my $md5sum = Params::Util::_STRING(shift) or return undef; | ||
| 163 | ($md5sum =~ /^[0-9a-f]{32}$/i) ? lc($md5sum) : undef; | ||
| 164 | } | ||
| 165 | |||
| 166 | sub _DISTNAME { | ||
| 167 | my $class = shift; | ||
| 168 | my $distname = Params::Util::_STRING(shift) or return undef; | ||
| 169 | ($distname =~ /^[a-z]\w*(?:\-[a-z]\w*)+$/is) ? $distname : undef; | ||
| 170 | } | ||
| 171 | |||
| 172 | sub _GUID { | ||
| 173 | my $class = shift; | ||
| 174 | my $guid = Params::Util::_STRING(shift) or return undef; | ||
| 175 | ($guid =~ /^[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}$/) ? $guid : undef; | ||
| 176 | } | ||
| 177 | |||
| 178 | 1; | ||
| 179 | |||
| 180 | __END__ |