 |
Changeset 3288
- Timestamp:
- 02/26/08 04:28:10
(9 months ago)
- Author:
- kris
- Message:
fixed DOM bug, and reorganized XPath support
-
Files:
-
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
| r3280 |
r3288 |
|
| 49 | 49 | w.start; |
|---|
| 50 | 50 | for (uint i = count; --i;) |
|---|
| 51 | | set = doc.query.descendant.filter((doc.Node n) {return n.hasText("value");}); |
|---|
| 52 | | result ("text-predicate lookups/s", count/w.stop, set); |
|---|
| | 51 | set = doc.query.descendant.filter((doc.Node n) {return n.hasData("value");}); |
|---|
| | 52 | result ("text-filter lookups/s", count/w.stop, set); |
|---|
| 53 | 53 | |
|---|
| 54 | 54 | // filtered lookup: locate all elements with attribute name "attrib1" |
|---|
| … | … | |
| 56 | 56 | for (uint i = count; --i;) |
|---|
| 57 | 57 | set = doc.query.descendant.filter((doc.Node n) {return n.hasAttribute("attrib1");}); |
|---|
| 58 | | result ("attr-predicate lookups/s", count/w.stop, set); |
|---|
| | 58 | result ("attr-filter lookups/s", count/w.stop, set); |
|---|
| 59 | 59 | |
|---|
| 60 | 60 | // filtered lookup: locate all elements with more than 1 child |
|---|
| … | … | |
| 62 | 62 | for (uint i = count; --i;) |
|---|
| 63 | 63 | set = doc.query.descendant.filter((doc.Node n) {return n.query[].count > 1;}); |
|---|
| 64 | | result ("text-predicate lookups/s", count/w.stop, set); |
|---|
| | 64 | result ("recursive-filter lookups/s", count/w.stop, set); |
|---|
| 65 | 65 | |
|---|
| 66 | 66 | } |
|---|
| r3282 |
r3288 |
|
| 144 | 144 | //namespaceURIs[xmlnsURI] = 2; |
|---|
| 145 | 145 | |
|---|
| | 146 | xpath = new XmlPath!(T); |
|---|
| | 147 | |
|---|
| 146 | 148 | chunks = nodes; |
|---|
| 147 | 149 | newlist; |
|---|
| 148 | 150 | root = allocate; |
|---|
| 149 | 151 | root.type = XmlNodeType.Document; |
|---|
| 150 | | |
|---|
| 151 | | xpath = new XmlPath!(T); |
|---|
| 152 | 152 | } |
|---|
| 153 | 153 | |
|---|
| … | … | |
| 178 | 178 | root.firstChild_ = null; |
|---|
| 179 | 179 | freelists = 0; |
|---|
| | 180 | newlist; |
|---|
| 180 | 181 | index = 1; |
|---|
| 181 | | //freelists = 0; // needed to align the codegen! |
|---|
| | 182 | version(d) |
|---|
| | 183 | { |
|---|
| | 184 | freelists = 0; // needed to align the codegen! |
|---|
| | 185 | } |
|---|
| 182 | 186 | return this; |
|---|
| 183 | 187 | } |
|---|
| … | … | |
| 191 | 195 | final Document header (T[] encoding = "UTF-8") |
|---|
| 192 | 196 | { |
|---|
| 193 | | root.prepend (root.create(XmlNodeType.PI, `xml version="1.0" encoding="`~encoding~`"`)); |
|---|
| | 197 | root.prepend (root.create(XmlNodeType.PI, |
|---|
| | 198 | `xml version="1.0" encoding="`~encoding~`"`)); |
|---|
| 194 | 199 | return this; |
|---|
| 195 | 200 | } |
|---|
| … | … | |
| 221 | 226 | |
|---|
| 222 | 227 | case XmlTokenType.Data: |
|---|
| | 228 | version (discrete) |
|---|
| | 229 | { |
|---|
| | 230 | auto node = allocate; |
|---|
| | 231 | node.rawValue = super.rawValue; |
|---|
| | 232 | node.type = XmlNodeType.Data; |
|---|
| | 233 | cur.append (node); |
|---|
| | 234 | } |
|---|
| | 235 | else |
|---|
| | 236 | { |
|---|
| 223 | 237 | if (cur.rawValue.length is 0) |
|---|
| 224 | 238 | cur.rawValue = super.rawValue; |
|---|
| … | … | |
| 226 | 240 | // multiple data sections |
|---|
| 227 | 241 | cur.data (super.rawValue); |
|---|
| 228 | | /+ |
|---|
| 229 | | auto node = allocate; |
|---|
| 230 | | node.rawValue = super.rawValue; |
|---|
| 231 | | node.type = XmlNodeType.Data; |
|---|
| 232 | | cur.append (node); |
|---|
| 233 | | +/ |
|---|
| | 242 | } |
|---|
| 234 | 243 | break; |
|---|
| 235 | 244 | |
|---|
| … | … | |
| 700 | 709 | ***************************************************************/ |
|---|
| 701 | 710 | |
|---|
| 702 | | Node getText (T[] text) |
|---|
| | 711 | Node getData (T[] text) |
|---|
| 703 | 712 | { |
|---|
| 704 | 713 | if (type is XmlNodeType.Element) |
|---|
| … | … | |
| 718 | 727 | ***************************************************************/ |
|---|
| 719 | 728 | |
|---|
| 720 | | bool hasText (T[] text) |
|---|
| 721 | | { |
|---|
| 722 | | return getText (text) !is null; |
|---|
| | 729 | bool hasData (T[] text) |
|---|
| | 730 | { |
|---|
| | 731 | return getData (text) !is null; |
|---|
| 723 | 732 | } |
|---|
| 724 | 733 | |
|---|
| … | … | |
| 1198 | 1207 | |
|---|
| 1199 | 1208 | Return a set containing all child elements of the |
|---|
| 1200 | | nodes within this set |
|---|
| 1201 | | |
|---|
| 1202 | | ***************************************************************/ |
|---|
| 1203 | | |
|---|
| 1204 | | NodeSet child () |
|---|
| 1205 | | { |
|---|
| 1206 | | return child ((Node node) |
|---|
| 1207 | | {return node.type is XmlNodeType.Element;}); |
|---|
| 1208 | | } |
|---|
| 1209 | | |
|---|
| 1210 | | /*************************************************************** |
|---|
| 1211 | | |
|---|
| 1212 | | Return a set containing all child elements of the |
|---|
| 1213 | | nodes within this set, which match the given name |
|---|
| 1214 | | |
|---|
| 1215 | | ***************************************************************/ |
|---|
| 1216 | | |
|---|
| 1217 | | NodeSet child (T[] name) |
|---|
| 1218 | | { |
|---|
| 1219 | | return child ((Node node) |
|---|
| 1220 | | {return node.type is XmlNodeType.Element && |
|---|
| 1221 | | node.name == name;}); |
|---|
| | 1209 | nodes within this set, which match the optional name |
|---|
| | 1210 | |
|---|
| | 1211 | ***************************************************************/ |
|---|
| | 1212 | |
|---|
| | 1213 | NodeSet child (T[] name = null) |
|---|
| | 1214 | { |
|---|
| | 1215 | if (name.ptr) |
|---|
| | 1216 | return child ((Node node){return node.name == name;}); |
|---|
| | 1217 | return child (&always); |
|---|
| 1222 | 1218 | } |
|---|
| 1223 | 1219 | |
|---|
| … | … | |
| 1231 | 1227 | NodeSet parent (T[] name = null) |
|---|
| 1232 | 1228 | { |
|---|
| 1233 | | return parent ((Node node) |
|---|
| 1234 | | {return name.ptr is null || |
|---|
| 1235 | | node.name == name;}); |
|---|
| | 1229 | if (name.ptr) |
|---|
| | 1230 | return parent ((Node node){return node.name == name;}); |
|---|
| | 1231 | return parent (&always); |
|---|
| 1236 | 1232 | } |
|---|
| 1237 | 1233 | |
|---|
| … | … | |
| 1239 | 1235 | |
|---|
| 1240 | 1236 | Return a set containing all text nodes of the |
|---|
| 1241 | | nodes within this set |
|---|
| 1242 | | |
|---|
| 1243 | | ***************************************************************/ |
|---|
| 1244 | | |
|---|
| 1245 | | NodeSet text () |
|---|
| 1246 | | { |
|---|
| 1247 | | return child ((Node node) |
|---|
| 1248 | | {return node.type is XmlNodeType.Data;}); |
|---|
| 1249 | | } |
|---|
| 1250 | | |
|---|
| 1251 | | /*************************************************************** |
|---|
| 1252 | | |
|---|
| 1253 | | Return a set containing all text nodes of the |
|---|
| 1254 | | nodes within this set, which have a matching value |
|---|
| 1255 | | |
|---|
| 1256 | | ***************************************************************/ |
|---|
| 1257 | | |
|---|
| 1258 | | NodeSet text (T[] value) |
|---|
| 1259 | | { |
|---|
| 1260 | | return child ((Node node) |
|---|
| 1261 | | {return node.type is XmlNodeType.Data && |
|---|
| 1262 | | node.value == value;}); |
|---|
| | 1237 | nodes within this set, which match the optional |
|---|
| | 1238 | value |
|---|
| | 1239 | |
|---|
| | 1240 | ***************************************************************/ |
|---|
| | 1241 | |
|---|
| | 1242 | NodeSet text (T[] value = null) |
|---|
| | 1243 | { |
|---|
| | 1244 | if (value.ptr) |
|---|
| | 1245 | return child ((Node node){return node.value == value;}, |
|---|
| | 1246 | XmlNodeType.Data); |
|---|
| | 1247 | return child (&always, XmlNodeType.Data); |
|---|
| 1263 | 1248 | } |
|---|
| 1264 | 1249 | |
|---|
| … | … | |
| 1266 | 1251 | |
|---|
| 1267 | 1252 | Return a set containing all attributes of the |
|---|
| 1268 | | nodes within this set, which have a matching value |
|---|
| 1269 | | |
|---|
| 1270 | | ***************************************************************/ |
|---|
| 1271 | | |
|---|
| 1272 | | NodeSet attribute () |
|---|
| 1273 | | { |
|---|
| 1274 | | return attributes ((Node node) |
|---|
| 1275 | | {return node.type is XmlNodeType.Attribute;}); |
|---|
| 1276 | | } |
|---|
| 1277 | | |
|---|
| 1278 | | /*************************************************************** |
|---|
| 1279 | | |
|---|
| 1280 | | Return a set containing all attributes of the |
|---|
| 1281 | | nodes within this set, which have a matching name |
|---|
| 1282 | | |
|---|
| 1283 | | ***************************************************************/ |
|---|
| 1284 | | |
|---|
| 1285 | | NodeSet attribute (T[] name) |
|---|
| 1286 | | { |
|---|
| 1287 | | return attributes ((Node node) |
|---|
| 1288 | | {return node.type is XmlNodeType.Attribute && |
|---|
| 1289 | | node.name == name;}); |
|---|
| 1290 | | } |
|---|
| 1291 | | |
|---|
| 1292 | | /*************************************************************** |
|---|
| 1293 | | |
|---|
| 1294 | | Return a set containing all descendant elements of |
|---|
| 1295 | | the nodes within this set |
|---|
| 1296 | | |
|---|
| 1297 | | ***************************************************************/ |
|---|
| 1298 | | |
|---|
| 1299 | | NodeSet descendant () |
|---|
| 1300 | | { |
|---|
| 1301 | | return descendant ((Node node) |
|---|
| 1302 | | {return node.type is XmlNodeType.Element;}); |
|---|
| | 1253 | nodes within this set, which match the optional |
|---|
| | 1254 | name |
|---|
| | 1255 | |
|---|
| | 1256 | ***************************************************************/ |
|---|
| | 1257 | |
|---|
| | 1258 | NodeSet attribute (T[] name = null) |
|---|
| | 1259 | { |
|---|
| | 1260 | if (name.ptr) |
|---|
| | 1261 | return attribute ((Node node){return node.name == name;}); |
|---|
| | 1262 | return attribute (&always); |
|---|
| 1303 | 1263 | } |
|---|
| 1304 | 1264 | |
|---|
| … | … | |
| 1310 | 1270 | ***************************************************************/ |
|---|
| 1311 | 1271 | |
|---|
| 1312 | | NodeSet descendant (T[] name) |
|---|
| 1313 | | { |
|---|
| 1314 | | return descendant ((Node node) |
|---|
| 1315 | | {return node.type is XmlNodeType.Element && |
|---|
| 1316 | | node.name == name;}); |
|---|
| | 1272 | NodeSet descendant (T[] name = null) |
|---|
| | 1273 | { |
|---|
| | 1274 | if (name.ptr) |
|---|
| | 1275 | return descendant ((Node node){return node.name == name;}); |
|---|
| | 1276 | return descendant (&always); |
|---|
| 1317 | 1277 | } |
|---|
| 1318 | 1278 | |
|---|
| … | … | |
| 1327 | 1287 | NodeSet ancestor (T[] name = null) |
|---|
| 1328 | 1288 | { |
|---|
| 1329 | | return ancestor ((Node node) |
|---|
| 1330 | | {return name.ptr is null || |
|---|
| 1331 | | node.name == name;}); |
|---|
| | 1289 | if (name.ptr) |
|---|
| | 1290 | return ancestor ((Node node){return node.name == name;}); |
|---|
| | 1291 | return ancestor (&always); |
|---|
| 1332 | 1292 | } |
|---|
| 1333 | 1293 | |
|---|
| … | … | |
| 1342 | 1302 | NodeSet prev (T[] name = null) |
|---|
| 1343 | 1303 | { |
|---|
| 1344 | | return prev ((Node node) |
|---|
| 1345 | | {return node.type is XmlNodeType.Element && |
|---|
| 1346 | | (name.ptr is null || |
|---|
| 1347 | | node.name == name);}); |
|---|
| | 1304 | if (name.ptr) |
|---|
| | 1305 | return prev ((Node node){return node.name == name;}); |
|---|
| | 1306 | return prev (&always); |
|---|
| 1348 | 1307 | } |
|---|
| 1349 | 1308 | |
|---|
| … | … | |
| 1358 | 1317 | NodeSet next (T[] name = null) |
|---|
| 1359 | 1318 | { |
|---|
| 1360 | | return next ((Node node) |
|---|
| 1361 | | {return node.type is XmlNodeType.Element && |
|---|
| 1362 | | (name.ptr is null || |
|---|
| 1363 | | node.name == name);}); |
|---|
| | 1319 | if (name.ptr) |
|---|
| | 1320 | return next ((Node node){return node.name == name;}); |
|---|
| | 1321 | return next (&always); |
|---|
| 1364 | 1322 | } |
|---|
| 1365 | 1323 | |
|---|
| … | … | |
| 1388 | 1346 | ***************************************************************/ |
|---|
| 1389 | 1347 | |
|---|
| 1390 | | NodeSet child (bool delegate(Node) filter) |
|---|
| | 1348 | NodeSet child (bool delegate(Node) filter, |
|---|
| | 1349 | XmlNodeType type = XmlNodeType.Element) |
|---|
| 1391 | 1350 | { |
|---|
| 1392 | 1351 | NodeSet set = {host}; |
|---|
| … | … | |
| 1394 | 1353 | foreach (parent; members) |
|---|
| 1395 | 1354 | foreach (child; parent.children) |
|---|
| 1396 | | test (filter, child); |
|---|
| | 1355 | if (child.type is type) |
|---|
| | 1356 | test (filter, child); |
|---|
| 1397 | 1357 | return set.assign (mark); |
|---|
| 1398 | 1358 | } |
|---|
| … | … | |
| 1427 | 1387 | ***************************************************************/ |
|---|
| 1428 | 1388 | |
|---|
| 1429 | | NodeSet attributes (bool delegate(Node) filter) |
|---|
| | 1389 | NodeSet attribute (bool delegate(Node) filter) |
|---|
| 1430 | 1390 | { |
|---|
| 1431 | 1391 | NodeSet set = {host}; |
|---|
| … | … | |
| 1445 | 1405 | ***************************************************************/ |
|---|
| 1446 | 1406 | |
|---|
| 1447 | | NodeSet descendant (bool delegate(Node) filter) |
|---|
| | 1407 | NodeSet descendant (bool delegate(Node) filter, |
|---|
| | 1408 | XmlNodeType type = XmlNodeType.Element) |
|---|
| 1448 | 1409 | { |
|---|
| 1449 | 1410 | void traverse (Node parent) |
|---|
| … | … | |
| 1451 | 1412 | foreach (child; parent.children) |
|---|
| 1452 | 1413 | { |
|---|
| 1453 | | test (filter, child); |
|---|
| 1454 | | traverse (child); |
|---|
| | 1414 | if (child.type is type) |
|---|
| | 1415 | test (filter, child); |
|---|
| | 1416 | if (child.firstChild_) |
|---|
| | 1417 | traverse (child); |
|---|
| 1455 | 1418 | } |
|---|
| 1456 | 1419 | } |
|---|
| … | … | |
| 1502 | 1465 | ***************************************************************/ |
|---|
| 1503 | 1466 | |
|---|
| 1504 | | NodeSet next (bool delegate(Node) filter) |
|---|
| | 1467 | NodeSet next (bool delegate(Node) filter, |
|---|
| | 1468 | XmlNodeType type = XmlNodeType.Element) |
|---|
| 1505 | 1469 | { |
|---|
| 1506 | 1470 | NodeSet set = {host}; |
|---|
| … | … | |
| 1511 | 1475 | while (p) |
|---|
| 1512 | 1476 | { |
|---|
| 1513 | | test (filter, p); |
|---|
| | 1477 | if (p.type is type) |
|---|
| | 1478 | test (filter, p); |
|---|
| 1514 | 1479 | p = p.nextSibling_; |
|---|
| 1515 | 1480 | } |
|---|
| … | … | |
| 1526 | 1491 | ***************************************************************/ |
|---|
| 1527 | 1492 | |
|---|
| 1528 | | NodeSet prev (bool delegate(Node) filter) |
|---|
| | 1493 | NodeSet prev (bool delegate(Node) filter, |
|---|
| | 1494 | XmlNodeType type = XmlNodeType.Element) |
|---|
| 1529 | 1495 | { |
|---|
| 1530 | 1496 | NodeSet set = {host}; |
|---|
| … | … | |
| 1535 | 1501 | while (p) |
|---|
| 1536 | 1502 | { |
|---|
| 1537 | | test (filter, p); |
|---|
| | 1503 | if (p.type is type) |
|---|
| | 1504 | test (filter, p); |
|---|
| 1538 | 1505 | p = p.prevSibling_; |
|---|
| 1539 | 1506 | } |
|---|
| … | … | |
| 1555 | 1522 | break; |
|---|
| 1556 | 1523 | return ret; |
|---|
| | 1524 | } |
|---|
| | 1525 | |
|---|
| | 1526 | /*************************************************************** |
|---|
| | 1527 | |
|---|
| | 1528 | Common predicate |
|---|
| | 1529 | |
|---|
| | 1530 | ***************************************************************/ |
|---|
| | 1531 | |
|---|
| | 1532 | private bool always (Node node) |
|---|
| | 1533 | { |
|---|
| | 1534 | return true; |
|---|
| 1557 | 1535 | } |
|---|
| 1558 | 1536 | |
|---|
| r3245 |
r3288 |
|
| 57 | 57 | emit (" ", attr.name, "=\"", attr.rawValue, "\""); |
|---|
| 58 | 58 | |
|---|
| 59 | | if (node.hasChildren)// || node.rawValue.length) |
|---|
| | 59 | if (node.hasChildren || node.rawValue.length) |
|---|
| 60 | 60 | { |
|---|
| 61 | | /+ |
|---|
| 62 | 61 | if (node.rawValue.length) |
|---|
| 63 | 62 | emit (">", node.rawValue); |
|---|
| 64 | 63 | else |
|---|
| 65 | | +/ |
|---|
| 66 | 64 | if (node.firstChild_.type is XmlNodeType.Data) |
|---|
| 67 | 65 | emit (">"); |
|---|
Download in other formats:
|
 |
 |
|
 |
Copyright © 2006-2008 Tango. All Rights Reserved. | Page Width:
Static or
Dynamic