blib/lib/XML/Stream/Namespace.pm untested

filter: Statementstmt 0.0% 0 / 49 Branchbran 0.0% 0 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 73 SCAR 24.8
CC
3
Cov
0%
new
6.0
GetAttributes
6.0
SetNamespace
2.0
CRAP 12.0
1 ##############################################################################
2 #
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU Library General Public
5 # License as published by the Free Software Foundation; either
6 # version 2 of the License, or (at your option) any later version.
7 #
8 # This library is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 # Library General Public License for more details.
12 #
13 # You should have received a copy of the GNU Library General Public
14 # License along with this library; if not, write to the
15 # Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 # Boston, MA 02111-1307, USA.
17 #
18 # Jabber
19 # Copyright (C) 1998-2004 Jabber Software Foundation http://jabber.org/
20 #
21 ##############################################################################
22  
23 package XML::Stream::Namespace;
24  
25 =head1 NAME
26  
27 XML::Stream::Namespace - Object to make defining Namespaces easier in
28 XML::Stream.
29  
30 =head1 SYNOPSIS
31  
32 XML::Stream::Namespace is a helper package to XML::Stream. It provides
33 a clean way of defining Namespaces for XML::Stream to use when connecting.
34  
35 =head1 DESCRIPTION
36  
37 This module allows you to set and read elements from an XML::Stream
38 Namespace.
39  
40 =head1 METHODS
41  
42 SetNamespace("mynamespace");
43 SetXMLNS("http://www.mynamespace.com/xmlns");
44 SetAttributes(attrib1=>"value1",
45 attrib2=>"value2");
46  
47 GetNamespace() returns "mynamespace"
48 GetXMLNS() returns "http://www.mynamespace.com/xmlns"
49 GetAttributes() returns a hash ( attrib1=>"value1",attrib2=>"value2")
50 GetStream() returns the following string:
51 "xmlns:mynamespace='http://www.nynamespace.com/xmlns'
52 mynamespace:attrib1='value1'
53 mynamespace:attrib2='value2'"
54  
55 =head1 EXAMPLES
56  
57  
58 $myNamespace = XML::Stream::Namespace->new("mynamspace");
59 $myNamespace->SetXMLNS("http://www.mynamespace.org/xmlns");
60 $myNamespace->SetAttributes(foo=>"bar",
61 bob=>"vila");
62  
63 $stream = XML::Stream->new;
64 $stream->Connect(name=>"foo.bar.org",
65 port=>1234,
66 namespace=>"foo:bar",
67 namespaces=>[ $myNamespace ]);
68  
69 #
70 # The above Connect will send the following as the opening string
71 # of the stream to foo.bar.org:1234...
72 #
73 # <stream:stream
74 # xmlns:stream="http://etherx.jabber.org/streams"
75 # to="foo.bar.org"
76 # xmlns="foo:bar"
77 # xmlns:mynamespace="http://www.mynamespace.org/xmlns"
78 # mynamespace:foo="bar"
79 # mynamespace:bob="vila">
80 #
81
82  
83 =head1 AUTHOR
84  
85 Written by Ryan Eatmon in February 2000
86 Idea By Thomas Charron in January of 2000 for http://etherx.jabber.org/streams/
87  
88 Currently maintained by Darian Anthony Patrick.
89  
90 =head1 COPYRIGHT
91  
92 Copyright (C) 1998-2004 Jabber Software Foundation http://jabber.org/
93  
94 This module licensed under the LGPL, version 2.1.
95  
96 =cut
97  
98 use strict;
99 use warnings;
100 use Carp;
101 use vars qw( $VERSION );
102  
103 $VERSION = "1.23_07";
104  
105 sub new
106 {
107 my $proto = shift;
108 my $self = { };
109  
110 ($self->{Namespace}) = @_ if ($#_ > -1);
111  
112 $self->{Attributes} = {};
113  
114 bless($self,$proto);
115 return $self;
116 }
117  
118  
119 sub SetNamespace
120 {
121 my $self = shift;
122 my ($namespace) = @_;
123  
124 $self->{Namespace} = $namespace;
125 }
126  
127  
128 sub SetXMLNS
129 {
130 my $self = shift;
131 my ($xmlns) = @_;
132  
133 $self->{XMLNS} = $xmlns;
134 }
135  
136  
137 sub SetAttributes
138 {
139 my $self = shift;
140 my %att = @_;
141  
142 my $key;
143 foreach $key (keys(%att))
144 {
145 $self->{Attributes}->{$key} = $att{$key};
146 }
147 }
148  
149  
150 sub GetNamespace
151 {
152 my $self = shift;
153  
154 return $self->{Namespace};
155 }
156  
157 sub GetXMLNS
158 {
159 my $self = shift;
160  
161 return $self->{XMLNS};
162 }
163  
164 sub GetAttributes
165 {
166 my $self = shift;
167 my ($attrib) = @_;
168  
169 return $self->{Attributes} if ($attrib eq "");
170 return $self->{Attributes}->{$attrib};
171 }
172  
173  
174 sub GetStream
175 {
176 my $self = shift;
177  
178 my $string = "";
179  
180 $string .= "xmlns:".$self->GetNamespace();
181 $string .= "='".$self->GetXMLNS()."'";
182 my $attrib;
183 foreach $attrib (keys(%{$self->GetAttributes()}))
184 {
185 $string .= " ".$self->GetNamespace().":";
186 $string .= $attrib;
187 $string .= "='".$self->GetAttributes($attrib)."'";
188 }
189
190 return $string;
191 }
192  
193 1;
194