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:
ContentHandler, DTDHandler, EntityResolver, ErrorHandler, LexicalHandler

public class XmlWorker
extends DefaultHandler
implements 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(String pURI, String pLocalName, String pName)
          
 void endEntity(String name)
          
 void error(SAXParseException pError)
          Throws the parse exception if validation is enabled.
 void fatalError(SAXParseException pError)
          Throws the parse exception if validation is enabled.
 int getIndentation()
          Gets the xml output indentation.
 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(String pNodeName)
          Gets whether the node with the given name should be encrypted.
 boolean isInsertNewLines()
          Gets whether new lines will be inserted for better readable.
 boolean isValidationEnabled()
          Gets the validation state.
 XmlNode read(File pSource)
          Parses a xml file and create a XmlNode structure.
 XmlNode read(InputStream pXmlSource)
          Parses a xml file and create a XmlNode structure.
 void setAutomaticDecrypt(boolean pAutoDecrypt)
          Sets whether automatic decryption should be used.
 void setDocumentLocator(Locator pLocator)
          Caches the document locator for line numbering.
 void setEncrypted(String pNodeName, boolean pEncrypt)
          Sets the encryption mode of a node.
 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(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(String name, String publicId, String systemId)
          
 void startElement(String pNameSpaceURI, String pLocalName, String pName, Attributes pAttr)
          
 void startEntity(String name)
          
 void startPrefixMapping(String pPrefix, String pUri)
          
 void warning(SAXParseException pError)
          Throws the parse exception if validation is enabled.
 void write(File pTarget, XmlNode pNode)
          Writes the structure of a XmlNode as xml stream to the specified target file.
 void write(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 SAXException

Specified by:
comment in interface LexicalHandler
Throws:
SAXException

startCDATA

public void startCDATA()
                throws SAXException

Specified by:
startCDATA in interface LexicalHandler
Throws:
SAXException

endCDATA

public void endCDATA()
              throws SAXException

Specified by:
endCDATA in interface LexicalHandler
Throws:
SAXException

startDTD

public void startDTD(String name,
                     String publicId,
                     String systemId)
              throws SAXException

Specified by:
startDTD in interface LexicalHandler
Throws:
SAXException

endDTD

public void endDTD()
            throws SAXException

Specified by:
endDTD in interface LexicalHandler
Throws:
SAXException

startEntity

public void startEntity(String name)
                 throws SAXException

Specified by:
startEntity in interface LexicalHandler
Throws:
SAXException

endEntity

public void endEntity(String name)
               throws SAXException

Specified by:
endEntity in interface LexicalHandler
Throws:
SAXException

setDocumentLocator

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

Specified by:
setDocumentLocator in interface ContentHandler
Overrides:
setDocumentLocator in class DefaultHandler
Parameters:
pLocator - the locator instance

warning

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

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

error

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

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

fatalError

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

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

startDocument

public void startDocument()

Specified by:
startDocument in interface ContentHandler
Overrides:
startDocument in class DefaultHandler

endDocument

public void endDocument()

Specified by:
endDocument in interface ContentHandler
Overrides:
endDocument in class DefaultHandler

startElement

public void startElement(String pNameSpaceURI,
                         String pLocalName,
                         String pName,
                         Attributes pAttr)
                  throws SAXException

Specified by:
startElement in interface ContentHandler
Overrides:
startElement in class DefaultHandler
Throws:
SAXException

characters

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

Specified by:
characters in interface ContentHandler
Overrides:
characters in class DefaultHandler

endElement

public void endElement(String pURI,
                       String pLocalName,
                       String pName)
                throws SAXException

Specified by:
endElement in interface ContentHandler
Overrides:
endElement in class DefaultHandler
Throws:
SAXException

startPrefixMapping

public void startPrefixMapping(String pPrefix,
                               String pUri)

Specified by:
startPrefixMapping in interface ContentHandler
Overrides:
startPrefixMapping in class DefaultHandler

read

public XmlNode read(File pSource)
             throws SAXException,
                    IOException,
                    ParserConfigurationException
Parses a xml file and create a XmlNode structure.

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

read

public XmlNode read(InputStream pXmlSource)
             throws SAXException,
                    IOException,
                    ParserConfigurationException
Parses a xml file and create a XmlNode structure.

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

write

public void write(File pTarget,
                  XmlNode pNode)
           throws 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:
IOException - if the xml can not be created

write

public void write(OutputStream pXmlTarget,
                  XmlNode pNode)
           throws 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:
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(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 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(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(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


Copyright © 2009 SIB Visions GmbH. All Rights Reserved.