Package com.microstrategy.utils.xml
Class SAXSupport
- java.lang.Object
-
- com.microstrategy.utils.xml.SAXSupport
-
public abstract class SAXSupport extends java.lang.Object
This is a wrapper around the SAX parser that provides the following services:- Hides the actual parser implementation
- Maintains the stack of active
ContentHandlers
- Provide set of static helper methods for dealing with XML content
This class is not thread-safe.
- Since:
- MicroStrategy Web 7.3.1 or earlier
-
-
Constructor Summary
Constructors Constructor Description SAXSupport()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description static boolean
getBoolAttribute(org.xml.sax.Attributes attrs, java.lang.String name)
This method returns true if specified attribute is present inattrs
collection and has value "1".static boolean
getBoolAttribute(org.xml.sax.Attributes attrs, java.lang.String name, boolean defaultVal)
If specified attribute is present inattrs
collection this method will return true if the attribute hase value "1" and false if it has value "0".java.lang.Object
getContextObject(java.lang.String key)
static javax.xml.parsers.SAXParserFactory
getDefaultFactory()
static SAXSupport
getInstance()
A factory method that returns newSAXSupport
instance.static SAXSupport
getInstance(java.util.Set<java.lang.String> allowEntityPath)
A factory method that returns newSAXSupport
instance.static SAXSupport
getInstance(javax.xml.parsers.SAXParserFactory factory)
A factory method that returns newSAXSupport
instance.static SAXSupport
getInstance(javax.xml.parsers.SAXParserFactory factory, java.util.Set<java.lang.String> allowEntityPath)
A factory method that returns newSAXSupport
instance.static int
getIntAttribute(java.lang.String tag, org.xml.sax.Attributes attrs, java.lang.String name)
This method returns attribute value from theattrs
collection converted to the integer.static int
getIntAttribute(java.lang.String tag, org.xml.sax.Attributes attrs, java.lang.String name, int defaultVal)
This method returns attribute value from theattrs
collection converted to the integer ordefaultVal
if the attribute does not exist.abstract javax.xml.parsers.SAXParser
getParser()
ReturnsSAXParser
associated with this object.static java.lang.String
getStrAttribute(java.lang.String tag, org.xml.sax.Attributes attrs, java.lang.String name)
This method returns attribute value from theattrs
collection.static java.lang.String
getStrAttribute(org.xml.sax.Attributes attrs, java.lang.String name, java.lang.String defaultVal)
This method returns attribute value from theattrs
collection ordefaultVal
if the attribute does not exist.abstract org.xml.sax.XMLReader
getXMLReader()
static void
invalidAttrFormat(java.lang.String tag, java.lang.String name, java.lang.String val)
ThrowsSAXSupportException
exception with "Invalid attribute value format" messagestatic void
missingAttribute(java.lang.String tag, java.lang.String name)
ThrowsSAXSupportException
exception with "Missing attribute" messagestatic javax.xml.parsers.SAXParserFactory
newSAXParserFactory()
ReturnsSAXParserFactory
with doctype disabled by default to avoid XXE attack.abstract void
parse(java.io.File xmlFile, org.xml.sax.ContentHandler contHandler)
Parses XML file using specified content handlerabstract void
parse(java.io.InputStream inputStream, org.xml.sax.ContentHandler contHandler)
Parses XML presented byinputStream
using specified content handlerabstract void
parse(java.lang.String xml, org.xml.sax.ContentHandler contHandler)
Parses XML string using specified content handlerabstract void
parse(org.xml.sax.InputSource iSrc, org.xml.sax.ContentHandler contHandler)
Parses XML presented byiSrc
using specified content handlerabstract org.xml.sax.ContentHandler
popContentHandler(java.lang.String uri, java.lang.String local, java.lang.String qName)
This method shall be called from the content handler'sendElelment
method to reattach the parent content handler to the xmlReader.abstract void
pushContentHandler(AbstractHandler contHandler)
This method shall be called from the content handler'sstartElelment
method to attach another content handler to the xmlReader.abstract void
pushContentHandler(org.xml.sax.ContentHandler contHandler)
This method shall be called from the content handler'sstartElelment
method to attach another content handler to the xmlReader.void
putContextObject(java.lang.String key, java.lang.Object value)
static void
unexpectedElement(java.lang.String tag, java.lang.String parent)
ThrowsSAXSupportException
exception with "Unexpected element" messagestatic void
unexpectedEndDocument(java.lang.Object handler)
Call this method to report unexpected end of document event.static void
unexpectedEndElement(java.lang.Object handler)
Call this method to report unexpected end of element event.
-
-
-
Method Detail
-
newSAXParserFactory
public static javax.xml.parsers.SAXParserFactory newSAXParserFactory()
ReturnsSAXParserFactory
with doctype disabled by default to avoid XXE attack.- Returns:
SAXParserFactory
-
getParser
public abstract javax.xml.parsers.SAXParser getParser()
ReturnsSAXParser
associated with this object. Please usegetXMLReader()
instead of this method as some application servers will create aXMLReader
with eachSAXParser.getXMLReader()
invocation.- Returns:
SAXParser
associated with this object.
-
getXMLReader
public abstract org.xml.sax.XMLReader getXMLReader()
- Returns:
- return the underlying
XMLReader
instance - Since:
- MicroStrategy Web 9.0.1
-
getContextObject
public java.lang.Object getContextObject(java.lang.String key)
- Since:
- MicroStrategy Web 8.0.0
-
putContextObject
public void putContextObject(java.lang.String key, java.lang.Object value)
- Since:
- MicroStrategy Web 8.0.0
-
parse
public abstract void parse(java.lang.String xml, org.xml.sax.ContentHandler contHandler) throws org.xml.sax.SAXException
Parses XML string using specified content handler- Parameters:
xml
- XML stringcontHandler
- a content handler- Throws:
org.xml.sax.SAXException
- if something is wrong with the input xml
-
parse
public abstract void parse(java.io.File xmlFile, org.xml.sax.ContentHandler contHandler) throws org.xml.sax.SAXException, java.io.IOException
Parses XML file using specified content handler- Parameters:
xmlFile
- XML filecontHandler
- a content handler- Throws:
org.xml.sax.SAXException
- if something is wrong with the input xmljava.io.IOException
- if there are problems with input file
-
parse
public abstract void parse(java.io.InputStream inputStream, org.xml.sax.ContentHandler contHandler) throws org.xml.sax.SAXException, java.io.IOException
Parses XML presented byinputStream
using specified content handler- Parameters:
inputStream
- Input Stream presenting XML documentcontHandler
- a content handler- Throws:
org.xml.sax.SAXException
- if something is wrong with the input xmljava.io.IOException
- if there are problems with input source
-
parse
public abstract void parse(org.xml.sax.InputSource iSrc, org.xml.sax.ContentHandler contHandler) throws org.xml.sax.SAXException, java.io.IOException
Parses XML presented byiSrc
using specified content handler- Parameters:
iSrc
- input source presenting XML documentcontHandler
- a content handler- Throws:
org.xml.sax.SAXException
- if something is wrong with the input xmljava.io.IOException
- if there are problems with input source
-
pushContentHandler
public abstract void pushContentHandler(AbstractHandler contHandler)
This method shall be called from the content handler'sstartElelment
method to attach another content handler to the xmlReader. It detaches the current content handler from the xmlReader and pushes it into the stack, then attaches theto the xmlReader. It also sets contHandler.parser
attribute tothis
.- Parameters:
contHandler
- a new content handler
-
pushContentHandler
public abstract void pushContentHandler(org.xml.sax.ContentHandler contHandler)
This method shall be called from the content handler'sstartElelment
method to attach another content handler to the xmlReader. It detaches the current content handler from the xmlReader and pushes it into the stack, then attaches theto the xmlReader. - Parameters:
contHandler
- a new content handler- Throws:
java.lang.IllegalStateException
- if this method is called before the parse method was called.
-
popContentHandler
public abstract org.xml.sax.ContentHandler popContentHandler(java.lang.String uri, java.lang.String local, java.lang.String qName) throws org.xml.sax.SAXException, java.lang.IllegalStateException
This method shall be called from the content handler'sendElelment
method to reattach the parent content handler to the xmlReader. Detaches the current content handler from the xmlReader. Pop's from the stack a "parent" content handler, and attaches it to the xmlReader.The
endElement
method is called on the new content handler before attaching it to the xmlReader. It is done to allow it to adjust it's level count properly and to do other actions, if any.- Parameters:
uri
- The Namespace URI, or the empty string if the element has no Namespace URI or if Namespace processing is not being performed.local
- The local name (without prefix), or the empty string if Namespace processing is not being performed.qName
- The qualified name (with prefix), or the empty string if qualified names are not available.- Returns:
- an old content handler
- Throws:
java.lang.IllegalStateException
- if the handler stack is emptyorg.xml.sax.SAXException
- can be thrown from theendElement
method
-
getInstance
public static SAXSupport getInstance() throws org.xml.sax.SAXException, XMLSupportRuntimeException
A factory method that returns newSAXSupport
instance. The default factory will be used to create theSAXParser
associated with the instance.- Returns:
- new
SAXSupport
instance. - Throws:
XMLSupportRuntimeException
- if a parser cannot be created which satisfies the requested configuration.org.xml.sax.SAXException
- If error occurs during create the parser or the xml reader.
-
getInstance
public static SAXSupport getInstance(java.util.Set<java.lang.String> allowEntityPath) throws org.xml.sax.SAXException, XMLSupportRuntimeException
A factory method that returns newSAXSupport
instance. The default factory will be used to create theSAXParser
associated with the instance.- Parameters:
allowEntityPath
- a set that includes the paths which are allowed to be used in external entities.- Returns:
- new
SAXSupport
instance. - Throws:
XMLSupportRuntimeException
- if a parser cannot be created which satisfies the requested configuration.org.xml.sax.SAXException
- If error occurs during create the parser or the xml reader.
-
getInstance
public static SAXSupport getInstance(javax.xml.parsers.SAXParserFactory factory) throws org.xml.sax.SAXException, XMLSupportRuntimeException
A factory method that returns newSAXSupport
instance.- Parameters:
factory
- a factory that shall be used to createSAXParser
associated with the instance.- Returns:
- new
SAXSupport
instance. - Throws:
XMLSupportRuntimeException
- if a parser cannot be created which satisfies the requested configuration.org.xml.sax.SAXException
- If error occurs during create the parser or the xml reader.
-
getInstance
public static SAXSupport getInstance(javax.xml.parsers.SAXParserFactory factory, java.util.Set<java.lang.String> allowEntityPath) throws org.xml.sax.SAXException, XMLSupportRuntimeException
A factory method that returns newSAXSupport
instance.- Parameters:
factory
- a factory that shall be used to createSAXParser
associated with the instance.allowEntityPath
- a set that includes the paths which are allowed to be used in external entities.- Returns:
- new
SAXSupport
instance. - Throws:
XMLSupportRuntimeException
- if a parser cannot be created which satisfies the requested configuration.org.xml.sax.SAXException
- If error occurs during create the parser or the xml reader.
-
getDefaultFactory
public static javax.xml.parsers.SAXParserFactory getDefaultFactory()
- Returns:
- default
SAXParserFactory
-
unexpectedEndDocument
public static void unexpectedEndDocument(java.lang.Object handler) throws java.lang.IllegalStateException
Call this method to report unexpected end of document event. Normally it shall be used in theendDocument
methods of inner event handlers.- Parameters:
handler
- the handler that detected the error- Throws:
java.lang.IllegalStateException
- If the handler does not support endDocument() method.
-
unexpectedEndElement
public static void unexpectedEndElement(java.lang.Object handler) throws java.lang.IllegalStateException
Call this method to report unexpected end of element event. Normally it shall be called from theendElement
methods if the level count goes below zero.- Parameters:
handler
- the handler that detected the error- Throws:
java.lang.IllegalStateException
- If the handler does not support endElement() method.
-
getStrAttribute
public static java.lang.String getStrAttribute(java.lang.String tag, org.xml.sax.Attributes attrs, java.lang.String name) throws SAXSupportException
This method returns attribute value from theattrs
collection. It will throwWebSAXException
if the attribute does not exist. Use this method for mandatory attributes.- Parameters:
tag
- element's tag name. Used in the diagnostic message.attrs
- attribute collectionname
- attribute name- Returns:
- attribute value
- Throws:
SAXSupportException
- if attribute does not exists in the collection
-
getStrAttribute
public static java.lang.String getStrAttribute(org.xml.sax.Attributes attrs, java.lang.String name, java.lang.String defaultVal)
This method returns attribute value from theattrs
collection ordefaultVal
if the attribute does not exist. Use this method for optional attributes.- Parameters:
attrs
- attribute collectionname
- attribute namedefaultVal
- default value to be used if the attribute does not exist- Returns:
- attribute value
-
getIntAttribute
public static int getIntAttribute(java.lang.String tag, org.xml.sax.Attributes attrs, java.lang.String name) throws SAXSupportException
This method returns attribute value from theattrs
collection converted to the integer. It will throwSAXSupportException
if the attribute does not exist. Use this method for mandatory attributes.- Parameters:
tag
- element's tag name. Used in the diagnostic message.attrs
- attribute collectionname
- attribute name- Returns:
- attribute value
- Throws:
SAXSupportException
- if attribute does not exists in the collection or if its value is not an integer
-
getIntAttribute
public static int getIntAttribute(java.lang.String tag, org.xml.sax.Attributes attrs, java.lang.String name, int defaultVal) throws SAXSupportException
This method returns attribute value from theattrs
collection converted to the integer ordefaultVal
if the attribute does not exist. Use this method for optional attributes.- Parameters:
tag
- element's tag name. Used in the diagnostic message.attrs
- attribute collectionname
- attribute namedefaultVal
- default value to be used if the attribute does not exist- Returns:
- attribute value
- Throws:
SAXSupportException
- if attribute value is not an integer
-
getBoolAttribute
public static boolean getBoolAttribute(org.xml.sax.Attributes attrs, java.lang.String name) throws SAXSupportException
This method returns true if specified attribute is present inattrs
collection and has value "1". It returns false if the attribute is absent or if it has value "0". It will throwSAXSupportException
in any other case.Use this method for boolean attributes that can be present with both "1" and "0" values
- Parameters:
attrs
- attribute collectionname
- attribute name- Returns:
- true or false.
- Throws:
SAXSupportException
- if attribute value is not "0" or "1"
-
getBoolAttribute
public static boolean getBoolAttribute(org.xml.sax.Attributes attrs, java.lang.String name, boolean defaultVal) throws SAXSupportException
If specified attribute is present inattrs
collection this method will return true if the attribute hase value "1" and false if it has value "0". If the attribute is absent then the method will returndefaultVal
.- Parameters:
attrs
- attribute collectionname
- attribute namedefaultVal
- a default value- Returns:
- true or false.
- Throws:
SAXSupportException
- if attribute value is not "0" or "1"
-
missingAttribute
public static void missingAttribute(java.lang.String tag, java.lang.String name) throws SAXSupportException
ThrowsSAXSupportException
exception with "Missing attribute" message- Parameters:
tag
- tag namename
- attribute name- Throws:
SAXSupportException
- exception with "Missing attribute" message.
-
invalidAttrFormat
public static void invalidAttrFormat(java.lang.String tag, java.lang.String name, java.lang.String val) throws SAXSupportException
ThrowsSAXSupportException
exception with "Invalid attribute value format" message- Parameters:
tag
- tag namename
- attribute nameval
- value- Throws:
SAXSupportException
- exception with "Invalid attribute value format" message
-
unexpectedElement
public static void unexpectedElement(java.lang.String tag, java.lang.String parent) throws SAXSupportException
ThrowsSAXSupportException
exception with "Unexpected element" message- Parameters:
tag
- tag nameparent
- parent tag name- Throws:
SAXSupportException
- exception with "Unexpected element" message
-
-