src.main.java.com.dd.plist.NSObject Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of dd-plist Show documentation
Show all versions of dd-plist Show documentation
This library enables Java applications to work with property lists in various formats.
Supported formats for reading and writing are OS X/iOS binary and XML property lists.
ASCII property lists are also supported.
The library also provides access to basic functions of NeXTSTEP/Cocoa classes like
NSDictionary, NSArray, etc.
/*
* plist - An open source library to parse and generate property lists
* Copyright (C) 2014 Daniel Dreibrodt
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.dd.plist;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.*;
/**
* Abstract interface for any object contained in a property list.
* The names and functions of the various objects orient themselves
* towards Apple's Cocoa API.
*
* @author Daniel Dreibrodt
*/
public abstract class NSObject {
/**
* The newline character used for generating the XML output.
* This constant will be different depending on the operating system on
* which you use this library.
*/
final static String NEWLINE = System.getProperty("line.separator");
/**
* The identation character used for generating the XML output. This is the
* tabulator character.
*/
final static String INDENT = "\t";
/**
* The maximum length of the text lines to be used when generating
* ASCII property lists. But this number is only a guideline it is not
* guaranteed that it will not be overstepped.
*/
final static int ASCII_LINE_LENGTH = 80;
/**
* Generates the XML representation of the object (without XML headers or enclosing plist-tags).
*
* @param xml The StringBuilder onto which the XML representation is appended.
* @param level The indentation level of the object.
*/
abstract void toXML(StringBuilder xml, int level);
/**
* Assigns IDs to all the objects in this NSObject subtree.
*
* @param out The writer object that handles the binary serialization.
*/
void assignIDs(BinaryPropertyListWriter out) {
out.assignID(this);
}
/**
* Generates the binary representation of the object.
*
* @param out The output stream to serialize the object to.
* @throws java.io.IOException When an IO error occurs while writing to the stream or the object structure contains
* data that cannot be saved.
*/
abstract void toBinary(BinaryPropertyListWriter out) throws IOException;
/**
* Generates a valid XML property list including headers using this object as root.
*
* @return The XML representation of the property list including XML header and doctype information.
*/
public String toXMLPropertyList() {
StringBuilder xml = new StringBuilder("");
xml.append(NSObject.NEWLINE);
xml.append("");
xml.append(NSObject.NEWLINE);
xml.append("");
xml.append(NSObject.NEWLINE);
toXML(xml, 0);
xml.append(NSObject.NEWLINE);
xml.append(" ");
return xml.toString();
}
/**
* Generates the ASCII representation of this object.
* The generated ASCII representation does not end with a newline.
* Complies with https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/PropertyLists/OldStylePlists/OldStylePLists.html
*
* @param ascii The StringBuilder onto which the ASCII representation is appended.
* @param level The indentation level of the object.
*/
protected abstract void toASCII(StringBuilder ascii, int level);
/**
* Generates the ASCII representation of this object in the GnuStep format.
* The generated ASCII representation does not end with a newline.
*
* @param ascii The StringBuilder onto which the ASCII representation is appended.
* @param level The indentation level of the object.
*/
protected abstract void toASCIIGnuStep(StringBuilder ascii, int level);
/**
* Helper method that adds correct identation to the xml output.
* Calling this method will add level
number of tab characters
* to the xml
string.
*
* @param xml The string builder for the XML document.
* @param level The level of identation.
*/
void indent(StringBuilder xml, int level) {
for (int i = 0; i < level; i++)
xml.append(INDENT);
}
/**
* Wraps the given value inside a NSObject.
*
* @param value The value to represent as a NSObject.
* @return A NSObject representing the given value.
*/
public static NSNumber wrap(long value) {
return new NSNumber(value);
}
/**
* Wraps the given value inside a NSObject.
*
* @param value The value to represent as a NSObject.
* @return A NSObject representing the given value.
*/
public static NSNumber wrap(double value) {
return new NSNumber(value);
}
/**
* Wraps the given value inside a NSObject.
*
* @param value The value to represent as a NSObject.
* @return A NSObject representing the given value.
*/
public static NSNumber wrap(boolean value) {
return new NSNumber(value);
}
/**
* Wraps the given value inside a NSObject.
*
* @param value The value to represent as a NSObject.
* @return A NSObject representing the given value.
*/
public static NSData wrap(byte[] value) {
return new NSData(value);
}
/**
* Creates a NSArray with the contents of the given array.
*
* @param value The value to represent as a NSObject.
* @return A NSObject representing the given value.
* @throws RuntimeException When one of the objects contained in the array cannot be represented by a NSObject.
*/
public static NSArray wrap(Object[] value) {
NSArray arr = new NSArray(value.length);
for (int i = 0; i < value.length; i++) {
arr.setValue(i, wrap(value[i]));
}
return arr;
}
/**
* Creates a NSDictionary with the contents of the given map.
*
* @param value The value to represent as a NSObject.
* @return A NSObject representing the given value.
* @throws RuntimeException When one of the values contained in the map cannot be represented by a NSObject.
*/
public static NSDictionary wrap(Map value) {
NSDictionary dict = new NSDictionary();
for (String key : value.keySet())
dict.put(key, wrap(value.get(key)));
return dict;
}
/**
* Creates a NSSet with the contents of this set.
*
* @param value The value to represent as a NSObject.
* @return A NSObject representing the given value.
* @throws RuntimeException When one of the values contained in the set cannot be represented by a NSObject.
*/
public static NSSet wrap(Set