def setDefaultsOnPit(self, pitFilename): ''' Create a new PIT file that has defaults set based on all sample files parsed. ''' # Load pit file uri = "file:" + pitFilename factory = InputSourceFactory(resolver=PeachResolver(), catalog=GetDefaultCatalog()) isrc = factory.fromUri(uri) doc = Ft.Xml.Domlette.NonvalidatingReader.parse(isrc) # Combine data into pit file print "[*] Setting defaults on XML document..." for k in self.crackedModels: self.setXmlFromPeach(doc, self.crackedModels[k]) # Output XML file print "[*] Writing combined PIT file to [%s]" % pitFilename + ".defaults" fd = open(pitFilename + ".defaults", "wb+") Ft.Xml.Domlette.PrettyPrint(doc, stream=fd) fd.close()
def setDefaultsOnPit(self, pitFilename): ''' Create a new PIT file that has defaults set based on all sample files parsed. ''' # Load pit file uri = "file:"+pitFilename factory = InputSourceFactory(resolver=PeachResolver(), catalog=GetDefaultCatalog()) isrc = factory.fromUri(uri) doc = Ft.Xml.Domlette.NonvalidatingReader.parse(isrc) # Combine data into pit file print "[*] Setting defaults on XML document..." for k in self.crackedModels: self.setXmlFromPeach(doc, self.crackedModels[k]) # Output XML file print "[*] Writing combined PIT file to [%s]" % pitFilename + ".defaults" fd = open(pitFilename + ".defaults", "wb+") Ft.Xml.Domlette.PrettyPrint(doc, stream=fd) fd.close()
def xml2Peach(self, url): factory = InputSourceFactory(resolver=_PeachResolver(), catalog=GetDefaultCatalog()) isrc = factory.fromUri(url) doc = Ft.Xml.Domlette.NonvalidatingReader.parse(isrc) peachDoc = Ft.Xml.Domlette.implementation.createDocument(EMPTY_NAMESPACE, None, None) self.handleElement(doc.firstChild, peachDoc) # Get the string representation import cStringIO buff = cStringIO.StringIO() PrettyPrint(peachDoc.firstChild, stream=buff, encoding="utf8") value = buff.getvalue() buff.close() return self.XmlContainer % value
def __init__(self, app_conf, app, **kw): self.ns = unicode(app_conf.get('xsltemplate_namespace', 'http://ionrock.org/ns/xsltemplate')) if app_conf.get('use_index_xml'): self.content = app_conf['use_index_xml'] self.template_key = XTemplate self.params_key = XParams self.source_key = XSource self.tdir = app_conf.get('template_directory', 'templates') self.resolver = LocalTemplateResolver() self.resolver.templates = self.tdir self.xslt_factory = InputSourceFactory(resolver=self.resolver) self.rs = '%s.xslt' self.app = app if kw.get('extensions'): self.extensions = kw['extensions'] else: self.extensions = None
def xml2Peach(self, url): factory = InputSourceFactory(resolver=_PeachResolver(), catalog=GetDefaultCatalog()) isrc = factory.fromUri(url) doc = Ft.Xml.Domlette.NonvalidatingReader.parse(isrc) peachDoc = Ft.Xml.Domlette.implementation.createDocument( EMPTY_NAMESPACE, None, None) child = doc.firstChild while (child.nodeName == "#comment"): child = child.nextSibling self.handleElement(child, peachDoc) # Get the string representation import cStringIO buff = cStringIO.StringIO() PrettyPrint(peachDoc.firstChild, stream=buff, encoding="utf8") value = buff.getvalue() buff.close() return self.XmlContainer % value
def parse_data(data, path): """Convert and XML document into a DOM Document.""" from Ft.Xml.InputSource import InputSourceFactory #from Ft.Xml.cDomlette import NonvalParse from Ft.Xml.FtMiniDom import NonvalParse isrc = InputSourceFactory() try: try: #print "Parsing (with entities)..." doc = NonvalParse(isrc.fromString(data, path)) except: print "Parse failed.. retry without entities..." data = entrefpattern.sub('&\\1;',data) doc = NonvalParse(isrc.fromString(data, path)) except: type, val, tb = sys.exc_info() traceback.print_exception(type, val, tb) print "parsing failed!" print "Data was:" print data #rox.report_exception() raise return doc
def Test(tester): tester.startGroup('XML and TR9401 Catalogs') tester.startTest('Parse an XML Catalog supplied via a URI') cat_path = os.path.join('Xml', 'Core', 'xcatalog.xml') cat_uri = Uri.OsPathToUri(cat_path, attemptAbsolute=1) cat = Catalog(cat_uri) tester.testDone() tf_path = os.path.join('Xml', 'Core', 'include1.xml') try: fd = open(tf_path) orig = fd.read() fd.close() except: fd.close() tester.warning('Could not read test file; skipping resolution tests.') return tester.startTest('Parse an XML Catalog supplied via an InputSource') fd = open(cat_path, 'rb') cat_data = fd.read() fd.close() isrc = NoCatalogFactory.fromString(cat_data, cat_uri) cat = Catalog() cat.isrc = isrc cat.parse() tester.testDone() # The catalog object should map 'urn:bogus:include1.xml' to # 'include1.xml' relative to the catalog's base URI. tester.startTest('Resolve a relative URI reference in the catalog (1)') expected = Uri.Absolutize('include1.xml', cat_uri) tester.compare(expected, cat.uris['urn:bogus:include1.xml']) tester.testDone() # The catalog object should map 'urn:bogus:include1-from-parent.xml' # to 'Core/include1.xml' relative to the catalog's base URI. tester.startTest('Resolve a relative URI reference in the catalog (2)') expected = Uri.Absolutize('Core/include1.xml', cat_uri) tester.compare(expected, cat.uris['urn:bogus:include1-from-parent.xml']) tester.testDone() # Same as above but we'll change the base URI of the catalog and # see if the results are still correct tester.startTest('Resolve a relative URI reference in the catalog (3)') alt_cat_path = os.path.join('Xml', 'xcatalog.xml') alt_cat_uri = Uri.OsPathToUri(alt_cat_path, attemptAbsolute=1) alt_cat_isrc = NoCatalogFactory.fromString(cat_data, alt_cat_uri) alt_cat = Catalog() alt_cat.isrc = alt_cat_isrc alt_cat.uri = alt_cat_uri alt_cat.parse() expected = Uri.Absolutize('Core/include1.xml', alt_cat_uri) tester.compare(expected, alt_cat.uris['urn:bogus:include1-from-parent.xml']) tester.testDone() # Make sure the default catalog exists and isn't empty tester.startTest('4Suite default catalog exists') from Ft.Xml.Catalog import FT_CATALOG entries = FT_CATALOG.publicIds or FT_CATALOG.uris tester.compare(True, len(entries) > 0) tester.testDone() # A test of catalog support in Ft.Xml.InputSource # (actually try to resolve a document URI with a catalog) tester.startTest( 'Resolve doc via InputSource w/Catalog containing rel. URI (1)') factory = InputSourceFactory(catalog=cat) isrc = factory.fromUri('urn:bogus:include1.xml') data = isrc.read() isrc.close() tester.compare(orig, data) tester.testDone() # A test of catalog support in Ft.Xml.InputSource # (same as previous, just with the different catalog base URI) tester.startTest( 'Resolve doc via InputSource w/Catalog containing rel. URI (2)') factory = InputSourceFactory(catalog=alt_cat) isrc = factory.fromUri('urn:bogus:include1-from-parent.xml') data = isrc.read() isrc.close() tester.compare(orig, data) tester.testDone() # A test of catalog support in Ft.Xml.InputSource # and resolving all combinations of Public and System IDs tester.startTest('Resolve ext. entities w/Catalog') xml_uri = Uri.Absolutize('test_catalog_INTERNAL.xml', cat_uri) factory = InputSourceFactory(catalog=cat) isrc = factory.fromString(XML_1, xml_uri) doc = Domlette.NonvalidatingReader.parse(isrc) st = cStringIO.StringIO() Domlette.Print(doc, stream=st) tester.compare(XML_EXPECTED_1, st.getvalue(), func=TreeCompare) tester.testDone() tester.groupDone() return
def Test(tester): tester.startGroup('XML and TR9401 Catalogs') tester.startTest('Parse an XML Catalog supplied via a URI') cat_path = os.path.join('Xml','Core','xcatalog.xml') cat_uri = Uri.OsPathToUri(cat_path, attemptAbsolute=1) cat = Catalog(cat_uri) tester.testDone() tf_path = os.path.join('Xml','Core','include1.xml') try: fd = open(tf_path) orig = fd.read() fd.close() except: fd.close() tester.warning('Could not read test file; skipping resolution tests.') return tester.startTest('Parse an XML Catalog supplied via an InputSource') fd = open(cat_path, 'rb') cat_data = fd.read() fd.close() isrc = NoCatalogFactory.fromString(cat_data, cat_uri) cat = Catalog() cat.isrc = isrc cat.parse() tester.testDone() # The catalog object should map 'urn:bogus:include1.xml' to # 'include1.xml' relative to the catalog's base URI. tester.startTest('Resolve a relative URI reference in the catalog (1)') expected = Uri.Absolutize('include1.xml', cat_uri) tester.compare(expected, cat.uris['urn:bogus:include1.xml']) tester.testDone() # The catalog object should map 'urn:bogus:include1-from-parent.xml' # to 'Core/include1.xml' relative to the catalog's base URI. tester.startTest('Resolve a relative URI reference in the catalog (2)') expected = Uri.Absolutize('Core/include1.xml', cat_uri) tester.compare(expected, cat.uris['urn:bogus:include1-from-parent.xml']) tester.testDone() # Same as above but we'll change the base URI of the catalog and # see if the results are still correct tester.startTest('Resolve a relative URI reference in the catalog (3)') alt_cat_path = os.path.join('Xml','xcatalog.xml') alt_cat_uri = Uri.OsPathToUri(alt_cat_path, attemptAbsolute=1) alt_cat_isrc = NoCatalogFactory.fromString(cat_data, alt_cat_uri) alt_cat = Catalog() alt_cat.isrc = alt_cat_isrc alt_cat.uri = alt_cat_uri alt_cat.parse() expected = Uri.Absolutize('Core/include1.xml', alt_cat_uri) tester.compare(expected, alt_cat.uris['urn:bogus:include1-from-parent.xml']) tester.testDone() # Make sure the default catalog exists and isn't empty tester.startTest('4Suite default catalog exists') from Ft.Xml.Catalog import FT_CATALOG entries = FT_CATALOG.publicIds or FT_CATALOG.uris tester.compare(True, len(entries) > 0) tester.testDone() # A test of catalog support in Ft.Xml.InputSource # (actually try to resolve a document URI with a catalog) tester.startTest('Resolve doc via InputSource w/Catalog containing rel. URI (1)') factory = InputSourceFactory(catalog=cat) isrc = factory.fromUri('urn:bogus:include1.xml') data = isrc.read() isrc.close() tester.compare(orig, data) tester.testDone() # A test of catalog support in Ft.Xml.InputSource # (same as previous, just with the different catalog base URI) tester.startTest('Resolve doc via InputSource w/Catalog containing rel. URI (2)') factory = InputSourceFactory(catalog=alt_cat) isrc = factory.fromUri('urn:bogus:include1-from-parent.xml') data = isrc.read() isrc.close() tester.compare(orig, data) tester.testDone() # A test of catalog support in Ft.Xml.InputSource # and resolving all combinations of Public and System IDs tester.startTest('Resolve ext. entities w/Catalog') xml_uri = Uri.Absolutize('test_catalog_INTERNAL.xml', cat_uri) factory = InputSourceFactory(catalog=cat) isrc = factory.fromString(XML_1, xml_uri) doc = Domlette.NonvalidatingReader.parse(isrc) st = cStringIO.StringIO() Domlette.Print(doc, stream=st) tester.compare(XML_EXPECTED_1, st.getvalue(), func=TreeCompare) tester.testDone() tester.groupDone() return
def Test(tester): source = test_harness.FileInfo(uri="Xml/Xslt/Core/addr_book1.xml") sty = test_harness.FileInfo(string=sheet_str_1) test_harness.XsltTest(tester, source, [sty], expected_1, title='xsl:include') styfactory = InputSourceFactory(resolver=TestResolver()) source = test_harness.FileInfo(string=DUMMY_XML) sty = test_harness.FileInfo(uri=OsPathToUri('self-include.xsl')) test_harness.XsltTest(tester, source, [sty], None, title='circular xsl:include (direct)', exceptionCode=Error.CIRCULAR_INCLUDE, stylesheetInputFactory=styfactory) source = test_harness.FileInfo(string=DUMMY_XML) sty = test_harness.FileInfo(uri=OsPathToUri('circ-A.xsl')) test_harness.XsltTest(tester, source, [sty], None, title='circular xsl:include (indirect)', exceptionCode=Error.CIRCULAR_INCLUDE, stylesheetInputFactory=styfactory) # conflicting match templates can raise an exception or produce a result source = test_harness.FileInfo(string=DUMMY_XML) sty = test_harness.FileInfo(uri=OsPathToUri('dup-include-match.xsl')) test_harness.XsltTest( tester, source, [sty], 'last', title='non-circular duplicate include of match templates (direct)', exceptionCode=Error.MULTIPLE_MATCH_TEMPLATES, stylesheetInputFactory=styfactory) # conflicting match templates can raise an exception or produce a result source = test_harness.FileInfo(string=DUMMY_XML) sty = test_harness.FileInfo(uri=OsPathToUri('dup-match.xsl')) test_harness.XsltTest( tester, source, [sty], 'last', title='include of conflicting match templates (direct)', exceptionCode=Error.MULTIPLE_MATCH_TEMPLATES, stylesheetInputFactory=styfactory) # conflicting match templates can raise an exception or produce a result source = test_harness.FileInfo(string=DUMMY_XML) sty = test_harness.FileInfo(uri=OsPathToUri('D.xsl')) test_harness.XsltTest( tester, source, [sty], 'hello world', title='non-circular duplicate include of match templates (indirect)', exceptionCode=Error.MULTIPLE_MATCH_TEMPLATES, stylesheetInputFactory=styfactory) # dup named templates must raise exception source = test_harness.FileInfo(string=DUMMY_XML) sty = test_harness.FileInfo(uri=OsPathToUri('dup-include-named.xsl')) test_harness.XsltTest( tester, source, [sty], None, title='non-circular duplicate include of named templates (direct)', exceptionCode=Error.DUPLICATE_NAMED_TEMPLATE, stylesheetInputFactory=styfactory) # dup top-level vars must raise exception source = test_harness.FileInfo(string=DUMMY_XML) sty = test_harness.FileInfo(uri=OsPathToUri('dup-include-var.xsl')) test_harness.XsltTest( tester, source, [sty], None, title='non-circular duplicate include of top-level vars (direct)', exceptionCode=Error.DUPLICATE_TOP_LEVEL_VAR, stylesheetInputFactory=styfactory) source = test_harness.FileInfo(string=DUMMY_XML) sty = test_harness.FileInfo(uri=OsPathToUri('xinc.xsl'), processIncludes=True) test_harness.XsltTest(tester, source, [sty], XINC_EXPECTED_1, title='process XInclude in stylesheet', stylesheetInputFactory=styfactory) source = test_harness.FileInfo(string=DUMMY_XML) sty = test_harness.FileInfo(uri=OsPathToUri('xinc.xsl'), processIncludes=False) test_harness.XsltTest(tester, source, [sty], XINC_EXPECTED_2, title='ignore XInclude in stylesheet', stylesheetInputFactory=styfactory) source = test_harness.FileInfo(string=DUMMY_XML) sty = test_harness.FileInfo(uri=OsPathToUri('circ-xinclude.xsl'), processIncludes=True) test_harness.XsltTest(tester, source, [sty], None, title='circular include from XInclude', exceptionCode=Error.STYLESHEET_PARSE_ERROR, stylesheetInputFactory=styfactory) return
class TemplateMiddleware(object): def __init__(self, app_conf, app, **kw): self.ns = unicode(app_conf.get('xsltemplate_namespace', 'http://ionrock.org/ns/xsltemplate')) if app_conf.get('use_index_xml'): self.content = app_conf['use_index_xml'] self.template_key = XTemplate self.params_key = XParams self.source_key = XSource self.tdir = app_conf.get('template_directory', 'templates') self.resolver = LocalTemplateResolver() self.resolver.templates = self.tdir self.xslt_factory = InputSourceFactory(resolver=self.resolver) self.rs = '%s.xslt' self.app = app if kw.get('extensions'): self.extensions = kw['extensions'] else: self.extensions = None def start_response(self, status, headers, exc_info=None): self.status = status self.headers = headers self.exc_info = exc_info def __call__(self, environ, start_response): source = ''.join(self.app(environ, self.start_response)) if not source and self.content: source = self.content if environ.get(self.template_key): xslt = environ[self.template_key] params = environ.get(self.params_key, {}) source = self.do_render(source, xslt, params) for i, value in enumerate(self.headers): k, v = value if k.lower() == 'content-length': del self.headers[i] start_response(self.status, self.headers, self.exc_info) return [source] def get_processor(self): proc = Processor() if self.extensions: for ext in self.extensions: proc.registerExtensionFunction(*(ext)) return proc def get(self, fn): if fn.startswith('pkg://'): package, sep, path = fn[6:].partition('#') if pkg_resources.resource_exists(package, path): return self.xslt_factory.fromString(pkg_resources.resource_string(package, path)) path = Uri.OsPathToUri(os.path.join(self.tdir, fn)) try: xslt = self.xslt_factory.fromUri(path) except UriException, e: xslt = self.xslt_factory.fromString( fn, Uri.OsPathToUri(os.path.abspath('.')) ) return xslt
def __init__(self, path, root_program = None, dome_data = None, do_load = 1): "If root_program is given, then no data is loaded (used for lock_and_copy)." self.uri = 'Prog.dome' import Namespaces self.namespaces = Namespaces.Namespaces() if dome_data: from Ft.Xml.InputSource import InputSourceFactory isrc = InputSourceFactory() dome_data = NonvalParse(isrc.fromUri(dome_data)) self.clear_undo() doc = None if path: if path != '-': self.uri = path if do_load and (path.endswith('.html') or path.endswith('.htm')): doc = self.load_html(path) if not doc and not root_program: from Ft.Xml.InputSource import InputSourceFactory isrc = InputSourceFactory() try: doc = NonvalParse(isrc.fromUri(path)) except: import rox rox.report_exception() if not doc: doc = implementation.createDocument(None, 'root', None) root = doc.documentElement self.root_program = None data_to_load = None from Program import Program, load_dome_program print root.namespaceURI, root.localName if root.namespaceURI == constants.DOME_NS and root.localName == 'dome': for x in root.childNodes: if x.namespaceURI == constants.DOME_NS: if x.localName == 'namespaces': self.load_ns(x) elif x.localName == 'dome-program': self.root_program = load_dome_program(x, self.namespaces) elif x.localName == 'dome-data': for y in x.childNodes: if y.nodeType == Node.ELEMENT_NODE: data_to_load = y if dome_data: data_to_load = dome_data.documentElement elif (root.namespaceURI == constants.XSLT_NS and root.localName in ['stylesheet', 'transform']) or \ root.hasAttributeNS(constants.XSLT_NS, 'version'): import xslt self.root_program = xslt.import_sheet(doc) self.doc = implementation.createDocument(None, 'xslt', None) data_to_load = None src = self.doc.createElementNS(None, 'Source') if dome_data: src.appendChild(self.import_with_ns(dome_data.documentElement)) self.doc.documentElement.appendChild(src) self.doc.documentElement.appendChild(self.doc.createElementNS(None, 'Result')) self.strip_space() data_to_load = None dome_data = None else: data_to_load = root if root_program: self.root_program = root_program else: if not self.root_program: self.root_program = Program('Root') if data_to_load: self.doc = implementation.createDocument(None, 'root', None) if not root_program: node = self.import_with_ns(data_to_load) self.doc.replaceChild(node, self.doc.documentElement) self.strip_space() self.views = [] # Notified when something changes self.locks = {} # Node -> number of locks self.hidden = {} # Node -> Message self.hidden_code = {} # XPath to generate Message, if any