Project

Profile

Help

Revision fd03e6ba

Added by Michael Kay about 12 years ago

Fix bug 2877000 Missing namespaces in .NET DOM constructed programmatically

View differences:

latest9.1/bj/net/sf/saxon/dotnet/NodeWrapper.java
2 2

  
3 3
import cli.System.Xml.*;
4 4
import net.sf.saxon.Configuration;
5
import net.sf.saxon.sort.IntSet;
6
import net.sf.saxon.sort.IntHashSet;
7
import net.sf.saxon.sort.IntIterator;
5 8
import net.sf.saxon.event.Receiver;
9
import net.sf.saxon.event.NamespaceReducer;
6 10
import net.sf.saxon.om.*;
7 11
import net.sf.saxon.pattern.NameTest;
8 12
import net.sf.saxon.pattern.NodeTest;
......
761 765
     */
762 766

  
763 767
    public void copy(Receiver out, int whichNamespaces, boolean copyAnnotations, int locationId) throws XPathException {
764
        Navigator.copy(this, out, docWrapper.getNamePool(), whichNamespaces, copyAnnotations, locationId);
768
        Receiver r = new NamespaceReducer(out);
769
        Navigator.copy(this, r, docWrapper.getNamePool(), whichNamespaces, copyAnnotations, locationId);
765 770
    }
766 771

  
767 772
    /**
......
780 785
     */
781 786

  
782 787
    public int[] getDeclaredNamespaces(int[] buffer) {
788
        // Note: in a DOM created by the XML parser, all namespaces are present as attribute nodes. But
789
        // in a DOM created programmatically, this is not necessarily the case. So we need to add
790
        // namespace bindings for the namespace of the element and any attributes
783 791
        if (node.get_NodeType().Value == XmlNodeType.Element) {
784 792
            XmlElement elem = (XmlElement)node;
785 793
            XmlNamedNodeMap atts = elem.get_Attributes();
786

  
787
            if (atts == null) {
788
                return EMPTY_NAMESPACE_LIST;
789
            }
790
            int count = 0;
794
            IntSet codes = new IntHashSet();
795
            NamePool pool = getNamePool();
791 796
            for (int i = 0; i < atts.get_Count(); i++) {
792 797
                XmlAttribute att = (XmlAttribute)atts.Item(i);
793 798
                String attName = att.get_Name();
794 799
                if (attName.equals("xmlns")) {
795
                    count++;
800
                    String prefix = "";
801
                    String uri = att.get_Value();
802
                    codes.add(pool.allocateNamespaceCode(prefix, uri));
796 803
                } else if (attName.startsWith("xmlns:")) {
797
                    count++;
804
                    String prefix = attName.substring(6);
805
                    String uri = att.get_Value();
806
                    codes.add(pool.allocateNamespaceCode(prefix, uri));
807
                } else if (att.get_NamespaceURI().length() != 0) {
808
                    codes.add(pool.allocateNamespaceCode(att.get_Prefix(), att.get_NamespaceURI()));
798 809
                }
799 810
            }
800
            if (count == 0) {
801
                return EMPTY_NAMESPACE_LIST;
802
            } else {
803
                int[] result = (buffer == null || count > buffer.length ? new int[count] : buffer);
804
                NamePool pool = getNamePool();
805
                int n = 0;
806
                for (int i = 0; i < atts.get_Count(); i++) {
807
                    XmlAttribute att = (XmlAttribute)atts.Item(i);
808
                    String attName = att.get_Name();
809
                    if (attName.equals("xmlns")) {
810
                        String prefix = "";
811
                        String uri = att.get_Value();
812
                        result[n++] = pool.allocateNamespaceCode(prefix, uri);
813
                    } else if (attName.startsWith("xmlns:")) {
814
                        String prefix = attName.substring(6);
815
                        String uri = att.get_Value();
816
                        result[n++] = pool.allocateNamespaceCode(prefix, uri);
817
                    }
818
                }
819
                if (count < result.length) {
820
                    result[count] = -1;
821
                }
822
                return result;
811

  
812
            if (elem.get_NamespaceURI().length() != 0) {
813
                codes.add(pool.allocateNamespaceCode(elem.get_Prefix(), elem.get_NamespaceURI()));
814
            }
815
            int count = codes.size();
816
            int[] result = new int[count];
817
            int p = 0;
818
            for (IntIterator ii = codes.iterator(); ii.hasNext();) {
819
                 result[p++] = ii.next();
823 820
            }
821
            return result;
824 822
        } else {
825 823
            return null;
826 824
        }
latest9.2/hej/net/sf/saxon/dotnet/NodeWrapper.java
2 2

  
3 3
import cli.System.Xml.*;
4 4
import net.sf.saxon.Configuration;
5
import net.sf.saxon.sort.IntSet;
6
import net.sf.saxon.sort.IntHashSet;
7
import net.sf.saxon.sort.IntIterator;
5 8
import net.sf.saxon.event.Receiver;
9
import net.sf.saxon.event.NamespaceReducer;
6 10
import net.sf.saxon.om.*;
7 11
import net.sf.saxon.pattern.NameTest;
8 12
import net.sf.saxon.pattern.NodeTest;
......
772 776
     */
773 777

  
774 778
    public void copy(Receiver out, int whichNamespaces, boolean copyAnnotations, int locationId) throws XPathException {
775
        Navigator.copy(this, out, docWrapper.getNamePool(), whichNamespaces, copyAnnotations, locationId);
779
        Receiver r = new NamespaceReducer(out);
780
        Navigator.copy(this, r, docWrapper.getNamePool(), whichNamespaces, copyAnnotations, locationId);
776 781
    }
777 782

  
778 783
    /**
......
791 796
     */
792 797

  
793 798
    public int[] getDeclaredNamespaces(int[] buffer) {
799
        // Note: in a DOM created by the XML parser, all namespaces are present as attribute nodes. But
800
        // in a DOM created programmatically, this is not necessarily the case. So we need to add
801
        // namespace bindings for the namespace of the element and any attributes
794 802
        if (node.get_NodeType().Value == XmlNodeType.Element) {
795 803
            XmlElement elem = (XmlElement)node;
796 804
            XmlNamedNodeMap atts = elem.get_Attributes();
797

  
798
            if (atts == null) {
799
                return EMPTY_NAMESPACE_LIST;
800
            }
801
            int count = 0;
805
            IntSet codes = new IntHashSet();
806
            NamePool pool = getNamePool();
802 807
            for (int i = 0; i < atts.get_Count(); i++) {
803 808
                XmlAttribute att = (XmlAttribute)atts.Item(i);
804 809
                String attName = att.get_Name();
805 810
                if (attName.equals("xmlns")) {
806
                    count++;
811
                    String prefix = "";
812
                    String uri = att.get_Value();
813
                    codes.add(pool.allocateNamespaceCode(prefix, uri));
807 814
                } else if (attName.startsWith("xmlns:")) {
808
                    count++;
815
                    String prefix = attName.substring(6);
816
                    String uri = att.get_Value();
817
                    codes.add(pool.allocateNamespaceCode(prefix, uri));
818
                } else if (att.get_NamespaceURI().length() != 0) {
819
                    codes.add(pool.allocateNamespaceCode(att.get_Prefix(), att.get_NamespaceURI()));
809 820
                }
810 821
            }
811
            if (count == 0) {
812
                return EMPTY_NAMESPACE_LIST;
813
            } else {
814
                int[] result = (buffer == null || count > buffer.length ? new int[count] : buffer);
815
                NamePool pool = getNamePool();
816
                int n = 0;
817
                for (int i = 0; i < atts.get_Count(); i++) {
818
                    XmlAttribute att = (XmlAttribute)atts.Item(i);
819
                    String attName = att.get_Name();
820
                    if (attName.equals("xmlns")) {
821
                        String prefix = "";
822
                        String uri = att.get_Value();
823
                        result[n++] = pool.allocateNamespaceCode(prefix, uri);
824
                    } else if (attName.startsWith("xmlns:")) {
825
                        String prefix = attName.substring(6);
826
                        String uri = att.get_Value();
827
                        result[n++] = pool.allocateNamespaceCode(prefix, uri);
828
                    }
829
                }
830
                if (count < result.length) {
831
                    result[count] = -1;
832
                }
833
                return result;
822

  
823
            if (elem.get_NamespaceURI().length() != 0) {
824
                codes.add(pool.allocateNamespaceCode(elem.get_Prefix(), elem.get_NamespaceURI()));
825
            }
826
            int count = codes.size();
827
            int[] result = new int[count];
828
            int p = 0;
829
            for (IntIterator ii = codes.iterator(); ii.hasNext();) {
830
                 result[p++] = ii.next();
834 831
            }
832
            return result;
835 833
        } else {
836 834
            return null;
837 835
        }

Also available in: Unified diff