我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用xml.sax.xmlreader.InputSource()。
def test_expat_nsattrs_wattr(self): parser = create_parser(1) gather = self.AttrGatherer() parser.setContentHandler(gather) parser.feed("<doc xmlns:ns='%s' ns:attr='val'/>" % ns_uri) parser.close() attrs = gather._attrs self.assertEqual(attrs.getLength(), 1) self.assertEqual(attrs.getNames(), [(ns_uri, "attr")]) self.assertTrue((attrs.getQNames() == [] or attrs.getQNames() == ["ns:attr"])) self.assertEqual(len(attrs), 1) self.assertIn((ns_uri, "attr"), attrs) self.assertEqual(attrs.get((ns_uri, "attr")), "val") self.assertEqual(attrs.get((ns_uri, "attr"), 25), "val") self.assertEqual(list(attrs.items()), [((ns_uri, "attr"), "val")]) self.assertEqual(list(attrs.values()), ["val"]) self.assertEqual(attrs.getValue((ns_uri, "attr")), "val") self.assertEqual(attrs[(ns_uri, "attr")], "val") # ===== InputSource support
def test_expat_nsattrs_wattr(self): parser = create_parser(1) gather = self.AttrGatherer() parser.setContentHandler(gather) parser.feed("<doc xmlns:ns='%s' ns:attr='val'/>" % ns_uri) parser.close() attrs = gather._attrs self.assertEqual(attrs.getLength(), 1) self.assertEqual(attrs.getNames(), [(ns_uri, "attr")]) self.assertTrue((attrs.getQNames() == [] or attrs.getQNames() == ["ns:attr"])) self.assertEqual(len(attrs), 1) self.assertTrue(attrs.has_key((ns_uri, "attr"))) self.assertEqual(attrs.get((ns_uri, "attr")), "val") self.assertEqual(attrs.get((ns_uri, "attr"), 25), "val") self.assertEqual(attrs.items(), [((ns_uri, "attr"), "val")]) self.assertEqual(attrs.values(), ["val"]) self.assertEqual(attrs.getValue((ns_uri, "attr")), "val") self.assertEqual(attrs[(ns_uri, "attr")], "val") # ===== InputSource support
def __init__(self, namespaceHandling=0, bufsize=2**16-20): xmlreader.IncrementalParser.__init__(self, bufsize) self._source = xmlreader.InputSource() self._parser = None self._namespaces = namespaceHandling self._lex_handler_prop = None self._parsing = 0 self._entity_stack = [] self._external_ges = 1 self._interning = None # XMLReader methods
def parse(self, source): "Parse an XML document from a URL or an InputSource." source = saxutils.prepare_input_source(source) self._source = source self.reset() self._cont_handler.setDocumentLocator(ExpatLocator(self)) xmlreader.IncrementalParser.parse(self, source)
def resolveEntity(self, publicId, systemId): inpsrc = InputSource() inpsrc.setByteStream(StringIO("<entity/>")) return inpsrc
def test_expat_inpsource_sysid(self): parser = create_parser() result = StringIO() xmlgen = XMLGenerator(result) parser.setContentHandler(xmlgen) parser.parse(InputSource(TEST_XMLFILE)) self.assertEqual(result.getvalue(), xml_test_out)
def test_expat_inpsource_stream(self): parser = create_parser() result = StringIO() xmlgen = XMLGenerator(result) parser.setContentHandler(xmlgen) inpsrc = InputSource() with open(TEST_XMLFILE) as f: inpsrc.setByteStream(f) parser.parse(inpsrc) self.assertEqual(result.getvalue(), xml_test_out) # ===== IncrementalParser support
def test_expat_inpsource_location(self): parser = create_parser() parser.setContentHandler(ContentHandler()) # do nothing source = InputSource() source.setByteStream(StringIO("<foo bar foobar>")) #ill-formed name = "a file name" source.setSystemId(name) try: parser.parse(source) self.fail() except SAXException as e: self.assertEqual(e.getSystemId(), name)
def test_parse_InputSource(self): # accept data without declared but with explicitly specified encoding make_xml_file(self.data, 'iso-8859-1', None) with io.open(TESTFN, 'rb') as f: input = InputSource() input.setByteStream(f) input.setEncoding('iso-8859-1') self.check_parse(input)
def test_byte_stream(self): # If the source is an InputSource that does not have a character # stream but does have a byte stream, use the byte stream. src = InputSource(self.file) src.setByteStream(self.make_byte_stream()) prep = prepare_input_source(src) self.assertIsNone(prep.getCharacterStream()) self.checkContent(prep.getByteStream(), b"This is a byte stream.")
def test_system_id(self): # If the source is an InputSource that has neither a character # stream nor a byte stream, open the system ID. src = InputSource(self.file) prep = prepare_input_source(src) self.assertIsNone(prep.getCharacterStream()) self.checkContent(prep.getByteStream(), b"This was read from a file.")
def test_expat_inpsource_sysid_unicode(self): fname = support.TESTFN_UNICODE shutil.copyfile(TEST_XMLFILE, fname) self.addCleanup(support.unlink, fname) parser = create_parser() result = StringIO() xmlgen = XMLGenerator(result) parser.setContentHandler(xmlgen) parser.parse(InputSource(fname)) self.assertEqual(result.getvalue(), xml_test_out)
def test_expat_inpsource_byte_stream(self): parser = create_parser() result = StringIO() xmlgen = XMLGenerator(result) parser.setContentHandler(xmlgen) inpsrc = InputSource() inpsrc.setByteStream(open(TEST_XMLFILE)) parser.parse(inpsrc) self.assertEqual(result.getvalue(), xml_test_out) # ===== IncrementalParser support
def test_expat_inpsource_location(self): parser = create_parser() parser.setContentHandler(ContentHandler()) # do nothing source = InputSource() source.setByteStream(StringIO("<foo bar foobar>")) #ill-formed name = "a file name" source.setSystemId(name) try: parser.parse(source) self.fail() except SAXException, e: self.assertEqual(e.getSystemId(), name)
def resolveEntity(self, publicId, systemId): inpsrc = InputSource() inpsrc.setByteStream(BytesIO(b"<entity/>")) return inpsrc
def test_expat_inpsource_sysid(self): parser = create_parser() result = BytesIO() xmlgen = XMLGenerator(result) parser.setContentHandler(xmlgen) parser.parse(InputSource(TEST_XMLFILE)) self.assertEqual(result.getvalue(), xml_test_out)
def test_expat_inpsource_sysid_nonascii(self): fname = support.TESTFN_UNICODE shutil.copyfile(TEST_XMLFILE, fname) self.addCleanup(support.unlink, fname) parser = create_parser() result = BytesIO() xmlgen = XMLGenerator(result) parser.setContentHandler(xmlgen) parser.parse(InputSource(fname)) self.assertEqual(result.getvalue(), xml_test_out)
def test_expat_inpsource_stream(self): parser = create_parser() result = BytesIO() xmlgen = XMLGenerator(result) parser.setContentHandler(xmlgen) inpsrc = InputSource() with open(TEST_XMLFILE, 'rb') as f: inpsrc.setByteStream(f) parser.parse(inpsrc) self.assertEqual(result.getvalue(), xml_test_out) # ===== IncrementalParser support
def resolveEntity(self, publicId, systemId): for p in self.path: fname = os.path.join(p, systemId) if os.path.exists(fname): source = InputSource(systemId) source.setByteStream(open(fname)) return source return InputSource(systemId)