Skip to content

Commit

Permalink
Detect old description formats when parsing component descriptions. See
Browse files Browse the repository at this point in the history
  • Loading branch information
aarranz committed Jul 22, 2016
1 parent a7a1110 commit ad1153e
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 13 deletions.
Binary file not shown.
11 changes: 10 additions & 1 deletion src/wirecloud/commons/tests/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import rdflib
from unittest import TestCase

from wirecloud.commons.utils.template.parsers import TemplateParser, TemplateParseException
from wirecloud.commons.utils.template.parsers import ObsoleteFormatError, TemplateFormatError, TemplateParser, TemplateParseException
from wirecloud.commons.utils.template.parsers.xml import WIRECLOUD_TEMPLATE_NS
from wirecloud.commons.utils.template.writers.json import write_json_description
from wirecloud.commons.utils.template.writers.rdf import write_rdf_description
Expand Down Expand Up @@ -1473,6 +1473,15 @@ def check_missing_xml_element(self, query):

self.assertRaises(TemplateParseException, TemplateParser, etree.tostring(document, method='xml', xml_declaration=True, encoding="UTF-8"))

def test_invalid_description_format(self):

self.assertRaises(TemplateFormatError, TemplateParser, b'invalid description format')

def test_deprecated_description_format(self):

json_description = read_template('old_description_format.xml')
self.assertRaises(ObsoleteFormatError, TemplateParser, json_description)

def test_json_parser_writer_basic_operator(self):

json_description = write_json_description(self.basic_operator_info)
Expand Down
10 changes: 10 additions & 0 deletions src/wirecloud/commons/utils/template/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ def __str__(self):
return text_type(self.msg)


class TemplateFormatError(TemplateParseException):
pass


class ObsoleteFormatError(TemplateFormatError):

def __init__(self):
super(ObsoleteFormatError, self).__init__('Component description uses a no longer supported format')


@python_2_unicode_compatible
class UnsupportedFeature(Exception):

Expand Down
14 changes: 12 additions & 2 deletions src/wirecloud/commons/utils/template/parsers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

import six

from wirecloud.commons.utils.template.base import TemplateParseException
from wirecloud.commons.utils.template.base import ObsoleteFormatError, TemplateFormatError, TemplateParseException
from wirecloud.commons.utils.template.parsers.json import JSONTemplateParser
from wirecloud.commons.utils.template.parsers.xml import ApplicationMashupTemplateParser
from wirecloud.commons.utils.template.parsers.rdf import RDFTemplateParser
Expand Down Expand Up @@ -55,12 +55,22 @@ def __init__(self, template, base=None):
for parser in self.parsers:
try:
self._parser = parser(template)
# We have found a valid parser for this document, stop
# searching
break
except TemplateParseException:
# A TemplateParseException means that the document uses the
# format associated with the parser (except when the
# ObsoleteFormatError is raised), but that something is not
# correct
raise
except:
# Any other exception means that the document cannot be read
# by the current parser, try the next one
pass

if self._parser is None:
raise TemplateParseException('No valid parser found')
raise TemplateFormatError('No valid parser found')

self._parser._init()

Expand Down
6 changes: 3 additions & 3 deletions src/wirecloud/commons/utils/template/parsers/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ def __init__(self, template, base=None):
elif isinstance(template, dict):
self._info = template
else:
raise TemplateParseException('Invalid input data')
raise ValueError('Invalid input data')

if 'type' not in self._info:
raise TemplateParseException(_('Missing resource type.'))
raise ValueError(_('Missing component type.'))

if self._info['type'] not in ('widget', 'operator', 'mashup'):
raise TemplateParseException(_('Invalid resource type: %s') % self._info['type'])
raise ValueError(_('Invalid component type: %s') % self._info['type'])

def _check_array_fields(self, fields, place=None, required=False):

Expand Down
6 changes: 3 additions & 3 deletions src/wirecloud/commons/utils/template/parsers/rdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,13 @@ def __init__(self, template):
root_element_qname = etree.QName(doc)

if root_element_qname.namespace is None:
raise TemplateParseException("XML document does not contain a valid rdf namespace")
raise ValueError("XML document does not contain a valid rdf namespace")

if root_element_qname.namespace != RDF_NS:
raise TemplateParseException("Invalid namespace: " + root_element_qname.namespace)
raise ValueError("Invalid namespace: " + root_element_qname.namespace)

if root_element_qname.localname != 'RDF':
raise TemplateParseException("Invalid root element: " + root_element_qname.localname)
raise ValueError("Invalid root element: " + root_element_qname.localname)

self._graph = rdflib.Graph()
self._graph.parse(data=template, format='xml')
Expand Down
13 changes: 9 additions & 4 deletions src/wirecloud/commons/utils/template/parsers/xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from django.utils.translation import ugettext as _
from six import text_type

from wirecloud.commons.utils.template.base import parse_contacts_info, TemplateParseException
from wirecloud.commons.utils.template.base import ObsoleteFormatError, parse_contacts_info, TemplateParseException
from wirecloud.commons.utils.translation import get_trans_index
from wirecloud.platform.wiring.utils import get_behaviour_skeleton, get_wiring_skeleton, parse_wiring_old_version

Expand All @@ -37,6 +37,7 @@
XMLSCHEMA = etree.XMLSchema(XMLSCHEMA_DOC)

WIRECLOUD_TEMPLATE_NS = 'http://wirecloud.conwet.fi.upm.es/ns/macdescription/1'
OLD_TEMPLATE_NAMESPACES = ('http://wirecloud.conwet.fi.upm.es/ns/template#', 'http://morfeo-project.org/2007/Template')

RESOURCE_DESCRIPTION_XPATH = 't:details'
DISPLAY_NAME_XPATH = 't:title'
Expand Down Expand Up @@ -100,7 +101,7 @@
class ApplicationMashupTemplateParser(object):

_doc = None
_resource_description = None
_component_description = None
_parsed = False

def __init__(self, template):
Expand All @@ -120,8 +121,12 @@ def __init__(self, template):
root_element_qname = etree.QName(self._doc)
xmlns = root_element_qname.namespace

if xmlns is not None and xmlns != WIRECLOUD_TEMPLATE_NS:
raise TemplateParseException("Invalid namespace: " + xmlns)
if xmlns is None:
raise ValueError("Missing document namespace")
elif xmlns in OLD_TEMPLATE_NAMESPACES:
raise ObsoleteFormatError()
elif xmlns != WIRECLOUD_TEMPLATE_NS:
raise ValueError("Invalid namespace: " + xmlns)

if root_element_qname.localname not in ('widget', 'operator', 'mashup'):
raise TemplateParseException("Invalid root element: " + root_element_qname.localname)
Expand Down

0 comments on commit ad1153e

Please sign in to comment.