summaryrefslogtreecommitdiff
path: root/lib/XML/LibXML.pm
blob: a9c8fbb718708d58407915f759eb868bb5229bfd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# -*- mode: perl6 -*- 
use NativeCall;
 
class XML::LibXML::Node is repr('CStruct') {
    sub xmlFreeNode(XML::LibXML::Node $doc) is native('libxml2') {*}
    submethod DESTROY { xmlFreeNode(self) }
 
    has Pointer $!_private;
    has int $.xmlElementType;
    has Str $.name;
    # etc etc, don't try to create this 
}
 
class XML::LibXML::Document is repr('CPointer') {
    sub xmlFreeDoc(XML::LibXML::Document $doc) is native('libxml2') {*}
    submethod DESTROY { xmlFreeDoc(self) }
 
    sub xmlDocGetRootElement(XML::LibXML::Document $doc)
    returns XML::LibXML::Node is native('libxml2') {*}
    method rootElement() {
        return xmlDocGetRootElement(self);
    }
}
 
class XML::LibXML::Context is repr('CStruct') {
    sub xmlFreeParserCtxt(XML::LibXML::Context $ctxt) is native('libxml2') {*}
    submethod DESTROY { xmlFreeParserCtxt(self) }
 
    sub xmlParseDocument(XML::LibXML::Context $ctxt) is native('libxml2') {*}
    method parse() { xmlParseDocument(self) }
 
    has Pointer $!sax; # currently unused 
    has Pointer $!userData; # useless? 
    has XML::LibXML::Document $.myDoc;
    # etc etc, don't try to create this 
}
 
class XML::LibXML {
    sub xmlCreateMemoryParserCtxt(Blob $docbufint $len) returns XML::LibXML::Context is native('libxml2') {*}
 
    method parse(Str $docstring) {
        my Blob $docbuf = $docstring.encode('utf8');
        my $ctxt = xmlCreateMemoryParserCtxt(
            $docbuf,
            $docbuf.bytes,
        );
        $ctxt.parse();
        return $ctxt.myDoc;
    }
}