Ejemplo n.º 1
0
    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()
Ejemplo n.º 2
0
    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
Ejemplo n.º 4
0
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
Ejemplo n.º 5
0
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