Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update package and add convert_plist_array func #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 42 additions & 39 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,51 +1,54 @@
# Don’t commit the following files and directories created by pub and dart2js
packages/
*.js_
*.js.deps
*.js.map

# Include when developing application packages
pubspec.lock

# Avoid committing generated JavaScript files
*.dart.js

# =========================
# Operating System Files
# =========================

# OSX
# =========================

# Don’t commit the following files and directories created by pub and dart2js
.dart_tool/
.packages
packages/
*.js_
*.js.deps
*.js.map

# Include when developing application packages
pubspec.lock

# Avoid committing generated JavaScript files
*.dart.js

# =========================
# Operating System Files
# =========================

# OSX
# =========================

.DS_Store
.AppleDouble
.LSOverride

# Icon must ends with two \r.
Icon
Icon


# Thumbnails
._*

# Files that might appear on external disk
.Spotlight-V100
.Trashes
# Windows
# =========================
# Windows image file caches
Thumbs.db
ehthumbs.db
# Folder config file
Desktop.ini
# Recycle Bin used on file shares
$RECYCLE.BIN/
# Windows Installer files
*.cab
*.msi
*.msm
*.msp

# Windows
# =========================

# Windows image file caches
Thumbs.db
ehthumbs.db

# Folder config file
Desktop.ini

# Recycle Bin used on file shares
$RECYCLE.BIN/

# Windows Installer files
*.cab
*.msi
*.msm
*.msp
91 changes: 91 additions & 0 deletions lib/convert_plist_array.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// import 'dart:convert';
import 'dart:collection';

enum _TokenType { BraceOpen, BraceClose, Content, Comma }

class _Token {
static const BraceOpen = '{';
static const BraceClose = '}';
static const Comma = ',';
String content;
_TokenType type;
_Token(String s) {
content = s;
type = _TokenType.Content;
}
_Token.braceOpen() {
type = _TokenType.BraceOpen;
}
_Token.braceClose() {
type = _TokenType.BraceClose;
}
_Token.comma() {
type = _TokenType.Comma;
}
@override
String toString() {
return {'type': type, 'content': content}.toString();
}
}

Iterable<_Token> _scan(String s) sync* {
var i = 0;
var len = s.length;
var content = '';
while (i < len) {
switch (s[i]) {
case _Token.BraceOpen:
yield _Token.braceOpen();
break;
case _Token.BraceClose:
if (content != '') {
var token = _Token(content);
yield token;
}
content = '';
yield _Token.braceClose();
break;
case _Token.Comma:
if (content != '') {
var token = _Token(content);
yield token;
}
content = '';
yield _Token.comma();
break;
default:
content += s[i];
break;
}
;
i++;
}
}

dynamic convertPlistArray(String s) {
// var b = s.replaceAll('{', '[').replaceAll('}', ']');
// var l = jsonDecode(b) as List;
var prevNodes = Queue();
var currNode = [];

for (var token in _scan(s)) {
switch (token.type) {
case _TokenType.BraceOpen:
prevNodes.add(currNode);
currNode = [];
break;
case _TokenType.BraceClose:
var childNode = currNode;
currNode = prevNodes.removeLast();
currNode.add(childNode);
break;
case _TokenType.Content:
currNode.add(int.tryParse(token.content));
break;
default:
break;
}
}

return currNode.first;
}
46 changes: 21 additions & 25 deletions lib/plist.dart
Original file line number Diff line number Diff line change
@@ -1,31 +1,27 @@
library plist;

import 'dart:typed_data';
import 'package:xml/xml.dart' as libxml;
import 'package:crypto/crypto.dart' show CryptoUtils;

/**
* Takes a xml string of Property list.
* Will in most praticaly situation return an Map.
* Coverts property list types to these dart types:
*
* * <string> to [String].
* * <real> to [double].
* * <integer> to [int].
* * <true> to true.
* * <false> to false.
* * <date> to [DateTime].
* * <data> to [Uint8List].
* * <array> to [List].
* * <dict> to [Map].
*/
import 'dart:convert' show base64;
export './convert_plist_array.dart';
/// Takes a xml string of Property list.
/// Will in most praticaly situation return an Map.
/// Coverts property list types to these dart types:
///
/// * <string> to [String].
/// * <real> to [double].
/// * <integer> to [int].
/// * <true> to true.
/// * <false> to false.
/// * <date> to [DateTime].
/// * <data> to [Uint8List].
/// * <array> to [List].
/// * <dict> to [Map].
Object parse(String xml){
var doc = libxml.parse(xml);
return _handleElem(doc.rootElement.children.where(_isElemet).first);
}

_handleElem(libxml.XmlElement elem){
switch (elem.name.local){
dynamic _handleElem(libxml.XmlNode elem){
switch ( (elem as libxml.XmlElement).name.local){
case 'string':
return elem.text;
case 'real':
Expand All @@ -39,7 +35,7 @@ _handleElem(libxml.XmlElement elem){
case 'date':
return DateTime.parse(elem.text);
case 'data':
return new Uint8List.fromList(CryptoUtils.base64StringToBytes(elem.text));
return Uint8List.fromList(base64.decode(elem.text));
case 'array':
return elem.children
.where(_isElemet)
Expand All @@ -53,12 +49,12 @@ _handleElem(libxml.XmlElement elem){
Map _handleDict(libxml.XmlElement elem){
var children = elem.children.where(_isElemet);
var key = children
.where((elem) => elem.name.local == 'key')
.where((elem) => (elem as libxml.XmlElement).name.local == 'key')
.map((elem) => elem.text);
var values = children
.where((elem) => elem.name.local != 'key')
.where((elem) => (elem as libxml.XmlElement).name.local != 'key')
.map(_handleElem);
return new Map.fromIterables(key, values);
return Map.fromIterables(key, values);
}

bool _isElemet(libxml.XmlNode node) => node is libxml.XmlElement;
8 changes: 5 additions & 3 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ author: Ole Martin Gjersvik <[email protected]>
description: A parser for the Property list xml format.
homepage: https://github.com/gjersvik/plist
documentation: http://www.dartdocs.org/documentation/plist/1.0.0/index.html
environment:
sdk: '>=2.0.0 <3.0.0'
dependencies:
crypto: '>=0.9.0 <0.10.0'
xml: '>=2.0.0 <3.0.0'
crypto: '>=0.9.0'
xml: '>=2.0.0'
dev_dependencies:
unittest: any
test: ^1.6.0
11 changes: 10 additions & 1 deletion test/unit.dart → test/unit_test.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'dart:typed_data';
import 'package:unittest/unittest.dart';
import 'package:test/test.dart';

//Package under test.
import '../lib/plist.dart';
Expand Down Expand Up @@ -44,6 +44,15 @@ void main(){
var plist = parse('<plist><array><string>anser</string><integer>42</integer></array></plist>');
expect(plist, ['anser', 42]);
});

test('Test convertPlistArray', () {
expect(
convertPlistArray('{{2,2},{240,240}}'),
equals([
[2, 2],
[240, 240]
]));
});

test('Plist parses <dict> to Map.', (){
var xml = '''
Expand Down