Changeset 47

Show
Ignore:
Timestamp:
04/18/05 23:13:57 (4 years ago)
Author:
pragma
Message:

- Added missing files from last commit
- Added draft of xmlns filter implementation

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/xml/xmlns/IXMLNamespaceConsumer.d

    r44 r47  
    2626module xml.xmlns.IXMLNamespaceConsumer; 
    2727 
    28 interface IXMLNamespaceConsumer{ 
    29     void xmlProlog(XMLNSAttributes attribs); 
     28private import xml.IXMLConsumer; 
     29private import xml.XMLAttributes; 
     30 
     31private import xml.xmlns.QName; 
     32private import xml.xmlns.XMLNSAttributes; 
     33 
     34interface IXMLNamespaceConsumer : IXMLConsumer{ 
     35    void xmlProlog(XMLAttributes attribs); 
    3036    void xmlStartDoctype(char[] name); 
    3137    void xmlEndDoctype(char[] name); 
  • trunk/xml/xmlns/IXMLNamespaceConsumerAdapter.d

    r44 r47  
    2626module xml.xmlns.IXMLNamespaceConsumerAdapter; 
    2727 
    28 interface IXMLNamespaceConsumerAdapter{ 
     28private import xml.xmlns.QName; 
     29private import xml.xmlns.XMLNSAttributes; 
     30private import xml.xmlns.IXMLNamespaceConsumer; 
     31 
     32abstract class IXMLNamespaceConsumerAdapter : IXMLNamespaceConsumer{ 
    2933    void xmlProlog(XMLNSAttributes attribs){} 
    3034    void xmlStartDoctype(char[] name){} 
  • trunk/xml/xmlns/QName.d

    r44 r47  
    2828import std.string; 
    2929 
    30 //TODO: should inherit from IComparable 
     30//TODO: make this a class instead? 
    3131struct QName{ 
    32     char[] namespace
    33     char[] localname
     32    char[] ns
     33    char[] local
    3434     
    35     int opEquals(Object other){ 
    36         return namespace == other.namespace && localname == other.localname; 
     35    /* 
     36    int opEquals(Object o){ 
     37        QName other = cast(QName)o; 
     38        return ns == other.ns && local == other.local; 
    3739    } 
    3840     
    39     int opCmp(Object other){ 
     41    int opCmp(Object o){ 
     42        QName other = cast(QName)o; 
    4043        if(ns != ns){ 
    41             return std.string.cmp(namespace,other,namespace); 
     44            return std.string.cmp(ns,other.ns); 
    4245        } 
    43         return std.string.cmp(localname,other,localname); 
     46        return std.string.cmp(local,other.local); 
    4447    } 
    4548     
    4649    int toHash(){ 
    47         return(this); //TODO: something better? 
     50        return(*this); //TODO: something better? 
     51    } 
     52    */ 
     53     
     54    char[] toString(){ 
     55        return(ns ~ ":" ~ local); 
    4856    } 
    4957} 
  • trunk/xml/xmlns/XMLNSAttributes.d

    r44 r47  
    2626module xml.xmlns.XMLNSAttributes; 
    2727 
     28private import xml.xmlns.QName; 
     29 
    2830alias char[][QName] XMLNSAttributes; 
  • trunk/xml/xmlns/XMLNamespaceParser.d

    r44 r47  
    2626module xml.xmlns.XMLNamespaceParser; 
    2727 
     28private import xml.XMLAttributes; 
     29private import xml.IXMLConsumer; 
     30private import xml.XMLException; 
     31 
     32private import xml.xmlns.QName; 
     33private import xml.xmlns.XMLNSAttributes; 
     34private import xml.xmlns.IXMLNamespaceConsumer; 
     35 
     36private import std.string; 
     37 
     38template push(T){ 
     39    void push(T[] array, T value){ 
     40        array.length = array.length + 1; 
     41        array[$-1] = value; 
     42    } 
     43} 
     44 
     45template pop(T){ 
     46    T pop(T[] array){ 
     47        T value = array[$-1]; 
     48        array.length = array.length - 1; 
     49        return value; 
     50    } 
     51} 
     52 
    2853class XMLNamespaceParser : IXMLConsumer{ 
    2954    IXMLNamespaceConsumer consumer; 
     55    QName[] tagNames; 
     56    XMLNSAttributes[] tagAttributes; 
     57    char[][][char[]] namespaces; 
    3058     
    3159    public this(IXMLNamespaceConsumer consumer){ 
    3260        this.consumer = consumer; 
    3361    } 
    34      
    35     void xmlProlog(XMLAttributes attribs){} 
    36     void xmlStartDoctype(char[] name){} 
    37     void xmlEndDoctype(char[] name){} 
    38  
    39     void xmlProcessingInstruction(char[] name,char[] data){} 
    40  
    41     void xmlComment(char[] text){} 
    42  
    43     void xmlStartElement(char[] name,XMLAttributes attribs,bit isEmpty){} 
    44     void xmlEndElement(char[] name,XMLAttributes attribs,bit isEmpty){} 
    45     void xmlText(char[] text){} 
     62         
     63    private QName parseQName(char[] name){ 
     64        QName newName; 
     65        int split = find(name,":"); 
     66         
     67        if(split>0){ 
     68            newName.ns = name[0..split+1]; 
     69            newName.local = name[split..name.length]; 
     70        } 
     71        return newName; 
     72    } 
     73     
     74    private void pushAttributes(XMLNSAttributes attribs){ 
     75        push!(XMLNSAttributes)(tagAttributes,attribs); 
     76    } 
     77     
     78    private XMLNSAttributes popAttributes(){ 
     79        return pop!(XMLNSAttributes)(tagAttributes); 
     80    } 
     81     
     82    private void pushTagname(QName tagname){ 
     83        push!(QName)(tagNames,tagname); 
     84    } 
     85     
     86    private QName popTagname(){ 
     87        return pop!(QName)(tagNames); 
     88    } 
     89     
     90    private void pushNamespace(char[] name, char[] value){ 
     91        namespaces[name].length = namespaces[name].length + 1; 
     92        namespaces[name][$-1] = value; 
     93    } 
     94     
     95    private void popNamespace(char[] name){ 
     96        namespaces[name].length = namespaces[name].length - 1; 
     97    } 
     98     
     99    private char[] getNamespaceURI(char[] name){ 
     100        if(name in namespaces){ 
     101            return namespaces[name][$-1]; 
     102        } 
     103        return ""; 
     104    } 
     105 
     106    void xmlProlog(XMLAttributes attribs){ 
     107        consumer.xmlProlog(attribs); 
     108    } 
     109     
     110    void xmlStartDoctype(char[] name){ 
     111        consumer.xmlStartDoctype(name); 
     112    } 
     113     
     114    void xmlEndDoctype(char[] name){ 
     115        consumer.xmlEndDoctype(name); 
     116    } 
     117 
     118    void xmlProcessingInstruction(char[] name,char[] data){ 
     119        consumer.xmlProcessingInstruction(name,data); 
     120    } 
     121 
     122    void xmlComment(char[] text){ 
     123        consumer.xmlComment(text); 
     124    } 
     125 
     126    void xmlStartElement(char[] name,XMLAttributes attribs,bit isEmpty){ 
     127        XMLNSAttributes nsAttribs; 
     128 
     129        foreach(char[] name, char[] value; attribs){ 
     130            // handle default namespace 
     131            if(name == "xmlns"){                 
     132                pushNamespace("",value); 
     133                consumer.xmlStartNamespace("",value); 
     134            } 
     135             
     136            // handle typical attribute 
     137            else{ 
     138                QName qname = parseQName(name); 
     139                 
     140                // handle namespace definition 
     141                if(qname.ns == "xmlns"){ 
     142                    if(qname.local == "xmlns"){ 
     143                        throw new XMLException("Cannot redefine namespace 'xmlns'."); 
     144                    } 
     145                    else if(qname.local == ""){ 
     146                        throw new XMLException("Namespace definition incomplete, need name to follow 'xmlns:'"); 
     147                    } 
     148                    else{ 
     149                        // create namespace alias 
     150                        pushNamespace(qname.local,value); 
     151                        consumer.xmlStartNamespace(qname.local,value); 
     152                    } 
     153                } 
     154                 
     155                // handle standard attribute, possibly with a namespace prefix 
     156                else{ 
     157                    qname.ns = getNamespaceURI(qname.ns); 
     158                    nsAttribs[qname] = value; 
     159                } 
     160            } 
     161 
     162        } 
     163        pushAttributes(nsAttribs); 
     164         
     165        // establish the namespace for the name 
     166        QName tagname = parseQName(name); 
     167        tagname.ns = getNamespaceURI(tagname.ns); 
     168        pushTagname(tagname); 
     169         
     170        // fire off consumer event 
     171        consumer.xmlStartElement(tagname,nsAttribs,isEmpty); 
     172    } 
     173 
     174    void xmlEndElement(char[] name,XMLAttributes attribs,bit isEmpty){   
     175        // keep things symmetrical: fire element end first 
     176        consumer.xmlEndElement(popTagname(),popAttributes(),isEmpty); 
     177     
     178        // unwind all that was done with these attributes 
     179        //TODO: get these in reverse order 
     180        foreach(char[] name, char[] value; attribs){ 
     181            // handle default namespace 
     182            if(name == "xmlns"){                 
     183                popNamespace(""); 
     184                consumer.xmlEndNamespace("",value); 
     185            } 
     186             
     187            // handle typical attribute 
     188            else{ 
     189                QName qname = parseQName(name); 
     190                 
     191                // handle namespace definition 
     192                if(qname.ns == "xmlns"){ 
     193                    // create namespace alias 
     194                    popNamespace(qname.local); 
     195                    consumer.xmlEndNamespace(qname.local,value); 
     196                } 
     197            } 
     198        } 
     199         
     200    } 
     201     
     202    void xmlText(char[] text){ 
     203        consumer.xmlText(text); 
     204    } 
    46205}