Commits

Julian Rosse committed 5e3cb18

element.text, .tag

Comments (0)

Files changed (9)

+cimport util
+
+cdef extern from "xercesc/dom/DOM.hpp" namespace "xercesc":
+    cdef cppclass DOMDocument:
+        DOMNode* getDocumentElement()
+    cdef cppclass DOMNode:
+        util.XMLCh* getNodeName()
+        util.XMLCh* getNodeValue()
+        DOMNode* getFirstChild()
+
+    cdef cppclass DOMElement:
+       util.XMLCh* getTagName()

pyces/framework.pxd

+cimport util
+cimport sax
+
+cdef extern from "xercesc/framework/MemBufInputSource.hpp" namespace "xercesc":
+    cdef cppclass MemBufInputSource( sax.InputSource ):
+        MemBufInputSource( util.XMLByte*,
+                           util.XMLSize_t,
+                           char* )

pyces/parsers.pxd

+cimport sax
+cimport dom
+
+cdef extern from "xercesc/parsers/XercesDOMParser.hpp" namespace "xercesc":
+    cdef cppclass XercesDOMParser:
+        void parse( sax.InputSource& )
+        dom.DOMDocument* getDocument()

pyces/pyces.etree.pyx

-cimport tree
+cimport util
+cimport parsers
+cimport framework
+cimport dom
+from libcpp.string cimport string
+from cython.operator cimport dereference as deref
 
-def hello( x ):
-    return x
+cdef class _Element:
+    cdef dom.DOMNode* _c_node
+    property tag:
+        def __get__( self ):
+            return util.transcode( self._c_node
+                                       .getNodeName())
+    property text:
+        def __get__( self ):
+            cdef dom.DOMNode* text_node = ( self._c_node
+                                                .getFirstChild())
+            return util.transcode( text_node
+                                    .getNodeValue())
+
+cdef _element( dom.DOMNode* node ):
+    el = _Element()
+    el._c_node = node
+    return el
+
+def initialize():
+    util.Initialize()
+
+def terminate():
+    util.Terminate()
+
+def fromstring( s ):
+    cdef parsers.XercesDOMParser parser
+    cdef string std_s = s
+    cdef framework.MemBufInputSource* yn = ( new framework
+                                                  .MemBufInputSource( <util.XMLByte*>
+                                                                      std_s
+                                                                       .c_str(),
+                                                                      std_s
+                                                                       .size(),
+                                                                      "(in memory)" ))
+    parser.parse( deref( yn ))
+    cdef dom.DOMDocument* doc = parser.getDocument()
+    cdef dom.DOMNode* root = doc.getDocumentElement()
+    return _element( root )
+cdef extern from "xercesc/sax/InputSource.hpp" namespace "xercesc":
+    cdef cppclass InputSource:
+        InputSource()

pyces/tree.pxd

-# cdef extern from "xercesc/util/XercesDefs.hpp" namespace "xercesc":
-#     ctypedef unsigned int XMLCh
-
-# cdef extern from "xercesc/dom/DOM.hpp" namespace "xercesc":
-#     cdef cppclass DOMElement:
-#        XMLCh* getTagName()
+cdef extern from "xercesc/util/PlatformUtils.hpp" namespace "xercesc":
+    cdef cppclass XMLPlatformUtils
+
+cdef extern from "xercesc/util/PlatformUtils.hpp" namespace "xercesc::XMLPlatformUtils":
+    void Initialize()
+    void Terminate()
+
+cdef extern from "xercesc/util/XercesDefs.hpp":
+    ctypedef unsigned short XMLCh
+    ctypedef unsigned char XMLByte
+    ctypedef size_t XMLSize_t
+
+cdef extern from "xercesc/util/XMLString.hpp":
+    cdef cppclass XMLString
+
+cdef extern from "xercesc/util/XMLString.hpp" namespace "xercesc::XMLString":
+    char* transcode( XMLCh* )
                                 [ "pyces/pyces.etree.pyx" ],
                                 language="c++",
                                 libraries=[ "stdc++",
-                                            # "xerces-c"
-                                            ] ) ] )
+                                            "xerces-c" ] ) ] )

test/test_etree.py

 from unittest import TestCase
+from pyces import etree
 
 class SimpleTest( TestCase ):
-    def test_hello( self ):
-        from pyces import etree
-        self.assertEquals( etree.hello( "abc" ), "abc" )
+    def setUp( self ):
+        etree.initialize()
+    def tearDown( self ):
+        etree.terminate()
+    def test_fromstring( self ):
+        root = etree.fromstring( "<abc>32</abc>" )
+        self.assertEquals( root.tag,
+                           "abc" )
+        self.assertEquals( root.text,
+                           "32" )