~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to src/addins/MonoDevelop.XmlEditor/MonoDevelop.XmlEditor.XPath/XPathNamespaceList.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// MonoDevelop XML Editor
 
3
//
 
4
// Copyright (C) 2006-2007 Matthew Ward
 
5
//
 
6
// Permission is hereby granted, free of charge, to any person obtaining
 
7
// a copy of this software and associated documentation files (the
 
8
// "Software"), to deal in the Software without restriction, including
 
9
// without limitation the rights to use, copy, modify, merge, publish,
 
10
// distribute, sublicense, and/or sell copies of the Software, and to
 
11
// permit persons to whom the Software is furnished to do so, subject to
 
12
// the following conditions:
 
13
// 
 
14
// The above copyright notice and this permission notice shall be
 
15
// included in all copies or substantial portions of the Software.
 
16
// 
 
17
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 
18
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 
19
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 
20
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 
21
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 
22
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 
23
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
24
 
 
25
using MonoDevelop.Core;
 
26
using System;
 
27
using System.Collections.Generic;
 
28
using System.Xml;
 
29
 
 
30
namespace MonoDevelop.XmlEditor
 
31
{
 
32
        public class XPathNamespaceList : ICustomXmlSerializer
 
33
        {
 
34
                const string xpathNamespaceListElementName = "XPathNamespaceList";
 
35
                const string xpathNamespaceElementName = "Namespace";
 
36
        
 
37
                List<XmlNamespace> namespaces = new List<XmlNamespace>();
 
38
 
 
39
                public XPathNamespaceList()
 
40
                {
 
41
                }
 
42
                
 
43
                /// <summary>
 
44
                /// Adds a single namespace to the list.
 
45
                /// </summary>
 
46
                public void Add(string prefix, string uri)
 
47
                {
 
48
                        namespaces.Add(new XmlNamespace(prefix, uri));
 
49
                }
 
50
                
 
51
                /// <summary>
 
52
                /// Gets the namespaces.
 
53
                /// </summary>
 
54
                public XmlNamespace[] GetNamespaces()
 
55
                {
 
56
                        return namespaces.ToArray();
 
57
                }
 
58
                                                
 
59
                /// <summary>
 
60
                /// Creates an XPathNamespaceList from the saved xml.
 
61
                /// </summary>
 
62
                public ICustomXmlSerializer ReadFrom(XmlReader reader)
 
63
                {
 
64
                        XPathNamespaceList list = new XPathNamespaceList();
 
65
                        bool finished = false;
 
66
                        while (reader.Read() && !finished) {
 
67
                                switch (reader.NodeType) {
 
68
                                        case XmlNodeType.Element:
 
69
                                                if (reader.Name == xpathNamespaceElementName) {
 
70
                                                        XmlNamespace ns = XmlNamespace.FromString(reader.ReadElementString());
 
71
                                                        list.Add(ns.Prefix, ns.Uri);
 
72
                                                }
 
73
                                                break;
 
74
                                        case XmlNodeType.EndElement:
 
75
                                                if (reader.Name != xpathNamespaceElementName) {
 
76
                                                        finished = true;
 
77
                                                }
 
78
                                                break;
 
79
                                }
 
80
                        }
 
81
                        return list;
 
82
                }
 
83
 
 
84
                /// <summary>
 
85
                /// Creates an xml element from an XPathNamespaceList.
 
86
                /// </summary>
 
87
                public void WriteTo(XmlWriter writer)
 
88
                {
 
89
                        writer.WriteStartElement(xpathNamespaceListElementName);
 
90
                        foreach (XmlNamespace ns in namespaces) {
 
91
                                writer.WriteElementString(xpathNamespaceElementName, ns.ToString());
 
92
                        }
 
93
                        writer.WriteEndElement();
 
94
                }       
 
95
        }       
 
96
}