Package cdc.util.data.xml
Class XmlDataReader
- java.lang.Object
-
- cdc.util.data.xml.XmlDataReader
-
public class XmlDataReader extends Object
Class used to read an XML source and produce a Document.It is possible, during loading, to:
- filter (keep or remove) attributes
- convert attributes names
- convert attributes values
- filter (keep or remove) elements at creation time (pre) and when all its children are known (post).
- convert elements names
- ignore (remove) comments
- ignore (remove) spaces
- Author:
- Damien Carbonne
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classXmlDataReader.Feature
-
Field Summary
Fields Modifier and Type Field Description protected AttributePredicateattributeFilterThe filter attributes.protected AttributeNameConverterattributeNameConverterAttribute name converter.protected AttributeValueConverterattributeValueConverterAttribute value converter.protected ElementNameConverterelementNameConverterElement name converter.protected ElementPredicateelementPostFilterThe filter to use when all the children of an element are created.protected ElementPredicateelementPreFilterThe filter to use when the element is created.protected EntityResolverentityResolverThe entity resolver.protected static org.apache.logging.log4j.LoggerLOGGERprotected TextContentConvertertextContentConverterText content converter.
-
Constructor Summary
Constructors Constructor Description XmlDataReader()XmlDataReader(XmlDataReader.Feature... features)
-
Method Summary
-
-
-
Field Detail
-
LOGGER
protected static final org.apache.logging.log4j.Logger LOGGER
-
entityResolver
protected EntityResolver entityResolver
The entity resolver.
-
attributeFilter
protected AttributePredicate attributeFilter
The filter attributes.Only accepted attributes are kept and transformed.
-
attributeNameConverter
protected AttributeNameConverter attributeNameConverter
Attribute name converter.Applied on accepted attributes.
-
attributeValueConverter
protected AttributeValueConverter attributeValueConverter
Attribute value converter.Applied on accepted attributes.
-
elementPreFilter
protected ElementPredicate elementPreFilter
The filter to use when the element is created.
-
elementPostFilter
protected ElementPredicate elementPostFilter
The filter to use when all the children of an element are created.
-
elementNameConverter
protected ElementNameConverter elementNameConverter
Element name converter.Applied on elements that are pre accepted.
-
textContentConverter
protected TextContentConverter textContentConverter
Text content converter.
-
-
Constructor Detail
-
XmlDataReader
public XmlDataReader()
-
XmlDataReader
public XmlDataReader(XmlDataReader.Feature... features)
-
-
Method Detail
-
isEnabled
public final boolean isEnabled(XmlDataReader.Feature feature)
Returnstruewhen a feature is enabled.- Parameters:
feature- The feature.- Returns:
trueiffeatureis enabled.
-
setEnabled
public final void setEnabled(XmlDataReader.Feature feature, boolean enabled)
Enables or disables a feature.- Parameters:
feature- The feature.enabled- Iftrue, the feature is enabled. It is disabled orthewise.
-
getEntityResolver
public EntityResolver getEntityResolver()
- Returns:
- The entity resolver.
-
setEntityResolver
public void setEntityResolver(EntityResolver resolver)
Sets the entity resolver.WARNING: This has interactions with
XmlDataReader.Feature.DUMMY_ENTITY_RESOLVER.- Parameters:
resolver- The entity resolver.
-
getAttributeFilter
public AttributePredicate getAttributeFilter()
- Returns:
- The attribute filter.
-
setAttributeFilter
public void setAttributeFilter(AttributePredicate filter)
Sets the attribute filter.The name and value that are passed to the filter are the original name and value, before any name or value conversion happens.
- Parameters:
filter- The filter.- Throws:
IllegalArgumentException- Whenfilteris null.
-
getAttributeNameConverter
public AttributeNameConverter getAttributeNameConverter()
- Returns:
- The attribute name converter.
-
setAttributeNameConverter
public void setAttributeNameConverter(AttributeNameConverter converter)
Sets the attribute name converter.The conversion is applied after attribute filtering.
- Parameters:
converter- The converter.- Throws:
IllegalArgumentException- Whenconverteris null.
-
getAttributeValueConverter
public AttributeValueConverter getAttributeValueConverter()
- Returns:
- The attribute value converter.
-
setAttributeValueConverter
public void setAttributeValueConverter(AttributeValueConverter converter)
Sets the attribute value converter.The conversion is applied after attribute filtering.
- Parameters:
converter- The converter.- Throws:
IllegalArgumentException- Whenconverteris null.
-
getElementPreFilter
public ElementPredicate getElementPreFilter()
- Returns:
- the element pre filter.
-
setElementPreFilter
public void setElementPreFilter(ElementPredicate filter)
Sets the element pre filter.WARNING:
- The element name is the original one (before any conversion happens).
- The attributes names and values are the converted ones (after all conversions have happened).
- Only remaining attributes are available.
- Parameters:
filter- The filter.- Throws:
IllegalArgumentException- Whenfilteris null.
-
getElementPostFilter
public ElementPredicate getElementPostFilter()
- Returns:
- the element post filter.
-
setElementPostFilter
public void setElementPostFilter(ElementPredicate filter)
Sets the element post filter.WARNING:
- The element name is the converted one (after conversion has happened).
- The attributes names and values are the converted ones (after all conversions have happened).
- Only remaining attributes are available.
- Parameters:
filter- The filter.- Throws:
IllegalArgumentException- Whenfilteris null.
-
getElementNameConverter
public ElementNameConverter getElementNameConverter()
- Returns:
- The element name converter.
-
setElementNameConverter
public void setElementNameConverter(ElementNameConverter converter)
Sets the element name converter.- Parameters:
converter- The converter.- Throws:
IllegalArgumentException- Whenconverteris null.
-
getTextContentConverter
public TextContentConverter getTextContentConverter()
-
setTextContentConverter
public void setTextContentConverter(TextContentConverter converter)
-
read
public Document read(InputStream is, Compressor compressor) throws IOException
- Throws:
IOException
-
read
public Document read(InputStream is) throws IOException
- Throws:
IOException
-
readRoot
public Element readRoot(InputStream is, Compressor compressor) throws IOException
- Throws:
IOException
-
readRoot
public Element readRoot(InputStream is) throws IOException
- Throws:
IOException
-
read
public Document read(InputStream is, String systemId, Compressor compressor) throws IOException
- Throws:
IOException
-
read
public Document read(InputStream is, String systemId) throws IOException
Reads an InputStream.- Parameters:
is- The InputStream.systemId- The systemId which is needed for resolving relative URIs.- Returns:
- The corresponding Document.
- Throws:
IOException- When an IO error occurs.
-
readRoot
public Element readRoot(InputStream is, String systemId, Compressor compressor) throws IOException
- Throws:
IOException
-
readRoot
public Element readRoot(InputStream is, String systemId) throws IOException
Reads an InputStream.- Parameters:
is- The InputStream.systemId- The systemId which is needed for resolving relative URIs.- Returns:
- The root element of the corresponding Document.
- Throws:
IOException- When an IO error occurs.
-
read
public Document read(String s, Charset charset) throws IOException
Reads a string.- Parameters:
s- The string.charset- The charset. Must be compliant with string content.- Returns:
- The corresponding Document.
- Throws:
IOException- When an IO error occurs.
-
readRoot
public Element readRoot(String s, Charset charset) throws IOException
Reads a string.- Parameters:
s- The string.charset- The charset. Must be compliant with string content.- Returns:
- The root element of the corresponding Document.
- Throws:
IOException- When an IO error occurs.
-
read
public Document read(URL url, Compressor compressor) throws IOException
Reads an URL.- Parameters:
url- The URL.compressor- The compressor used to compress file.- Returns:
- The corresponding Document.
- Throws:
IOException- When an IO error occurs.
-
read
public Document read(URL url) throws IOException
Reads an URL.- Parameters:
url- The URL.- Returns:
- The corresponding Document.
- Throws:
IOException- When an IO error occurs.
-
readRoot
public Element readRoot(URL url, Compressor compressor) throws IOException
Reads an URL.- Parameters:
url- The URL.compressor- The compressor used to compress file.- Returns:
- The root element of the corresponding Document.
- Throws:
IOException- When an IO error occurs.
-
readRoot
public Element readRoot(URL url) throws IOException
Reads an URL.- Parameters:
url- The URL.- Returns:
- The root element of the corresponding Document.
- Throws:
IOException- When an IO error occurs.
-
read
public Document read(String filename, Compressor compressor) throws IOException
Reads a file.- Parameters:
filename- The file name.compressor- The compressor used to compress file.- Returns:
- The corresponding Document.
- Throws:
IOException- When an IO error occurs.
-
read
public Document read(String filename) throws IOException
Reads a file.- Parameters:
filename- The file name.- Returns:
- The corresponding Document.
- Throws:
IOException- When an IO error occurs.
-
readRoot
public Element readRoot(String filename, Compressor compressor) throws IOException
Reads a file.- Parameters:
filename- The file name.compressor- The compressor used to compress file.- Returns:
- The root element of the corresponding Document.
- Throws:
IOException- When an IO error occurs.
-
readRoot
public Element readRoot(String filename) throws IOException
Reads a file.- Parameters:
filename- The file name.- Returns:
- The root element of the corresponding Document.
- Throws:
IOException- When an IO error occurs.
-
read
public Document read(File file, Compressor compressor) throws IOException
Reads a file.- Parameters:
file- The file.compressor- The compressor used to compress file.- Returns:
- The corresponding Document.
- Throws:
IOException- When an IO error occurs.
-
read
public Document read(File file) throws IOException
Reads a file.- Parameters:
file- The file.- Returns:
- The corresponding Document.
- Throws:
IOException- When an IO error occurs.
-
readRoot
public Element readRoot(File file, Compressor compressor) throws IOException
Reads a file.- Parameters:
file- The file.compressor- The compressor used to compress file.- Returns:
- The root element of the corresponding Document.
- Throws:
IOException- When an IO error occurs.
-
readRoot
public Element readRoot(File file) throws IOException
Reads a file.- Parameters:
file- The file.- Returns:
- The root element of the corresponding Document.
- Throws:
IOException- When an IO error occurs.
-
create
public static XmlDataReader create(XmlDataReader.Feature... features)
-
load
public static Document load(InputStream is, XmlDataReader.Feature... features) throws IOException
- Throws:
IOException
-
loadRoot
public static Element loadRoot(InputStream is, XmlDataReader.Feature... features) throws IOException
- Throws:
IOException
-
load
public static Document load(InputStream is, String systemId, XmlDataReader.Feature... features) throws IOException
- Throws:
IOException
-
loadRoot
public static Element loadRoot(InputStream is, String systemId, Compressor compressor, XmlDataReader.Feature... features) throws IOException
- Throws:
IOException
-
loadRoot
public static Element loadRoot(InputStream is, String systemId, XmlDataReader.Feature... features) throws IOException
- Throws:
IOException
-
load
public static Document load(String s, Charset charset, XmlDataReader.Feature... features) throws IOException
- Throws:
IOException
-
loadRoot
public static Element loadRoot(String s, Charset charset, XmlDataReader.Feature... features) throws IOException
- Throws:
IOException
-
load
public static Document load(URL url, Compressor compressor, XmlDataReader.Feature... features) throws IOException
- Throws:
IOException
-
load
public static Document load(URL url, XmlDataReader.Feature... features) throws IOException
- Throws:
IOException
-
loadRoot
public static Element loadRoot(URL url, Compressor compressor, XmlDataReader.Feature... features) throws IOException
- Throws:
IOException
-
loadRoot
public static Element loadRoot(URL url, XmlDataReader.Feature... features) throws IOException
- Throws:
IOException
-
load
public static Document load(String filename, Compressor compressor, XmlDataReader.Feature... features) throws IOException
- Throws:
IOException
-
load
public static Document load(String filename, XmlDataReader.Feature... features) throws IOException
- Throws:
IOException
-
loadRoot
public static Element loadRoot(String filename, Compressor compressor, XmlDataReader.Feature... features) throws IOException
- Throws:
IOException
-
loadRoot
public static Element loadRoot(String filename, XmlDataReader.Feature... features) throws IOException
- Throws:
IOException
-
load
public static Document load(File file, Compressor compressor, XmlDataReader.Feature... features) throws IOException
- Throws:
IOException
-
load
public static Document load(File file, XmlDataReader.Feature... features) throws IOException
- Throws:
IOException
-
loadRoot
public static Element loadRoot(File file, Compressor compressor, XmlDataReader.Feature... features) throws IOException
- Throws:
IOException
-
loadRoot
public static Element loadRoot(File file, XmlDataReader.Feature... features) throws IOException
- Throws:
IOException
-
-