Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package xmlwise;
import java.io.Closeable;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;
/** Plist xml handling (serialization and deserialization).
* The xml plist dtd can be found at http://www.apple.com/DTDs/PropertyList-1.0.dtd.
* @author Christoffer Lerno. */
public final class Plist
{
/**
* Singleton instance.
*/
private final static Plist PLIST = new Plist();
/**
* All element types possible for a plist.
*/
private static enum ElementType
{
INTEGER,
STRING,
REAL,
DATA,
DATE,
DICT,
ARRAY,
TRUE,
FALSE,
}
private static final String BASE64_STRING
= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; //$NON-NLS-1$
private static final char[] BASE64_CHARS = BASE64_STRING.toCharArray();
private final DateFormat m_dateFormat;
private final Map, ElementType> m_simpleTypes;
/**
* Utility method to close a closeable.
*
* @param closeable or null.
*/
static void silentlyClose(final Closeable closeable)
{
try
{
if (closeable != null) {
closeable.close();
}
}
catch (final IOException e)
{
// Ignore
}
}
/**
* Create a nested {@code map} from a plist xml string using the default mapping.
*
* @param xml the plist xml data as a string.
* @return the resulting map as read from the plist data.
* @throws XmlParseException if the plist could not be properly parsed.
*/
public static Map fromXml(final String xml) throws XmlParseException
{
return PLIST.parse(Xmlwise.createXml(xml));
}
/**
* Create a plist handler.
*/
Plist()
{
this.m_dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); //$NON-NLS-1$
this.m_dateFormat.setTimeZone(TimeZone.getTimeZone("Z")); //$NON-NLS-1$
this.m_simpleTypes = new HashMap<>();
this.m_simpleTypes.put(Integer.class, ElementType.INTEGER);
this.m_simpleTypes.put(Byte.class, ElementType.INTEGER);
this.m_simpleTypes.put(Short.class, ElementType.INTEGER);
this.m_simpleTypes.put(Short.class, ElementType.INTEGER);
this.m_simpleTypes.put(Long.class, ElementType.INTEGER);
this.m_simpleTypes.put(String.class, ElementType.STRING);
this.m_simpleTypes.put(Float.class, ElementType.REAL);
this.m_simpleTypes.put(Double.class, ElementType.REAL);
this.m_simpleTypes.put(byte[].class, ElementType.DATA);
this.m_simpleTypes.put(Boolean.class, ElementType.TRUE);
this.m_simpleTypes.put(Date.class, ElementType.DATE);
}
/**
* Convert an object to its plist representation.
*
* @param o the object to convert, must be Integer, Double, String, Date, Boolean, byte[],
* Map or List.
* @return an XmlElement containing the serialized version of the object.
*/
XmlElement objectToXml(final Object o)
{
final ElementType type = this.m_simpleTypes.get(o.getClass());
if (type != null)
{
switch (type) {
case REAL:
return new XmlElement("real", o.toString()); //$NON-NLS-1$
case INTEGER:
return new XmlElement("integer", o.toString()); //$NON-NLS-1$
case TRUE:
return new XmlElement(((Boolean) o).booleanValue() ? "true" : "false"); //$NON-NLS-1$ //$NON-NLS-2$
case DATE:
return new XmlElement("date", this.m_dateFormat.format((Date) o)); //$NON-NLS-1$
case STRING:
return new XmlElement("string", (String) o); //$NON-NLS-1$
case DATA:
return new XmlElement("data", base64encode((byte[]) o)); //$NON-NLS-1$
}
}
if (o instanceof Map)
{
return toXmlDict((Map) o);
}
else if (o instanceof List)
{
return toXmlArray((List) o);
}
else {
throw new RuntimeException("Cannot use " + o.getClass() + " in plist."); //$NON-NLS-1$ //$NON-NLS-2$
}
}
/**
* Convert a list to its plist representation.
*
* @param list the list to convert.
* @return an XmlElement representing the list.
*/
private XmlElement toXmlArray(final List