Class PListParser
- java.lang.Object
-
- com.github.markusbernhardt.proxy.util.PListParser
-
public final class PListParser extends java.lang.Object
Plist xml handling (serialization and deserialization)The xml plist dtd can be found at http://www.apple.com/DTDs/PropertyList-1.0.dtd
The plist spec handles 8 types of objects: booleans, real, integers, dates, binary data, strings, arrays (lists) and dictionaries (maps).
The java Plist lib handles converting xml plists to a nested
Map<String, Object>
that can be trivially read from java. It also provides a simple way to convert a nestedMap<String, Object>
into an xml plist representation.The following mapping will be done when converting from plist to Map :
true/false -> Boolean real -> Double integer -> Integer/Long (depends on size, values exceeding an int will be rendered as longs) data -> byte[] string -> String array -> List dict -> Map
When converting from Map -> plist the conversion is as follows:
Boolean -> true/false Float/Double -> real Byte/Short/Integer/Long -> integer byte[] -> data List -> array Map -> dict
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
PListParser.Dict
Small helper class representing a tree node.private static class
PListParser.ElementType
All element types possible for a plist.static class
PListParser.XmlParseException
Exception is used for XML parse problems.
-
Field Summary
Fields Modifier and Type Field Description private static char[]
BASE64_CHARS
private static java.lang.String
BASE64_STRING
private java.text.DateFormat
m_dateFormat
private java.util.Map<java.lang.Class<?>,PListParser.ElementType>
m_simpleTypes
private static PListParser
PLIST
Singleton instance.
-
Constructor Summary
Constructors Constructor Description PListParser()
Create a plist handler.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description (package private) static byte[]
base64decode(java.lang.String base64)
Converts a string to a byte array assuming the string uses base64-encoding.(package private) static java.lang.String
base64encode(byte[] bytes)
Encode an array of bytes to a string using base64 encoding.private java.lang.String
getValue(org.w3c.dom.Node n)
Parses a string value from a node.static PListParser.Dict
load(java.io.File file)
Create a nestedmap<String, Object>
from a plist xml file using the default mapping.(package private) PListParser.Dict
parse(org.w3c.dom.Element element)
Parses a plist top element into a map dictionary containing all the data in the plist.private static PListParser.Dict
parse(org.xml.sax.InputSource input)
Reads from the given input stream and parses it to a Dict.private java.util.List<java.lang.Object>
parseArray(org.w3c.dom.NodeList elements)
Parse a list of xml elements as a plist array.private PListParser.Dict
parseDict(org.w3c.dom.NodeList elements)
Parse a list of xml elements as a plist dict.private java.lang.Object
parseElement(org.w3c.dom.Node element)
Parses a (non-top) xml element.private java.lang.Object
parseElementRaw(org.w3c.dom.Node element)
Parses a (non-top) xml element.private java.lang.Number
parseInt(java.lang.String value)
Parses a string into a Long or Integer depending on size.(package private) static void
silentlyClose(java.io.Closeable closeable)
Utility method to close a closeable.
-
-
-
Field Detail
-
PLIST
private static final PListParser PLIST
Singleton instance.
-
BASE64_STRING
private static final java.lang.String BASE64_STRING
- See Also:
- Constant Field Values
-
BASE64_CHARS
private static final char[] BASE64_CHARS
-
m_dateFormat
private final java.text.DateFormat m_dateFormat
-
m_simpleTypes
private final java.util.Map<java.lang.Class<?>,PListParser.ElementType> m_simpleTypes
-
-
Method Detail
-
silentlyClose
static void silentlyClose(java.io.Closeable closeable)
Utility method to close a closeable.- Parameters:
closeable
- or null.
-
parse
private static PListParser.Dict parse(org.xml.sax.InputSource input) throws PListParser.XmlParseException
Reads from the given input stream and parses it to a Dict.- Parameters:
input
- the input stream to read from.- Returns:
- the parsed dictionary.
- Throws:
PListParser.XmlParseException
- on parsing error.
-
load
public static PListParser.Dict load(java.io.File file) throws PListParser.XmlParseException, java.io.IOException
Create a nestedmap<String, Object>
from a plist xml file using the default mapping.- Parameters:
file
- the File containing the the plist xml.- Returns:
- the resulting map as read from the plist data.
- Throws:
PListParser.XmlParseException
- if the plist could not be properly parsed.java.io.IOException
- if there was an issue reading the plist file.
-
parse
PListParser.Dict parse(org.w3c.dom.Element element) throws PListParser.XmlParseException
Parses a plist top element into a map dictionary containing all the data in the plist.- Parameters:
element
- the top plist element.- Returns:
- the resulting data tree structure.
- Throws:
PListParser.XmlParseException
- if there was any error parsing the xml.
-
parseElement
private java.lang.Object parseElement(org.w3c.dom.Node element) throws PListParser.XmlParseException
Parses a (non-top) xml element.- Parameters:
element
- the element to parse.- Returns:
- the resulting object.
- Throws:
PListParser.XmlParseException
- if there was some error in the xml.
-
parseElementRaw
private java.lang.Object parseElementRaw(org.w3c.dom.Node element) throws java.text.ParseException
Parses a (non-top) xml element.- Parameters:
element
- the element to parse.- Returns:
- the resulting object.
- Throws:
java.text.ParseException
- if there was some error parsing the xml.
-
getValue
private java.lang.String getValue(org.w3c.dom.Node n)
Parses a string value from a node.- Parameters:
n
- the node to extract the value from.- Returns:
- the content of the node
-
parseInt
private java.lang.Number parseInt(java.lang.String value)
Parses a string into a Long or Integer depending on size.- Parameters:
value
- the value as a string.- Returns:
- the long value of this string is the value doesn't fit in an integer, otherwise the int value of the string.
-
parseDict
private PListParser.Dict parseDict(org.w3c.dom.NodeList elements) throws java.text.ParseException
Parse a list of xml elements as a plist dict.- Parameters:
elements
- the elements to parse.- Returns:
- the dict deserialized as a map.
- Throws:
java.text.ParseException
- if there are any problems deserializing the map.
-
parseArray
private java.util.List<java.lang.Object> parseArray(org.w3c.dom.NodeList elements) throws java.text.ParseException
Parse a list of xml elements as a plist array.- Parameters:
elements
- the elements to parse.- Returns:
- the array deserialized as a list.
- Throws:
java.text.ParseException
- if there are any problems deserializing the list.
-
base64encode
static java.lang.String base64encode(byte[] bytes)
Encode an array of bytes to a string using base64 encoding.- Parameters:
bytes
- the bytes to convert.- Returns:
- the base64 representation of the bytes.
-
base64decode
static byte[] base64decode(java.lang.String base64)
Converts a string to a byte array assuming the string uses base64-encoding.- Parameters:
base64
- the string to convert.- Returns:
- the resulting byte array.
-
-