Class PListParser

java.lang.Object
com.github.markusbernhardt.proxy.util.PListParser

public final class PListParser extends 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 nested Map<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
 
  • Field Details

  • Constructor Details

    • PListParser

      PListParser()
      Create a plist handler.
  • Method Details

    • silentlyClose

      static void silentlyClose(Closeable closeable)
      Utility method to close a closeable.
      Parameters:
      closeable - or null.
    • parse

      private static PListParser.Dict parse(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

      Create a nested map<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.
      IOException - if there was an issue reading the plist file.
    • parse

      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 Object parseElement(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 Object parseElementRaw(Node element) throws ParseException
      Parses a (non-top) xml element.
      Parameters:
      element - the element to parse.
      Returns:
      the resulting object.
      Throws:
      ParseException - if there was some error parsing the xml.
    • getValue

      private String getValue(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 Number parseInt(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(NodeList elements) throws 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:
      ParseException - if there are any problems deserializing the map.
    • parseArray

      private List<Object> parseArray(NodeList elements) throws 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:
      ParseException - if there are any problems deserializing the list.
    • base64encode

      static 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(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.