Skip to content

Commit

Permalink
Add fixes found trying to parse Redfish style CSDL
Browse files Browse the repository at this point in the history
  • Loading branch information
pboyd04 committed Nov 15, 2016
1 parent 8e5230a commit 306f7f2
Show file tree
Hide file tree
Showing 10 changed files with 208 additions and 48 deletions.
30 changes: 25 additions & 5 deletions lib/Annotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@ function Annotation(xml) {
for(var i = 0; i < children.length; i++)
{
var elemType = children[i].type();
if(elemType === 'element')
{
if(elemType === 'element') {
this.parseElement(children[i], name);
}
else
{
throw new Error('Unknown element type in Annotation '+name+'!');
else if(elemType === 'text') {
var text = children[i].toString().trim();
if(text.length !== 0) {
throw new Error('Unknown text element in schema! Text = "'+text+'"');
}
}
else {
throw new Error('Unknown element '+elemType+' type in Annotation '+name+'!');
}
}
var attributes = xml.attrs();
Expand Down Expand Up @@ -42,6 +46,22 @@ Annotation.prototype.parseAttribute = function(attribute, entityName) {
case 'Term':
//Already used... drop on floor
break;
case 'Bool':
var value = attribute.value();
if(value === 'true') {
this.Bool = true;
}
else if(value === 'false') {
this.Bool = false;
}
else {
throw new Error('Unknown boolean value '+value);
}
break;
case 'String':
case 'EnumMember':
this[attrName] = attribute.value();
break;
default:
throw new Error('Unknown attribute name '+attrName+' in Annotation '+entityName);
break;
Expand Down
22 changes: 17 additions & 5 deletions lib/Collection.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
const assert = require('assert');

const Record = require('./Record');

function Collection(xml) {
var children = xml.childNodes();
for(var i = 0; i < children.length; i++)
{
var elemType = children[i].type();
if(elemType === 'element')
{
if(elemType === 'element') {
this.parseElement(children[i]);
}
else
{
throw new Error('Unknown element type in Collection!');
else if(elemType === 'text') {
var text = children[i].toString().trim();
if(text.length !== 0) {
throw new Error('Unknown text element in schema! Text = "'+text+'"');
}
}
else {
throw new Error('Unknown element type '+elemType+' in Collection!');
}
}
var attributes = xml.attrs();
Expand All @@ -31,6 +37,12 @@ Collection.prototype.parseElement = function(element) {
}
this.PropertyPaths.push(element.text());
break;
case 'Record':
if(this.Records === undefined) {
this.Records = [];
}
this.Records.push(new Record(element));
break;
default:
throw new Error('Unknown element name '+elemName);
break;
Expand Down
31 changes: 20 additions & 11 deletions lib/ComplexType.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
const assert = require('assert');

const Annotation = require('./Annotation');
const Property = require('./Property');
const NavigationProperty = require('./NavigationProperty');

function ComplexType(metadata, xml) {
this._metadata = metadata;
this.Properties = {};
this.Annotations = {};
var name = xml.attr('Name').value();
var children = xml.childNodes();
for(var i = 0; i < children.length; i++)
{
var elemType = children[i].type();
if(elemType === 'element')
{
if(elemType === 'element') {
this.parseElement(children[i], name);
}
else
{
throw new Error('Unknown element type in EntityType '+name+'!');
else if(elemType === 'text') {
var text = children[i].toString().trim();
if(text.length !== 0) {
throw new Error('Unknown text element in ComplexType! Text = "'+text+'"');
}
}
else {
throw new Error('Unknown element type in EntityType '+elemType+'!');
}
}
var attributes = xml.attrs();
Expand All @@ -37,7 +43,11 @@ ComplexType.prototype.parseElement = function(element, entityName) {
break;
case 'NavigationProperty':
var name = element.attr('Name').value();
this.NavigationProperty[name] = new NavigationProperty(this, element);
this.Properties[name] = new NavigationProperty(this, element);
break;
case 'Annotation':
var name = element.attr('Term').value();
this.Annotations[name] = new Annotation(element);
break;
default:
throw new Error('Unknown element name '+elemName);
Expand All @@ -54,18 +64,17 @@ ComplexType.prototype.parseAttribute = function(attribute, entityName) {
case 'BaseType':
this.BaseType = attribute.value();
break;
case 'Abstract':
this.parseBooleanAttribute(attribute, attrName);
break;
default:
throw new Error('Unknown attribute name '+attrName+' in ComplexType '+entityName);
break;
}
}

ComplexType.prototype.parseBooleanAttribute = function(xml, name) {
var attr = xml.attr(name);
if(attr === null) {
return;
}
var value = attr.value();
var value = xml.value();
if(value === 'true') {
this[name] = true;
}
Expand Down
36 changes: 31 additions & 5 deletions lib/EntityType.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
const assert = require('assert');

const Annotation = require('./Annotation');
const Property = require('./Property');
const NavigationProperty = require('./NavigationProperty');

function EntityType(metadata, xml) {
this._metadata = metadata;
this._key = null;
this.Annotations = {};
this.Properties = {};
var name = xml.attr('Name').value();
var children = xml.childNodes();
for(var i = 0; i < children.length; i++)
{
var elemType = children[i].type();
if(elemType === 'element')
{
if(elemType === 'element') {
this.parseElement(children[i], name);
}
else
{
throw new Error('Unknown element type in EntityType '+name+'!');
else if(elemType === 'text') {
var text = children[i].toString().trim();
if(text.length !== 0) {
throw new Error('Unknown text element in EntityType! Text = "'+text+'"');
}
}
else {
throw new Error('Unknown element type '+elemType+' in EntityType '+name+'!');
}
}
var attributes = xml.attrs();
Expand Down Expand Up @@ -58,6 +64,10 @@ EntityType.prototype.parseElement = function(element, entityName) {
var name = element.attr('Name').value();
this.Properties[name] = new NavigationProperty(this, element);
break;
case 'Annotation':
var name = element.attr('Term').value();
this.Annotations[name] = new Annotation(element);
break;
default:
throw new Error('Unknown element name '+elemName);
break;
Expand All @@ -73,11 +83,27 @@ EntityType.prototype.parseAttribute = function(attribute, entityName) {
case 'BaseType':
this.BaseType = attribute.value();
break;
case 'Abstract':
this.parseBooleanAttribute(attribute, attrName);
break;
default:
throw new Error('Unknown attribute name '+attrName+' in EntityType '+entityName);
break;
}
}

EntityType.prototype.parseBooleanAttribute = function(xml, name) {
var value = xml.value();
if(value === 'true') {
this[name] = true;
}
else if(value === 'false') {
this[name] = false;
}
else {
throw new Error('Unknown value '+value+' for attribute named '+name);
}
}

module.exports = EntityType;
/* vim: set tabstop=2 shiftwidth=2 expandtab: */
14 changes: 9 additions & 5 deletions lib/EnumType.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@ function EnumType(metadata, xml) {
for(var i = 0; i < children.length; i++)
{
var elemType = children[i].type();
if(elemType === 'element')
{
if(elemType === 'element') {
this.parseElement(children[i], name);
}
else
{
throw new Error('Unknown element type in EnumType '+name+'!');
else if(elemType === 'text') {
var text = children[i].toString().trim();
if(text.length !== 0) {
throw new Error('Unknown text element in EnumType! Text = "'+text+'"');
}
}
else {
throw new Error('Unknown element type '+elemType+' in EnumType '+name+'!');
}
}
var attributes = xml.attrs();
Expand Down
21 changes: 16 additions & 5 deletions lib/Metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,30 @@ Metadata.prototype.parseString = function(string) {
}
}

Metadata.prototype.parseReference = function(reference) {
//console.log(reference.toString());
}

Metadata.prototype.parseSchema = function(schema) {
var namespace = schema.attr('Namespace').value();
var children = schema.childNodes();
for(var i = 0; i < children.length; i++)
{
var elemType = children[i].type();
if(elemType === 'element')
{
if(elemType === 'element') {
this.parseElement(namespace, children[i]);
}
else
{
throw new Error('Unknown element type in schema!');
else if(elemType === 'text') {
var text = children[i].toString().trim();
if(text.length !== 0) {
throw new Error('Unknown text element in schema! Text = "'+text+'"');
}
}
else if(elemType === 'comment') {
//Ignore comments
}
else {
throw new Error('Unknown element type '+elemType+' in schema!');
}
}
}
Expand Down
21 changes: 16 additions & 5 deletions lib/Property.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
const Annotation = require('./Annotation');

function Property(entity, xml) {
var name = xml.attr('Name').value();
this.Annotations = {};
var children = xml.childNodes();
for(var i = 0; i < children.length; i++)
{
var elemType = children[i].type();
if(elemType === 'element')
{
if(elemType === 'element') {
this.parseElement(children[i], name);
}
else
{
throw new Error('Unknown element type in NavigationProperty '+name+'!');
else if(elemType === 'text') {
var text = children[i].toString().trim();
if(text.length !== 0) {
throw new Error('Unknown text element in schema! Text = "'+text+'"');
}
}
else {
throw new Error('Unknown element type '+elemType+' in Property '+name+'!');
}
}
var attributes = xml.attrs();
Expand All @@ -25,6 +32,10 @@ function Property(entity, xml) {
Property.prototype.parseElement = function(element, entityName) {
var elemName = element.name();
switch(elemName) {
case 'Annotation':
var name = element.attr('Term').value();
this.Annotations[name] = new Annotation(element);
break;
default:
throw new Error('Unknown element name '+elemName);
break;
Expand Down
53 changes: 53 additions & 0 deletions lib/Record.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const assert = require('assert');

function Record(xml) {
var children = xml.childNodes();
this.PropertyValues = {};
for(var i = 0; i < children.length; i++)
{
var elemType = children[i].type();
if(elemType === 'element') {
this.parseElement(children[i]);
}
else if(elemType === 'text') {
var text = children[i].toString().trim();
if(text.length !== 0) {
throw new Error('Unknown text element in record! Text = "'+text+'"');
}
}
else {
throw new Error('Unknown element type '+elemType+' in Record!');
}
}
var attributes = xml.attrs();
for(var i = 0; i < attributes.length; i++)
{
this.parseAttribute(attributes[i]);
}
return this;
}

Record.prototype.parseElement = function(element) {
var elemName = element.name();
switch(elemName) {
case 'PropertyValue':
var name = element.attr('Property').value();
this.PropertyValues[name] = element.attr('String').value();
break;
default:
throw new Error('Unknown element name '+elemName);
break;
}
}

Record.prototype.parseAttribute = function(attribute) {
var attrName = attribute.name();
switch(attrName) {
default:
throw new Error('Unknown attribute name '+attrName+' in Record');
break;
}
}

module.exports = Record;
/* vim: set tabstop=2 shiftwidth=2 expandtab: */
Loading

0 comments on commit 306f7f2

Please sign in to comment.