com.sibvisions.util.xml
Class XmlWorker

java.lang.Object
  extended by org.xml.sax.helpers.DefaultHandler
      extended by com.sibvisions.util.xml.XmlWorker
All Implemented Interfaces:
org.xml.sax.ContentHandler, org.xml.sax.DTDHandler, org.xml.sax.EntityResolver, org.xml.sax.ErrorHandler, org.xml.sax.ext.LexicalHandler

public class XmlWorker
extends org.xml.sax.helpers.DefaultHandler
implements org.xml.sax.ext.LexicalHandler

Simple parser and writer for xml files. The parser extends the DefaultHandler to overwrite the necessary functions.
The newly created xml files will be encoded in UTF-8.

Example to work with a xml file:

 XmlWorker xmw = new XmlWorker();
 
 XmlNode xmnRead;
 XmlNode xmnChange;
 
 ArrayList<XmlNode> alElements;
 
 
 // Parse a xml file
 xmnRead = xmw.read(new FileInputStream("example.xml"));
     
 // Access a node list
 alElements = xmnRead.getNodes("/archive/element");
 
 // Count node list elements
 System.out.println(xmnRead.size("/archive/element"));
 
 // Access a node from a node list
 System.out.println(xmnRead.getNode("/archive/element(0)"));
 
 // Access a single node
 System.out.println(xmnRead.getNode("/archive/name"));
 
 // Access an attribute from a node
 System.out.println(xmnRead.getNode("/archive/element/attribute0"));
 
 // Change the value of a node
 xmnRead.setNode("/archive/element(1)/user", "xml");
 
 // Replace one node with another node (changes the node type to attribute and uses the new node name)
 xmnChange = new XmlNode(XmlNode.TYPE_ATTRIBUTE, "new");
 xmnChange.setValue("changed");
 
 xmnRead.setNode("/archive/element(1)/type", xmnChange);
 
 // Add a new node (the required elements (newelement, type) 
 // will be created automaticly)
 xmnRead.setNode("/archive/newelement/type", "new");

 // Set a new attribute with the name real_attrib_name
 xmnRead.insertNode("/archive/element/attrib_name", new XmlNode(XmlNode.TYPE_ATTRIBUTE, "real_attrib_name", "value"));
 
 // Remove a node
 xmnRead.removeNode("/archive/element(2)");
 
 // Insert a list-node with a value, between first and second element
 xmnRead.insertNode("/archive/element(1)", "between");
 
 // Insert a new-node without a value, between first and second element
 xmnRead.insertNode("/archive/element(1)", new XmlNode("break"));

 // Insert a new-node without a value, as last node in the "element" list
 xmnRead.insertNode("/archive/element", new XmlNode("last"));
 
 // Save the new xml structure
 xmw.write(new FileOutputStream("example_v2.xml"), xmnRead);
 


Constructor Summary
XmlWorker()
           
 
Method Summary
 void characters(char[] pChar, int pStart, int pLength)
          
 void comment(char[] pChar, int pStart, int pLength)
          
 void endCDATA()
          
 void endDocument()
          
 void endDTD()
          
 void endElement(java.lang.String pURI, java.lang.String pLocalName, java.lang.String pName)
          
 void endEntity(java.lang.String name)
          
 void error(org.xml.sax.SAXParseException pError)
          Throws the parse exception if validation is enabled.
 void fatalError(org.xml.sax.SAXParseException pError)
          Throws the parse exception if validation is enabled.
 int getIndentation()
          Gets the xml output indentation.
 java.lang.String getSchema()
          Gets the resource name of the schema file for validation of the xml.
 boolean isAutomaticDecrypt()
          Gets whether automatic decryption is enabled.
 boolean isEncrypted(java.lang.String pNodeName)
          Gets whether the node with the given name should be encrypted.
 boolean isFormatWhitespaces()
          Gets whether to format whitespaces in tag values.
 boolean isInsertNewLines()
          Gets whether new lines will be inserted for better readable.
 boolean isValidationEnabled()
          Gets the validation state.
 XmlNode read(java.io.File pSource)
          Parses a xml file and create a XmlNode structure.
 XmlNode read(java.io.InputStream pXmlSource)
          Parses a xml file and create a XmlNode structure.
 XmlNode readAndClose(java.io.InputStream pXmlSource)
          Parses a xml file and create a XmlNode structure.
static XmlNode readNode(java.io.File pFile)
          Reads a file and returns the node.
static XmlNode readNode(java.io.InputStream pStream)
          Reads a stream and returns the node.
 void setAutomaticDecrypt(boolean pAutoDecrypt)
          Sets whether automatic decryption should be used.
 void setDocumentLocator(org.xml.sax.Locator pLocator)
          Caches the document locator for line numbering.
 void setEncrypted(java.lang.String pNodeName, boolean pEncrypt)
          Sets the encryption mode of a node.
 void setFormatWhitespaces(boolean pFormat)
          Sets whether to format whitespaces in tag values.
 void setIndentation(int pIndent)
          Sets the xml output indentation.
 void setInsertNewLines(boolean pInsertNewLines)
          Sets whether new lines should be inserted for better readable xml files.
 void setSchema(java.lang.String pSchema)
          Sets the resource name of the schema file for validation of the xml.
 void setValidationEnabled(boolean pValidation)
          Set the general validation of xml en- or disabled.
 void startCDATA()
          
 void startDocument()
          
 void startDTD(java.lang.String name, java.lang.String publicId, java.lang.String systemId)
          
 void startElement(java.lang.String pNameSpaceURI, java.lang.String pLocalName, java.lang.String pName, org.xml.sax.Attributes pAttr)
          
 void startEntity(java.lang.String name)
          
 void startPrefixMapping(java.lang.String pPrefix, java.lang.String pUri)
          
 void warning(org.xml.sax.SAXParseException pError)
          Throws the parse exception if validation is enabled.
 void write(java.io.File pTarget, XmlNode pNode)
          Writes the structure of a XmlNode as xml stream to the specified target file.
 void write(java.io.OutputStream pXmlTarget, XmlNode pNode)
          Writes the structure of a XmlNode as xml stream to the specified target stream.
 
Methods inherited from class org.xml.sax.helpers.DefaultHandler
endPrefixMapping, ignorableWhitespace, notationDecl, processingInstruction, resolveEntity, skippedEntity, unparsedEntityDecl
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XmlWorker

public XmlWorker()
Method Detail

comment

public void comment(char[] pChar,
                    int pStart,
                    int pLength)
             throws org.xml.sax.SAXException

Specified by:
comment in interface org.xml.sax.ext.LexicalHandler
Throws:
org.xml.sax.SAXException

startCDATA

public void startCDATA()
                throws org.xml.sax.SAXException

Specified by:
startCDATA in interface org.xml.sax.ext.LexicalHandler
Throws:
org.xml.sax.SAXException

endCDATA

public void endCDATA()
              throws org.xml.sax.SAXException

Specified by:
endCDATA in interface org.xml.sax.ext.LexicalHandler
Throws:
org.xml.sax.SAXException

startDTD

public void startDTD(java.lang.String name,
                     java.lang.String publicId,
                     java.lang.String systemId)
              throws org.xml.sax.SAXException

Specified by:
startDTD in interface org.xml.sax.ext.LexicalHandler
Throws:
org.xml.sax.SAXException

endDTD

public void endDTD()
            throws org.xml.sax.SAXException

Specified by:
endDTD in interface org.xml.sax.ext.LexicalHandler
Throws:
org.xml.sax.SAXException

startEntity

public void startEntity(java.lang.String name)
                 throws org.xml.sax.SAXException

Specified by:
startEntity in interface org.xml.sax.ext.LexicalHandler
Throws:
org.xml.sax.SAXException

endEntity

public void endEntity(java.lang.String name)
               throws org.xml.sax.SAXException

Specified by:
endEntity in interface org.xml.sax.ext.LexicalHandler
Throws:
org.xml.sax.SAXException

setDocumentLocator

public void setDocumentLocator(org.xml.sax.Locator pLocator)
Caches the document locator for line numbering.

Specified by:
setDocumentLocator in interface org.xml.sax.ContentHandler
Overrides:
setDocumentLocator in class org.xml.sax.helpers.DefaultHandler
Parameters:
pLocator - the locator instance

warning

public void warning(org.xml.sax.SAXParseException pError)
             throws org.xml.sax.SAXException
Throws the parse exception if validation is enabled.

Specified by:
warning in interface org.xml.sax.ErrorHandler
Overrides:
warning in class org.xml.sax.helpers.DefaultHandler
Parameters:
pError -
Throws:
org.xml.sax.SAXException
See Also:
setValidationEnabled(boolean)

error

public void error(org.xml.sax.SAXParseException pError)
           throws org.xml.sax.SAXException
Throws the parse exception if validation is enabled.

Specified by:
error in interface org.xml.sax.ErrorHandler
Overrides:
error in class org.xml.sax.helpers.DefaultHandler
Parameters:
pError -
Throws:
org.xml.sax.SAXException
See Also:
setValidationEnabled(boolean)

fatalError

public void fatalError(org.xml.sax.SAXParseException pError)
                throws org.xml.sax.SAXException
Throws the parse exception if validation is enabled.

Specified by:
fatalError in interface org.xml.sax.ErrorHandler
Overrides:
fatalError in class org.xml.sax.helpers.DefaultHandler
Parameters:
pError -
Throws:
org.xml.sax.SAXException
See Also:
setValidationEnabled(boolean)

startDocument

public void startDocument()

Specified by:
startDocument in interface org.xml.sax.ContentHandler
Overrides:
startDocument in class org.xml.sax.helpers.DefaultHandler

endDocument

public void endDocument()

Specified by:
endDocument in interface org.xml.sax.ContentHandler
Overrides:
endDocument in class org.xml.sax.helpers.DefaultHandler

startElement

public void startElement(java.lang.String pNameSpaceURI,
                         java.lang.String pLocalName,
                         java.lang.String pName,
                         org.xml.sax.Attributes pAttr)
                  throws org.xml.sax.SAXException

Specified by:
startElement in interface org.xml.sax.ContentHandler
Overrides:
startElement in class org.xml.sax.helpers.DefaultHandler
Throws:
org.xml.sax.SAXException

characters

public void characters(char[] pChar,
                       int pStart,
                       int pLength)

Specified by:
characters in interface org.xml.sax.ContentHandler
Overrides:
characters in class org.xml.sax.helpers.DefaultHandler

endElement

public void endElement(java.lang.String pURI,
                       java.lang.String pLocalName,
                       java.lang.String pName)
                throws org.xml.sax.SAXException

Specified by:
endElement in interface org.xml.sax.ContentHandler
Overrides:
endElement in class org.xml.sax.helpers.DefaultHandler
Throws:
org.xml.sax.SAXException

startPrefixMapping

public void startPrefixMapping(java.lang.String pPrefix,
                               java.lang.String pUri)

Specified by:
startPrefixMapping in interface org.xml.sax.ContentHandler
Overrides:
startPrefixMapping in class org.xml.sax.helpers.DefaultHandler

read

public XmlNode read(java.io.File pSource)
             throws org.xml.sax.SAXException,
                    java.io.IOException,
                    javax.xml.parsers.ParserConfigurationException
Parses a xml file and create a XmlNode structure.

Parameters:
pSource - XML input file
Returns:
xml file in node structure
Throws:
org.xml.sax.SAXException - if the file has parse errors
java.io.IOException - if any IO error occurs
javax.xml.parsers.ParserConfigurationException - if the SAX parser is not well configured

readAndClose

public XmlNode readAndClose(java.io.InputStream pXmlSource)
                     throws org.xml.sax.SAXException,
                            java.io.IOException,
                            javax.xml.parsers.ParserConfigurationException
Parses a xml file and create a XmlNode structure. This closes the stream after reading.

Parameters:
pXmlSource - XML input source
Returns:
xml file in node structure
Throws:
org.xml.sax.SAXException - if the xml has parse errors
java.io.IOException - if any IO error occurs
javax.xml.parsers.ParserConfigurationException - if the SAX parser is not well configured

read

public XmlNode read(java.io.InputStream pXmlSource)
             throws org.xml.sax.SAXException,
                    java.io.IOException,
                    javax.xml.parsers.ParserConfigurationException
Parses a xml file and create a XmlNode structure.

Parameters:
pXmlSource - XML input source
Returns:
xml file in node structure
Throws:
org.xml.sax.SAXException - if the xml has parse errors
java.io.IOException - if any IO error occurs
javax.xml.parsers.ParserConfigurationException - if the SAX parser is not well configured

write

public void write(java.io.File pTarget,
                  XmlNode pNode)
           throws java.io.IOException
Writes the structure of a XmlNode as xml stream to the specified target file.

Parameters:
pTarget - target file for the xml content
pNode - node for xml creation
Throws:
java.io.IOException - if the xml can not be created

write

public void write(java.io.OutputStream pXmlTarget,
                  XmlNode pNode)
           throws java.io.IOException
Writes the structure of a XmlNode as xml stream to the specified target stream.

Parameters:
pXmlTarget - target stream for the xml content
pNode - node for xml creation
Throws:
java.io.IOException - if the xml can not be created

setIndentation

public void setIndentation(int pIndent)
Sets the xml output indentation.

Parameters:
pIndent - space character count

getIndentation

public int getIndentation()
Gets the xml output indentation. This methods doesn't support the intentation of read xml files.

Returns:
space character count

setSchema

public void setSchema(java.lang.String pSchema)
Sets the resource name of the schema file for validation of the xml.

Parameters:
pSchema - the resource name of the schema file /package/schema.xsd

getSchema

public java.lang.String getSchema()
Gets the resource name of the schema file for validation of the xml.

Returns:
the resource name or null if schema validation is disabled

setValidationEnabled

public void setValidationEnabled(boolean pValidation)
Set the general validation of xml en- or disabled. The validation is disabled by default.

Parameters:
pValidation - true to enable the general validation or false to disable it
See Also:
isValidationEnabled()

isValidationEnabled

public boolean isValidationEnabled()
Gets the validation state.

Returns:
true if validation of xml is enabled, false otherwise
See Also:
setValidationEnabled(boolean)

setInsertNewLines

public void setInsertNewLines(boolean pInsertNewLines)
Sets whether new lines should be inserted for better readable xml files. This option is only used when a node will be written to an OutputStream. The default setting is false.

Parameters:
pInsertNewLines - true to insert new lines
See Also:
write(OutputStream, XmlNode)

isInsertNewLines

public boolean isInsertNewLines()
Gets whether new lines will be inserted for better readable.

Returns:
true if new lines will be inserted, false otherwise
See Also:
setInsertNewLines(boolean)

setEncrypted

public void setEncrypted(java.lang.String pNodeName,
                         boolean pEncrypt)
Sets the encryption mode of a node.

Parameters:
pNodeName - the node name without index. It is not possible to encrypt single list elements.
pEncrypt - true to encrypt the node, false otherwise

isEncrypted

public boolean isEncrypted(java.lang.String pNodeName)
Gets whether the node with the given name should be encrypted.

Parameters:
pNodeName - the node name without index
Returns:
true if the node name is encrypted

setAutomaticDecrypt

public void setAutomaticDecrypt(boolean pAutoDecrypt)
Sets whether automatic decryption should be used.

Parameters:
pAutoDecrypt - true to decrypt all tags with the attribute encoded="true", false to ignore automatic decryption

isAutomaticDecrypt

public boolean isAutomaticDecrypt()
Gets whether automatic decryption is enabled.

Returns:
true if automatic node decryption is enabled, false otherwise

readNode

public static XmlNode readNode(java.io.File pFile)
                        throws java.lang.Exception
Reads a file and returns the node.

Parameters:
pFile - the file
Returns:
the parsed xml node
Throws:
java.lang.Exception - if reading file failed

readNode

public static XmlNode readNode(java.io.InputStream pStream)
                        throws java.lang.Exception
Reads a stream and returns the node.

Parameters:
pStream - the stream
Returns:
the parsed xml node
Throws:
java.lang.Exception - if reading file failed

setFormatWhitespaces

public void setFormatWhitespaces(boolean pFormat)
Sets whether to format whitespaces in tag values.

Parameters:
pFormat - true to format, false to keep the value as it is

isFormatWhitespaces

public boolean isFormatWhitespaces()
Gets whether to format whitespaces in tag values.

Returns:
true if whitespaces in tag values will be formatted, false otherwise


Copyright © 2009 SIB Visions GmbH. All Rights Reserved.