Example #1
0
def checkXML(ui, repo, node):
  """ Ensure than any XML files being commited are well-formed. """

  ctx = repo[node]
  status = repo.status(ctx.parents()[0].node(), ctx.node())
  changedOrAdded = status[0] + status[1]
  for filename in changedOrAdded:
    data = ctx[filename].data()
    if isbinary(data):
      continue
    if data.startswith("<?xml ") and not re.search("\.dtd", filename):
      try:
        # Get rid of DTD declaration, since we don't care if it exists or not.
        data = re.sub("<!DOCTYPE [^>]*>", "", data)

        # Now try to parse it
        sax.parseString(data, sax.ContentHandler())

      # Inform the user if anything goes wrong, and reject the file.
      except Exception as e:
        msg = str(e)
        msg = msg.replace("<unknown>:", "location ")
        return ruleError(ui, "XML file '%s' may not be well formed. %s: %s\n" % \
          (filename, e.__class__.__name__, msg))

  return True
Example #2
0
    def test_constructor(self):
        fun = sys._getframe().f_code.co_name
        print("Run: %s.%s() " % (self.__class__.__name__, fun))

        parser = sax.make_parser()
        handler = sax.ContentHandler()
        name = "datasource"
        attrs = {"type": "TANGO"}
        el = InnerXMLHandler(parser, handler, name, attrs)
        parser.setContentHandler(el)
        self.assertEqual(el.xml, None)

        inpsrc = sax.InputSource()
        inpsrc.setByteStream(StringIO('<device> Something</device>'))
        parser.parse(inpsrc)

        self.assertEqual(parser.getContentHandler(), el)

        self.assertEqual(el.endElement("datasource"), None)
        self.assertEqual(len(el.xml), 3)
        self.assertEqual(el.xml[0], '<datasource type="TANGO">')
        self.assertEqual(el.xml[1], '<device> Something</device>')
        self.assertEqual(el.xml[2], '</datasource>')

        self.assertEqual(parser.getContentHandler(), handler)
Example #3
0
    def test_XML_group_name(self):
        fun = sys._getframe().f_code.co_name
        print("Run: %s.%s() " % (self.__class__.__name__, fun))

        parser = sax.make_parser()
        handler = sax.ContentHandler()
        name = "datasource"

        el = InnerXMLHandler(parser, handler, name, {})
        parser.setContentHandler(el)
        self.assertEqual(el.xml, None)

        inpsrc = sax.InputSource()
        inpsrc.setByteStream(
            StringIO('<group type="NXentry" name="entry"></group>'))
        parser.parse(inpsrc)

        self.assertEqual(parser.getContentHandler(), el)

        self.assertEqual(el.endElement("datasource"), None)

        self.assertTrue(
            (el.xml
             == ('<datasource>', '<group type="NXentry" name="entry"></group>',
                 '</datasource>'))
            or (el.xml == ('<datasource>',
                           '<group name="entry" type="NXentry"></group>',
                           '</datasource>')))

        self.assertEqual(parser.getContentHandler(), handler)
Example #4
0
    def test_group_names(self):
        fun = sys._getframe().f_code.co_name
        print("Run: %s.%s() " % (self.__class__.__name__, fun))

        attr1 = {"name": "entry1", "type": "NXentry"}
        # sattr1 = {attr1["type"]: attr1["name"]}

        attr2 = {"name": "instrument", "type": "NXinstrument"}
        # sattr2 = {attr2["type"]: attr2["name"]}

        parser = sax.make_parser()
        handler = sax.ContentHandler()
        name = "group"
        attrs = {}
        el = InnerXMLHandler(parser, handler, name, attrs)
        parser.setContentHandler(el)
        self.assertEqual(el.xml, None)
        self.assertTrue(isinstance(el, sax.ContentHandler))
        self.assertEqual(el.startElement("group", attr1), None)
        self.assertEqual(el.endElement("group"), None)
        self.assertEqual(el.startElement("field", attr2), None)
        self.assertEqual(el.endElement("field"), None)
        self.assertEqual(el.endElement("group"), None)
        self.assertEqual(len(el.xml), 3)
        self.assertEqual(el.xml[0], '<group>')
        self.assertTrue(
            (el.xml[1] == '<group type="NXentry" name="entry1"></group>'
             '<field type="NXinstrument" name="instrument"></field>')
            or (el.xml[1] == '<group type="NXentry" name="entry1"></group>'
                '<field name="instrument" type="NXinstrument"></field>')
            or (el.xml[1] == '<group name="entry1" type="NXentry"></group>'
                '<field type="NXinstrument" name="instrument"></field>')
            or (el.xml[1] == '<group name="entry1" type="NXentry"></group>'
                '<field name="instrument" type="NXinstrument"></field>'))
        self.assertEqual(el.xml[2], '</group>')
Example #5
0
    def test_group_name(self):
        fun = sys._getframe().f_code.co_name
        print("Run: %s.%s() " % (self.__class__.__name__, fun))

        attr1 = {"name": "entry", "type": "NXentry"}
        # sattr1 = {attr1["type"]: attr1["name"]}

        parser = sax.make_parser()
        handler = sax.ContentHandler()
        name = "datasource"
        attrs = {}
        el = InnerXMLHandler(parser, handler, name, attrs)
        parser.setContentHandler(el)
        self.assertEqual(el.xml, None)
        self.assertTrue(isinstance(el, sax.ContentHandler))
        self.assertEqual(el.startElement("group", attr1), None)
        self.assertEqual(el.endElement("group"), None)
        self.assertEqual(el.endElement("datasource"), None)
        self.assertTrue(
            (el.xml
             == ('<datasource>', '<group type="NXentry" name="entry"></group>',
                 '</datasource>'))
            or (el.xml == ('<datasource>',
                           '<group name="entry" type="NXentry"></group>',
                           '</datasource>')))
Example #6
0
    def test_XML_group_group(self):
        fun = sys._getframe().f_code.co_name
        print("Run: %s.%s() " % (self.__class__.__name__, fun))

        parser = sax.make_parser()
        handler = sax.ContentHandler()
        attrs = {"type": "NXentry"}

        name = "group"
        xml = '<group type="NXentry" name="entry1">' + \
              '<group type="NXinstrument" name="instrument"></group></group>'

        el = InnerXMLHandler(parser, handler, name, attrs)
        parser.setContentHandler(el)
        self.assertEqual(el.xml, None)

        inpsrc = sax.InputSource()
        inpsrc.setByteStream(StringIO(xml))
        parser.parse(inpsrc)

        self.assertEqual(parser.getContentHandler(), el)
        self.assertEqual(el.endElement("group"), None)

        self.assertEqual(len(el.xml), 3)
        self.assertEqual(el.xml[0], '<group type="NXentry">')
        self.assertTrue(
            (el.xml[1] == '<group type="NXentry" name="entry1">'
             '<group type="NXinstrument" name="instrument"></group></group>')
            or
            (el.xml[1] == '<group type="NXentry" name="entry1">'
             '<group name="instrument" type="NXinstrument"></group></group>')
            or
            (el.xml[1] == '<group name="entry1" type="NXentry">'
             '<group type="NXinstrument" name="instrument"></group></group>')
            or
            (el.xml[1] == '<group name="entry1" type="NXentry">'
             '<group name="instrument" type="NXinstrument"></group></group>'))
        self.assertEqual(el.xml[2], '</group>')

        self.assertEqual(parser.getContentHandler(), handler)