Skip to content

Commit 65131a5

Browse files
committed
Унифицированы объекты определения типов
1 parent f59e417 commit 65131a5

File tree

3 files changed

+142
-105
lines changed

3 files changed

+142
-105
lines changed

src/OneScript.StandardLibrary/XMLSchema/Interfaces/IXSType.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,7 @@ namespace OneScript.StandardLibrary.XMLSchema.Interfaces
99
{
1010
public interface IXSType : IXSAnnotated
1111
{
12+
string NamespaceURI { get; }
13+
string Name { get; set; }
1214
}
1315
}

src/OneScript.StandardLibrary/XMLSchema/Objects/XSComplexTypeDefinition.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ public XSAnnotation Annotation
318318
public IValue DOMElement => ValueFactory.Create();
319319

320320
[ContextProperty("URIПространстваИмен", "NamespaceURI")]
321-
public string URIПространстваИмен => _type.SourceUri;
321+
public string NamespaceURI => _type.SourceUri;
322322

323323
[ContextProperty("Имя", "Name")]
324324
public string Name

src/OneScript.StandardLibrary/XMLSchema/Objects/XSSimpleTypeDefinition.cs

Lines changed: 139 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ This Source Code Form is subject to the terms of the
66
----------------------------------------------------------*/
77

88
using System;
9+
using System.Diagnostics.Contracts;
910
using System.Xml;
1011
using System.Xml.Schema;
1112
using OneScript.Contexts;
12-
using OneScript.Exceptions;
1313
using OneScript.StandardLibrary.Xml;
1414
using OneScript.StandardLibrary.XMLSchema.Collections;
1515
using OneScript.StandardLibrary.XMLSchema.Enumerations;
@@ -23,28 +23,18 @@ namespace OneScript.StandardLibrary.XMLSchema.Objects
2323
public sealed class XSSimpleTypeDefinition : AutoContext<XSSimpleTypeDefinition>, IXSType, IXSNamedComponent
2424
{
2525
private readonly XmlSchemaSimpleType _type;
26+
private readonly XSComponentFixedList _components = new XSComponentFixedList();
27+
private readonly XSComponentList _typeDefinitions = new XSComponentList();
28+
private readonly XSComponentList _facets = new XSComponentList();
29+
2630
private XSAnnotation _annotation;
2731
private XMLExpandedName _baseTypeName;
2832
private XMLExpandedName _itemTypeName;
2933
private XSSimpleTypeVariety _variety;
3034

31-
private XSSimpleTypeDefinition()
32-
{
33-
_type = new XmlSchemaSimpleType();
34-
Facets = new XSComponentList();
35-
Facets.Inserted += Facets_Inserted;
36-
Facets.Cleared += Facets_Cleared;
37-
38-
MemberTypeDefinitions = new XSComponentList();
39-
MemberTypeDefinitions.Inserted += MemberTypeDefinitions_Inserted;
40-
MemberTypeDefinitions.Cleared += MemberTypeDefinitions_Cleared;
41-
42-
Components = new XSComponentFixedList();
43-
Variety = XSSimpleTypeVariety.Atomic;
44-
}
35+
private XSSimpleTypeDefinition() : this(new XmlSchemaSimpleType()) { }
4536

4637
internal XSSimpleTypeDefinition(XmlSchemaSimpleType simpleType)
47-
: this()
4838
{
4939
_type = simpleType;
5040

@@ -54,47 +44,122 @@ internal XSSimpleTypeDefinition(XmlSchemaSimpleType simpleType)
5444
_annotation.BindToContainer(RootContainer, this);
5545
}
5646

47+
InitContentTypeVariety();
48+
49+
_facets.Inserted += Facets_Inserted;
50+
_facets.Cleared += Facets_Cleared;
51+
52+
_typeDefinitions.Inserted += MemberTypeDefinitions_Inserted;
53+
_typeDefinitions.Cleared += MemberTypeDefinitions_Cleared;
54+
}
55+
56+
private void InitContentTypeVariety()
57+
{
5758
if (_type.Content is XmlSchemaSimpleTypeList typeList)
59+
InitListVariety(typeList);
60+
61+
else if (_type.Content is XmlSchemaSimpleTypeUnion typeUnion)
62+
InitUnionVariety(typeUnion);
63+
64+
else if (_type.Content is XmlSchemaSimpleTypeRestriction typeRestriction)
65+
InitAtomicVariety(typeRestriction);
66+
67+
else
68+
_variety = XSSimpleTypeVariety.Atomic;
69+
}
70+
71+
private void InitListVariety(XmlSchemaSimpleTypeList typeList)
72+
{
73+
_variety = XSSimpleTypeVariety.List;
74+
InitItemTypeName(typeList.ItemTypeName);
75+
}
76+
77+
private void InitUnionVariety(XmlSchemaSimpleTypeUnion typeUnion)
78+
{
79+
_variety = XSSimpleTypeVariety.Union;
80+
foreach (var item in typeUnion.BaseTypes)
5881
{
59-
_variety = XSSimpleTypeVariety.List;
82+
var component = XMLSchemaSerializer.CreateInstance(item);
83+
component.BindToContainer(RootContainer, this);
84+
_typeDefinitions.Add(component);
85+
_components.Add(component);
86+
}
87+
}
6088

61-
if (typeList.ItemTypeName is XmlQualifiedName qualifiedName)
62-
_itemTypeName = new XMLExpandedName(qualifiedName);
89+
private void InitAtomicVariety(XmlSchemaSimpleTypeRestriction typeRestriction)
90+
{
91+
_variety = XSSimpleTypeVariety.Atomic;
92+
InitBaseTypeName(typeRestriction.BaseTypeName);
6393

64-
}
65-
else if (_type.Content is XmlSchemaSimpleTypeUnion typeUnion)
94+
foreach (var item in typeRestriction.Facets)
6695
{
67-
_variety = XSSimpleTypeVariety.Union;
68-
69-
MemberTypeDefinitions.Inserted -= MemberTypeDefinitions_Inserted;
70-
foreach (XmlSchemaObject item in typeUnion.BaseTypes)
71-
{
72-
IXSComponent component = XMLSchemaSerializer.CreateInstance(item);
73-
component.BindToContainer(RootContainer, this);
74-
MemberTypeDefinitions.Add(component);
75-
Components.Add(component);
76-
}
77-
MemberTypeDefinitions.Inserted += MemberTypeDefinitions_Inserted;
96+
var component = XMLSchemaSerializer.CreateInstance(item);
97+
component.BindToContainer(RootContainer, this);
98+
_facets.Add(component);
99+
_components.Add(component);
78100
}
79-
else if (_type.Content is XmlSchemaSimpleTypeRestriction typeRestriction)
101+
}
102+
103+
private void InitItemTypeName(XmlQualifiedName xmlQualifiedName)
104+
{
105+
if (xmlQualifiedName is XmlQualifiedName qualifiedName)
106+
_itemTypeName = new XMLExpandedName(qualifiedName);
107+
}
108+
109+
private void InitBaseTypeName(XmlQualifiedName xmlQualifiedName)
110+
{
111+
if (xmlQualifiedName is XmlQualifiedName qualifiedName)
112+
_baseTypeName = new XMLExpandedName(qualifiedName);
113+
}
114+
115+
private void SetContentTypeVariety(XSSimpleTypeVariety value)
116+
{
117+
if (_variety == value) return;
118+
_variety = value;
119+
120+
switch (_variety)
80121
{
81-
_variety = XSSimpleTypeVariety.Atomic;
122+
case XSSimpleTypeVariety.List:
123+
_type.Content = new XmlSchemaSimpleTypeList();
124+
_itemTypeName = default;
125+
break;
126+
127+
case XSSimpleTypeVariety.Union:
128+
_type.Content = new XmlSchemaSimpleTypeUnion();
129+
_typeDefinitions.Clear();
130+
break;
131+
132+
case XSSimpleTypeVariety.Atomic:
133+
_type.Content = new XmlSchemaSimpleTypeRestriction();
134+
_baseTypeName = default;
135+
_facets.Clear();
136+
break;
82137

83-
if (typeRestriction.BaseTypeName is XmlQualifiedName qualifiedName)
84-
_baseTypeName = new XMLExpandedName(qualifiedName);
85-
86-
Facets.Inserted -= Facets_Inserted;
87-
foreach (XmlSchemaObject item in typeRestriction.Facets)
88-
{
89-
IXSComponent component = XMLSchemaSerializer.CreateInstance(item);
90-
component.BindToContainer(RootContainer, this);
91-
Facets.Add(component);
92-
Components.Add(component);
93-
}
94-
Facets.Inserted += Facets_Inserted;
138+
default:
139+
break;
95140
}
96141
}
97142

143+
private void SetBaseTypeName(XMLExpandedName value)
144+
{
145+
if (_baseTypeName == value) return;
146+
Contract.Requires(Variety == XSSimpleTypeVariety.Atomic);
147+
_baseTypeName = value;
148+
149+
var content = _type.Content as XmlSchemaSimpleTypeRestriction;
150+
content.BaseTypeName = _baseTypeName?.NativeValue;
151+
}
152+
153+
private void SetItemTypeName(XMLExpandedName value)
154+
{
155+
if (_itemTypeName == value) return;
156+
Contract.Requires(Variety == XSSimpleTypeVariety.List);
157+
_itemTypeName = value;
158+
159+
var content = _type.Content as XmlSchemaSimpleTypeList;
160+
content.ItemTypeName = _itemTypeName?.NativeValue;
161+
}
162+
98163
#region OneScript
99164

100165
#region Properties
@@ -112,7 +177,7 @@ public XSAnnotation Annotation
112177
}
113178

114179
[ContextProperty("Компоненты", "Components")]
115-
public XSComponentFixedList Components { get; }
180+
public XSComponentFixedList Components => _components;
116181

117182
[ContextProperty("Контейнер", "Container")]
118183
public IXSComponent Container { get; private set; }
@@ -130,7 +195,7 @@ public XSAnnotation Annotation
130195
public IValue DOMElement => ValueFactory.Create();
131196

132197
[ContextProperty("URIПространстваИмен", "NamespaceURI")]
133-
public string URIПространстваИмен => _type.SourceUri;
198+
public string NamespaceURI => _type.SourceUri;
134199

135200
[ContextProperty("Имя", "Name")]
136201
public string Name
@@ -152,19 +217,7 @@ public string Name
152217
public XSSimpleTypeVariety Variety
153218
{
154219
get => _variety;
155-
set
156-
{
157-
_variety = value;
158-
159-
if (_variety == XSSimpleTypeVariety.List)
160-
_type.Content = new XmlSchemaSimpleTypeList();
161-
162-
else if (_variety == XSSimpleTypeVariety.Union)
163-
_type.Content = new XmlSchemaSimpleTypeUnion();
164-
165-
else
166-
_type.Content = new XmlSchemaSimpleTypeRestriction();
167-
}
220+
set => SetContentTypeVariety(value);
168221
}
169222

170223
[ContextProperty("Завершенность", "Final")]
@@ -177,32 +230,14 @@ public XSSimpleTypeVariety Variety
177230
public XMLExpandedName BaseTypeName
178231
{
179232
get => _baseTypeName;
180-
set
181-
{
182-
_baseTypeName = value;
183-
if (Variety == XSSimpleTypeVariety.Atomic)
184-
{
185-
XmlSchemaSimpleTypeRestriction content = _type.Content as XmlSchemaSimpleTypeRestriction;
186-
content.BaseTypeName = _baseTypeName.NativeValue;
187-
}
188-
else
189-
throw RuntimeException.InvalidArgumentValue();
190-
}
233+
set => SetBaseTypeName(value);
191234
}
192235

193236
[ContextProperty("ИмяТипаЭлемента", "ItemTypeName")]
194237
public XMLExpandedName ItemTypeName
195238
{
196239
get => _itemTypeName;
197-
set
198-
{
199-
_itemTypeName = value;
200-
if (Variety == XSSimpleTypeVariety.List)
201-
{
202-
XmlSchemaSimpleTypeList content = (XmlSchemaSimpleTypeList)_type.Content;
203-
content.ItemTypeName = _itemTypeName.NativeValue;
204-
}
205-
}
240+
set => SetItemTypeName(value);
206241
}
207242

208243
[ContextProperty("ОпределениеБазовогоТипа", "BaseTypeDefinition")]
@@ -215,17 +250,17 @@ public XMLExpandedName ItemTypeName
215250
public XSSimpleTypeDefinition ItemTypeDefinition { get; set; }
216251

217252
[ContextProperty("ОпределенияТиповОбъединения", "MemberTypeDefinitions")]
218-
public XSComponentList MemberTypeDefinitions { get; }
253+
public XSComponentList MemberTypeDefinitions => _typeDefinitions;
219254

220255
[ContextProperty("Фасеты", "Facets")]
221-
public XSComponentList Facets { get; }
256+
public XSComponentList Facets => _facets;
222257

223258
#endregion
224259

225260
#region Methods
226261

227262
[ContextMethod("КлонироватьКомпоненту", "CloneComponent")]
228-
public IXSComponent CloneComponent(bool recursive = true) => throw new NotImplementedException();
263+
public IXSComponent CloneComponent(bool recursive) => throw new NotImplementedException();
229264

230265
[ContextMethod("ОбновитьЭлементDOM", "UpdateDOMElement")]
231266
public void UpdateDOMElement() => throw new NotImplementedException();
@@ -264,46 +299,46 @@ public void BindToContainer(IXSComponent rootContainer, IXSComponent container)
264299

265300
private void Facets_Inserted(object sender, XSComponentListEventArgs e)
266301
{
267-
IXSComponent component = e.Component;
268-
269-
if (!(component is IXSFacet))
270-
throw RuntimeException.InvalidArgumentType();
302+
var component = e.Component;
303+
Contract.Requires(_variety == XSSimpleTypeVariety.Atomic);
304+
Contract.Requires(component is IXSFacet);
271305

272306
component.BindToContainer(RootContainer, this);
273-
Components.Add(component);
307+
_components.Add(component);
274308

275-
if (_type.Content is XmlSchemaSimpleTypeRestriction content)
276-
content.Facets.Add(component.SchemaObject);
309+
var content = _type.Content as XmlSchemaSimpleTypeRestriction;
310+
content.Facets.Add(component.SchemaObject);
277311
}
278312

279313
private void Facets_Cleared(object sender, EventArgs e)
280314
{
281-
Components.Clear();
315+
Contract.Requires(_variety == XSSimpleTypeVariety.Atomic);
316+
_components.Clear();
282317

283-
if (_type.Content is XmlSchemaSimpleTypeRestriction content)
284-
content.Facets.Clear();
318+
var content = _type.Content as XmlSchemaSimpleTypeRestriction;
319+
content.Facets.Clear();
285320
}
286321

287322
private void MemberTypeDefinitions_Inserted(object sender, XSComponentListEventArgs e)
288323
{
289-
IXSComponent component = e.Component;
290-
291-
if (!(component is XSSimpleTypeDefinition))
292-
throw RuntimeException.InvalidArgumentType();
324+
var component = e.Component;
325+
Contract.Requires(_variety == XSSimpleTypeVariety.Union);
326+
Contract.Requires(component is XSSimpleTypeDefinition);
293327

294328
component.BindToContainer(RootContainer, this);
295-
Components.Add(component);
329+
_components.Add(component);
296330

297-
if (_type.Content is XmlSchemaSimpleTypeUnion content)
298-
content.BaseTypes.Add(component.SchemaObject);
331+
var content = _type.Content as XmlSchemaSimpleTypeUnion;
332+
content.BaseTypes.Add(component.SchemaObject);
299333
}
300334

301335
private void MemberTypeDefinitions_Cleared(object sender, EventArgs e)
302336
{
303-
Components.Clear();
337+
Contract.Requires(_variety == XSSimpleTypeVariety.Union);
338+
_components.Clear();
304339

305-
if (_type.Content is XmlSchemaSimpleTypeUnion content)
306-
content.BaseTypes.Clear();
340+
var content = _type.Content as XmlSchemaSimpleTypeUnion;
341+
content.BaseTypes.Clear();
307342
}
308343

309344
#endregion

0 commit comments

Comments
 (0)