Created
January 13, 2012 03:46
-
-
Save vgrichina/1604592 to your computer and use it in GitHub Desktop.
Simple OS X XML property list parser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.codehaus.groovy.grails.plugins.codecs.Base64Codec | |
class PlistUtility { | |
static parseXmlPlistText(String text) { | |
def xml = new XmlParser().parseText(text) | |
assert xml.name() == "plist" | |
def parseNode | |
parseNode = { node -> | |
def childNodes = node."*" | |
switch (node.name()) { | |
case "dict": | |
def dict = [:] | |
for (int i = 0; i < childNodes.size(); i += 2) { | |
assert childNodes[i].name() == "key" | |
dict[childNodes[i].text().trim()] = parseNode(childNodes[i + 1]) | |
} | |
return dict | |
case "array": | |
return childNodes.collect(parseNode) | |
case "string": | |
return node.text() | |
case "integer": | |
return Integer.parseInt(node.text()) | |
case "date": | |
return Date.parse("yyyy-MM-dd'T'HH:mm:ss'Z'", node.text()) | |
case "false": | |
return false | |
case "true": | |
return true | |
case "data": | |
return node.text().decodeBase64() | |
default: | |
assert false | |
} | |
} | |
return parseNode(xml."*"[0]) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import static org.junit.Assert.* | |
import grails.test.mixin.* | |
import grails.test.mixin.support.* | |
import org.junit.* | |
/** | |
* See the API for {@link grails.test.mixin.support.GrailsUnitTestMixin} for usage instructions | |
*/ | |
@TestMixin(GrailsUnitTestMixin) | |
class PlistUtilityTests { | |
void setUp() { | |
// Setup logic here | |
} | |
void tearDown() { | |
// Tear down logic here | |
} | |
void testParseXmlPlist() { | |
def result = PlistUtility.parseXmlPlistText("""<?xml version="1.0" encoding="UTF-8"?> | |
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | |
<plist version="1.0"> | |
<dict> | |
<key>ApplicationIdentifierPrefix</key> | |
<array> | |
<string>42CXT4V7NB</string> | |
</array> | |
<key>CreationDate</key> | |
<date>2011-12-29T13:05:35Z</date> | |
<key>DeveloperCertificates</key> | |
<array> | |
<data> | |
SGVsbG8x | |
</data> | |
</array> | |
<key>Entitlements</key> | |
<dict> | |
<key>application-identifier</key> | |
<string>42CXT4V7NB.*</string> | |
<key>get-task-allow</key> | |
<false/> | |
<key>keychain-access-groups</key> | |
<array> | |
<string>42CXT4V7NB.*</string> | |
</array> | |
</dict> | |
<key>ExpirationDate</key> | |
<date>2012-05-06T13:05:35Z</date> | |
<key>Name</key> | |
<string>Vladimir Grichina Ad Hoc</string> | |
<key>ProvisionedDevices</key> | |
<array> | |
<string>7a99608d0f1456c26e3d3667a86b67f4130acefa</string> | |
<string>76db6e3f0d0186a107160b9b2462a571ea5a0d83</string> | |
</array> | |
<key>TeamIdentifier</key> | |
<array> | |
<string>9532C74ZP2</string> | |
</array> | |
<key>TimeToLive</key> | |
<integer>129</integer> | |
<key>UUID</key> | |
<string>54CB68CA-9D3D-48D2-8715-BD7F6962143E</string> | |
<key>Version</key> | |
<integer>1</integer> | |
</dict> | |
</plist> | |
"""); | |
assert result instanceof Map | |
assert result.ApplicationIdentifierPrefix instanceof List | |
assert result.ApplicationIdentifierPrefix[0] == "42CXT4V7NB" | |
assert result.CreationDate == Date.parse("yyyy-MM-dd'T'HH:mm:ss'Z'", "2011-12-29T13:05:35Z") | |
assert result.DeveloperCertificates instanceof List | |
assert result.DeveloperCertificates[0] instanceof byte[] | |
assert result.Entitlements instanceof Map | |
assert result.Entitlements["application-identifier"] == "42CXT4V7NB.*" | |
assert result.Entitlements["get-task-allow"] == false | |
assert result.ExpirationDate == Date.parse("yyyy-MM-dd'T'HH:mm:ss'Z'", "2012-05-06T13:05:35Z") | |
assert result.Name == "Vladimir Grichina Ad Hoc" | |
assert result.ProvisionedDevices instanceof List | |
assert result.ProvisionedDevices[0] == "7a99608d0f1456c26e3d3667a86b67f4130acefa" | |
assert result.ProvisionedDevices[1] == "76db6e3f0d0186a107160b9b2462a571ea5a0d83" | |
assert result.TeamIdentifier instanceof List | |
assert result.TeamIdentifier[0] == "9532C74ZP2" | |
assert result.TimeToLive == 129 | |
assert result.UUID == "54CB68CA-9D3D-48D2-8715-BD7F6962143E" | |
assert result.Version == 1 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment