| 1 |
/* |
| 2 |
* Copyright (C)2005-2016 Haxe Foundation |
| 3 |
* |
| 4 |
* Permission is hereby granted, free of charge, to any person obtaining a |
| 5 |
* copy of this software and associated documentation files (the "Software"), |
| 6 |
* to deal in the Software without restriction, including without limitation |
| 7 |
* the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 |
* and/or sell copies of the Software, and to permit persons to whom the |
| 9 |
* Software is furnished to do so, subject to the following conditions: |
| 10 |
* |
| 11 |
* The above copyright notice and this permission notice shall be included in |
| 12 |
* all copies or substantial portions of the Software. |
| 13 |
* |
| 14 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 19 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 20 |
* DEALINGS IN THE SOFTWARE. |
| 21 |
*/ |
| 22 |
package haxelib.client; |
| 23 |
|
| 24 |
import haxe.Json; |
| 25 |
|
| 26 |
class ConvertXml { |
| 27 |
public static function convert(inXml:String) { |
| 28 |
// Set up the default JSON structure |
| 29 |
var json = { |
| 30 |
"name": "", |
| 31 |
"url" : "", |
| 32 |
"license": "", |
| 33 |
"tags": [], |
| 34 |
"description": "", |
| 35 |
"version": "0.0.1", |
| 36 |
"releasenote": "", |
| 37 |
"contributors": [], |
| 38 |
"dependencies": {} |
| 39 |
}; |
| 40 |
|
| 41 |
// Parse the XML and set the JSON |
| 42 |
var xml = Xml.parse(inXml); |
| 43 |
var project = xml.firstChild(); |
| 44 |
json.name = project.get("name"); |
| 45 |
json.license = project.get("license"); |
| 46 |
json.url = project.get("url"); |
| 47 |
for (node in project) { |
| 48 |
switch (node.nodeType) { |
| 49 |
case #if (haxe_ver >= 3.2) Element #else Xml.Element #end: |
| 50 |
switch (node.nodeName) { |
| 51 |
case "tag": |
| 52 |
json.tags.push(node.get("v")); |
| 53 |
case "user": |
| 54 |
json.contributors.push(node.get("name")); |
| 55 |
case "version": |
| 56 |
json.version = node.get("name"); |
| 57 |
json.releasenote = node.firstChild().toString(); |
| 58 |
case "description": |
| 59 |
json.description = node.firstChild().toString(); |
| 60 |
case "depends": |
| 61 |
var name = node.get("name"); |
| 62 |
var version = node.get("version"); |
| 63 |
if (version == null) version = ""; |
| 64 |
Reflect.setField(json.dependencies, name, version); |
| 65 |
default: |
| 66 |
} |
| 67 |
default: |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
return json; |
| 72 |
} |
| 73 |
|
| 74 |
public static function prettyPrint(json:Dynamic, indent="") { |
| 75 |
var sb = new StringBuf(); |
| 76 |
sb.add("{\n"); |
| 77 |
|
| 78 |
var firstRun = true; |
| 79 |
for (f in Reflect.fields(json)) { |
| 80 |
if (!firstRun) sb.add(",\n"); |
| 81 |
firstRun = false; |
| 82 |
|
| 83 |
var value = switch (f) { |
| 84 |
case "dependencies": |
| 85 |
var d = Reflect.field(json, f); |
| 86 |
prettyPrint(d, indent + " "); |
| 87 |
default: |
| 88 |
Json.stringify(Reflect.field(json, f)); |
| 89 |
} |
| 90 |
sb.add(indent+' "$f": $value'); |
| 91 |
} |
| 92 |
|
| 93 |
sb.add('\n$indent}'); |
| 94 |
return sb.toString(); |
| 95 |
} |
| 96 |
} |