Skip to content

Commit 306f7f2

Browse files
committed
Add fixes found trying to parse Redfish style CSDL
1 parent 8e5230a commit 306f7f2

10 files changed

+208
-48
lines changed

lib/Annotation.js

+25-5
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,17 @@ function Annotation(xml) {
77
for(var i = 0; i < children.length; i++)
88
{
99
var elemType = children[i].type();
10-
if(elemType === 'element')
11-
{
10+
if(elemType === 'element') {
1211
this.parseElement(children[i], name);
1312
}
14-
else
15-
{
16-
throw new Error('Unknown element type in Annotation '+name+'!');
13+
else if(elemType === 'text') {
14+
var text = children[i].toString().trim();
15+
if(text.length !== 0) {
16+
throw new Error('Unknown text element in schema! Text = "'+text+'"');
17+
}
18+
}
19+
else {
20+
throw new Error('Unknown element '+elemType+' type in Annotation '+name+'!');
1721
}
1822
}
1923
var attributes = xml.attrs();
@@ -42,6 +46,22 @@ Annotation.prototype.parseAttribute = function(attribute, entityName) {
4246
case 'Term':
4347
//Already used... drop on floor
4448
break;
49+
case 'Bool':
50+
var value = attribute.value();
51+
if(value === 'true') {
52+
this.Bool = true;
53+
}
54+
else if(value === 'false') {
55+
this.Bool = false;
56+
}
57+
else {
58+
throw new Error('Unknown boolean value '+value);
59+
}
60+
break;
61+
case 'String':
62+
case 'EnumMember':
63+
this[attrName] = attribute.value();
64+
break;
4565
default:
4666
throw new Error('Unknown attribute name '+attrName+' in Annotation '+entityName);
4767
break;

lib/Collection.js

+17-5
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
const assert = require('assert');
22

3+
const Record = require('./Record');
4+
35
function Collection(xml) {
46
var children = xml.childNodes();
57
for(var i = 0; i < children.length; i++)
68
{
79
var elemType = children[i].type();
8-
if(elemType === 'element')
9-
{
10+
if(elemType === 'element') {
1011
this.parseElement(children[i]);
1112
}
12-
else
13-
{
14-
throw new Error('Unknown element type in Collection!');
13+
else if(elemType === 'text') {
14+
var text = children[i].toString().trim();
15+
if(text.length !== 0) {
16+
throw new Error('Unknown text element in schema! Text = "'+text+'"');
17+
}
18+
}
19+
else {
20+
throw new Error('Unknown element type '+elemType+' in Collection!');
1521
}
1622
}
1723
var attributes = xml.attrs();
@@ -31,6 +37,12 @@ Collection.prototype.parseElement = function(element) {
3137
}
3238
this.PropertyPaths.push(element.text());
3339
break;
40+
case 'Record':
41+
if(this.Records === undefined) {
42+
this.Records = [];
43+
}
44+
this.Records.push(new Record(element));
45+
break;
3446
default:
3547
throw new Error('Unknown element name '+elemName);
3648
break;

lib/ComplexType.js

+20-11
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,29 @@
11
const assert = require('assert');
22

3+
const Annotation = require('./Annotation');
34
const Property = require('./Property');
45
const NavigationProperty = require('./NavigationProperty');
56

67
function ComplexType(metadata, xml) {
78
this._metadata = metadata;
89
this.Properties = {};
10+
this.Annotations = {};
911
var name = xml.attr('Name').value();
1012
var children = xml.childNodes();
1113
for(var i = 0; i < children.length; i++)
1214
{
1315
var elemType = children[i].type();
14-
if(elemType === 'element')
15-
{
16+
if(elemType === 'element') {
1617
this.parseElement(children[i], name);
1718
}
18-
else
19-
{
20-
throw new Error('Unknown element type in EntityType '+name+'!');
19+
else if(elemType === 'text') {
20+
var text = children[i].toString().trim();
21+
if(text.length !== 0) {
22+
throw new Error('Unknown text element in ComplexType! Text = "'+text+'"');
23+
}
24+
}
25+
else {
26+
throw new Error('Unknown element type in EntityType '+elemType+'!');
2127
}
2228
}
2329
var attributes = xml.attrs();
@@ -37,7 +43,11 @@ ComplexType.prototype.parseElement = function(element, entityName) {
3743
break;
3844
case 'NavigationProperty':
3945
var name = element.attr('Name').value();
40-
this.NavigationProperty[name] = new NavigationProperty(this, element);
46+
this.Properties[name] = new NavigationProperty(this, element);
47+
break;
48+
case 'Annotation':
49+
var name = element.attr('Term').value();
50+
this.Annotations[name] = new Annotation(element);
4151
break;
4252
default:
4353
throw new Error('Unknown element name '+elemName);
@@ -54,18 +64,17 @@ ComplexType.prototype.parseAttribute = function(attribute, entityName) {
5464
case 'BaseType':
5565
this.BaseType = attribute.value();
5666
break;
67+
case 'Abstract':
68+
this.parseBooleanAttribute(attribute, attrName);
69+
break;
5770
default:
5871
throw new Error('Unknown attribute name '+attrName+' in ComplexType '+entityName);
5972
break;
6073
}
6174
}
6275

6376
ComplexType.prototype.parseBooleanAttribute = function(xml, name) {
64-
var attr = xml.attr(name);
65-
if(attr === null) {
66-
return;
67-
}
68-
var value = attr.value();
77+
var value = xml.value();
6978
if(value === 'true') {
7079
this[name] = true;
7180
}

lib/EntityType.js

+31-5
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,30 @@
11
const assert = require('assert');
22

3+
const Annotation = require('./Annotation');
34
const Property = require('./Property');
45
const NavigationProperty = require('./NavigationProperty');
56

67
function EntityType(metadata, xml) {
78
this._metadata = metadata;
89
this._key = null;
10+
this.Annotations = {};
911
this.Properties = {};
1012
var name = xml.attr('Name').value();
1113
var children = xml.childNodes();
1214
for(var i = 0; i < children.length; i++)
1315
{
1416
var elemType = children[i].type();
15-
if(elemType === 'element')
16-
{
17+
if(elemType === 'element') {
1718
this.parseElement(children[i], name);
1819
}
19-
else
20-
{
21-
throw new Error('Unknown element type in EntityType '+name+'!');
20+
else if(elemType === 'text') {
21+
var text = children[i].toString().trim();
22+
if(text.length !== 0) {
23+
throw new Error('Unknown text element in EntityType! Text = "'+text+'"');
24+
}
25+
}
26+
else {
27+
throw new Error('Unknown element type '+elemType+' in EntityType '+name+'!');
2228
}
2329
}
2430
var attributes = xml.attrs();
@@ -58,6 +64,10 @@ EntityType.prototype.parseElement = function(element, entityName) {
5864
var name = element.attr('Name').value();
5965
this.Properties[name] = new NavigationProperty(this, element);
6066
break;
67+
case 'Annotation':
68+
var name = element.attr('Term').value();
69+
this.Annotations[name] = new Annotation(element);
70+
break;
6171
default:
6272
throw new Error('Unknown element name '+elemName);
6373
break;
@@ -73,11 +83,27 @@ EntityType.prototype.parseAttribute = function(attribute, entityName) {
7383
case 'BaseType':
7484
this.BaseType = attribute.value();
7585
break;
86+
case 'Abstract':
87+
this.parseBooleanAttribute(attribute, attrName);
88+
break;
7689
default:
7790
throw new Error('Unknown attribute name '+attrName+' in EntityType '+entityName);
7891
break;
7992
}
8093
}
8194

95+
EntityType.prototype.parseBooleanAttribute = function(xml, name) {
96+
var value = xml.value();
97+
if(value === 'true') {
98+
this[name] = true;
99+
}
100+
else if(value === 'false') {
101+
this[name] = false;
102+
}
103+
else {
104+
throw new Error('Unknown value '+value+' for attribute named '+name);
105+
}
106+
}
107+
82108
module.exports = EntityType;
83109
/* vim: set tabstop=2 shiftwidth=2 expandtab: */

lib/EnumType.js

+9-5
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,17 @@ function EnumType(metadata, xml) {
99
for(var i = 0; i < children.length; i++)
1010
{
1111
var elemType = children[i].type();
12-
if(elemType === 'element')
13-
{
12+
if(elemType === 'element') {
1413
this.parseElement(children[i], name);
1514
}
16-
else
17-
{
18-
throw new Error('Unknown element type in EnumType '+name+'!');
15+
else if(elemType === 'text') {
16+
var text = children[i].toString().trim();
17+
if(text.length !== 0) {
18+
throw new Error('Unknown text element in EnumType! Text = "'+text+'"');
19+
}
20+
}
21+
else {
22+
throw new Error('Unknown element type '+elemType+' in EnumType '+name+'!');
1923
}
2024
}
2125
var attributes = xml.attrs();

lib/Metadata.js

+16-5
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,30 @@ Metadata.prototype.parseString = function(string) {
5555
}
5656
}
5757

58+
Metadata.prototype.parseReference = function(reference) {
59+
//console.log(reference.toString());
60+
}
61+
5862
Metadata.prototype.parseSchema = function(schema) {
5963
var namespace = schema.attr('Namespace').value();
6064
var children = schema.childNodes();
6165
for(var i = 0; i < children.length; i++)
6266
{
6367
var elemType = children[i].type();
64-
if(elemType === 'element')
65-
{
68+
if(elemType === 'element') {
6669
this.parseElement(namespace, children[i]);
6770
}
68-
else
69-
{
70-
throw new Error('Unknown element type in schema!');
71+
else if(elemType === 'text') {
72+
var text = children[i].toString().trim();
73+
if(text.length !== 0) {
74+
throw new Error('Unknown text element in schema! Text = "'+text+'"');
75+
}
76+
}
77+
else if(elemType === 'comment') {
78+
//Ignore comments
79+
}
80+
else {
81+
throw new Error('Unknown element type '+elemType+' in schema!');
7182
}
7283
}
7384
}

lib/Property.js

+16-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
1+
const Annotation = require('./Annotation');
2+
13
function Property(entity, xml) {
24
var name = xml.attr('Name').value();
5+
this.Annotations = {};
36
var children = xml.childNodes();
47
for(var i = 0; i < children.length; i++)
58
{
69
var elemType = children[i].type();
7-
if(elemType === 'element')
8-
{
10+
if(elemType === 'element') {
911
this.parseElement(children[i], name);
1012
}
11-
else
12-
{
13-
throw new Error('Unknown element type in NavigationProperty '+name+'!');
13+
else if(elemType === 'text') {
14+
var text = children[i].toString().trim();
15+
if(text.length !== 0) {
16+
throw new Error('Unknown text element in schema! Text = "'+text+'"');
17+
}
18+
}
19+
else {
20+
throw new Error('Unknown element type '+elemType+' in Property '+name+'!');
1421
}
1522
}
1623
var attributes = xml.attrs();
@@ -25,6 +32,10 @@ function Property(entity, xml) {
2532
Property.prototype.parseElement = function(element, entityName) {
2633
var elemName = element.name();
2734
switch(elemName) {
35+
case 'Annotation':
36+
var name = element.attr('Term').value();
37+
this.Annotations[name] = new Annotation(element);
38+
break;
2839
default:
2940
throw new Error('Unknown element name '+elemName);
3041
break;

lib/Record.js

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
const assert = require('assert');
2+
3+
function Record(xml) {
4+
var children = xml.childNodes();
5+
this.PropertyValues = {};
6+
for(var i = 0; i < children.length; i++)
7+
{
8+
var elemType = children[i].type();
9+
if(elemType === 'element') {
10+
this.parseElement(children[i]);
11+
}
12+
else if(elemType === 'text') {
13+
var text = children[i].toString().trim();
14+
if(text.length !== 0) {
15+
throw new Error('Unknown text element in record! Text = "'+text+'"');
16+
}
17+
}
18+
else {
19+
throw new Error('Unknown element type '+elemType+' in Record!');
20+
}
21+
}
22+
var attributes = xml.attrs();
23+
for(var i = 0; i < attributes.length; i++)
24+
{
25+
this.parseAttribute(attributes[i]);
26+
}
27+
return this;
28+
}
29+
30+
Record.prototype.parseElement = function(element) {
31+
var elemName = element.name();
32+
switch(elemName) {
33+
case 'PropertyValue':
34+
var name = element.attr('Property').value();
35+
this.PropertyValues[name] = element.attr('String').value();
36+
break;
37+
default:
38+
throw new Error('Unknown element name '+elemName);
39+
break;
40+
}
41+
}
42+
43+
Record.prototype.parseAttribute = function(attribute) {
44+
var attrName = attribute.name();
45+
switch(attrName) {
46+
default:
47+
throw new Error('Unknown attribute name '+attrName+' in Record');
48+
break;
49+
}
50+
}
51+
52+
module.exports = Record;
53+
/* vim: set tabstop=2 shiftwidth=2 expandtab: */

0 commit comments

Comments
 (0)