Example #1
0
 def test_parse_InputSource(self):
     make_xml_file(self.data, 'iso-8859-1', None)
     with open(TESTFN, 'rb') as f:
         input = InputSource()
         input.setByteStream(f)
         input.setEncoding('iso-8859-1')
         self.check_parse(input)
Example #2
0
  def parse(self) :
    if (isinstance(self.source,unicode)) :
      # Create a string source
      file = io.StringIO(self.source)
      input = InputSource(file)
      input.setEncoding("utf-8")
      input.setCharacterStream(file)
      # There is a bug in xml.sax.saxutils.prepare_input_source
      input.setByteStream(file)
      input.setSystemId(None)
    elif (isinstance(self.source,InputSource)):
      input = self.source
    else:
      raise Exception("Parse source must be either string or InputSource")

    # Create the parser/xmlreader
    parser = xml.sax.make_parser()

    # Tell the parser to use our handler(s)
    parser.setContentHandler(self)
    #parser.setErrorHandler(self)

    #parser.setFeature(xml.sax.handler.feature_namespaces,True)
    # Shut off dtd validation
    parser.setFeature(xml.sax.handler.feature_validation,False)
    parser.setFeature(xml.sax.handler.feature_external_ges, False)

    # Parse the document
    parser.parse(input)
Example #3
0
 def test_parse_InputSource(self):
     # accept data without declared but with explicitly specified encoding
     make_xml_file(self.data, 'iso-8859-1', None)
     with open(TESTFN, 'rb') as f:
         input = InputSource()
         input.setByteStream(f)
         input.setEncoding('iso-8859-1')
         self.check_parse(input)
Example #4
0
 def test_parse_InputSource(self):
     # accept data without declared but with explicitly specified encoding
     make_xml_file(self.data, 'iso-8859-1', None)
     with open(TESTFN, 'rb') as f:
         input = InputSource()
         input.setByteStream(f)
         input.setEncoding('iso-8859-1')
         self.check_parse(input)
Example #5
0
 def parse(self, charSet, file):
     '''
     Parses the provided content.
     
     @param charSet: string
         The character set of the content.
     @param file: byte file
         The bytes file object providing the content.
     @return: object
         The object obtained from parsing.
     '''
     inpsrc = InputSource()
     inpsrc.setByteStream(file)
     inpsrc.setEncoding(charSet)
     return self.parseInputSource(inpsrc)
Example #6
0
 def parse(self, charSet, file):
     '''
     Parses the provided content.
     
     @param charSet: string
         The character set of the content.
     @param file: byte file
         The bytes file object providing the content.
     @return: object
         The object obtained from parsing.
     '''
     inpsrc = InputSource()
     inpsrc.setByteStream(file)
     inpsrc.setEncoding(charSet)
     return self.parseInputSource(inpsrc)
Example #7
0
 def parse(self, source, charSet, decoding, target):
     '''
     @see: ParseBaseHandler.parse
     '''
     assert isinstance(source, IInputStream), 'Invalid stream %s' % source
     assert isinstance(charSet, str), 'Invalid character set %s' % charSet
     assert isinstance(target, Target), 'Invalid target %s' % target
     
     parse = Parse(self.parser, decoding, target)
     self.parser.setContentHandler(parse)
     inpsrc = InputSource()
     inpsrc.setByteStream(source)
     inpsrc.setEncoding(charSet)
     try: self.parser.parse(source)
     except SAXParseException as e:
         assert isinstance(e, SAXParseException)
         addFailure(target, decoding, 'Bad XML content at line %(line)s and column %(column)s',
                    line=e.getLineNumber(), column=e.getColumnNumber())
Example #8
0
    def parse(self, decoder, data, source, charSet):
        '''
        @see: ParseBaseHandler.parse
        '''
        assert callable(decoder), 'Invalid decoder %s' % decoder
        assert isinstance(data, dict), 'Invalid data %s' % data
        assert isinstance(source, IInputStream), 'Invalid stream %s' % source
        assert isinstance(charSet, str), 'Invalid character set %s' % charSet

        parse = Parse(self.parser, decoder, data)
        self.parser.setContentHandler(parse)
        inpsrc = InputSource()
        inpsrc.setByteStream(source)
        inpsrc.setEncoding(charSet)
        try: self.parser.parse(source)
        except SAXParseException as e:
            assert isinstance(e, SAXParseException)
            return 'Bad XML content at line %s and column %s' % (e.getLineNumber(), e.getColumnNumber())
        except ParseError as e:
            assert isinstance(e, ParseError)
            return str(e)
Example #9
0
    def parse(self, source, charSet, decoding, target):
        '''
        @see: ParseBaseHandler.parse
        '''
        assert isinstance(source, IInputStream), 'Invalid stream %s' % source
        assert isinstance(charSet, str), 'Invalid character set %s' % charSet
        assert isinstance(target, Target), 'Invalid target %s' % target

        parse = Parse(self.parser, decoding, target)
        self.parser.setContentHandler(parse)
        inpsrc = InputSource()
        inpsrc.setByteStream(source)
        inpsrc.setEncoding(charSet)
        try:
            self.parser.parse(source)
        except SAXParseException as e:
            assert isinstance(e, SAXParseException)
            addFailure(
                target,
                decoding,
                'Bad XML content at line %(line)s and column %(column)s',
                line=e.getLineNumber(),
                column=e.getColumnNumber())