@@ -5,6 +5,15 @@ var libxml = require('libxmljs2');
55var request = require ( 'sync-request' ) ;
66var SCHEMA_URL = 'https://raw.githubusercontent.com/Wirecloud/wirecloud/master/src/wirecloud/commons/utils/template/schemas/xml_schema.xsd' ;
77
8+ /* jshint latedef:nofunc */
9+
10+ class TemplateParseException extends Error {
11+ constructor ( message ) {
12+ super ( message ) ;
13+ this . name = "TemplateParseException" ;
14+ }
15+ }
16+
817function getContent ( path ) {
918 try {
1019 return fs . readFileSync ( path ) . toString ( ) ;
@@ -26,7 +35,7 @@ ConfigParser.prototype.parseContent = function (content) {
2635 throw new Error ( 'Invalid config file format' ) ;
2736 }
2837 }
29- }
38+ } ;
3039
3140function ConfigParser ( options ) {
3241 if ( typeof options === 'string' ) {
@@ -35,7 +44,7 @@ function ConfigParser(options) {
3544
3645 var content = options . path ? getContent ( options . path ) : options . content ;
3746 this . data = this . parseContent ( content ) ;
38- if ( ! this . validate ( ) ) {
47+ if ( options . validate && ! this . validate ( ) ) {
3948 throw new Error ( 'Validation Error: Invalid config.xml file' ) ;
4049 }
4150}
@@ -55,11 +64,11 @@ ConfigParser.prototype.validate = function () {
5564 }
5665 else if ( this . type === "json" ) {
5766 try {
58- this . validateJson ( body ) ;
67+ this . validateJson ( ) ;
5968 return true ;
6069 }
6170 catch ( e ) {
62- throw e ;
71+ return false ;
6372 }
6473 }
6574
@@ -165,7 +174,7 @@ function checkContactsFields(fields, place, required = false) {
165174 }
166175
167176 place [ field ] = [ ] ;
168- } else if ( typeof place [ field ] === 'string' || Array . isArray ( place [ field ] ) || place [ field ] instanceof Tuple ) {
177+ } else if ( typeof place [ field ] === 'string' || Array . isArray ( place [ field ] ) ) {
169178
170179 } else {
171180 throw new TemplateParseException ( `${ field } field must be a list or string` ) ;
@@ -211,13 +220,21 @@ function checkComponentInfo(data, componentType) {
211220ConfigParser . prototype . validateJson = function ( ) {
212221 checkStringFields ( [ 'title' , 'description' , 'longdescription' , 'email' , 'homepage' , 'doc' , 'changelog' , 'image' , 'smartphoneimage' , 'license' , 'licenseurl' , 'issuetracker' ] , this . data ) ;
213222 checkContactsFields ( [ 'authors' , 'contributors' ] , this . data ) ;
223+ checkIntegerFields ( [ 'macversion' ] , this . data , false ) ;
224+ if ( ! ( 'macversion' in this . data ) ) {
225+ this . data [ 'macversion' ] = 1 ;
226+ }
227+ // Extra check for the macversion field, as it currently only supports 1 and 2
228+ if ( this . data [ 'macversion' ] !== 1 && this . data [ 'macversion' ] !== 2 ) {
229+ throw new TemplateParseException ( 'Invalid value for the macversion field (currently only 1 or 2 are supported)' ) ;
230+ }
214231 // TODO ???checkStringFields(['type'], this.data, true)
215232 // Normalize/check preferences and properties (only for widgets and operators)
216233 if ( this . data [ 'type' ] !== 'mashup' ) {
217234 checkArrayFields ( [ 'preferences' , 'properties' ] , this . data ) ;
218235 for ( let preference of this . data [ 'preferences' ] ) {
219236 checkStringFields ( [ 'name' , 'type' ] , preference , true ) ;
220- checkStringFields ( [ 'label' , 'description' , 'default' ] , reference ) ;
237+ checkStringFields ( [ 'label' , 'description' , 'default' ] , preference ) ;
221238 checkBooleanFields ( [ 'readonly' , 'secure' ] , preference ) ;
222239 checkStringFields ( [ 'value' ] , preference , undefined , true ) ;
223240 checkBooleanFields ( 'required' , preference ) ;
@@ -232,6 +249,10 @@ ConfigParser.prototype.validateJson = function () {
232249 }
233250
234251 if ( this . data [ 'type' ] === 'widget' ) {
252+ if ( this . data [ 'macversion' ] > 1 ) {
253+ checkStringFields ( [ 'entrypoint' ] , this . data , true ) ;
254+ }
255+
235256 checkArrayFields ( [ 'altcontents' ] , this . data ) ;
236257 if ( this . data [ 'contents' ] === null || typeof this . data [ 'contents' ] !== 'object' ) {
237258 throw new TemplateParseException ( 'Missing widget content info' ) ;
@@ -243,6 +264,10 @@ ConfigParser.prototype.validateJson = function () {
243264 checkContentsField ( altcontent ) ;
244265 }
245266 }
267+ } else if ( this . data [ 'type' ] === 'operator' ) {
268+ if ( this . data [ 'macversion' ] > 1 ) {
269+ checkStringFields ( [ 'entrypoint' ] , this . data , true ) ;
270+ }
246271 } else if ( this . data [ 'type' ] === 'mashup' ) {
247272 checkArrayFields ( [ 'params' , 'tabs' , 'embedded' ] , this . data ) ;
248273
@@ -317,20 +342,14 @@ ConfigParser.prototype.validateJson = function () {
317342 // Requirements
318343 checkArrayFields ( [ 'requirements' ] , this . data ) ;
319344 }
320- }
345+ } ;
321346
322- class TemplateParseException extends Error {
323- constructor ( message ) {
324- super ( message ) ;
325- this . name = "TemplateParseException" ;
326- }
327- }
328347ConfigParser . prototype . getData = function ( configFile ) {
329348 return {
330- name : this . data . root ( ) . _attr ( 'name' ) . value ( ) ,
331- vendor : this . data . root ( ) . _attr ( 'vendor' ) . value ( ) ,
332- version : this . data . root ( ) . _attr ( 'version' ) . value ( ) ,
333- type : this . data . root ( ) . name ( )
349+ name : ( this . type === 'xml' ) ? this . data . root ( ) . _attr ( 'name' ) . value ( ) : this . data [ 'name' ] ,
350+ vendor : ( this . type === 'xml' ) ? this . data . root ( ) . _attr ( 'vendor' ) . value ( ) : this . data [ 'vendor' ] ,
351+ version : ( this . type === 'xml' ) ? this . data . root ( ) . _attr ( 'version' ) . value ( ) : this . data [ 'version' ] ,
352+ type : ( this . type === 'xml' ) ? this . data . root ( ) . name ( ) : this . data [ 'type' ]
334353 } ;
335354} ;
336355
0 commit comments