| 1 |
/* |
| 2 |
* Copyright (C)2005-2022 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 |
|
| 23 |
type t { |
| 24 |
Node : (string, (string, string) list , t list); |
| 25 |
PCData : string; |
| 26 |
CData : string; |
| 27 |
Document : t list; |
| 28 |
} |
| 29 |
|
| 30 |
exception Error : string; |
| 31 |
|
| 32 |
var __list = List.hd; |
| 33 |
|
| 34 |
neko(" |
| 35 |
|
| 36 |
@parser = function() { |
| 37 |
return { |
| 38 |
stack => null, |
| 39 |
doc => null, |
| 40 |
cdata => function(s) { |
| 41 |
this.doc = $array(CData(s),this.doc); |
| 42 |
}, |
| 43 |
pcdata => function(s) { |
| 44 |
this.doc = $array(PCData(s),this.doc); |
| 45 |
}, |
| 46 |
xml => function(name,att) { |
| 47 |
var fields = $objfields(att); |
| 48 |
var nf = $asize(fields); |
| 49 |
var fl = null; |
| 50 |
var i = 0; |
| 51 |
while( i < nf ) { |
| 52 |
var f = fields[i]; |
| 53 |
fl = $array($array($field(f),$objget(att,f)),fl); |
| 54 |
i = i + 1; |
| 55 |
} |
| 56 |
this.stack = $array(name,fl,this.doc,this.stack); |
| 57 |
this.doc = null; |
| 58 |
}, |
| 59 |
done => function() { |
| 60 |
var s = this.stack; |
| 61 |
var d = this.doc; |
| 62 |
this.doc = $array(Node(s[0],@List.@rmake(s[1]),@List.@rmake(d)),s[2]); |
| 63 |
this.stack = s[3]; |
| 64 |
} |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
@parse = $loader.loadprim('std@parse_xml',2); |
| 69 |
|
| 70 |
"); |
| 71 |
|
| 72 |
function parse( s : string ) : t { |
| 73 |
neko(" |
| 74 |
var p = @parser(); |
| 75 |
try { |
| 76 |
@parse(s,p); |
| 77 |
return Document(@List.@rmake(p.doc)); |
| 78 |
} catch e { |
| 79 |
if( $typeof(e) == $tstring ) |
| 80 |
$rethrow(Error(e)); |
| 81 |
$rethrow(e); |
| 82 |
} |
| 83 |
"); |
| 84 |
} |
| 85 |
|
| 86 |
function is_node(x) { |
| 87 |
match x { |
| 88 |
| Node _ -> true |
| 89 |
| _ -> false |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
function is_pcdata(x) { |
| 94 |
match x { |
| 95 |
| PCData _ -> true |
| 96 |
| _ -> false |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
function is_cdata(x) { |
| 101 |
match x { |
| 102 |
| CData _ -> true |
| 103 |
| _ -> false |
| 104 |
} |
| 105 |
} |
| 106 |
function firstNode(x) { |
| 107 |
match x { |
| 108 |
| Node (_,_,l) | Document l -> List.find is_node l |
| 109 |
| CData _ | PCData _ -> invalid_arg() |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
function nodes(x) { |
| 114 |
match x { |
| 115 |
| Node (_,_,l) | Document l -> List.filter is_node l |
| 116 |
| CData _ | PCData _ -> invalid_arg() |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
function node_name(x) { |
| 121 |
match x { |
| 122 |
| Node (name,_,_) -> name |
| 123 |
| _ -> invalid_arg() |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
function node_text(x) { |
| 128 |
match x { |
| 129 |
| Node (_,_,children) -> |
| 130 |
var b = Buffer.create(); |
| 131 |
List.iter (function(x) { |
| 132 |
match x { |
| 133 |
| CData t | PCData t -> Buffer.add b t |
| 134 |
| _ -> invalid_arg() |
| 135 |
} |
| 136 |
}) children; |
| 137 |
Buffer.string b |
| 138 |
| _ -> invalid_arg() |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
function attrib(x,n) { |
| 143 |
var n = String.lowercase n; |
| 144 |
match x { |
| 145 |
| Node (_,att,_) -> snd (List.find (function((n2,_)) { String.lowercase n2 == n }) att) |
| 146 |
| _ -> invalid_arg() |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
function rec write(ch,x) { |
| 151 |
match x { |
| 152 |
| Document l -> List.iter write(ch) l |
| 153 |
| CData c -> IO.write ch "<![CDATA["; IO.write ch c; IO.write ch "]]>"; |
| 154 |
| PCData c -> IO.write ch c; |
| 155 |
| Node (name,att,children) -> |
| 156 |
IO.printf ch "<%s%s" (name,String.concat "" (List.map (function((a,v)) { " "+a+"=\""+String.escape v+"\"" }) att)); |
| 157 |
match children { |
| 158 |
| [] -> IO.write ch "/>" |
| 159 |
| l -> IO.write ch ">"; List.iter write(ch) l; IO.printf ch "</%s>" name |
| 160 |
} |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
function to_string(x) { |
| 165 |
var ch , str = IO.write_string(); |
| 166 |
write ch x; |
| 167 |
str() |
| 168 |
} |
| 169 |
|