~halega/+junk/sharpdevelop

« back to all changes in this revision

Viewing changes to src/AddIns/DisplayBindings/Data/ICSharpCode.Data.EDMDesigner.Core/IO/EDMXIO.cs

  • Committer: sk
  • Date: 2011-09-10 05:17:57 UTC
  • Revision ID: halega@halega.com-20110910051757-qfouz1llya9m6boy
4.1.0.7915 Release Candidate 1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
 
2
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
 
3
 
 
4
#region Usings
 
5
 
 
6
using System;
 
7
using System.Linq;
 
8
using System.Xml.Linq;
 
9
using ICSharpCode.Data.Core.Common;
 
10
using ICSharpCode.Data.EDMDesigner.Core.EDMObjects;
 
11
using ICSharpCode.Data.EDMDesigner.Core.EDMObjects.SSDL;
 
12
using ICSharpCode.Data.EDMDesigner.Core.EDMObjects.CSDL;
 
13
using ICSharpCode.Data.EDMDesigner.Core.EDMObjects.Designer.Common;
 
14
using System.IO;
 
15
using ICSharpCode.Data.EDMDesigner.Core.EDMObjects.Designer;
 
16
 
 
17
#endregion
 
18
 
 
19
namespace ICSharpCode.Data.EDMDesigner.Core.IO
 
20
{
 
21
    public class EDMXIO : IO
 
22
    {
 
23
        public static EDM Read(string edmxPath, Action<XElement> readMoreAction)
 
24
        {
 
25
            XElement edmx = XElement.Load(edmxPath);
 
26
            return Read(edmx, readMoreAction);
 
27
        }
 
28
 
 
29
        public static EDM Read(Stream stream, Action<XElement> readMoreAction)
 
30
        {
 
31
            XElement edmx = XElement.Load(stream);
 
32
            return Read(edmx, readMoreAction);
 
33
        }
 
34
        
 
35
        public static EDM Read(XDocument edmxDocument, Action<XElement> readMoreAction)
 
36
        {
 
37
                return Read(edmxDocument.Root, readMoreAction);
 
38
        }
 
39
 
 
40
        private static EDM Read(XElement edmx, Action<XElement> readMoreAction)
 
41
        {
 
42
            XElement edmxRuntime = edmx.Element(XName.Get("Runtime", edmxNamespace.NamespaceName));
 
43
 
 
44
            SSDLContainer ssdlContainer = SSDLIO.ReadXElement(edmxRuntime);
 
45
            CSDLContainer csdlContainer = CSDLIO.ReadXElement(edmxRuntime);
 
46
            XElement edmxDesigner = edmx.Element(XName.Get("Designer", edmxNamespace.NamespaceName));
 
47
            
 
48
            if (ssdlContainer == null && csdlContainer == null)
 
49
                return new EDM();
 
50
 
 
51
            EDM edm = new EDM()
 
52
            {
 
53
                SSDLContainer = ssdlContainer,
 
54
                CSDLContainer = MSLIO.IntegrateMSLInCSDLContainer(csdlContainer, ssdlContainer, edmxRuntime),
 
55
            };
 
56
 
 
57
            if (edmxDesigner != null)
 
58
            {
 
59
                if (edmxDesigner.Element(XName.Get("Connection", edmxNamespace.NamespaceName)) != null)
 
60
                    edm.DesignerProperties = edmxDesigner.Element(XName.Get("Connection", edmxNamespace.NamespaceName)).Element(XName.Get("DesignerInfoPropertySet", edmxNamespace.NamespaceName)).Elements(XName.Get("DesignerProperty", edmxNamespace.NamespaceName)).Select(e => new DesignerProperty { Name = e.Attribute("Name").Value, Value = e.Attribute("Value").Value });
 
61
 
 
62
                if (edmxDesigner.Element(XName.Get("Options", edmxNamespace.NamespaceName)) != null)
 
63
                    edm.EDMXDesignerDesignerProperties = edmxDesigner.Element(XName.Get("Options", edmxNamespace.NamespaceName)).Element(XName.Get("DesignerInfoPropertySet", edmxNamespace.NamespaceName)).Elements(XName.Get("DesignerProperty", edmxNamespace.NamespaceName)).Select(e => new DesignerProperty { Name = e.Attribute("Name").Value, Value = e.Attribute("Value").Value });
 
64
 
 
65
                if (edmxDesigner.Element(XName.Get("Diagrams", edmxNamespace.NamespaceName)) != null)
 
66
                    edm.EDMXDesignerDiagrams = edmxDesigner.Element(XName.Get("Diagrams", edmxNamespace.NamespaceName)).Elements(XName.Get("Diagram", edmxNamespace.NamespaceName));
 
67
            }
 
68
 
 
69
            readMoreAction(edmx);
 
70
            return edm;
 
71
        }
 
72
 
 
73
        public enum EDMXSection
 
74
        { 
 
75
            EDMX,
 
76
            Runtime,
 
77
            SSDL,
 
78
            CSDL,
 
79
            MSL,
 
80
            Designer,
 
81
            DesignerViews
 
82
        }
 
83
 
 
84
        public static XElement ReadSection(XDocument edmxDocument, EDMXSection section)
 
85
        {
 
86
            return ReadSection(edmxDocument.Root, section);
 
87
        }
 
88
 
 
89
        public static XElement ReadSection(XElement edmxElement, EDMXSection section)
 
90
        {
 
91
            if (section == EDMXSection.EDMX)
 
92
                return edmxElement;
 
93
            
 
94
            if (edmxElement == null)
 
95
                throw new ArgumentException("Input file is not a valid EDMX file.");
 
96
 
 
97
            if (section == EDMXSection.Designer || section == EDMXSection.DesignerViews)
 
98
            {
 
99
                XElement designerElement = edmxElement.Element(XName.Get("Designer", edmxNamespace.NamespaceName));
 
100
 
 
101
                if (designerElement == null)
 
102
                    return null;
 
103
 
 
104
                if (section == EDMXSection.Designer)
 
105
                    return designerElement;
 
106
                else
 
107
                {
 
108
                    XElement diagramsElement = designerElement.Element(XName.Get("Diagrams", edmxNamespace.NamespaceName));
 
109
 
 
110
                    if (diagramsElement == null)
 
111
                        throw new ArgumentException("Input file is not a valid EDMX file.");
 
112
 
 
113
                    return diagramsElement.Element(XName.Get("DesignerViews"));
 
114
                }
 
115
            }
 
116
 
 
117
            XElement runtimeElement = edmxElement.Element(XName.Get("Runtime", edmxNamespace.NamespaceName));
 
118
 
 
119
            if (runtimeElement == null)
 
120
                throw new ArgumentException("Input file is not a valid EDMX file.");
 
121
 
 
122
            if (section == EDMXSection.Runtime)
 
123
                return runtimeElement;
 
124
 
 
125
            switch (section)
 
126
            { 
 
127
                case EDMXSection.SSDL:
 
128
                    XElement storageModelsElement = runtimeElement.Element(XName.Get("StorageModels", edmxNamespace.NamespaceName));
 
129
 
 
130
                    if (storageModelsElement == null)
 
131
                        throw new ArgumentException("Input file is not a valid EDMX file.");
 
132
 
 
133
                    return storageModelsElement;
 
134
                case EDMXSection.CSDL:
 
135
                    XElement conceptualModelsElement = runtimeElement.Element(XName.Get("ConceptualModels", edmxNamespace.NamespaceName));
 
136
 
 
137
                    if (conceptualModelsElement == null)
 
138
                        throw new ArgumentException("Input file is not a valid EDMX file.");
 
139
 
 
140
                    return conceptualModelsElement;
 
141
                case EDMXSection.MSL:
 
142
                    XElement mappingsElement = runtimeElement.Element(XName.Get("Mappings", edmxNamespace.NamespaceName));
 
143
 
 
144
                    if (mappingsElement == null)
 
145
                        throw new ArgumentException("Input file is not a valid EDMX file.");
 
146
 
 
147
                    return mappingsElement;
 
148
            }
 
149
 
 
150
            return null;
 
151
        }
 
152
 
 
153
        public static XDocument WriteXDocument(XDocument ssdlDocument, XDocument csdlDocument, XDocument mslDocument)
 
154
        {
 
155
            return WriteXDocument(ssdlDocument.Root, csdlDocument.Root, mslDocument.Root, null);
 
156
        }
 
157
 
 
158
        public static XDocument WriteXDocument(XElement ssdlElement, XElement csdlElement, XElement mslElement, XElement designerElement)
 
159
        {
 
160
            return new XDocument(new XDeclaration("1.0", "utf-8", null),
 
161
                new XElement(edmxNamespace + "Edmx", new XAttribute("Version", "1.0"), new XAttribute(XNamespace.Xmlns + "edmx", edmxNamespace.NamespaceName),
 
162
                    new XElement(edmxNamespace + "Runtime",
 
163
                        new XElement(edmxNamespace + "StorageModels",
 
164
                            ssdlElement),
 
165
                        new XElement(edmxNamespace + "ConceptualModels",
 
166
                            csdlElement),
 
167
                        new XElement(edmxNamespace + "Mappings",
 
168
                            mslElement))).AddElement(designerElement));
 
169
        }
 
170
 
 
171
        public static XDocument WriteXDocument(EDMView edmView)
 
172
        {
 
173
            if (edmView.EDM.IsEmpty) {
 
174
                return WriteXDocument(null, null, null, null);
 
175
            }
 
176
            XElement ssdlElement = SSDLIO.WriteXElement(edmView.EDM.SSDLContainer);
 
177
            XElement csdlElement = CSDLIO.Write(edmView.EDM.CSDLContainer);
 
178
            XElement mslElement = MSLIO.Write(edmView.EDM);
 
179
            XElement designerElement = DesignerIO.Write(edmView);
 
180
 
 
181
            return WriteXDocument(ssdlElement, csdlElement, mslElement, designerElement);
 
182
        }
 
183
    }
 
184
}