com.reverseXSL.util
Class Formatters

java.lang.Object
  extended by com.reverseXSL.util.Formatters

public class Formatters
extends java.lang.Object

A collection of utility methods bound to formatting data.


Constructor Summary
Formatters()
           
 
Method Summary
static java.lang.String hexEscape(byte[] bytes, int start, int length)
          Utility method to hex-encode a byte array so that it becomes XML-safe and readable whatever the enclosed byte values.
static byte[] hexUnEscape(java.lang.String s)
          Inverse of hexEscape(byte[], int, int).
static java.lang.String niceXML(java.lang.StringBuffer uglyXML, java.lang.String indentPattern, java.lang.String EOL)
          There a BIG issue with indentation according to the JVM version and/or preferred XML parser on the classpath.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Formatters

public Formatters()
Method Detail

hexEscape

public static java.lang.String hexEscape(byte[] bytes,
                                         int start,
                                         int length)
Utility method to hex-encode a byte array so that it becomes XML-safe and readable whatever the enclosed byte values. Works like 'quoted-printable' because most printable characters are not encoded.

NOTE: I have not found a proper XML character escaping method so here is one custom-developed based on a bare conversion table (fastest access) taking care of '&' , '>' , and '<' (for XML safeness) and all non-printable 7bit ASCII. Magic is, most EDI messages would just look like they are really (but for CR replaced by %0D)

Parameters:
bytes - the binary (byte array) version of the data to encode
start - the starting offset in the byte array
length - the number of bytes to hex-encode. If start+length is greater than the size of the byte array, an array out of bounds exception is thrown as one shall expect.
Returns:
null if arg is null, else the hex-encoded string

hexUnEscape

public static byte[] hexUnEscape(java.lang.String s)
Inverse of hexEscape(byte[], int, int).

Parameters:
s - the string to decode into a byte array
Returns:
a byte array of the original binary data

niceXML

public static java.lang.String niceXML(java.lang.StringBuffer uglyXML,
                                       java.lang.String indentPattern,
                                       java.lang.String EOL)
There a BIG issue with indentation according to the JVM version and/or preferred XML parser on the classpath. The following sprouted from an afternoon of despair. A last resort method that is guaranteed to work... It took me less than 2 hours to build and test... if I knew, I would have done earlier to save my time.

I wanted a method that worked in all circumstances; the issue is actually with space-only text nodes attached elsewhere in the XML document tree that reflect indentation and line feeds. Not only these may eat a significant portion of your XML documents, but diverse XSLT, DOM and SAX tools actually exhibit different behaviours with the result of scrambling the output. I was unable to find a common compatible core.

BEWARE OF LIMITATIONS: The formatting utility doesn't work on XML documents that mix non-blank text nodes with child elements within the same parent element... kind of bad practice indeed but actually fully allowed by the XML standards.

THEREFORE, PLEASE ENJOY THIS FACILITY ONLY FOR TESTING AND CONTINUE FEEDING LOCAL APPLICATIONS WITH POTENTIALLY UGLY XMLs. THEY DON'T CARE LIKE OUR EYES DO.

Parameters:
uglyXML - whatever raw output that is anyhow a valid XML document
indentPattern - try for instance " |" and see the magic!
Returns:
nicely indented XML as String