XML::Stream (3)
Leading comments
Automatically generated by Pod::Man 2.28 (Pod::Simple 3.28) Standard preamble: ========================================================================
NAME
XML::Stream - Creates an XML Stream connection and parses return dataSYNOPSIS
XML::Stream is an attempt at solidifying the use ofDESCRIPTION
This module provides the user with methods to connect to a remote server, send a stream ofXML::Stream gives the user the ability to define a central callback that will be used to handle the tags received from the server. These tags are passed in the format defined at instantiation time. the closing tag of an object is seen, the tree is finished and passed to the call back function. What the user does with it from there is up to them.
For a detailed description of how this module works, and about the data structure that it returns, please view the source of Stream.pm and look at the detailed description at the end of the file.
METHODS
new
new( debug => string, debugfh => FileHandle, debuglevel => 0|1|N, debugtime => 0|1, style => string)
Creates the XML::Stream object. debug should be set to the path for the debug log to be written. If set to ``stdout'' then the debug will go there. Also, you can specify a filehandle that already exists by using debugfh.
debuglevel determines the amount of debug to generate. 0 is the least, 1 is a little more, N is the limit you want.
debugtime determines wether a timestamp should be preappended to the entry. style defines the way the data structure is returned. The two available styles are:
tree - L<XML::Parser> Tree format node - L<XML::Stream::Node> format
For more information see the respective man pages.
Listen
Starts the stream by listening on a port for someone to connect, and send the opening stream tag, and then sending a response based on if the received header was correct for this stream. Server name, port, and namespace are required otherwise we don't know where to listen and what namespace to accept.ConnectionAccept
Accept an incoming connection.Respond
If this is a listening socket then we need to respond to the opening <stream:stream/>.Connect
Starts the stream by connecting to the server, sending the opening stream tag, and then waiting for a response and verifying that it is correct for this stream. Server name, port, and namespace are required otherwise we don't know where to send the stream to...
Connect(hostname=>string, port=>integer, to=>string, from=>string, myhostname=>string, namespace=>string, namespaces=>array, connectiontype=>string, ssl=>0|1, ssl_verify =>0x00|0x01|0x02|0x04, ssl_ca_path=>string, srv=>string)
Opens a tcp connection to the specified server and sends the proper opening
"to" is needed if you want the stream to attribute to be something other than the hostname you are connecting to.
"from" is needed if you want the stream from attribute to be something other than the hostname you are connecting from.
"myhostname" should not be needed but if the module cannot determine your hostname properly (check the debug log), set this to the correct value, or if you want the other side of the stream to think that you are someone else. The type determines the kind of connection that is made:
"tcpip" - TCP/IP (default) "stdinout" - STDIN/STDOUT "http" - HTTP
"ssl" specifies whether an
"ssl_verify" determines whether peer certificate verification takes place. See the documentation for the SSL_verify_mode parameter to IO::Socket::SSL-new()|IO::Socket::SSL>. The default value is 0x01 causing the server certificate to be verified, and requiring that ssl_ca_path be set.
"ssl_ca_path" should be set to the path to either a directory containing hashed
If srv is specified
This function returns the same hash from GetRoot() below. Make sure you get the
OpenStream
Send the opening stream and save the root element info.OpenFile
Starts the stream by opening a file and setting it up so that Process reads from the filehandle to get the incoming stream.
OpenFile(string)
Opens a filehandle to the argument specified, and pretends that it is a stream. It will ignore the outer tag, and not check if it was a <stream:stream/>. This is useful for writing a program that has to parse any
Disconnect
Sends the closing
Disconnect(sid)
Sends the proper closing
InitConnection
Initialize the connection data structureParseStream
Takes the incoming stream and makes sure that only fullProcess
Checks for data on the socket and returns a status code depending on if there was data or not. If a timeout is not defined in the call then the timeout defined in Connect() is used. If a timeout of 0 is used then the call blocks until it gets some data, otherwise it returns after the timeout period.
Process(integer)
Waits for data to be available on the socket. If a timeout is specified then the Process function waits that period of time before returning nothing. If a timeout period is not specified then the function blocks until data is received. The function returns a hash with session ids as the key, and status values or data as the hash values.
Read
Takes the data from the server and returns a stringSend
Takes the data string and sends it to the server
Send(sid, string);
Sends the string over the specified connection as is. This does no checking if valid
ProcessStreamFeatures
Process the <stream:featutres/> block.GetStreamFeature
Return the value of the stream feature (if any).ReceivedStreamFeatures
Have we received the stream:features yet?ProcessTLSPacket
Process aStartTLS
Client function to have the socket startTLSStartTLS
Send a <starttls/> in theTLSClientProceed
Handle a <proceed/> packet.TLSClientSecure
Return 1 if the socket is secure, 0 otherwise.TLSClientDone
Return 1 if theTLSClientError
return theTLSClientFailure
Handle a <failure/>TLSFailure
Send a <failure/> in theProcessSASLPacket
Process aSASLAnswerChallenge
When we get a <challenge/> we need to do the grunt work to return a <response/>.SASLAuth
Send an <auth/> in theSASLChallenge
Send a <challenge/> in theSASLClient
This is a helper function to perform all of the required steps for doingSASLClientAuthed
Return 1 if we authed viaSASLClientDone
Return 1 if theSASLClientError
Return the error if anySASLClientFailure
Handle a received <failure/>SASLClientSuccess
handle a received <success/>SASLFailure
Send a <failure/> tag in theSASLResponse
Send a <response/> tag in theGetErrorCode
if you are returned an undef, you can call this function and hopefully learn more information about the problem.
GetErrorCode(sid)
returns a string for the specified session that will hopefully contain some useful information about why Process or Connect returned an undef to you.
StreamError
Given a type and text, generate a <stream:error/> packet to send back to the other side.SetXMLData
Takes a host of arguments and sets a portion of the specified data strucure with that data. The function works in two modes ``single'' or ``multiple''. ``single'' denotes that the function should locate the current tag that matches this data and overwrite it's contents with data passed in. ``multiple'' denotes that a new tag should be created even if others exist.
type - single or multiple
XMLTree - pointer to XML::Stream data object (tree or node)
tag - name of tag to create/modify (if blank assumes
working with top level tag)
data -
GetXMLData
Takes a host of arguments and returns various data structures that match them.type "existence" - returns 1 or 0 if the tag exists in the top level.
"value" - returns either the
"value array" returns an array of strings representing all of the
"tree" - returns a data structure that represents the
"tree array" returns an array of data structures each with the specified tag as the root tag.
"child array" - returns a list of all children nodes
not including
"attribs" - returns a hash with the attributes, and
their values, for the things that match
the parameters
"count" - returns the number of things that match
the arguments
"tag" - returns the root tag of this tree
XMLTree - pointer to XML::Stream data structure
"tag" - tag to pull data from. If blank then the top level
tag is accessed.
"attrib" - attribute value to retrieve. Ignored for types
``value array'', ``tree'', ``tree array''. If paired
with value can be used to filter tags based on
attributes and values.
"value" - only valid if an attribute is supplied. Used to
filter for tags that only contain this attribute.
Useful to search through multiple tags that all
reference different name spaces.
XPath
Run an xpath query on a node and return back the result.XPath(node,path) returns an array of results that match the xpath. node can be any of the three types (Tree, Node).
XPathCheck
Run an xpath query on a node and return 1 or 0 if the path is valid.XML2Config
Takes an
<foo> <bar>1</bar> <x> <y>foo</y> </x> <z>5</z> <z>6</z> </foo>
The resulting hash would be:
$hash{bar} = 1; $hash{x}->{y} = "foo"; $hash{z}->[0] = 5; $hash{z}->[1] = 6;
Good for config files.
Config2XML
Takes a hash and produces an
$hash{bar} = 1; $hash{x}->{y} = "foo"; $hash{z}->[0] = 5; $hash{z}->[1] = 6;
The resulting xml would be:
<foo> <bar>1</bar> <x> <y>foo</y> </x> <z>5</z> <z>6</z> </foo>
Good for config files.
EscapeXML
Simple function to make sure that no bad characters make it into in theUnescapeXML
Simple function to take an escaped string and return it to normal.BuildXML
Takes one of the data formats that XML::Stream supports and call the proper BuildXML_xxx function on it.ConstXMLNS
Return the namespace from the constant string.GetRoot
Returns the hash of attributes for the root <stream:stream/> tag so that any attributes returned can be accessed. from and any xmlns:foobar might be important.
GetRoot(sid)
Returns the attributes that the stream:stream tag sent by the other end listed in a hash for the specified session.
GetSock
returns the Socket so that an outside function can access it if desired.
GetSock(sid)
Returns a pointer to the IO::Socket object for the specified session.
NewSID
Returns a sessionSetCallBacks
Takes a hash with top level tags to look for as the keys and pointers to functions as the values.
SetCallBacks(node=>function, update=>function);
Sets the callback that should be called in various situations.
"node" is used to handle the data structures that are built for each top level tag. "update" is used for when Process is blocking waiting for data, but you want your original code to be updated.
VARIABLES
$NONBLOCKING
Tells the Parser to enter into a nonblocking state. This might cause some funky behavior since you can get nested callbacks while things are waiting. 1=on, 0=off(default).
EXAMPLES
simple example
use XML::Stream qw( Tree ); $stream = XML::Stream->new; my $status = $stream->Connect(hostname => "jabber.org", port => 5222, namespace => "jabber:client"); if (!defined($status)) { print "ERROR: Could not connect to server\n"; print " (",$stream->GetErrorCode(),")\n"; exit(0); } while($node = $stream->Process()) { # do something with $node } $stream->Disconnect();
Example using a handler
use XML::Stream qw( Tree ); $stream = XML::Stream->new; $stream->SetCallBacks(node=>\&noder); $stream->Connect(hostname => "jabber.org", port => 5222, namespace => "jabber:client", timeout => undef) || die $!; # Blocks here forever, noder is called for incoming # packets when they arrive. while(defined($stream->Process())) { } print "ERROR: Stream died (",$stream->GetErrorCode(),")\n"; sub noder { my $sid = shift; my $node = shift; # do something with $node }
AUTHOR
Tweaked, tuned, and brightness changes by Ryan Eatmon, reatmon@ti.com in May of 2000. Colorized, and Dolby Surround sound added by Thomas Charron, tcharron@jabber.org By Jeremie in October of 1999 for etherx.jabber.org/streamsCurrently maintained by Darian Anthony Patrick.
COPYRIGHT
Copyright (C) 1998-2004 Jabber Software Foundation jabber.orgThis module licensed under the