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)
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;
15
using ICSharpCode.Data.EDMDesigner.Core.EDMObjects.Designer;
19
namespace ICSharpCode.Data.EDMDesigner.Core.IO
21
public class EDMXIO : IO
23
public static EDM Read(string edmxPath, Action<XElement> readMoreAction)
25
XElement edmx = XElement.Load(edmxPath);
26
return Read(edmx, readMoreAction);
29
public static EDM Read(Stream stream, Action<XElement> readMoreAction)
31
XElement edmx = XElement.Load(stream);
32
return Read(edmx, readMoreAction);
35
public static EDM Read(XDocument edmxDocument, Action<XElement> readMoreAction)
37
return Read(edmxDocument.Root, readMoreAction);
40
private static EDM Read(XElement edmx, Action<XElement> readMoreAction)
42
XElement edmxRuntime = edmx.Element(XName.Get("Runtime", edmxNamespace.NamespaceName));
44
SSDLContainer ssdlContainer = SSDLIO.ReadXElement(edmxRuntime);
45
CSDLContainer csdlContainer = CSDLIO.ReadXElement(edmxRuntime);
46
XElement edmxDesigner = edmx.Element(XName.Get("Designer", edmxNamespace.NamespaceName));
48
if (ssdlContainer == null && csdlContainer == null)
53
SSDLContainer = ssdlContainer,
54
CSDLContainer = MSLIO.IntegrateMSLInCSDLContainer(csdlContainer, ssdlContainer, edmxRuntime),
57
if (edmxDesigner != null)
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 });
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 });
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));
73
public enum EDMXSection
84
public static XElement ReadSection(XDocument edmxDocument, EDMXSection section)
86
return ReadSection(edmxDocument.Root, section);
89
public static XElement ReadSection(XElement edmxElement, EDMXSection section)
91
if (section == EDMXSection.EDMX)
94
if (edmxElement == null)
95
throw new ArgumentException("Input file is not a valid EDMX file.");
97
if (section == EDMXSection.Designer || section == EDMXSection.DesignerViews)
99
XElement designerElement = edmxElement.Element(XName.Get("Designer", edmxNamespace.NamespaceName));
101
if (designerElement == null)
104
if (section == EDMXSection.Designer)
105
return designerElement;
108
XElement diagramsElement = designerElement.Element(XName.Get("Diagrams", edmxNamespace.NamespaceName));
110
if (diagramsElement == null)
111
throw new ArgumentException("Input file is not a valid EDMX file.");
113
return diagramsElement.Element(XName.Get("DesignerViews"));
117
XElement runtimeElement = edmxElement.Element(XName.Get("Runtime", edmxNamespace.NamespaceName));
119
if (runtimeElement == null)
120
throw new ArgumentException("Input file is not a valid EDMX file.");
122
if (section == EDMXSection.Runtime)
123
return runtimeElement;
127
case EDMXSection.SSDL:
128
XElement storageModelsElement = runtimeElement.Element(XName.Get("StorageModels", edmxNamespace.NamespaceName));
130
if (storageModelsElement == null)
131
throw new ArgumentException("Input file is not a valid EDMX file.");
133
return storageModelsElement;
134
case EDMXSection.CSDL:
135
XElement conceptualModelsElement = runtimeElement.Element(XName.Get("ConceptualModels", edmxNamespace.NamespaceName));
137
if (conceptualModelsElement == null)
138
throw new ArgumentException("Input file is not a valid EDMX file.");
140
return conceptualModelsElement;
141
case EDMXSection.MSL:
142
XElement mappingsElement = runtimeElement.Element(XName.Get("Mappings", edmxNamespace.NamespaceName));
144
if (mappingsElement == null)
145
throw new ArgumentException("Input file is not a valid EDMX file.");
147
return mappingsElement;
153
public static XDocument WriteXDocument(XDocument ssdlDocument, XDocument csdlDocument, XDocument mslDocument)
155
return WriteXDocument(ssdlDocument.Root, csdlDocument.Root, mslDocument.Root, null);
158
public static XDocument WriteXDocument(XElement ssdlElement, XElement csdlElement, XElement mslElement, XElement designerElement)
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",
165
new XElement(edmxNamespace + "ConceptualModels",
167
new XElement(edmxNamespace + "Mappings",
168
mslElement))).AddElement(designerElement));
171
public static XDocument WriteXDocument(EDMView edmView)
173
if (edmView.EDM.IsEmpty) {
174
return WriteXDocument(null, null, null, null);
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);
181
return WriteXDocument(ssdlElement, csdlElement, mslElement, designerElement);