Skip to content
This repository has been archived by the owner on Feb 9, 2018. It is now read-only.

Commit

Permalink
Format net/pms/xmlwise
Browse files Browse the repository at this point in the history
  • Loading branch information
SubJunk committed Jun 26, 2011
1 parent f35f42e commit 599f6b9
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 268 deletions.
176 changes: 67 additions & 109 deletions ps3mediaserver/net/pms/xmlwise/Plist.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@
import java.io.IOException;
import java.io.FileOutputStream;
import java.io.Closeable;
import net.pms.xmlwise.XmlParseException;

@SuppressWarnings("unchecked")

@SuppressWarnings("unchecked")
/**
* Plist xml handling (serialization and deserialization)
* <p>
Expand Down Expand Up @@ -46,8 +44,7 @@
*
* @author Christoffer Lerno
*/
public final class Plist
{
public final class Plist {
/**
* Singleton instance.
*/
Expand All @@ -56,8 +53,7 @@ public final class Plist
/**
* All element types possible for a plist.
*/
private static enum ElementType
{
private static enum ElementType {
INTEGER,
STRING,
REAL,
Expand All @@ -66,11 +62,8 @@ private static enum ElementType
DICT,
ARRAY,
TRUE,
FALSE,
}

private static final String BASE64_STRING
= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
FALSE,}
private static final String BASE64_STRING = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
private static final char[] BASE64_CHARS = BASE64_STRING.toCharArray();
private final DateFormat m_dateFormat;
private final Map<Class<?>, ElementType> m_simpleTypes;
Expand All @@ -82,13 +75,12 @@ private static enum ElementType
* @param data the nested data to store as a plist.
* @return the resulting xml as a string.
*/
public static String toXml(Map<String, Object> data)
{
public static String toXml(Map<String, Object> data) {
StringBuilder builder = new StringBuilder(
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" " +
"\"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n" +
"<plist version=\"1.0\">");
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+ "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" "
+ "\"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
+ "<plist version=\"1.0\">");
builder.append(PLIST.objectToXml(data).toXml());
return builder.append("</plist>").toString();
}
Expand All @@ -100,28 +92,23 @@ public static String toXml(Map<String, Object> data)
* @param filename the destination file to store the data to.
* @throws IOException if there was an IO error saving the file.
*/
public static void store(Map<String, Object> data, String filename) throws IOException
{
public static void store(Map<String, Object> data, String filename) throws IOException {
store(data, new File(filename));
}

/**
* Store a nested {@code map<String, Object>} as a plist using the default mapping.
*
* @param data the nested data to store as a plist.
* @param file the destination File to store the data to.
* @throws IOException if there was an IO error saving the file.
*/
public static void store(Map<String, Object> data, File file) throws IOException
{
public static void store(Map<String, Object> data, File file) throws IOException {
FileOutputStream stream = null;
try
{
try {
stream = new FileOutputStream(file);
stream.write(toXml(data).getBytes());
}
finally
{
} finally {
silentlyClose(stream);
}
}
Expand All @@ -131,14 +118,12 @@ public static void store(Map<String, Object> data, File file) throws IOException
*
* @param closeable or null.
*/
static void silentlyClose(Closeable closeable)
{
try
{
if (closeable != null) closeable.close();
}
catch (IOException e)
{
static void silentlyClose(Closeable closeable) {
try {
if (closeable != null) {
closeable.close();
}
} catch (IOException e) {
// Ignore
}
}
Expand All @@ -150,8 +135,7 @@ static void silentlyClose(Closeable closeable)
* @return the resulting map as read from the plist data.
* @throws XmlParseException if the plist could not be properly parsed.
*/
public static Map<String, Object> fromXml(String xml) throws XmlParseException
{
public static Map<String, Object> fromXml(String xml) throws XmlParseException {
return PLIST.parse(Xmlwise.createXml(xml));
}

Expand All @@ -163,8 +147,7 @@ public static Map<String, Object> fromXml(String xml) throws XmlParseException
* @throws XmlParseException if the plist could not be properly parsed.
* @throws IOException if there was an issue reading the plist file.
*/
public static Map<String, Object> load(File file) throws XmlParseException, IOException
{
public static Map<String, Object> load(File file) throws XmlParseException, IOException {
return PLIST.parse(Xmlwise.loadXml(file));
}

Expand All @@ -176,16 +159,14 @@ public static Map<String, Object> load(File file) throws XmlParseException, IOEx
* @throws XmlParseException if the plist could not be properly parsed.
* @throws IOException if there was an issue reading the plist file.
*/
public static Map<String, Object> load(String filename) throws XmlParseException, IOException
{
public static Map<String, Object> load(String filename) throws XmlParseException, IOException {
return load(new File(filename));
}

/**
* Create a plist handler.
*/
Plist()
{
Plist() {
m_dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
m_dateFormat.setTimeZone(TimeZone.getTimeZone("Z"));
m_simpleTypes = new HashMap<Class<?>, ElementType>();
Expand All @@ -209,13 +190,10 @@ public static Map<String, Object> load(String filename) throws XmlParseException
* Map or List.
* @return an <tt>XmlElement</tt> containing the serialized version of the object.
*/
XmlElement objectToXml(Object o)
{
XmlElement objectToXml(Object o) {
ElementType type = m_simpleTypes.get(o.getClass());
if (type != null)
{
switch (type)
{
if (type != null) {
switch (type) {
case REAL:
return new XmlElement("real", o.toString());
case INTEGER:
Expand All @@ -230,15 +208,13 @@ XmlElement objectToXml(Object o)
return new XmlElement("data", base64encode((byte[]) o));
}
}
if (o instanceof Map)
{
if (o instanceof Map) {
return toXmlDict((Map<String, Object>) o);
}
else if (o instanceof List)
{
} else if (o instanceof List) {
return toXmlArray((List<?>) o);
} else {
throw new RuntimeException("Cannot use " + o.getClass() + " in plist.");
}
else throw new RuntimeException("Cannot use " + o.getClass() + " in plist.");
}

/**
Expand All @@ -247,11 +223,9 @@ else if (o instanceof List)
* @param list the list to convert.
* @return an <tt>XmlElement</tt> representing the list.
*/
private XmlElement toXmlArray(List<?> list)
{
private XmlElement toXmlArray(List<?> list) {
XmlElement array = new XmlElement("array");
for (Object o : list)
{
for (Object o : list) {
array.add(objectToXml(o));
}
return array;
Expand All @@ -263,11 +237,9 @@ private XmlElement toXmlArray(List<?> list)
* @param map the map to convert, assumed to have string keys.
* @return an <tt>XmlElement</tt> representing the map.
*/
private XmlElement toXmlDict(Map<String, Object> map)
{
private XmlElement toXmlDict(Map<String, Object> map) {
XmlElement dict = new XmlElement("dict");
for (Map.Entry<String, Object> entry : map.entrySet())
{
for (Map.Entry<String, Object> entry : map.entrySet()) {
dict.add(new XmlElement("key", entry.getKey()));
dict.add(objectToXml(entry.getValue()));
}
Expand All @@ -282,16 +254,18 @@ private XmlElement toXmlDict(Map<String, Object> map)
* @return the resulting data tree structure.
* @throws XmlParseException if there was any error parsing the xml.
*/
Map<String, Object> parse(XmlElement element) throws XmlParseException
{
if (!"plist".equalsIgnoreCase(element.getName()))
Map<String, Object> parse(XmlElement element) throws XmlParseException {
if (!"plist".equalsIgnoreCase(element.getName())) {
throw new XmlParseException("Expected plist top element, was: " + element.getName());
}

// Assure that the top element is a dict and the single child element.
if (element.size() != 1) throw new XmlParseException("Expected single 'dict' child element.");
if (element.size() != 1) {
throw new XmlParseException("Expected single 'dict' child element.");
}
element.getUnique("dict");

return (Map<String, Object>)parseElement(element.getUnique("dict"));
return (Map<String, Object>) parseElement(element.getUnique("dict"));
}

/**
Expand All @@ -301,31 +275,24 @@ Map<String, Object> parse(XmlElement element) throws XmlParseException
* @return the resulting object.
* @throws XmlParseException if there was some error in the xml.
*/
private Object parseElement(XmlElement element) throws XmlParseException
{
try
{
private Object parseElement(XmlElement element) throws XmlParseException {
try {
return parseElementRaw(element);
}
catch (Exception e)
{
} catch (Exception e) {
throw new XmlParseException("Failed to parse: " + element.toXml(), e);
}
}


/**
* Parses a (non-top) xml element.
*
* @param element the element to parse.
* @return the resulting object.
* @throws Exception if there was some error parsing the xml.
*/
private Object parseElementRaw(XmlElement element) throws Exception
{
private Object parseElementRaw(XmlElement element) throws Exception {
ElementType type = ElementType.valueOf(element.getName().toUpperCase());
switch (type)
{
switch (type) {
case INTEGER:
return parseInt(element.getValue());
case REAL:
Expand Down Expand Up @@ -356,10 +323,11 @@ private Object parseElementRaw(XmlElement element) throws Exception
* @return the long value of this string is the value doesn't fit in an integer,
* otherwise the int value of the string.
*/
private Number parseInt(String value)
{
private Number parseInt(String value) {
Long l = Long.valueOf(value);
if (l.intValue() == l) return l.intValue();
if (l.intValue() == l) {
return l.intValue();
}
return l;
}

Expand All @@ -370,14 +338,14 @@ private Number parseInt(String value)
* @return the dict deserialized as a map.
* @throws Exception if there are any problems deserializing the map.
*/
private Map<String, Object> parseDict(List<XmlElement> elements) throws Exception
{
private Map<String, Object> parseDict(List<XmlElement> elements) throws Exception {
Iterator<XmlElement> element = elements.iterator();
HashMap<String, Object> dict = new HashMap<String, Object>();
while (element.hasNext())
{
while (element.hasNext()) {
XmlElement key = element.next();
if (!"key".equals(key.getName())) throw new Exception("Expected key but was " + key.getName());
if (!"key".equals(key.getName())) {
throw new Exception("Expected key but was " + key.getName());
}
Object o = parseElementRaw(element.next());
dict.put(key.getValue(), o);
}
Expand All @@ -391,11 +359,9 @@ private Map<String, Object> parseDict(List<XmlElement> elements) throws Exceptio
* @return the array deserialized as a list.
* @throws Exception if there are any problems deserializing the list.
*/
private List<Object> parseArray(List<XmlElement> elements) throws Exception
{
private List<Object> parseArray(List<XmlElement> elements) throws Exception {
ArrayList<Object> list = new ArrayList<Object>(elements.size());
for (XmlElement element : elements)
{
for (XmlElement element : elements) {
list.add(parseElementRaw(element));
}
return list;
Expand All @@ -407,16 +373,14 @@ private List<Object> parseArray(List<XmlElement> elements) throws Exception
* @param bytes the bytes to convert.
* @return the base64 representation of the bytes.
*/
static String base64encode(byte[] bytes)
{
StringBuilder builder = new StringBuilder(((bytes.length + 2)/ 3) * 4);
for (int i = 0; i < bytes.length; i += 3)
{
static String base64encode(byte[] bytes) {
StringBuilder builder = new StringBuilder(((bytes.length + 2) / 3) * 4);
for (int i = 0; i < bytes.length; i += 3) {
byte b0 = bytes[i];
byte b1 = i < bytes.length - 1 ? bytes[i + 1] : 0;
byte b2 = i < bytes.length - 2 ? bytes[i + 2] : 0;
builder.append(BASE64_CHARS[(b0 & 0xFF) >> 2]);
builder.append(BASE64_CHARS[((b0 & 0x03) << 4) | ((b1 & 0xF0) >> 4)]);
builder.append(BASE64_CHARS[((b0 & 0x03) << 4) | ((b1 & 0xF0) >> 4)]);
builder.append(i < bytes.length - 1 ? BASE64_CHARS[((b1 & 0x0F) << 2) | ((b2 & 0xC0) >> 6)] : "=");
builder.append(i < bytes.length - 2 ? BASE64_CHARS[b2 & 0x3F] : "=");
}
Expand All @@ -429,17 +393,15 @@ static String base64encode(byte[] bytes)
* @param base64 the string to convert.
* @return the resulting byte array.
*/
static byte[] base64decode(String base64)
{
static byte[] base64decode(String base64) {
base64 = base64.trim();
int endTrim = base64.endsWith("==") ? 2 : base64.endsWith("=") ? 1 : 0;
int length = (base64.length() / 4) * 3 - endTrim;
base64 = base64.replace('=', 'A');
byte[] result = new byte[length];
int stringLength = base64.length();
int index = 0;
for (int i = 0; i < stringLength; i += 4)
{
for (int i = 0; i < stringLength; i += 4) {
int i0 = BASE64_STRING.indexOf(base64.charAt(i));
int i1 = BASE64_STRING.indexOf(base64.charAt(i + 1));
int i2 = BASE64_STRING.indexOf(base64.charAt(i + 2));
Expand All @@ -448,17 +410,13 @@ static byte[] base64decode(String base64)
byte b1 = (byte) ((i1 << 4) | (i2 >> 2));
byte b2 = (byte) ((i2 << 6) | i3);
result[index++] = b0;
if (index < length)
{
if (index < length) {
result[index++] = b1;
if (index < length)
{
if (index < length) {
result[index++] = b2;
}
}
}
return result;
}


}
Loading

0 comments on commit 599f6b9

Please sign in to comment.