 |
Changeset 3226
- Timestamp:
- 02/19/08 01:37:19
(10 months ago)
- Author:
- kris
- Message:
added XmlPrinter?, fixes the allocator issue, and fixes #921
-
Files:
-
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
| r3221 |
r3226 |
|
| 11 | 11 | module tango.text.xml.Document; |
|---|
| 12 | 12 | |
|---|
| 13 | | private import tango.text.xml.PullParser; |
|---|
| | 13 | package import tango.text.xml.PullParser; |
|---|
| 14 | 14 | |
|---|
| 15 | 15 | /******************************************************************************* |
|---|
| … | … | |
| 46 | 46 | doc.parse (content); |
|---|
| 47 | 47 | |
|---|
| 48 | | Stdout(doc.print).newline; |
|---|
| 49 | | --- |
|---|
| | 48 | auto print = new XmlPrinter!(char); |
|---|
| | 49 | Stdout(print(doc)).newline; |
|---|
| | 50 | --- |
|---|
| 50 | 51 | |
|---|
| 51 | 52 | API example: |
|---|
| … | … | |
| 63 | 64 | .element (null, "child", "value"); |
|---|
| 64 | 65 | |
|---|
| 65 | | Stdout(doc.print).newline; |
|---|
| | 66 | auto print = new XmlPrinter!(char); |
|---|
| | 67 | Stdout(print(doc)).newline; |
|---|
| 66 | 68 | --- |
|---|
| 67 | 69 | |
|---|
| … | … | |
| 74 | 76 | public Node root; |
|---|
| 75 | 77 | private NodeImpl[] list; |
|---|
| 76 | | private int index; |
|---|
| | 78 | private NodeImpl[][] lists; |
|---|
| | 79 | private int index, |
|---|
| | 80 | chunks, |
|---|
| | 81 | freelists; |
|---|
| 77 | 82 | private uint[T[]] namespaceURIs; |
|---|
| 78 | 83 | |
|---|
| … | … | |
| 88 | 93 | ***********************************************************************/ |
|---|
| 89 | 94 | |
|---|
| 90 | | this (uint nodes = 1000) |
|---|
| | 95 | this (uint nodes = 5000) |
|---|
| 91 | 96 | { |
|---|
| 92 | | assert (nodes); |
|---|
| 93 | | |
|---|
| | 97 | assert (nodes > 50); |
|---|
| 94 | 98 | super (null); |
|---|
| 95 | 99 | namespaceURIs[xmlURI] = 1; |
|---|
| 96 | 100 | namespaceURIs[xmlnsURI] = 2; |
|---|
| 97 | | list = new NodeImpl [nodes]; |
|---|
| 98 | | |
|---|
| | 101 | |
|---|
| | 102 | chunks = nodes; |
|---|
| | 103 | newlist; |
|---|
| 99 | 104 | root = allocate; |
|---|
| 100 | 105 | root.type = XmlNodeType.Document; |
|---|
| … | … | |
| 110 | 115 | final Document collect () |
|---|
| 111 | 116 | { |
|---|
| | 117 | root.lastChild_ = |
|---|
| | 118 | root.firstChild_ = null; |
|---|
| | 119 | freelists = 0; |
|---|
| 112 | 120 | index = 1; |
|---|
| | 121 | freelists = 0; // needed to align the codegen! |
|---|
| 113 | 122 | return this; |
|---|
| 114 | 123 | } |
|---|
| … | … | |
| 120 | 129 | ***********************************************************************/ |
|---|
| 121 | 130 | |
|---|
| 122 | | final Document header () |
|---|
| | 131 | final Document header (T[] encoding = "UTF-8") |
|---|
| 123 | 132 | { |
|---|
| 124 | | root.prepend (root.create(XmlNodeType.PI, `xml version="1.0"`)); |
|---|
| | 133 | root.prepend (root.create(XmlNodeType.PI, `xml version="1.0" encoding="`~encoding~`"`)); |
|---|
| 125 | 134 | return this; |
|---|
| 126 | 135 | } |
|---|
| … | … | |
| 277 | 286 | { |
|---|
| 278 | 287 | if (index >= list.length) |
|---|
| 279 | | list.length = list.length + list.length / 2; |
|---|
| | 288 | newlist; |
|---|
| 280 | 289 | |
|---|
| 281 | 290 | auto p = &list[index++]; |
|---|
| … | … | |
| 290 | 299 | p.rawValue = null; |
|---|
| 291 | 300 | return p; |
|---|
| | 301 | } |
|---|
| | 302 | |
|---|
| | 303 | /*********************************************************************** |
|---|
| | 304 | |
|---|
| | 305 | allocate a node from the freelist |
|---|
| | 306 | |
|---|
| | 307 | ***********************************************************************/ |
|---|
| | 308 | |
|---|
| | 309 | private final void newlist () |
|---|
| | 310 | { |
|---|
| | 311 | index = 0; |
|---|
| | 312 | if (freelists >= lists.length) |
|---|
| | 313 | { |
|---|
| | 314 | lists.length = lists.length + 1; |
|---|
| | 315 | lists[$-1] = new NodeImpl [chunks]; |
|---|
| | 316 | } |
|---|
| | 317 | list = lists[freelists++]; |
|---|
| 292 | 318 | } |
|---|
| 293 | 319 | |
|---|
| … | … | |
| 862 | 888 | } |
|---|
| 863 | 889 | } |
|---|
| 864 | | |
|---|
| 865 | | /******************************************************************************* |
|---|
| | 890 | } |
|---|
| | 891 | |
|---|
| | 892 | |
|---|
| | 893 | /******************************************************************************* |
|---|
| | 894 | |
|---|
| | 895 | *******************************************************************************/ |
|---|
| | 896 | |
|---|
| | 897 | interface IXmlPrinter(T) |
|---|
| | 898 | { |
|---|
| | 899 | public alias Document!(T) Doc; /// the typed document |
|---|
| | 900 | public alias Doc.Node Node; /// generic document node |
|---|
| | 901 | public alias print opCall; /// alias for print method |
|---|
| | 902 | |
|---|
| | 903 | /*********************************************************************** |
|---|
| 866 | 904 | |
|---|
| 867 | 905 | Generate a text representation of the document tree |
|---|
| 868 | 906 | |
|---|
| 869 | | *******************************************************************************/ |
|---|
| 870 | | |
|---|
| 871 | | final T[] print() |
|---|
| 872 | | { |
|---|
| 873 | | T[] content; |
|---|
| 874 | | |
|---|
| 875 | | print (this.root, (T[][] s...){foreach(t; s) content ~= t;}); |
|---|
| 876 | | return content; |
|---|
| 877 | | } |
|---|
| 878 | | |
|---|
| 879 | | /******************************************************************************* |
|---|
| | 907 | ***********************************************************************/ |
|---|
| | 908 | |
|---|
| | 909 | T[] print (Doc doc); |
|---|
| | 910 | |
|---|
| | 911 | /*********************************************************************** |
|---|
| 880 | 912 | |
|---|
| 881 | 913 | Generate a representation of the given node-subtree |
|---|
| 882 | 914 | |
|---|
| 883 | | *******************************************************************************/ |
|---|
| 884 | | |
|---|
| 885 | | final void print (Node root, void delegate(T[][]...) emit) |
|---|
| 886 | | { |
|---|
| 887 | | T[256] spaces = ' '; |
|---|
| 888 | | |
|---|
| 889 | | void printNode (Node node, uint indent) |
|---|
| 890 | | { |
|---|
| 891 | | switch (node.type) |
|---|
| 892 | | { |
|---|
| 893 | | case XmlNodeType.Document: |
|---|
| 894 | | foreach (n; node.children) |
|---|
| 895 | | printNode (n, indent + 2); |
|---|
| 896 | | break; |
|---|
| 897 | | |
|---|
| 898 | | case XmlNodeType.Element: |
|---|
| 899 | | emit ("<", node.name); |
|---|
| 900 | | foreach (attr; node.attributes) |
|---|
| 901 | | emit (" ", attr.name, "=\"", attr.rawValue, "\""); |
|---|
| 902 | | |
|---|
| 903 | | if (node.hasChildren || node.rawValue.length) |
|---|
| 904 | | { |
|---|
| 905 | | if (node.rawValue.length) |
|---|
| 906 | | emit (">", node.rawValue); |
|---|
| 907 | | else |
|---|
| 908 | | emit (">\r\n"); |
|---|
| 909 | | foreach (n; node.children) |
|---|
| 910 | | { |
|---|
| 911 | | emit (spaces[0..indent]); |
|---|
| 912 | | printNode (n, indent + 2); |
|---|
| 913 | | } |
|---|
| 914 | | emit ("</", node.name, ">\r\n"); |
|---|
| 915 | | } |
|---|
| 916 | | else |
|---|
| 917 | | emit ("/>\r\n"); |
|---|
| 918 | | break; |
|---|
| 919 | | |
|---|
| 920 | | case XmlNodeType.Data: |
|---|
| 921 | | emit (node.rawValue); |
|---|
| 922 | | break; |
|---|
| 923 | | |
|---|
| 924 | | case XmlNodeType.Attribute: |
|---|
| 925 | | emit (node.name, "=\"", node.rawValue, "\""); |
|---|
| 926 | | break; |
|---|
| 927 | | |
|---|
| 928 | | case XmlNodeType.Comment: |
|---|
| 929 | | emit ("<!--", node.rawValue, "-->\r\n"); |
|---|
| 930 | | break; |
|---|
| 931 | | |
|---|
| 932 | | case XmlNodeType.PI: |
|---|
| 933 | | emit ("<?", node.rawValue, "?>\r\n"); |
|---|
| 934 | | break; |
|---|
| 935 | | |
|---|
| 936 | | case XmlNodeType.CData: |
|---|
| 937 | | emit ("<![CDATA[", node.rawValue, "]]>"); |
|---|
| 938 | | break; |
|---|
| 939 | | |
|---|
| 940 | | case XmlNodeType.Doctype: |
|---|
| 941 | | emit ("<!DOCTYPE ", node.rawValue, ">\r\n"); |
|---|
| 942 | | break; |
|---|
| 943 | | |
|---|
| 944 | | default: |
|---|
| 945 | | break; |
|---|
| 946 | | } |
|---|
| 947 | | } |
|---|
| 948 | | |
|---|
| 949 | | printNode (root, 0); |
|---|
| 950 | | } |
|---|
| | 915 | ***********************************************************************/ |
|---|
| | 916 | |
|---|
| | 917 | void print (Node root, void delegate(T[][]...) emit); |
|---|
| 951 | 918 | } |
|---|
Download in other formats:
|
 |
 |
|
 |
Copyright © 2006-2008 Tango. All Rights Reserved. | Page Width:
Static or
Dynamic