Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Changeset 3288

Show
Ignore:
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
  • trunk/example/text/xmlpath.d

    r3280 r3288  
    4949        w.start; 
    5050        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); 
    5353 
    5454        // filtered lookup: locate all elements with attribute name "attrib1" 
     
    5656        for (uint i = count; --i;) 
    5757             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); 
    5959 
    6060        // filtered lookup: locate all elements with more than 1 child 
     
    6262        for (uint i = count; --i;) 
    6363             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); 
    6565 
    6666} 
  • trunk/tango/text/xml/Document.d

    r3282 r3288  
    144144                //namespaceURIs[xmlnsURI] = 2; 
    145145 
     146                xpath = new XmlPath!(T); 
     147 
    146148                chunks = nodes; 
    147149                newlist; 
    148150                root = allocate; 
    149151                root.type = XmlNodeType.Document; 
    150  
    151                 xpath = new XmlPath!(T); 
    152152        } 
    153153 
     
    178178                root.firstChild_ = null; 
    179179                freelists = 0; 
     180                newlist; 
    180181                index = 1; 
    181                 //freelists = 0;          // needed to align the codegen! 
     182version(d) 
     183
     184                freelists = 0;          // needed to align the codegen! 
     185
    182186                return this; 
    183187        } 
     
    191195        final Document header (T[] encoding = "UTF-8") 
    192196        { 
    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~`"`)); 
    194199                return this; 
    195200        } 
     
    221226         
    222227                             case XmlTokenType.Data: 
     228version (discrete) 
     229{ 
     230                                  auto node = allocate; 
     231                                  node.rawValue = super.rawValue; 
     232                                  node.type = XmlNodeType.Data; 
     233                                  cur.append (node); 
     234} 
     235else 
     236{ 
    223237                                  if (cur.rawValue.length is 0) 
    224238                                      cur.rawValue = super.rawValue; 
     
    226240                                     // multiple data sections 
    227241                                     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
    234243                                  break; 
    235244         
     
    700709                ***************************************************************/ 
    701710         
    702                 Node getText (T[] text) 
     711                Node getData (T[] text) 
    703712                { 
    704713                        if (type is XmlNodeType.Element) 
     
    718727                ***************************************************************/ 
    719728         
    720                 bool hasText (T[] text) 
    721                 { 
    722                         return getText (text) !is null; 
     729                bool hasData (T[] text) 
     730                { 
     731                        return getData (text) !is null; 
    723732                } 
    724733 
     
    11981207         
    11991208                        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); 
    12221218                } 
    12231219 
     
    12311227                NodeSet parent (T[] name = null) 
    12321228                { 
    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); 
    12361232                } 
    12371233 
     
    12391235         
    12401236                        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); 
    12631248                } 
    12641249 
     
    12661251         
    12671252                        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); 
    13031263                } 
    13041264 
     
    13101270                ***************************************************************/ 
    13111271         
    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); 
    13171277                } 
    13181278 
     
    13271287                NodeSet ancestor (T[] name = null) 
    13281288                { 
    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); 
    13321292                } 
    13331293 
     
    13421302                NodeSet prev (T[] name = null) 
    13431303                { 
    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); 
    13481307                } 
    13491308 
     
    13581317                NodeSet next (T[] name = null) 
    13591318                { 
    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); 
    13641322                } 
    13651323 
     
    13881346                ***************************************************************/ 
    13891347         
    1390                 NodeSet child (bool delegate(Node) filter) 
     1348                NodeSet child (bool delegate(Node) filter,  
     1349                               XmlNodeType type = XmlNodeType.Element) 
    13911350                { 
    13921351                        NodeSet set = {host}; 
     
    13941353                        foreach (parent; members) 
    13951354                                 foreach (child; parent.children) 
    1396                                           test (filter, child); 
     1355                                          if (child.type is type) 
     1356                                              test (filter, child); 
    13971357                        return set.assign (mark); 
    13981358                } 
     
    14271387                ***************************************************************/ 
    14281388         
    1429                 NodeSet attributes (bool delegate(Node) filter) 
     1389                NodeSet attribute (bool delegate(Node) filter) 
    14301390                { 
    14311391                        NodeSet set = {host}; 
     
    14451405                ***************************************************************/ 
    14461406         
    1447                 NodeSet descendant (bool delegate(Node) filter) 
     1407                NodeSet descendant (bool delegate(Node) filter,  
     1408                                    XmlNodeType type = XmlNodeType.Element) 
    14481409                { 
    14491410                        void traverse (Node parent) 
     
    14511412                                 foreach (child; parent.children) 
    14521413                                         { 
    1453                                          test (filter, child); 
    1454                                          traverse (child); 
     1414                                         if (child.type is type) 
     1415                                             test (filter, child); 
     1416                                         if (child.firstChild_) 
     1417                                             traverse (child); 
    14551418                                         }                                                 
    14561419                        } 
     
    15021465                ***************************************************************/ 
    15031466         
    1504                 NodeSet next (bool delegate(Node) filter) 
     1467                NodeSet next (bool delegate(Node) filter,  
     1468                              XmlNodeType type = XmlNodeType.Element) 
    15051469                { 
    15061470                        NodeSet set = {host}; 
     
    15111475                                while (p) 
    15121476                                      { 
    1513                                       test (filter, p); 
     1477                                      if (p.type is type) 
     1478                                          test (filter, p); 
    15141479                                      p = p.nextSibling_; 
    15151480                                      } 
     
    15261491                ***************************************************************/ 
    15271492         
    1528                 NodeSet prev (bool delegate(Node) filter) 
     1493                NodeSet prev (bool delegate(Node) filter,  
     1494                              XmlNodeType type = XmlNodeType.Element) 
    15291495                { 
    15301496                        NodeSet set = {host}; 
     
    15351501                                while (p) 
    15361502                                      { 
    1537                                       test (filter, p); 
     1503                                      if (p.type is type) 
     1504                                          test (filter, p); 
    15381505                                      p = p.prevSibling_; 
    15391506                                      } 
     
    15551522                                      break; 
    15561523                        return ret; 
     1524                } 
     1525 
     1526                /*************************************************************** 
     1527         
     1528                        Common predicate 
     1529                                 
     1530                ***************************************************************/ 
     1531         
     1532                private bool always (Node node) 
     1533                { 
     1534                        return true; 
    15571535                } 
    15581536 
  • trunk/tango/text/xml/XmlPrinter.d

    r3245 r3288  
    5757                                             emit (" ", attr.name, "=\"", attr.rawValue, "\""); 
    5858 
    59                                     if (node.hasChildren)// || node.rawValue.length) 
     59                                    if (node.hasChildren || node.rawValue.length) 
    6060                                       { 
    61 /+ 
    6261                                       if (node.rawValue.length) 
    6362                                           emit (">", node.rawValue); 
    6463                                       else 
    65 +/ 
    6664                                       if (node.firstChild_.type is XmlNodeType.Data) 
    6765                                           emit (">");