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

Changeset 3226

Show
Ignore:
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
  • trunk/tango/text/xml/Document.d

    r3221 r3226  
    1111module tango.text.xml.Document; 
    1212 
    13 private import tango.text.xml.PullParser; 
     13package import tango.text.xml.PullParser; 
    1414 
    1515/******************************************************************************* 
     
    4646        doc.parse (content); 
    4747 
    48         Stdout(doc.print).newline; 
    49         --- 
     48        auto print = new XmlPrinter!(char); 
     49        Stdout(print(doc)).newline; 
     50         --- 
    5051 
    5152        API example: 
     
    6364                .element   (null, "child", "value"); 
    6465 
    65         Stdout(doc.print).newline; 
     66        auto print = new XmlPrinter!(char); 
     67        Stdout(print(doc)).newline; 
    6668        --- 
    6769 
     
    7476        public  Node            root; 
    7577        private NodeImpl[]      list; 
    76         private int             index; 
     78        private NodeImpl[][]    lists; 
     79        private int             index, 
     80                                chunks, 
     81                                freelists; 
    7782        private uint[T[]]       namespaceURIs; 
    7883         
     
    8893        ***********************************************************************/ 
    8994 
    90         this (uint nodes = 1000) 
     95        this (uint nodes = 5000) 
    9196        { 
    92                 assert (nodes); 
    93  
     97                assert (nodes > 50); 
    9498                super (null); 
    9599                namespaceURIs[xmlURI] = 1; 
    96100                namespaceURIs[xmlnsURI] = 2; 
    97                 list = new NodeImpl [nodes]; 
    98  
     101 
     102                chunks = nodes; 
     103                newlist; 
    99104                root = allocate; 
    100105                root.type = XmlNodeType.Document; 
     
    110115        final Document collect () 
    111116        { 
     117                root.lastChild_ =  
     118                root.firstChild_ = null; 
     119                freelists = 0; 
    112120                index = 1; 
     121                freelists = 0;          // needed to align the codegen! 
    113122                return this; 
    114123        } 
     
    120129        ***********************************************************************/ 
    121130         
    122         final Document header (
     131        final Document header (T[] encoding = "UTF-8"
    123132        { 
    124                 root.prepend (root.create(XmlNodeType.PI, `xml version="1.0"`)); 
     133                root.prepend (root.create(XmlNodeType.PI, `xml version="1.0" encoding="`~encoding~`"`)); 
    125134                return this; 
    126135        } 
     
    277286        { 
    278287                if (index >= list.length) 
    279                     list.length = list.length + list.length / 2
     288                    newlist
    280289 
    281290                auto p = &list[index++]; 
     
    290299                p.rawValue = null; 
    291300                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++]; 
    292318        } 
    293319 
     
    862888                } 
    863889        } 
    864  
    865         /******************************************************************************* 
     890
     891 
     892 
     893/******************************************************************************* 
     894 
     895*******************************************************************************/ 
     896 
     897interface 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        /*********************************************************************** 
    866904         
    867905                Generate a text representation of the document tree 
    868906 
    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        /*********************************************************************** 
    880912         
    881913                Generate a representation of the given node-subtree  
    882914 
    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); 
    951918}