Changeset 611
- Timestamp:
- 02/22/08 09:48:09 (9 months ago)
- Files:
-
- candidate/phobos/std/xml.d (modified) (16 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
candidate/phobos/std/xml.d
r574 r611 1 2 // Written in the D programming language. 3 1 4 /** 2 5 Classes and functions for creating and parsing XML … … 12 15 13 16 Date: 2006.02.12 17 18 License: Public Domain 14 19 15 20 Examples: … … 79 84 } 80 85 81 // Now let's seepretty-print it to see what it looks like86 // Now let's pretty-print it to see what it looks like 82 87 writefln(join(doc.pretty(3),"\n")); 83 88 } … … 88 93 import std.string; 89 94 import std.utf; 90 import std.stdio; //TEMP91 95 92 96 /** … … 596 600 if (items[i] != element.items[i]) return items[i].opCmp(element.items[i]); 597 601 } 602 assert(false); 598 603 } 599 604 … … 643 648 * indent = (optional) number of spaces by which to indent this element. Defaults to 2. 644 649 */ 645 string[] pretty(uint indent=2)650 override string[] pretty(uint indent=2) 646 651 { 647 652 … … 1351 1356 class DocumentParser : ElementParser 1352 1357 { 1358 string xmlText; 1359 1353 1360 /** 1354 1361 * Constructs a DocumentParser … … 1358 1365 * 1359 1366 */ 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 1363 1372 parse(); // Parse through the root tag (but not beyond) 1364 1373 } … … 1385 1394 Tag tag_; 1386 1395 string elementStart; 1387 string s;1396 string* s; 1388 1397 1389 1398 Handler commentHandler; … … 1395 1404 this(ElementParser parent) 1396 1405 { 1397 this(parent.s); 1406 s = parent.s; 1407 this(); 1398 1408 tag_ = parent.tag_; 1399 1409 } … … 1466 1476 ElementHandler[string] onEndTag; 1467 1477 1468 protected this( string s_)1478 protected this() 1469 1479 { 1470 1480 commentHandler = &defaultHandler; … … 1476 1486 onEndTag[null] = &defaultElementHandler; 1477 1487 1478 elementStart = s = s_;1488 elementStart = *s; 1479 1489 } 1480 1490 … … 1596 1606 while(s.length != 0) 1597 1607 { 1598 if (startsWith( s,"<!--"))1608 if (startsWith(*s,"<!--")) 1599 1609 { 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); 1603 1613 } 1604 else if (startsWith( s,"<![CDATA["))1614 else if (startsWith(*s,"<![CDATA[")) 1605 1615 { 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); 1609 1619 } 1610 else if (startsWith( s,"<!"))1620 else if (startsWith(*s,"<!")) 1611 1621 { 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); 1615 1625 } 1616 else if (startsWith( s,"<?"))1626 else if (startsWith(*s,"<?")) 1617 1627 { 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); 1621 1631 } 1622 else if (startsWith( s,"<"))1632 else if (startsWith(*s,"<")) 1623 1633 { 1624 tag_ = new Tag( s,true);1634 tag_ = new Tag(*s,true); 1625 1635 if (root is null) return; // Return to constructor of derived class 1626 1636 if (tag_.isStart || tag_.isEmpty) … … 1657 1667 else 1658 1668 { 1659 textHandler( chop(s,find(s,"<")));1669 textHandler(decode(chop(*s,find(*s,"<")))); 1660 1670 } 1661 1671 } … … 2224 2234 unittest 2225 2235 { 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); 2261 2272 } 2262 2273 catch(CheckException e) 2263 2274 { 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); 2273 2277 } 2274 2278 } … … 2398 2402 string startOf(string s) 2399 2403 { 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
