com.limegroup.gnutella.xml
Class LimeXMLUtils

java.lang.Object
  extended bycom.limegroup.gnutella.xml.LimeXMLUtils

public class LimeXMLUtils
extends java.lang.Object

Contains utility methods


Constructor Summary
LimeXMLUtils()
           
 
Method Summary
static java.lang.String capitalizeFirst(java.lang.String str)
          takes a string and returns the same string with the first letter capitalized
static byte[] compress(byte[] data)
           
static Response createResponse(java.lang.String xml)
          Creates a Response instance from the passed xml string
static java.lang.String encodeXML(java.lang.String inData)
          Parses the passed string, and encodes the special characters (used in xml for special purposes) with the appropriate codes.
static java.util.List getAttributes(org.w3c.dom.NamedNodeMap nodeMap)
           
static java.lang.String getAttributeValue(org.w3c.dom.NamedNodeMap attributes, java.lang.String soughtAttribute)
          Returns the value of the specified attribute
static org.w3c.dom.Document getDocument(java.io.File file)
          Returns an instance of org.w3c.dom.Document after parsing the passed xml file
static java.util.List getElements(org.w3c.dom.NodeList nodeList)
          Extracts only the Element nodes from a NodeList.
static org.xml.sax.InputSource getInputSource(java.io.File file)
          Returns an instance of InputSource after reading the file, and trimming the extraneous white spaces.
static Response[] getResponseArray(java.util.List xmlDocuments)
          Converts the given list of xml documents to an array of responses
static java.lang.String getText(org.w3c.dom.NodeList nodeList)
          Collapses a list of CDATASection, Text, and predefined EntityReference nodes into a single string.
static byte[] hashFile(java.io.File toHash)
          Hashes the file using bits and pieces of the file.
static boolean isMP3File(java.io.File in)
           
static boolean isMP3File(java.lang.String in)
           
static boolean match(LimeXMLDocument replyDoc, LimeXMLDocument queryDoc)
          Compares the queryDoc with the replyDoc and finds out if the replyDoc is a match for the queryDoc
static boolean notValidXML(java.lang.String inputStr)
          Does an approximate check on the passed string for xml validity
static java.lang.String processColName(java.lang.String colName)
          picks up the last strng in the colName and return it.
static byte[] readFully(java.io.InputStream in)
          Reads all the bytes from the passed input stream till end of stream reached.
static byte[] uncompress(byte[] data)
           
static void writeEscapedString(java.io.Writer writer, java.lang.String string)
          Writes string into writer, escaping &, ', ", <, and > with the XML excape strings.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

LimeXMLUtils

public LimeXMLUtils()
Method Detail

getInputSource

public static org.xml.sax.InputSource getInputSource(java.io.File file)
                                              throws java.io.IOException
Returns an instance of InputSource after reading the file, and trimming the extraneous white spaces.

Parameters:
file - The file from where to read
Returns:
The instance of InpiutSource created from the passed file
Throws:
java.io.IOException - If file doesnt get opened or other I/O problems

getDocument

public static org.w3c.dom.Document getDocument(java.io.File file)
                                        throws java.io.IOException,
                                               javax.xml.parsers.ParserConfigurationException,
                                               org.xml.sax.SAXException
Returns an instance of org.w3c.dom.Document after parsing the passed xml file

Parameters:
file - The file from where to read
Returns:
The instance of org.w3c.dom.Document after parsing the passed xml file
Throws:
java.io.IOException - If file doesnt get opened or other I/O problems
javax.xml.parsers.ParserConfigurationException - if problem in getting parser
org.xml.sax.SAXException - If any problem in parsing

getAttributeValue

public static java.lang.String getAttributeValue(org.w3c.dom.NamedNodeMap attributes,
                                                 java.lang.String soughtAttribute)
Returns the value of the specified attribute

Parameters:
attributes - attribute nodes in which to search for the specified attribute
soughtAttribute - The attribute whose value is sought
Returns:
the value of the specified attribute, or null if the specified attribute doesnt exist in the passed set of attributes

getElements

public static java.util.List getElements(org.w3c.dom.NodeList nodeList)
Extracts only the Element nodes from a NodeList. This is useful when the DTD guarantees that the node list's parent contains only elements. Unfortunately, the node list can contain comments and whitespace.


getAttributes

public static java.util.List getAttributes(org.w3c.dom.NamedNodeMap nodeMap)

getText

public static java.lang.String getText(org.w3c.dom.NodeList nodeList)
Collapses a list of CDATASection, Text, and predefined EntityReference nodes into a single string. If the list contains other types of nodes, those other nodes are ignored.


writeEscapedString

public static void writeEscapedString(java.io.Writer writer,
                                      java.lang.String string)
                               throws java.io.IOException
Writes string into writer, escaping &, ', ", <, and > with the XML excape strings.

Throws:
java.io.IOException

createResponse

public static Response createResponse(java.lang.String xml)
Creates a Response instance from the passed xml string


readFully

public static byte[] readFully(java.io.InputStream in)
                        throws java.io.IOException
Reads all the bytes from the passed input stream till end of stream reached.

Parameters:
in - The input stream to read from
Returns:
array of bytes read
Throws:
java.io.IOException - If any I/O exception occurs while reading data

match

public static boolean match(LimeXMLDocument replyDoc,
                            LimeXMLDocument queryDoc)
Compares the queryDoc with the replyDoc and finds out if the replyDoc is a match for the queryDoc

Parameters:
queryDoc - The query Document
replyDoc - potential reply Document
Returns:
true if the replyDoc is a match for the queryDoc, false otherwise

isMP3File

public static boolean isMP3File(java.lang.String in)

isMP3File

public static boolean isMP3File(java.io.File in)

getResponseArray

public static Response[] getResponseArray(java.util.List xmlDocuments)
Converts the given list of xml documents to an array of responses

Parameters:
xmlDocuments - List (of LimeXMLDocument) of xml documentst that need to be converted to instances of Response class
Returns:
Array of responses after converting passed xml documents

encodeXML

public static java.lang.String encodeXML(java.lang.String inData)
Parses the passed string, and encodes the special characters (used in xml for special purposes) with the appropriate codes. e.g. '<' is changed to '<'

Returns:
the encoded string. Returns null, if null is passed as argument

capitalizeFirst

public static java.lang.String capitalizeFirst(java.lang.String str)
takes a string and returns the same string with the first letter capitalized

11/2/01 Also replaces any "_" with " "


processColName

public static java.lang.String processColName(java.lang.String colName)
picks up the last strng in the colName and return it.


compress

public static byte[] compress(byte[] data)
Returns:
A properly formatted version of the input data.

uncompress

public static byte[] uncompress(byte[] data)
                         throws java.io.IOException
Returns:
Correctly uncompressed data (according to Content-Type header) May return a byte[] of length 0 if something bad happens.
Throws:
java.io.IOException

hashFile

public static byte[] hashFile(java.io.File toHash)
                       throws java.io.IOException
Hashes the file using bits and pieces of the file.

Returns:
The SHA hash bytes of hte input bytes.
Throws:
java.io.IOException - if hashing failed for any reason.

notValidXML

public static boolean notValidXML(java.lang.String inputStr)
Does an approximate check on the passed string for xml validity

Parameters:
inputStr - The string to be tested
Returns:
true, if the passed can be determined to be non-valid XML, false otherwise. Note that this method does only approximate evaluation. It guarantees only that if true is returned, then the passed string was not a valid xml string. The converse is not true.