Changeset 611

Show
Ignore:
Timestamp:
02/22/08 09:48:09 (9 months ago)
Author:
Janice Caron
Message:

Bug fixes.
(1) Text handler did not decode text before passing to user. Now it does.
(2) When using nested handlers, the parse position was not correctly updated. Now it is.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • candidate/phobos/std/xml.d

    r574 r611  
     1 
     2// Written in the D programming language. 
     3 
    14/** 
    25Classes and functions for creating and parsing XML 
     
    1215 
    1316Date: 2006.02.12 
     17 
     18License: Public Domain 
    1419 
    1520Examples: 
     
    7984    } 
    8085 
    81     // Now let's see pretty-print it to see what it looks like 
     86    // Now let's pretty-print it to see what it looks like 
    8287    writefln(join(doc.pretty(3),"\n")); 
    8388} 
     
    8893import std.string; 
    8994import std.utf; 
    90 import std.stdio; //TEMP 
    9195 
    9296/** 
     
    596600            if (items[i] != element.items[i]) return items[i].opCmp(element.items[i]); 
    597601        } 
     602        assert(false); 
    598603    } 
    599604 
     
    643648         *      indent = (optional) number of spaces by which to indent this element. Defaults to 2. 
    644649         */ 
    645         string[] pretty(uint indent=2) 
     650        override string[] pretty(uint indent=2) 
    646651        { 
    647652 
     
    13511356class DocumentParser : ElementParser 
    13521357{ 
     1358    string xmlText; 
     1359 
    13531360    /** 
    13541361     * Constructs a DocumentParser 
     
    13581365     * 
    13591366     */ 
    1360     this(string xmltext) 
    1361     { 
    1362         super(xmltext); // Initialize everything 
     1367    this(string xmlText_) 
     1368    { 
     1369        xmlText = xmlText_; 
     1370        s = &xmlText; 
     1371        super();    // Initialize everything 
    13631372        parse();        // Parse through the root tag (but not beyond) 
    13641373    } 
     
    13851394        Tag tag_; 
    13861395        string elementStart; 
    1387         string s; 
     1396        string* s; 
    13881397 
    13891398        Handler commentHandler; 
     
    13951404        this(ElementParser parent) 
    13961405        { 
    1397             this(parent.s); 
     1406            s = parent.s; 
     1407            this(); 
    13981408            tag_ = parent.tag_; 
    13991409        } 
     
    14661476    ElementHandler[string] onEndTag; 
    14671477 
    1468     protected this(string s_
     1478    protected this(
    14691479    { 
    14701480        commentHandler      = &defaultHandler; 
     
    14761486        onEndTag[null]      = &defaultElementHandler; 
    14771487 
    1478         elementStart = s = s_
     1488        elementStart = *s
    14791489    } 
    14801490 
     
    15961606        while(s.length != 0) 
    15971607        { 
    1598             if (startsWith(s,"<!--")) 
     1608            if (startsWith(*s,"<!--")) 
    15991609            { 
    1600                 chop(s,4); 
    1601                 commentHandler(chop(s,find(s,"-->"))); 
    1602                 chop(s,3); 
     1610                chop(*s,4); 
     1611                commentHandler(chop(*s,find(*s,"-->"))); 
     1612                chop(*s,3); 
    16031613            } 
    1604             else if (startsWith(s,"<![CDATA[")) 
     1614            else if (startsWith(*s,"<![CDATA[")) 
    16051615            { 
    1606                 chop(s,9); 
    1607                 cdataHandler(chop(s,find(s,"]]>"))); 
    1608                 chop(s,3); 
     1616                chop(*s,9); 
     1617                cdataHandler(chop(*s,find(*s,"]]>"))); 
     1618                chop(*s,3); 
    16091619            } 
    1610             else if (startsWith(s,"<!")) 
     1620            else if (startsWith(*s,"<!")) 
    16111621            { 
    1612                 chop(s,2); 
    1613                 xiHandler(chop(s,find(s,">"))); 
    1614                 chop(s,1); 
     1622                chop(*s,2); 
     1623                xiHandler(chop(*s,find(*s,">"))); 
     1624                chop(*s,1); 
    16151625            } 
    1616             else if (startsWith(s,"<?")) 
     1626            else if (startsWith(*s,"<?")) 
    16171627            { 
    1618                 chop(s,2); 
    1619                 piHandler(chop(s,find(s,"?>"))); 
    1620                 chop(s,2); 
     1628                chop(*s,2); 
     1629                piHandler(chop(*s,find(*s,"?>"))); 
     1630                chop(*s,2); 
    16211631            } 
    1622             else if (startsWith(s,"<")) 
     1632            else if (startsWith(*s,"<")) 
    16231633            { 
    1624                 tag_ = new Tag(s,true); 
     1634                tag_ = new Tag(*s,true); 
    16251635                if (root is null) return; // Return to constructor of derived class 
    16261636                if (tag_.isStart || tag_.isEmpty) 
     
    16571667            else 
    16581668            { 
    1659                 textHandler(chop(s,find(s,"<"))); 
     1669                textHandler(decode(chop(*s,find(*s,"<")))); 
    16601670            } 
    16611671        } 
     
    22242234unittest 
    22252235{ 
    2226     try 
    2227     { 
    2228         check(q"[<?xml version="1.0"?> 
    2229 <catalog> 
    2230    <book id="bk101"> 
    2231       <author>Gambardella, Matthew</author> 
    2232       <title>XML Developer's Guide</title> 
    2233       <genre>Computer</genre> 
    2234       <price>44.95</price> 
    2235       <publish_date>2000-10-01</publish_date> 
    2236       <description>An in-depth look at creating applications 
    2237       with XML.</description> 
    2238    </book> 
    2239    <book id="bk102"> 
    2240       <author>Ralls, Kim</author> 
    2241       <title>Midnight Rain</title> 
    2242       <genre>Fantasy</genres> 
    2243       <price>5.95</price> 
    2244       <publish_date>2000-12-16</publish_date> 
    2245       <description>A former architect battles corporate zombies, 
    2246       an evil sorceress, and her own childhood to become queen 
    2247       of the world.</description> 
    2248    </book> 
    2249    <book id="bk103"> 
    2250       <author>Corets, Eva</author> 
    2251       <title>Maeve Ascendant</title> 
    2252       <genre>Fantasy</genre> 
    2253       <price>5.95</price> 
    2254       <publish_date>2000-11-17</publish_date> 
    2255       <description>After the collapse of a nanotechnology 
    2256       society in England, the young survivors lay the 
    2257       foundation for a new society.</description> 
    2258    </book> 
    2259 </catalog> 
    2260 ]"); 
     2236    try 
     2237    { 
     2238        check(q"[<?xml version="1.0"?> 
     2239        <catalog> 
     2240           <book id="bk101"> 
     2241              <author>Gambardella, Matthew</author> 
     2242              <title>XML Developer's Guide</title> 
     2243              <genre>Computer</genre> 
     2244              <price>44.95</price> 
     2245              <publish_date>2000-10-01</publish_date> 
     2246              <description>An in-depth look at creating applications 
     2247              with XML.</description> 
     2248           </book> 
     2249           <book id="bk102"> 
     2250              <author>Ralls, Kim</author> 
     2251              <title>Midnight Rain</title> 
     2252              <genre>Fantasy</genres> 
     2253              <price>5.95</price> 
     2254              <publish_date>2000-12-16</publish_date> 
     2255              <description>A former architect battles corporate zombies, 
     2256              an evil sorceress, and her own childhood to become queen 
     2257              of the world.</description> 
     2258           </book> 
     2259           <book id="bk103"> 
     2260              <author>Corets, Eva</author> 
     2261              <title>Maeve Ascendant</title> 
     2262              <genre>Fantasy</genre> 
     2263              <price>5.95</price> 
     2264              <publish_date>2000-11-17</publish_date> 
     2265              <description>After the collapse of a nanotechnology 
     2266              society in England, the young survivors lay the 
     2267              foundation for a new society.</description> 
     2268           </book> 
     2269        </catalog> 
     2270        ]"); 
     2271    assert(false); 
    22612272    } 
    22622273    catch(CheckException e) 
    22632274    { 
    2264         assert(e.toString == 
    2265 q"[Line 15, column 21: end tag name "genres" differs from start tag name "genre" 
    2266 Line 15, column 7: Element 
    2267 Line 15, column 7: Content 
    2268 Line 12, column 4: Element 
    2269 Line 12, column 4: Content 
    2270 Line 2, column 1: Element 
    2271 Line 1, column 1: Document 
    2272 ]"); 
     2275        int n = e.toString().find("end tag name \"genres\" differs from start tag name \"genre\""); 
     2276        assert(n != -1); 
    22732277    } 
    22742278} 
     
    23982402    string startOf(string s) 
    23992403    { 
    2400         return s.length < 20 ? s : s[0..20]~"..."; 
    2401     } 
    2402 
    2403  
     2404        string r; 
     2405        foreach(char c;s) 
     2406        { 
     2407            r ~= (c < 0x20 || c > 0x7F) ? '.' : c; 
     2408            if (r.length >= 20) { r ~= "..."; break; } 
     2409        } 
     2410        return r; 
     2411    } 
     2412
     2413