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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603
|
/*
FALCON - The Falcon Programming Language.
FILE: mxml_ext.cpp
MXML module main file - extension implementation.
-------------------------------------------------------------------
Author: Giancarlo Niccolai
Begin: Thu, 06 Mar 2008 19:44:41 +0100
-------------------------------------------------------------------
(C) Copyright 2004: the FALCON developers (see list in AUTHORS file)
See LICENSE file for licensing details.
*/
/** \file
MXML module main file - extension implementation.
*/
#include <falcon/vm.h>
#include <falcon/transcoding.h>
#include <falcon/fstream.h>
#include <falcon/lineardict.h>
#include "mxml_ext.h"
#include "mxml_mod.h"
#include "mxml_st.h"
#include "mxml.h"
/*#
@beginmodule feathers.mxml
*/
namespace MXML {
Falcon::CoreObject *Node::makeShell( Falcon::VMachine *vm )
{
static Falcon::Item *node_class = 0;
if( m_objOwner != 0 )
return m_objOwner;
if( node_class == 0 )
node_class = vm->findWKI( "MXMLNode" );
fassert( node_class != 0 );
Falcon::CoreObject *co = node_class->asClass()->createInstance();
co->setUserData( new Falcon::Ext::NodeCarrier( this, co ) );
return co;
}
}
namespace Falcon {
namespace Ext {
static MXML::Node *internal_getNodeParameter( VMachine *vm, int pid )
{
Item *i_child = vm->param(pid);
if ( i_child == 0 || ! i_child->isObject() || ! i_child->asObject()->derivedFrom( "MXMLNode" ) )
{
throw new ParamError( ErrorParam( e_inv_params, __LINE__ ).
extra( "MXMLNode" ) );
return 0;
}
return static_cast<NodeCarrier *>( i_child->asObject()->getUserData() )->node();
}
/*#
@class MXMLDocument
@brief Encapsulates a complete XML document.
@optparam encoding Encoding suggested for document load or required for document write.
@optparam style Style required in document write.
Class containing a logical XML file representation.
To work with MXML, you need to instantiate at least one object from this class;
this represents the logic of a document. It is derived for an element, as
an XML document must still be valid as an XML element of another document,
but it implements some data specific for handling documents.
It is possible to specify a @b encoding parameter which must be one of the
encoding names know by Falcon (see the @b TranscoderFactory function in the
RTL documentation). In this version, this parameter is ignored if the object
is used to deserialize an XML document, but it's used as output encoding (and
set in the "encoding" field of the XML heading) when writing the document.
The @b style parameter requires that a particular formatting is used when
writing the document. It can be overridden in the @a MXMLDocument.write method,
and if not provided there, the default set in the constructor will be used.
The @b style parameter must be in @a MXMLStyle enumeration.
@note It is not necessary to create and serialize a whole XML document to
create just XML compliant data representations. Single nodes can be serialized
with the @a MXMLNode.serialize method; in this way it is possible to create
small xml valid fragments for storage, network streaming, template filling
etc. At the same time, it is possible to de-serialize a single XML node
through the @a MXMLNode.deserialize method, which tries to decode an XML
document fragment configuring the node and eventually re-creating its subtree.
@section mxml_doc_struct MXML document structure.
The XML document, as seen by the MXML module, is a tree of nodes. Some nodes have
meta-informative value, and are meant to be used by the XML parser programs to
determine how the tree is expected to be built.
The tree itself has a topmost node (called top node), which is the parent for every
other node, and a node called "root" which is actually the root of the "informative
hierarchy" of the XML document, called 'tag nodes'.
Tag nodes can have some "attributes", zero or more children and a partent.
It is also possible to access the previous node at the same level of the tree,
or the next one, and it is possible to insert nodes in any position. Tag nodes
can have other tag nodes, data nodes or comment nodes as children. Processing Instruction
nodes can also be placed at any level of the XML tree.
A valid XML document can have only one root node, or in other words, it can declare
only one tag node at top level. In example, the following is a valid XML document:
@code
<?xml encoding="utf-8" version="1.0" ?>
<!-- The above was an XML special PI node, and this is a comment -->
<!DOCTYPE greeting SYSTEM "hello.dtd">
<!-- We see a doctype above -->
<MyDocumentRootTag>
...
</MyDocumentRootTag>
@endcode
In the above document, the top node would hold a comment, a DOCTYPE node, another comment
and then a tag node, which is also the "root" node.
The special XML instruction at the beginning is not translated into a node; instead,
its attribute becomes readable properties of the MXMLDocument instance (or are written
taking them from the instance properties, if the document is being written).
Falcon MXML node allows to create automatically simple data nodes attacched to tag nodes
by specifying a "data" value for the node. In example,
@code
<some_tag>Some data</some_tag>
@endcode
this node can be generated by creating a either a "some_tag" node and adding a data
child to it, or setting its @a MXMLNode.data to "Some tag". Falcon will automatically
import data for such nodes in the data field of the parent tag node.
On the other hand, it is possible to create combination of data and
tag children as in the following sample:
@code
<p>A paragraph <b>with bold text</b>.</p>
@endcode
In this case, it is necessary to create a "p" tag node with a child data node
containing "A paragraph ", a tag "b" node having "with bold text" as single data and
a data node containing a single "." character. The data in the "p" tag node will
be empty.
*/
FALCON_FUNC MXMLDocument_init( ::Falcon::VMachine *vm )
{
CoreObject *self = vm->self().asObject();
Item *i_encoding = vm->param(0);
Item *i_style = vm->param(1);
if ( ( i_encoding != 0 && ! i_encoding->isString() && ! i_encoding->isNil() ) ||
( i_style != 0 && ! i_style->isInteger()) )
{
throw new ParamError( ErrorParam( e_inv_params, __LINE__ ).
extra( "[S,I]" ) );
return;
}
int style = i_style == 0 ? 0 : (int) i_style->forceInteger();
MXML::Document *doc;
if( i_encoding == 0 || i_encoding->isNil() )
doc = new MXML::Document( "C", style );
else
doc = new MXML::Document( *i_encoding->asString(), style );
self->setUserData( new MXML::DocumentCarrier( doc ) );
}
/*#
@method deserialize MXMLDocument
@brief Loads an XML document from a stream.
@param istream A Falcon Stream instance opened for input.
@raise MXMLError on load error.
Loads a document from a Falcon Stream. After a succesful call,
the document is ready for inspection and can be modified.
@see MXMLDocument.read
*/
FALCON_FUNC MXMLDocument_deserialize( ::Falcon::VMachine *vm )
{
CoreObject *self = vm->self().asObject();
Item *i_stream = vm->param(0);
if ( i_stream == 0 || ! i_stream->isObject() || ! i_stream->asObject()->derivedFrom( "Stream" ) )
{
throw new ParamError( ErrorParam( e_inv_params, __LINE__ ).
extra( "Stream" ) );
return;
}
Stream *stream = static_cast<Stream *>( i_stream->asObject()->getUserData() );
MXML::Document *doc = static_cast<MXML::DocumentCarrier *>( self->getUserData() )->document();
try
{
doc->read( *stream );
vm->retval( true );
}
catch( MXML::MalformedError &err )
{
throw new MXMLError( ErrorParam( FALCON_MXML_ERROR_BASE + err.numericCode(), __LINE__ )
.desc( err.description() )
.extra( err.describeLine() ) );
}
catch( MXML::IOError &err )
{
throw new IoError( ErrorParam( FALCON_MXML_ERROR_BASE + err.numericCode(), __LINE__ )
.desc( err.description() )
.extra( err.describeLine() ) );
}
}
/*#
@method serialize MXMLDocument
@brief Stores the document as an XML file.
@param ostream A Falcon Stream opened for output.
@raise MXMLError on write error.
This method stores an XML document created using the
three in this instance on the stream passed as parameter.
@see MXMLDocument.write
*/
FALCON_FUNC MXMLDocument_serialize( ::Falcon::VMachine *vm )
{
CoreObject *self = vm->self().asObject();
Item *i_stream = vm->param(0);
if ( i_stream == 0 || ! i_stream->isObject() || ! i_stream->asObject()->derivedFrom( "Stream" ) )
{
throw new ParamError( ErrorParam( e_inv_params, __LINE__ ).
extra( "Stream" ) );
return;
}
Stream *stream = static_cast<Stream *>( i_stream->asObject()->getUserData() );
MXML::Document *doc = static_cast<MXML::DocumentCarrier *>( self->getUserData() )->document();
try
{
doc->write( *stream, doc->style() );
vm->retval( true );
}
catch( MXML::MalformedError &err )
{
throw new MXMLError( ErrorParam( FALCON_MXML_ERROR_BASE + err.numericCode(), __LINE__ )
.desc( err.description() )
.extra( err.describeLine() ) );
}
catch( MXML::IOError &err )
{
throw new IoError( ErrorParam( FALCON_MXML_ERROR_BASE + err.numericCode(), __LINE__ )
.desc( err.description() )
.extra( err.describeLine() ) );
}
}
/*#
@method style MXMLDocument
@brief Reads or changes the style applied to this XML document.
@optparam setting If given, changes the style.
@return The current style settings.
This method allows to read or change the style used for serialization
and deserialization of this document, which is usually set in the
constructor.
The @b setting paramter must be in @a MXMLStyle enumeration.
The method returns the current style as a combination of the bitfields
from the @a MXMLStyle enumeration.
@see MXMLDocument.init
*/
FALCON_FUNC MXMLDocument_style( ::Falcon::VMachine *vm )
{
CoreObject *self = vm->self().asObject();
MXML::Document *doc = static_cast<MXML::DocumentCarrier *>( self->getUserData() )->document();
Item *i_style = vm->param(0);
// read immediately the style, we always return it
vm->retval( (int64) doc->style() );
if ( i_style == 0 )
{
return;
}
if ( i_style == 0 || ! i_style->isInteger() )
{
throw new ParamError( ErrorParam( e_inv_params, __LINE__ ).
extra( "N" ) );
return;
}
doc->style( (int) i_style->asInteger() );
}
/*#
@method top MXMLDocument
@brief Retrieves the topmost node in the document.
@return The overall topmost node of the XML document.
This method returns the topmost node of the document;
this is actually an invisible node which is used as a
"container" for the top nodes in the document: comments,
directives as DOCTYPE and the "root" tag node.
@see MXMLDocument.root
@see MXMLDocument
*/
FALCON_FUNC MXMLDocument_top( ::Falcon::VMachine *vm )
{
CoreObject *self = vm->self().asObject();
MXML::Document *doc = static_cast<MXML::DocumentCarrier *>( self->getUserData() )->document();
MXML::Node *root = doc->root();
vm->retval( root->getShell( vm ) );
}
/*#
@method root MXMLDocument
@brief Retrieves the root tag node in the document.
@return The root tag node of the XML document.
This method returns the "root" node, which is the unique
topmost node of type "tag", and that defines the information
contents of the XML document.
The default name for this node is "root"; the implementor
should change the name to something more sensible before
serializing a document generated from this instance.
As a valid XML document must have exactly one root node,
an instance for this node is always generated when then
document is created, so it is always available.
@see MXMLDocument.top
@see MXMLDocument
*/
FALCON_FUNC MXMLDocument_root( ::Falcon::VMachine *vm )
{
CoreObject *self = vm->self().asObject();
MXML::Document *doc = static_cast<MXML::DocumentCarrier *>( self->getUserData() )->document();
MXML::Node *root = doc->main();
// if we don't have a root (main) node, create it.
if ( root == 0 ) {
root = new MXML::Node( MXML::Node::typeTag, "root" );
doc->root()->addBelow( root );
}
vm->retval( root->getShell( vm ) );
}
/*#
@method find MXMLDocument
@brief Finds the first (tag) node matching a certain criterion.
@param name Tag name of the searched node.
@optparam attrib Name of one of the attributes of the searched node.
@optparam value Value for one of the attributes in the node.
@optparam data Part of the data stored in the searched tag node.
@return The node matching the given criterion or nil if not found.
This method performs a search in the XML tree, starting from the root,
from a tag node with the given name, attribute (eventually having a certain
value) and specified data portion. All the paramters are optional, and
can be substituted with a nil or not given to match "everything".
The @a MXMLDocument.findNext method will repeat the search starting from
the last matching node; direction of the search is down towards the leaves
of the tree, then forward towards the next siblings. When the nodes matching
the criterion are exhausted, the two methods return nil.
In example, to search in a tree for all the nodes named "interesting", the
following code can be used:
@code
// doc is a MXMLDocument
node = doc.find( "interesting" )
while node != nil
> "Found an interesting node:", node.path()
...
node = doc.findNext()
end
@endcode
To find a node which has an attribute named "cute" (at which value
we're not interested), and which data node contains the word "suspect",
the following code can be used:
@code
node = doc.find( nil, "cute", nil, "suspect" )
while node != nil
> "Found a suspect node:", node.path()
...
node = doc.findNext()
end
@endcode
@note Checks are case sensitive.
@see MXMLDocument.findNext
*/
FALCON_FUNC MXMLDocument_find( ::Falcon::VMachine *vm )
{
Item *i_name = vm->param(0);
Item *i_attrib = vm->param(1);
Item *i_valattr = vm->param(2);
Item *i_data = vm->param(3);
CoreObject *self = vm->self().asObject();
// parameter sanity check
if( ( i_name == 0 || (! i_name->isString() && ! i_name->isNil() )) ||
( i_attrib != 0 && (! i_attrib->isString() && ! i_attrib->isNil() )) ||
( i_valattr != 0 && (! i_valattr->isString() && ! i_valattr->isNil() )) ||
( i_data != 0 && (! i_data->isString() && ! i_data->isNil() ))
)
{
throw new ParamError( ErrorParam( e_inv_params, __LINE__ ).
extra( "S,[S,S,S]" ) );
return;
}
String dummy;
String *sName, *sValue, *sValAttr, *sData;
sName = i_name == 0 || i_name->isNil() ? &dummy : i_name->asString();
sValue = i_attrib == 0 || i_attrib->isNil() ? &dummy : i_attrib->asString();
sValAttr = i_valattr == 0 || i_valattr->isNil() ? &dummy : i_valattr->asString();
sData = i_data == 0 || i_data->isNil() ? &dummy : i_data->asString();
MXML::Document *doc = static_cast<MXML::DocumentCarrier *>( self->getUserData() )->document();
// the real find
MXML::Node *node = doc->find( *sName, *sValue, *sValAttr, *sData );
if ( node == 0 )
vm->retnil();
else
vm->retval( node->getShell( vm ) );
}
/*#
@method findNext MXMLDocument
@brief Finds the next (tag) node matching a certain criterion.
@return The next node matching the given criterion or nil if not found.
This method is meant to be used after a @a MXMLDocument.find call has
returned a valid node to iterate through all the matching nodes in a tree.
@see MXMLDocument.find
*/
FALCON_FUNC MXMLDocument_findNext( ::Falcon::VMachine *vm )
{
CoreObject *self = vm->self().asObject();
MXML::Document *doc = static_cast<MXML::DocumentCarrier *>( self->getUserData() )->document();
// the real find
MXML::Node *node = doc->findNext();
if ( node == 0 )
vm->retnil();
else
vm->retval( node->getShell( vm ) );
}
/*#
@method findPath MXMLDocument
@brief Finds the first (tag) node matching a certain XML path.
@param path The XML path to be searched.
@return The next node matching the given criterion or nil if not found.
This method provides limited (at this time, very limited) support for xpath.
A tag node can be found through a virtual "path" staring from the root node and
leading to it; each element of the path is a tag parent node. In example, the
path for the node "inner" in the following example:
@code
<root>
<outer>
<middle>
<inner>Inner content</inner>
</middle>
</outer>
</root>
@endcode
would be "/root/outer/middle/inner".
Paths are not unique keys for the nodes; in the above example, more than one "inner" node may
be stacked inside the "middle" node, and all of them would have the same path.
This method allows to use a "*" wildcard to substitute a level of the path with "anything". In
example, the path "/root/\*\/middle/inner" would find the inner node in the above sample no matter
what the second-topmost node name was.
If the path cannot match any node in the three, the method returns nil. It is possible to iterate
through all the nodes having the same path (or matching wildcard paths) in a tree by using the
@a MXMLDocument.findPathNext method. In example, the following code would find all the nodes
which have exactly two parents:
@code
node = doc.findPath( "/\*\/\*\/\*" )
while node != nil
> "Found a node at level 3:", node.path()
...
node = doc.findPathNext()
end
@endcode
@see MXMLDocument.findPathNext
*/
FALCON_FUNC MXMLDocument_findPath( ::Falcon::VMachine *vm )
{
Item *i_name = vm->param(0);
CoreObject *self = vm->self().asObject();
// parameter sanity check
if( i_name == 0 || ! i_name->isString() )
{
throw new ParamError( ErrorParam( e_inv_params, __LINE__ ).
extra( "S" ) );
return;
}
MXML::Document *doc = static_cast<MXML::DocumentCarrier *>( self->getUserData() )->document();
// the real find
MXML::Node *node = doc->findPath( *i_name->asString() );
if ( node == 0 )
vm->retnil();
else
vm->retval( node->getShell( vm ) );
}
/*#
@method findPathNext MXMLDocument
@brief Finds the next (tag) node matching a certain XML path.
@return The next node matching the given criterion or nil if not found.
This method is meant to be used together with @a MXMLDocument.findPath
method to traverse a tree in search of nodes matching certain paths.
@see MXMLDocument.findPath
*/
FALCON_FUNC MXMLDocument_findPathNext( ::Falcon::VMachine *vm )
{
CoreObject *self = vm->self().asObject();
MXML::Document *doc = static_cast<MXML::DocumentCarrier *>( self->getUserData() )->document();
// the real find
MXML::Node *node = doc->findNextPath();
if ( node == 0 )
vm->retnil();
else
vm->retval( node->getShell( vm ) );
}
/*#
@method write MXMLDocument
@brief Stores a document to an XML file on a filesystem.
@param filename Name of the destination XML file.
@raise MXMLError on error during the serialization.
This method saves the XML document to a file on a
reachable filesystem. It takes care of opening a suitable
stream, transcoding it using the chosen encoding and
performing a complete serialization through
@a MXMLDocument.serialize.
@see MXMLDocument.setEncoding
*/
FALCON_FUNC MXMLDocument_save( ::Falcon::VMachine *vm )
{
CoreObject *self = vm->self().asObject();
Item *i_uri = vm->param(0);
if ( i_uri == 0 || ! i_uri->isString() )
{
throw new ParamError( ErrorParam( e_inv_params, __LINE__ ).
extra( "N" ) );
return;
}
String &uri = *i_uri->asString();
MXML::Document *doc = static_cast<MXML::DocumentCarrier *>( self->getUserData() )->document();
vm->idle();
//TODO: use parsing uri
FileStream out;
if ( out.create( uri, BaseFileStream::e_aUserRead | BaseFileStream::e_aUserWrite | BaseFileStream::e_aGroupRead | BaseFileStream::e_aOtherRead ) )
{
Stream *output = &out;
if ( doc->encoding() != "C" )
{
output = TranscoderFactory( doc->encoding(), &out, false );
if ( output == 0 )
{
vm->unidle();
throw new MXMLError( ErrorParam( e_inv_params, __LINE__ )
.extra( "Invalid encoding " + doc->encoding() ) );
return;
}
}
try
{
doc->write( *output, doc->style() );
vm->unidle();
vm->retval( true );
}
catch( MXML::MalformedError &err )
{
vm->unidle();
throw new MXMLError( ErrorParam( FALCON_MXML_ERROR_BASE + err.numericCode(), __LINE__ )
.desc( err.description() )
.extra( err.describeLine() ) );
}
catch( MXML::IOError &err )
{
vm->unidle();
throw new IoError( ErrorParam( FALCON_MXML_ERROR_BASE + err.numericCode(), __LINE__ )
.desc( err.description() )
.extra( err.describeLine() ) );
}
}
else
{
vm->unidle();
throw new IoError( ErrorParam(
FALCON_MXML_ERROR_BASE + (int) MXML::Error::errIo , __LINE__ )
.desc( FAL_STR( MXML_ERR_IO ) ) );
}
}
/*#
@method read MXMLDocument
@brief Loads a document to an XML file on a filesystem.
@param filename Name of the source XML file.
@raise MXMLError on error during the deserialization.
This method loads the XML document from a file on a
reachable filesystem. It takes care of opening a suitable
stream, transcoding it using the chosen encoding and
performing a complete deserialization through
@a MXMLDocument.deserialize.
@see MXMLDocument.setEncoding
*/
FALCON_FUNC MXMLDocument_load( ::Falcon::VMachine *vm )
{
CoreObject *self = vm->self().asObject();
Item *i_uri = vm->param(0);
if ( i_uri == 0 || ! i_uri->isString() )
{
throw new ParamError( ErrorParam( e_inv_params, __LINE__ ).
extra( "S" ) );
return;
}
String &uri = *i_uri->asString();
MXML::Document *doc = static_cast<MXML::DocumentCarrier *>( self->getUserData() )->document();
vm->idle();
//TODO: use parsing uri
FileStream in;
if ( in.open( uri ) )
{
Stream *input = ∈
if ( doc->encoding() != "C" )
{
input = TranscoderFactory( doc->encoding(), &in, false );
if ( input == 0 )
{
vm->unidle();
throw new MXMLError( ErrorParam( e_inv_params, __LINE__ )
.extra( FAL_STR( MXML_ERR_INVENC ) + doc->encoding() ) );
return;
}
}
try
{
doc->read( *input );
vm->unidle();
vm->retval( true );
}
catch( MXML::MalformedError &err )
{
vm->unidle();
throw new MXMLError(
ErrorParam( FALCON_MXML_ERROR_BASE + err.numericCode(), __LINE__ )
.desc( err.description() )
.extra( err.describeLine() ) );
}
catch( MXML::IOError &err )
{
vm->unidle();
throw new IoError(
ErrorParam( FALCON_MXML_ERROR_BASE + err.numericCode(), __LINE__ )
.desc( err.description() )
.extra( err.describeLine() ) );
}
in.close();
return;
}
if ( ! in.good() )
{
throw new IoError( ErrorParam(
FALCON_MXML_ERROR_BASE + (int) MXML::Error::errIo , __LINE__ )
.desc( FAL_STR( MXML_ERR_IO ) ) );
}
in.close();
}
/*#
@method setEncoding MXMLDocument
@brief Changes the document encoding for stream operations.
@param encoding A valid falcon encoding name
@raise ParamError if the encoding name is unknown.
This method sets the encoding used for I/O operations on this
XML document. It also determines the value of the "encoding"
attribute that will be set in the the special PI ?xml at
document heading.
@see MXMLDocument.getEncoding
*/
FALCON_FUNC MXMLDocument_setEncoding( ::Falcon::VMachine *vm )
{
CoreObject *self = vm->self().asObject();
Item *i_encoding = vm->param(0);
if ( i_encoding == 0 || ! i_encoding->isString() )
{
throw new ParamError( ErrorParam( e_inv_params, __LINE__ ).
extra( "S" ) );
return;
}
String &encoding = *i_encoding->asString();
Transcoder *tr = TranscoderFactory( encoding );
if ( tr == 0 )
{
throw new ParamError( ErrorParam( e_param_range, __LINE__ ).
extra( encoding ) );
return;
}
delete tr;
MXML::Document *doc = static_cast<MXML::DocumentCarrier *>( self->getUserData() )->document();
doc->encoding( encoding );
}
/*#
@method getEncoding MXMLDocument
@brief Returns the encoding for stream operations.
@return Previously set or read XML encoding.
This method returns the encoding that has been previously set
either at document creation or through the @a MXMLDocument.setEncoding.
Also, after a succesful deserialization, this method will return the
value of the "encoding" attribute of the ?xml PI heading directive,
if provided.
If still unset, this method will return nil.
@see MXMLDocument.setEncoding
*/
FALCON_FUNC MXMLDocument_getEncoding( ::Falcon::VMachine *vm )
{
CoreObject *self = vm->self().asObject();
MXML::Document *doc = static_cast<MXML::DocumentCarrier *>( self->getUserData() )->document();
vm->retval( new CoreString( doc->encoding() ) );
}
/*#
@class MXMLNode
@param type One of the node type defined by the @a MXMLType enumeration.
@optparam name Name of the node, if this is a tag node.
@optparam data Optional data content attached to this node..
@brief Minimal entity of the XML document.
@raise ParamError if the type is invalid.
This class encapsulates a minimal adressable entity in an XML document.
Nodes can be of different types, some of which, like CDATA, tag and comment nodes
can have a simple textual data attached to them (equivalent to a single data node
being their only child).
Nodes can be attached and detached from trees or serialized on their own. The
subtrees of child nodes stays attached to its parent also when the MXMLDocument
they are attached to is changed. Also, serializing a node directly allows to
write mini xml valid fragments which may be used for network transmissions,
database storage, template filling etc., without the need to build a whole XML
document and writing the ?xml heading declarator.
The @b type must be one of the @a MXMLType enumeration elements.
The @b name of the node is relevant only for Processing Instruction
nodes and tag node, while data can be specified for comment, tag and
data nodes.
If the node is created just to be de-serialized, create it as an empty
comment and then deserialize the node from a stream.
*/
FALCON_FUNC MXMLNode_init( ::Falcon::VMachine *vm )
{
CoreObject *self = vm->self().asObject();
Item *i_type = vm->param(0);
Item *i_name = vm->param(1);
Item *i_data = vm->param(2);
if ( ( i_type != 0 && ! i_type->isInteger() ) ||
( i_name != 0 && ! (i_name->isString() || i_name->isNil()) ) ||
( i_data != 0 && ! i_data->isString() ) )
{
throw new ParamError( ErrorParam( e_inv_params, __LINE__ ).
extra( "[N,S,S]" ) );
return;
}
// verify type range
int type = i_type != 0 ? (int) i_type->asInteger() : 0;
if ( type < 0 || type > (int) MXML::Node::typeFakeClosing )
{
throw new ParamError( ErrorParam( e_inv_params, __LINE__ ).
extra( "Invalid type" ) );
return;
}
String dummy;
String *name = i_name == 0 || i_name->isNil() ? &dummy : i_name->asString();
String *data = i_data == 0 ? &dummy : i_data->asString();
MXML::Node *node = new MXML::Node( (MXML::Node::type) type, *name, *data );
self->setUserData( new NodeCarrier( node, self ) );
}
/*#
@method serialize MXMLNode
@brief Stores an XML node on a stream.
@raise MXMLError If the serialization failed.
This method allows the storage of a single node and all its
children in an XML compliant format. The resulting data
is an valid XML fragment that may be included verbatim in
an XML document.
*/
FALCON_FUNC MXMLNode_serialize( ::Falcon::VMachine *vm )
{
CoreObject *self = vm->self().asObject();
Item *i_stream = vm->param(0);
if ( i_stream == 0 || ! i_stream->isObject() || ! i_stream->asObject()->derivedFrom( "Stream" ) )
{
throw new ParamError( ErrorParam( e_inv_params, __LINE__ ).
extra( "Stream" ) );
return;
}
Stream *stream = static_cast<Stream *>( i_stream->asObject()->getUserData() );
MXML::Node *node = static_cast<NodeCarrier *>( self->getUserData() )->node();
try
{
node->write( *stream, 0 );
vm->retval( true );
}
catch( MXML::MalformedError &err )
{
throw new MXMLError(
ErrorParam( FALCON_MXML_ERROR_BASE + err.numericCode(), __LINE__ )
.desc( err.description() )
.extra( err.describeLine() ) );
}
catch( MXML::IOError &err )
{
throw new MXMLError(
ErrorParam( FALCON_MXML_ERROR_BASE + err.numericCode(), __LINE__ )
.desc( err.description() )
.extra( err.describeLine() ) );
}
}
/*#
@method deserialize MXMLNode
@brief Stores an XML node on a stream.
@raise MXMLError If the serialization failed.
This method allows the storage of a single node and all its
children in an XML compliant format. The resulting data
is an valid XML fragment that may be included verbatim in
an XML document.
*/
FALCON_FUNC MXMLNode_deserialize( ::Falcon::VMachine *vm )
{
CoreObject *self = vm->self().asObject();
Item *i_stream = vm->param(0);
if ( i_stream == 0 || ! i_stream->isObject() || ! i_stream->asObject()->derivedFrom( "Stream" ) )
{
throw new ParamError( ErrorParam( e_inv_params, __LINE__ ).
extra( "Stream" ) );
return;
}
Stream *stream = static_cast<Stream *>( i_stream->asObject()->getUserData() );
delete static_cast<NodeCarrier *>( self->getUserData() );
MXML::Node *node = new MXML::Node();
try
{
node->read( *stream );
self->setUserData( new NodeCarrier( node, self ) );
vm->retval( self );
}
catch( MXML::MalformedError &err )
{
throw new MXMLError(
ErrorParam( FALCON_MXML_ERROR_BASE + err.numericCode(), __LINE__ )
.desc( err.description() )
.extra( err.describeLine() ) );
delete node;
}
catch( MXML::IOError &err )
{
throw new MXMLError(
ErrorParam( FALCON_MXML_ERROR_BASE + err.numericCode(), __LINE__ )
.desc( err.description() )
.extra( err.describeLine() ) );
delete node;
}
}
/*#
@method nodeType MXMLNode
@brief Returns the type of this node.
@return A value in @a MXMLType enumeration.
*/
FALCON_FUNC MXMLNode_nodeType( ::Falcon::VMachine *vm )
{
CoreObject *self = vm->self().asObject();
MXML::Node *node = static_cast<NodeCarrier *>( self->getUserData() )->node();
vm->retval( (int64)node->nodeType() );
}
/*#
@method name MXMLNode
@brief Set and/or return the name of this node.
@optparam name If provided, the new name of this node.
@return If a new @b name is not given, the current node name.
A name can be assigned to any node, but it will be meaningful only
for tag and PI nodes.
*/
FALCON_FUNC MXMLNode_name( ::Falcon::VMachine *vm )
{
Item *i_name = vm->param(0);
if ( i_name != 0 && ! i_name->isString() )
{
throw new ParamError( ErrorParam( e_inv_params, __LINE__ ).
extra( "[S]" ) );
return;
}
CoreObject *self = vm->self().asObject();
MXML::Node *node = static_cast<NodeCarrier *>( self->getUserData() )->node();
if ( i_name == 0 )
vm->retval( new CoreString( node->name() ) );
else
node->name( *i_name->asString() );
}
/*#
@method data MXMLNode
@brief Set and/or return the content of this node.
@optparam data If provided, the new data of this node.
@return If a new @b data is not given, the current node data.
A data can be assigned to any node, but it will be meaningful only
for data, tag, comment and CDATA nodes. Moreover, tag nodes can have
also other children; in this case, the data set with this method will
be serialized as if it was a first child data node.
*/
FALCON_FUNC MXMLNode_data( ::Falcon::VMachine *vm )
{
Item *i_data = vm->param(0);
if ( i_data != 0 && ! i_data->isString() )
{
throw new ParamError( ErrorParam( e_inv_params, __LINE__ ).
extra( "[S]" ) );
return;
}
CoreObject *self = vm->self().asObject();
MXML::Node *node = static_cast<NodeCarrier *>( self->getUserData() )->node();
if ( i_data == 0 )
vm->retval( new CoreString( node->data() ) );
else
node->data( *i_data->asString() );
}
/*#
@method setAttribute MXMLNode
@brief Sets an XML attribute of this node to a given value.
@param attribute The XML attribute to be set.
@param value The value for this XML attribute.
This method sets the value for an XML attribute of the node.
Attributes can be assigned to PI, Tag and DOCTYPE nodes.
The @b value parameter can be any Falcon type; if it's not
a string, the @b FBOM.toString method will be applied to transform
it into a string.
If the attribute doesn't exist, it is added, otherwise it's value
is changed.
@note Don't confuse XML attributes with Falcon attributes.
*/
FALCON_FUNC MXMLNode_setAttribute( ::Falcon::VMachine *vm )
{
CoreObject *self = vm->self().asObject();
MXML::Node *node = static_cast<NodeCarrier *>( self->getUserData() )->node();
Item *i_attrName = vm->param(0);
Item *i_attrValue = vm->param(1);
if ( i_attrName == 0 || ! i_attrName->isString() ||
i_attrValue == 0
)
{
throw new ParamError( ErrorParam( e_inv_params, __LINE__ ).
extra( "S,X" ) );
return;
}
String content;
String *value;
if ( i_attrValue->isString() )
{
value = i_attrValue->asString();
}
else {
vm->itemToString( content, i_attrValue );
value = &content;
}
const String &attrName = *i_attrName->asString();
if( ! node->hasAttribute( attrName ) )
{
node->addAttribute( new MXML::Attribute( attrName, *value ) );
}
node->setAttribute( attrName, *value );
}
/*#
@method getAttribute MXMLNode
@brief Gets the value of an XML attribute of this node.
@param attribute The XML attribute to be read.
@return The value for this XML attribute (as a string).
This method retreives the value for an XML attribute of the node.
Attributes can be assigned to PI, Tag and DOCTYPE nodes.
If the attribute doesn't exist, nil is returned.
@note Don't confuse XML attributes with Falcon attributes.
*/
FALCON_FUNC MXMLNode_getAttribute( ::Falcon::VMachine *vm )
{
CoreObject *self = vm->self().asObject();
MXML::Node *node = static_cast<NodeCarrier *>( self->getUserData() )->node();
Item *i_attrName = vm->param(0);
if ( i_attrName == 0 || ! i_attrName->isString() )
{
throw new ParamError( ErrorParam( e_inv_params, __LINE__ ).
extra( "S" ) );
return;
}
if ( ! node->hasAttribute( *i_attrName->asString() ) )
{
vm->retnil();
return;
}
const String &val = node->getAttribute( *i_attrName->asString() );
vm->retval( new CoreString( val ) );
}
/*#
@method getAttribs MXMLNode
@brief Gets the all the XML attributes of this node.
@return A dictionary containing all the XML attributes and their values.
This method retreives all the attributes of the node, and stores them
in a dictionary as a pair of key => value strings.
Attributes can be assigned to PI, Tag and DOCTYPE nodes.
If the node doesn't have any XML attribute, an empty dictionary is
returned.
The dictionary is read-only; values in the dictionary can be changed,
but this won't change the values of the original XML attributes in
the source node.
@note Don't confuse XML attributes with Falcon attributes.
*/
FALCON_FUNC MXMLNode_getAttribs( ::Falcon::VMachine *vm )
{
CoreObject *self = vm->self().asObject();
MXML::Node *node = static_cast<NodeCarrier *>( self->getUserData() )->node();
const MXML::AttribList &attribs = node->attribs();
LinearDict *dict = new LinearDict( attribs.size() );
MXML::AttribList::const_iterator iter = attribs.begin();
while( iter != attribs.end() )
{
dict->put( new CoreString( (*iter)->name()),
new CoreString( (*iter)->value()) );
++iter;
}
vm->retval( new CoreDict(dict) );
}
/*#
@method getChildren MXMLNode
@brief Gets the all the children nodes of this node.
@return An array containing all the children node.
This method stores all the children of an XML node in an
array.
If the node doesn't have any child, an empty array is
returned.
The array is read-only; it is possible to change it but
inserting or removing nodes from it won't change the children
list of the source node.
*/
FALCON_FUNC MXMLNode_getChildren( ::Falcon::VMachine *vm )
{
CoreObject *self = vm->self().asObject();
MXML::Node *node = static_cast<NodeCarrier *>( self->getUserData() )->node();
CoreArray *arr = new CoreArray;
node = node->child();
while( node != 0 )
{
arr->append( node->getShell( vm ) );
node = node->next();
}
vm->retval( arr );
}
/*#
@method unlink MXMLNode
@brief Removes a node from its parent tree.
This method removes a node from the list of node of
its parent node. The node is removed together with all
its children and their whole subtree.
After an unlink, it is possible to insert the node into
another place of the same tree or of another tree.
Actually, all the insertion routines perform an @b unlink on
the node that is going to be inserted, so it is not
necessary to call @b unlink from the falcon script before
adding it elsewhere. However, explicitly unlinked node may be
kept elsewhere (i.e. in a script maintained dictionary) for
later usage.
*/
FALCON_FUNC MXMLNode_unlink( ::Falcon::VMachine *vm )
{
CoreObject *self = vm->self().asObject();
MXML::Node *node = static_cast<NodeCarrier *>( self->getUserData() )->node();
node->unlink();
}
/*#
@method removeChild MXMLNode
@brief Removes a child from its parent tree.
@param child The child node to be removed.
@return True if the @b child node is actually a child of this node, false otherwise.
This method is equivalent to @b MXMLNode.unlink applied to the child node,
but it checks if the removed node is really a child of this node before actually
removing it.
If the @b child parameter is really a child of this node it is unlinked and the
method returns true, otherwise the node is untouched and the method returns false.
*/
FALCON_FUNC MXMLNode_removeChild( ::Falcon::VMachine *vm )
{
MXML::Node *child = internal_getNodeParameter( vm, 0 );
if ( child == 0 )
return;
CoreObject *self = vm->self().asObject();
MXML::Node *node = static_cast<NodeCarrier *>( self->getUserData() )->node();
try {
node->removeChild( child );
vm->retval(true);
}
catch( ... )
{
vm->retval( false );
}
}
/*#
@method parent MXMLNode
@brief Return the parent node of this node.
@return The parent node or nil if this node has no parents.
This method returns the node that is currently
parent of this node in the XML tree.
The method returns nil if the node hasn't a parent; this may mean
that this node is the topmost node in an XMLDocument (the node
returned by @a MXMLDocument.top ) or if it has not still been added
to a tree, or if it has been removed with @a MXMLNode.removeChild or
@a MXMLNode.unlink.
*/
FALCON_FUNC MXMLNode_parent( ::Falcon::VMachine *vm )
{
CoreObject *self = vm->self().asObject();
MXML::Node *node = static_cast<NodeCarrier *>( self->getUserData() )->node();
MXML::Node *parent = node->parent();
if ( parent != 0 )
vm->retval( parent->getShell( vm ) );
else
vm->retnil();
}
/*#
@method firstChild MXMLNode
@brief Return the first child of a node.
@return The first child of this node or nil if the node hasn't any child.
This method returns the first child of a node; it's the node that will
be delivered for first in the rendering of the final XML document, and that
will appare on the topmost postition between the nodes below the current
one.
To iterate through all the child nodes of a node, it is possible to
get the first child and the iteratively @a MXMLNode.nextSibling
until it returns nil. In example:
@code
// node is an MXMLNode...
child = node.firstChild()
while child != nil
> "Child of ", node.name(), ": ", child.name()
child = child.nextSibling()
end
@endcode
*/
FALCON_FUNC MXMLNode_firstChild( ::Falcon::VMachine *vm )
{
CoreObject *self = vm->self().asObject();
MXML::Node *node = static_cast<NodeCarrier *>( self->getUserData() )->node();
MXML::Node *child = node->child();
if ( child != 0 )
vm->retval( child->getShell( vm ) );
else
vm->retnil();
}
/*#
@method nextSibling MXMLNode
@brief Return the next node child of the same parent.
@return The next node at the same level, or nil.
This method returns the next node that would be found in
the rendered XML document right after this one, at the same level.
If such node doesn't exist, it returns nil.
@see MXMLNode.firstChild
*/
FALCON_FUNC MXMLNode_nextSibling( ::Falcon::VMachine *vm )
{
CoreObject *self = vm->self().asObject();
MXML::Node *node = static_cast<NodeCarrier *>( self->getUserData() )->node();
MXML::Node *sibling = node->next();
if ( sibling != 0 )
vm->retval( sibling->getShell( vm ) );
else
vm->retnil();
}
/*#
@method prevSibling MXMLNode
@brief Return the previous node child of the same parent.
@return The previous node at the same level, or nil.
This method returns the previous node that would be found in
the rendered XML document right after this one, at the same level.
If such node doesn't exist, it returns nil.
@see MXMLNode.lastChild
*/
FALCON_FUNC MXMLNode_prevSibling( ::Falcon::VMachine *vm )
{
CoreObject *self = vm->self().asObject();
MXML::Node *node = static_cast<NodeCarrier *>( self->getUserData() )->node();
MXML::Node *sibling = node->prev();
if ( sibling != 0 )
vm->retval( sibling->getShell( vm ) );
else
vm->retnil();
}
/*#
@method lastChild MXMLNode
@brief Return the last child of a node.
@return The last child of this node or nil if the node hasn't any child.
This method returns the last child of a node; it's the node that will
be delivered for last in the rendering of the final XML document, and that
will appare on the lowermost postition between the nodes below the current
one.
To iterate through all the child nodes of a node in reverse order,
it is possible to get the last child and the iteratively
@a MXMLNode.prevSibling
until it returns nil. In example:
@code
// node is an MXMLNode...
child = node.lastChild()
while child != nil
> "Child of ", node.name(), " reverse: ", child.name()
child = child.prevSibling()
end
@endcode
*/
FALCON_FUNC MXMLNode_lastChild( ::Falcon::VMachine *vm )
{
CoreObject *self = vm->self().asObject();
MXML::Node *node = static_cast<NodeCarrier *>( self->getUserData() )->node();
MXML::Node *sibling = node->lastChild();
if ( sibling != 0 )
vm->retval( sibling->getShell( vm ) );
else
vm->retnil();
}
/*#
@method addBelow MXMLNode
@brief Adds a node below this one.
@param node The node to be added below this one.
This method appends the given @b node as the last child
of this node, eventually removing it from a prevuious tree
structure to which it was linked if needed.
After this method returns, @b node can be retreived calling the
@a MXMLNode.lastChild on this node, until another @b addBelow
adds another node at the end of the children list.
*/
FALCON_FUNC MXMLNode_addBelow( ::Falcon::VMachine *vm )
{
MXML::Node *child = internal_getNodeParameter( vm, 0 );
if ( child == 0 )
return;
CoreObject *self = vm->self().asObject();
MXML::Node *node = static_cast<NodeCarrier *>( self->getUserData() )->node();
// just to be sure
child->unlink();
node->addBelow( child );
}
/*#
@method insertBelow MXMLNode
@brief Inserts a node below this one.
@param node The node to be added below this one.
This method prepends the given @b node as the first child
of this node, eventually removing it from a prevuious tree
structure to which it was linked if needed.
After this method returns, @b node can be retreived calling the
@a MXMLNode.firstChild on this node, until another @b insertBelow
adds another node at the beginning of the children list.
*/
FALCON_FUNC MXMLNode_insertBelow( ::Falcon::VMachine *vm )
{
MXML::Node *child = internal_getNodeParameter( vm, 0 );
if ( child == 0 )
return;
CoreObject *self = vm->self().asObject();
MXML::Node *node = static_cast<NodeCarrier *>( self->getUserData() )->node();
// just to be sure
child->unlink();
node->insertBelow( child );
}
/*#
@method insertBefore MXMLNode
@brief Inserts a node before this one.
@param node The node to be added before this one.
This method prepends the given @b node in front of this one
in the list of sibling nodes, eventually removing it from a prevuious tree
structure to which it was linked if needed. This is equivalent to inserting
the node exactly before this one, at the same level, in the final
XML document.
If this node was the first child of its parent, the inserted node
becomes the new first child.
*/
FALCON_FUNC MXMLNode_insertBefore( ::Falcon::VMachine *vm )
{
MXML::Node *child = internal_getNodeParameter( vm, 0 );
if ( child == 0 )
return;
CoreObject *self = vm->self().asObject();
MXML::Node *node = static_cast<NodeCarrier *>( self->getUserData() )->node();
// just to be sure
child->unlink();
node->insertBefore( child );
}
/*#
@method insertAfter MXMLNode
@brief Inserts a node after this one.
@param node The node to be added after this one.
This method prepends the given @b node after of this one
in the list of sibling nodes, eventually removing it from a prevuious tree
structure to which it was linked if needed. This is equivalent to inserting
the node exactly after this one, at the same level, in the final
XML document.
If this node was the last child of its parent, the inserted node
becomes the new last child.
*/
FALCON_FUNC MXMLNode_insertAfter( ::Falcon::VMachine *vm )
{
MXML::Node *child = internal_getNodeParameter( vm, 0 );
if ( child == 0 )
return;
CoreObject *self = vm->self().asObject();
MXML::Node *node = static_cast<NodeCarrier *>( self->getUserData() )->node();
// just to be sure
child->unlink();
node->insertAfter( child );
}
/*#
@method depth MXMLNode
@brief Calculates the depth of this node.
@return The depth of this node.
This method returns the number of steps needed to find a
node without parents in the parent hierarchy of this node.
The dept for a topmost tree node is 0, for a root node in a tree
is 1 and for its direct child is 2.
*/
FALCON_FUNC MXMLNode_depth( ::Falcon::VMachine *vm )
{
CoreObject *self = vm->self().asObject();
MXML::Node *node = static_cast<NodeCarrier *>( self->getUserData() )->node();
vm->retval( (int64) node->depth() );
}
/*#
@method path MXMLNode
@brief Returns the path from the root to this node.
@return The path of this node in its XML document tree.
The path of a node is the list of parent node names separated
by a slash "/", starting from the root node (or from the first
node of a separate tree) and terminating with the node itself.
In example, the path of the node "item" in the following XML document:
@code
<root>
<content>
<item/>
</content>
</root>
@endcode
would be "/root/content/item"
@see MXMLDocument.findPath
*/
FALCON_FUNC MXMLNode_path( ::Falcon::VMachine *vm )
{
CoreObject *self = vm->self().asObject();
MXML::Node *node = static_cast<NodeCarrier *>( self->getUserData() )->node();
CoreString *gs = new CoreString( node->path() );
gs->bufferize();
vm->retval( gs );
}
/*#
@method clone MXMLNode
@brief Clones a whole XML hierarcy starting from this node.
@return A copy of this node, with all its children copied.
*/
FALCON_FUNC MXMLNode_clone( ::Falcon::VMachine *vm )
{
CoreObject *self = vm->self().asObject();
MXML::Node *node = static_cast<NodeCarrier *>( self->getUserData() )->node();
vm->retval( node->clone()->getShell( vm ) );
}
//=======================================================================
// MXML error class
//
/*#
@class MXMLError
@brief Error raised by the MXML module in case of problems.
@optparam code A numeric error code.
@optparam description A textual description of the error code.
@optparam extra A descriptive message explaining the error conditions.
@from Error code, description, extra
An instance of this class is raised whenever some problem is
found. The error codes generated by this module are in the
@a MXMLErrorCode enumeration.
*/
FALCON_FUNC MXMLError_init ( ::Falcon::VMachine *vm )
{
CoreObject *einst = vm->self().asObject();
if( einst->getUserData() == 0 )
einst->setUserData( new MXMLError );
::Falcon::core::Error_init( vm );
}
}
}
/* end of mxml_ext.cpp */
|