blib/lib/MOBY/RDF/Utils.pm untested

filter: Statementstmt 0.0% 0 / 57 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 94 CC 7 SCAR 20.3
CC
7
Cov
0%
prettyPrintXML
12.0
getHttpRequestByURL
12.0
trim
12.0
CRAP 7.6
1 #-----------------------------------------------------------------
2 # MOBY::RDF::Utils
3 # Author: Edward Kawas <edward.kawas@gmail.com>,
4 # For copyright and disclaimer see below.
5 #
6 # $Id: Utils.pm,v 1.9 2009/01/28 14:38:41 kawas Exp $
7 #-----------------------------------------------------------------
8  
9 package MOBY::RDF::Utils;
10  
11 use XML::LibXML;
12 use LWP::UserAgent;
13 use HTTP::Request;
14  
15 use strict;
16  
17 use vars qw /$VERSION/;
18 $VERSION = sprintf "%d.%02d", q$Revision: 1.9 $ =~ /: (\d+)\.(\d+)/;
19  
20 #-----------------------------------------------------------------
21 # load all modules needed for my attributes
22 #-----------------------------------------------------------------
23  
24 =head1 NAME
25  
26 MOBY::RDF::Utils - Create RDF/OWL for Moby datatypes
27  
28 =head1 SYNOPSIS
29  
30 use MOBY::RDF::Utils;
31  
32  
33 =head1 DESCRIPTION
34  
35 This module contains utility methods
36  
37 =cut
38  
39 =head1 AUTHORS
40  
41 Edward Kawas (edward.kawas [at] gmail [dot] com)
42  
43 =cut
44  
45 #-----------------------------------------------------------------
46  
47 =head1 SUBROUTINES
48  
49 =cut
50  
51 #-----------------------------------------------------------------
52 # new
53 #-----------------------------------------------------------------
54 sub new {
55 my ($class) = @_;
56  
57 # create an object
58 my $self = bless {}, ref($class) || $class;
59  
60 $self->{xslt} = <<END;
61 <xsl:stylesheet version="1.0"
62 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
63 <xsl:output method="xml" version="1.0" encoding="UTF-8" omit-xml-declaration="yes" indend="yes"/>
64 <xsl:param name="indent-increment" select="' '" />
65 <xsl:template match="*">
66 <xsl:param name="indent" select="'&#xA;'"/>
67 <xsl:value-of select="\$indent"/>
68 <xsl:copy>
69 <xsl:copy-of select="@*" />
70 <xsl:apply-templates>
71 <xsl:with-param name="indent"
72 select="concat(\$indent, \$indent-increment)"/>
73 </xsl:apply-templates>
74 <xsl:if test="*">
75 <xsl:value-of select="\$indent"/>
76 </xsl:if>
77 </xsl:copy>
78 </xsl:template>
79 <xsl:template match="comment()|processing-instruction()">
80 <xsl:copy ></xsl:copy>
81 </xsl:template>
82 <!-- WARNING: this is dangerous. Handle with care -->
83 <xsl:template match="text()[normalize-space(.)='']"/>
84 </xsl:stylesheet>
85 END
86  
87 # done
88 return $self;
89 }
90  
91 =head2 prettyPrintXML
92  
93 Return a string of XML that is formatted nicely
94  
95 =cut
96  
97 sub prettyPrintXML {
98 my ( $self, $hash ) = @_;
99 my $xml = $hash->{xml};
100 unless ($xml) {
101 $xml = <<END;
102 <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"></rdf:RDF>
103 END
104 }
105  
106 eval "require XML::LibXSLT";
107 if ($@) {
108 my $parser = XML::LibXML->new();
109 my $source = $parser->parse_string($xml);
110 $xml = $source->toString(0);
111 return $xml;
112 } else {
113 my $parser = XML::LibXML->new();
114 my $xslt = XML::LibXSLT->new();
115 my $source = $parser->parse_string($xml);
116 my $style_doc = $parser->parse_string( $self->{xslt} );
117 my $stylesheet = $xslt->parse_stylesheet($style_doc);
118 my $results = $stylesheet->transform($source);
119 $xml = $stylesheet->output_string($results);
120 return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" . $xml;
121 }
122 }
123  
124 =head2 getHttpRequestByURL
125  
126 returns a scalar of text obtained from the url or dies if there was no success
127  
128 =cut
129  
130 sub getHttpRequestByURL {
131 my ( $self, $url ) = @_;
132 my $ua = LWP::UserAgent->new;
133 $ua->agent( "pMOBY/Central/$VERSION");
134  
135 my $req =
136 HTTP::Request->new( GET =>
137 $url );
138  
139 # accept gzip encoding
140 $req->header( 'Accept-Encoding' => 'gzip' );
141  
142 # send request
143 my $res = $ua->request($req);
144  
145 # check the outcome
146 if ( $res->is_success ) {
147 if ( $res->header('content-encoding') and $res->header('content-encoding') eq 'gzip' ) {
148 return $res->decoded_content;
149 } else {
150 return $res->content;
151 }
152 } else {
153 die "Error getting data from URL:\n\t" . $res->status_line;
154 }
155 }
156  
157 =head2 empty_rdf
158  
159 returns a string of RDF that represents a syntactically correct RDF file
160  
161 =cut
162  
163 sub empty_rdf {
164 return <<'END_OF_RDF';
165 <?xml version="1.0"?>
166 <rdf:RDF
167 xmlns:b="http://www.w3.org/2000/01/rdf-schema#"
168 xmlns:a="http://protege.stanford.edu/plugins/owl/dc/protege-dc.owl#"
169 xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
170 </rdf:RDF>
171 END_OF_RDF
172 }
173  
174 =head2 trim
175  
176 trims whitespace from the begining and end of a string
177  
178 =cut
179  
180 sub trim {
181 my ($self, $text) = @_;
182 $text = $self
183 unless ref($self) =~ m/^MOBY::RDF::Utils/;
184 # return empty string if $text is not defined
185 return "" unless $text;
186 $text =~ s/^\s+//;
187 $text =~ s/\s+$//;
188 return $text;
189 }
190  
191  
192  
193 1;
194 __END__