def Test(tester): tester.startGroup("instantiation of runNode") base = Uri.OsPathToUri(__file__, attemptAbsolute=True) src_uri = Uri.Absolutize('addr_book1.xml', base) sty_uri = Uri.Absolutize('addr_book1.xsl', base) tester.startTest("without whitespace preservation") p = Processor.Processor() node = Domlette.NonvalidatingReader.parseUri(src_uri) isrc = InputSource.DefaultFactory.fromUri(sty_uri) p.appendStylesheet(isrc) res = p.runNode(node, src_uri, ignorePis=True, preserveSrc=False) tester.compare(expected, res, func=TreeCompare.TreeCompare) tester.testDone() tester.startTest("with whitespace preservation") p = Processor.Processor() node = Domlette.NonvalidatingReader.parseUri(src_uri) isrc = InputSource.DefaultFactory.fromUri(sty_uri) p.appendStylesheet(isrc) res = p.runNode(node, src_uri, ignorePis=True, preserveSrc=True) tester.compare(expected, res, func=TreeCompare.TreeCompare) tester.testDone() tester.groupDone() return
def _compile_xsl(XSLT_FILE, XSLT_COMPILED_FILE): """ compiling docbook stylesheet reference: http://155.210.85.193:8010/ccia/nodes/2005-03-18/compileXslt?xslt=/akara/akara.xslt """ from Ft.Xml.Xslt.Processor import Processor from Ft.Xml.Xslt import Stylesheet from Ft.Xml import InputSource from Ft.Lib import Uri # New docbook processor db_processor = Processor() # Docbook Stylesheet my_sheet_uri = Uri.OsPathToUri(XSLT_FILE, 1) sty_isrc = InputSource.DefaultFactory.fromUri(my_sheet_uri) # Append Stylesheet db_processor.appendStylesheet(sty_isrc) # Pickled stylesheet will be self.abs_db_compiled_xsl file db_root = db_processor.stylesheet.root fw = file(XSLT_COMPILED_FILE, 'wb') cPickle.dump(db_root, fw) # , protocol=2) fw.close()
def Test(tester): base = Uri.OsPathToUri(__file__, attemptAbsolute=True) tester.startGroup('Simple XLinks') from Ft.Xml.XLink.Processor import Processor tester.startTest('Process w/xlink:show="embed"') uri1 = Uri.Absolutize('addr_book1.xml', base) isrc = InputSource.DefaultFactory.fromUri(uri1) p = Processor() doc = p.run(isrc) stream = cStringIO.StringIO() Print(doc, stream) actual = stream.getvalue() tester.compare(EXPECTED_1, actual, func=TreeCompare.TreeCompare) tester.testDone() tester.startTest('Process w/xlink:show="replace"') uri3 = Uri.Absolutize('addr_book3.xml', base) isrc = InputSource.DefaultFactory.fromUri(uri3) p = Processor() doc = p.run(isrc) stream = cStringIO.StringIO() Print(doc, stream) actual = stream.getvalue() tester.compare(EXPECTED_2, actual, func=TreeCompare.TreeCompare) tester.testDone() return tester.groupDone()
def Test(tester): tester.startGroup('Exercise namespace nodes') isrc = InputSource.DefaultFactory.fromString(SRC_1, Uri.OsPathToUri(os.getcwd())) doc = NonvalidatingReader.parse(isrc) con = Context.Context(doc, 1, 1) EXPR = '//namespace::node()' expr = Compile(EXPR) #expr is <AbbreviatedAbsoluteLocationPath: /descendant-or-self::node()/namespace::node()> #expr._rel is <Step: namespace::node()> #expr._step is <Step: descendant-or-self::node()> tester.startTest(EXPR) actual = expr.evaluate(con) tester.compare(7, len(actual)) tester.testDone() EXPR = '//node()/namespace::node()' expr = Compile(EXPR) tester.startTest(EXPR) EXPECTED = [] actual = expr.evaluate(con) tester.compare(7, len(actual)) tester.testDone() EXPR = '//*/namespace::node()' expr = Compile(EXPR) tester.startTest(EXPR) EXPECTED = [] actual = expr.evaluate(con) tester.compare(7, len(actual)) tester.testDone() EXPR = '/*/*/namespace::node()' expr = Compile(EXPR) tester.startTest(EXPR) EXPECTED = [] actual = expr.evaluate(con) tester.compare(6, len(actual)) tester.testDone() EXPR = '/*/namespace::node()|/*/*/namespace::node()' expr = Compile(EXPR) tester.startTest(EXPR) EXPECTED = [] actual = expr.evaluate(con) tester.compare(7, len(actual)) tester.testDone() EXPR = '//*' expr = Compile(EXPR) #expr is <AbbreviatedAbsoluteLocationPath: /descendant-or-self::node()/child::*> tester.startTest(EXPR) EXPECTED = [] actual = expr.evaluate(con) tester.compare(4, len(actual)) tester.testDone() return tester.groupDone()
def _AttachStylesheetToProcessor(stylesheet, processor): from Ft.Lib import Uri, Uuid from Ft.Xml import InputSource from Ft.Xml.Catalog import IsXml if isinstance(stylesheet, InputSource.InputSource): processor.appendStylesheet(stylesheet) #elif stylesheet.find(XSL_NAMESPACE) > 0 and IsXml(stylesheet): #Note: this would break in pathological cases such as a user #passing in a stylesheet string with only an XInclude to the actual XSLT elif IsXml(stylesheet): #Create dummy Uri to use as base dummy_uri = 'urn:uuid:' + Uuid.UuidAsString(Uuid.GenerateUuid()) processor.appendStylesheet( InputSource.DefaultFactory.fromString(stylesheet, dummy_uri)) elif hasattr(stylesheet, 'read'): #Create dummy Uri to use as base dummy_uri = 'urn:uuid:' + Uuid.UuidAsString(Uuid.GenerateUuid()) processor.appendStylesheet( InputSource.DefaultFactory.fromStream(stylesheet, dummy_uri)) elif Uri.IsAbsolute(stylesheet): # or not os.path.isfile(stylesheet): processor.appendStylesheet( InputSource.DefaultFactory.fromUri(stylesheet)) else: processor.appendStylesheet( InputSource.DefaultFactory.fromUri(Uri.OsPathToUri(stylesheet))) return
def CreateInputSource(obj, uri=None): """ Convenience function for creating an InputSource. obj - a string, Unicode object (only if you really know what you're doing), file-like object (stream), file path or URI. You can also pass an InputSource object, in which case the return value is just the same object, possibly with the URI modified uri - optional override URI. The base URI for the IS will be set to this value Returns an InputSource which can be passed to 4Suite APIs. """ #do the imports within the function: a tad bit less efficient, but #avoid circular crap from Ft.Xml import InputSource factory = InputSource.DefaultFactory from Ft.Lib import Uri, Uuid from Ft.Xml.Lib.XmlString import IsXml if isinstance(obj, InputSource.InputSource): isrc = obj elif hasattr(obj, 'read'): #Create dummy Uri to use as base dummy_uri = 'urn:uuid:' + Uuid.UuidAsString(Uuid.GenerateUuid()) isrc = factory.fromStream(obj, dummy_uri) elif IsXml(obj): dummy_uri = 'urn:uuid:' + Uuid.UuidAsString(Uuid.GenerateUuid()) isrc = factory.fromString(obj, dummy_uri) elif Uri.IsAbsolute(obj): #or not os.path.isfile(obj): isrc = factory.fromUri(obj) else: isrc = factory.fromUri(Uri.OsPathToUri(obj)) if uri: isrc.uri = uri return isrc
def SourceArgToUri(arg, resolver=Uri.BASIC_RESOLVER): """ Some command-line scripts take an argument that is supposed to be either "-" (denoting standard input) or a URI reference that can be resolved against the URI equivalent of the current working directory. This function processes such an argument, given as a string, and returns an appropriate URI. Since users tend to expect local OS paths to work as URIs, this function will accept and use an OS path argument if does appear to point to an existing local file, even though this could interfere with catalog-based resolution. Raises a ValueError if arg is neither a local file nor a valid URI reference nor "-". The resolver object must support a normalize() method that can resolve a URI reference against a base URI, returning a URI. """ if not isinstance(resolver, Uri.UriResolverBase): msg = 'It appears there is a bug in this command-line' \ ' script. A %s was passed as URI resolver to a function that' \ ' requires an instance of Ft.Lib.Uri.UriResolverBase (or' \ ' a subclass thereof).' raise TypeError(msg % type(resolver)) if not isinstance(arg, str) and not isinstance(arg, unicode): msg = 'It appears there is a bug in this command-line' \ ' script. A %s was passed as an argument needing to be' \ ' converted to a URI. A string must be provided instead.' raise TypeError(msg % type(arg)) if arg == '-': return Uri.OsPathToUri('unknown-STDIN', attemptAbsolute=True) elif arg: if os.path.isfile(arg): return Uri.OsPathToUri(arg, attemptAbsolute=True) elif not Uri.MatchesUriRefSyntax(arg): raise ValueError("'%s' is not a valid URI reference." % arg) elif Uri.IsAbsolute(arg): return arg base = Uri.OsPathToUri(os.getcwd(), attemptAbsolute=True) if base[-1] != '/': base += '/' return resolver.normalize(arg, base)
def Test(tester): tester.startGroup('EXSLT exsl:document and indent') fname = tempfile.mktemp() furi = Uri.OsPathToUri(fname) source = test_harness.FileInfo(string="<dummy/>") sheet = test_harness.FileInfo(string=SHEET_1 % (furi, "no")) test_harness.XsltTest(tester, source, [sheet], EXPECTED, title='run transform') tester.startTest("With indent='no'") tester.compare(True, os.path.exists(fname)) file = open(fname, 'r') fcontent = file.read() file.close() tester.compare(EXPECTED_FILE_1, fcontent) os.unlink(fname) tester.testDone() fname = tempfile.mktemp() furi = Uri.OsPathToUri(fname) source = test_harness.FileInfo(string="<dummy/>") sheet = test_harness.FileInfo(string=SHEET_1 % (furi, "yes")) test_harness.XsltTest(tester, source, [sheet], EXPECTED, title='run transform') tester.startTest("With indent='yes'") tester.compare(True, os.path.exists(fname)) file = open(fname, 'r') fcontent = file.read() file.close() tester.compare(EXPECTED_FILE_2, fcontent) os.unlink(fname) tester.testDone() tester.groupDone() return
def Test(tester): source = test_harness.FileInfo(string=source_1) sheet = test_harness.FileInfo(string=sheet_1) test_harness.XsltTest(tester, source, [sheet], expected_1) #Test again with a specfied baseUri uri = Uri.OsPathToUri(os.path.abspath(__file__)) source = test_harness.FileInfo(string=source_1) sheet = test_harness.FileInfo(string=sheet_1, baseUri=uri) test_harness.XsltTest(tester, source, [sheet], expected_1) return
def build_dom_and_apply_style_sheet(self, xsl, file): #doc = xml.dom.minidom.Document() doc = implementation.createRootNode('file:///article.xml') self.app.ann_frame.build_xml(doc) xsltproc = Processor() xsltproc.appendStylesheet(DefaultFactory.fromUri(Uri.OsPathToUri(xsl))) output = xsltproc.runNode(doc, 'file:///article.xml') outFile = open(file, 'w') outFile.write(output) outFile.close()
def get_source_files(self): sources = [] for doc in self.distribution.doc_files: if isinstance(doc, Structures.File): source = util.convert_path(doc.source) sources.append(source) elif isinstance(doc, Structures.Document): source = util.convert_path(doc.source) sources.append(source) prefix = len(os.getcwd()) + len(os.sep) for path in self.find_xml_includes(Uri.OsPathToUri(source)): sources.append(path[prefix:]) if self.inplace: sources.extend(self.get_outputs()) return sources
def __init__(self, uri=None, string=None, baseUri='', validate=False, **kwords): self.uri = uri self.string = string self.baseUri = baseUri self.validate = validate self.args = kwords # Same logic that exists in _4xslt.py # The processor only deals with URIs, not file paths if uri and os.path.isfile(uri): self.uri = Uri.OsPathToUri(os.path.abspath(uri)) if not (uri or string): raise ValueError('Either string or uri are required') if string and not baseUri: base = os.path.join(os.path.abspath(os.getcwd()), Uuid.UuidAsString(Uuid.GenerateUuid())) self.baseUri = Uri.OsPathToUri(base) return
def Test(tester): tester.startGroup('CDATA sections in doc') isrc = InputSource.DefaultFactory.fromString(SRC_1, Uri.OsPathToUri(os.getcwd())) doc = NonvalidatingReader.parse(isrc) con = Context.Context(doc, 1, 1) EXPR = '/doc/elem/text()' expr = Compile(EXPR) tester.startTest(EXPR) actual = [node.data for node in expr.evaluate(con)] tester.compare(actual, ["abc"] * 3) tester.testDone() return tester.groupDone()
def instantiate(self, context, processor): context.processorNss = self.namespaces context.currentInstruction = self # this uses attributes directly from self self._output_parameters.avtParse(self, context) href = self._href.evaluate(context) if Uri.IsAbsolute(href): uri = href else: try: uri = Uri.Absolutize(href, Uri.OsPathToUri(processor.writer.getStream().name)) except Exception, e: raise XsltRuntimeException( ExsltError.NO_EXSLTDOCUMENT_BASE_URI, context.currentInstruction, href)
def Parse(source): """ Convenience function for parsing XML. Use this function with a single argument, which must either be a string (not Unicode object), file-like object (stream), file path or URI. Returns a Domlette node. Only pass strings or streams to this function if the XML is self-contained XML (i.e. not requiring access to any other resource such as external entities or includes). If you get URI resolution errors, do not use this function: use the lower-level APIs instead. As an example, if you want such resolution to use the current working directory as a base, parse as follows for strings: from Ft.Xml.Domlette import NonvalidatingReader from Ft.Lib import Uri XML = "<!DOCTYPE a [ <!ENTITY b "b.xml"> ]><a>&b;</a>" base = Uri.OsPathToUri('') #Turn CWD into a file: URL doc = NonvalidatingReader.parseString(XML, base) # during parsing, the replacement text for &b; # will be obtained from b.xml in the CWD For streams, use "parseStream" rather than "parseString" in the above. """ #do the imports within the function: a tad bit less efficient, but #avoid circular crap from Ft.Xml.Domlette import NonvalidatingReader from Ft.Lib import Uri, Uuid from Ft.Xml.Lib.XmlString import IsXml if hasattr(source, 'read'): #Create dummy Uri to use as base dummy_uri = 'urn:uuid:' + Uuid.UuidAsString(Uuid.GenerateUuid()) return NonvalidatingReader.parseStream(source, dummy_uri) elif IsXml(source): dummy_uri = 'urn:uuid:' + Uuid.UuidAsString(Uuid.GenerateUuid()) return NonvalidatingReader.parseString(source, dummy_uri) elif Uri.IsAbsolute(source): #or not os.path.isfile(source): return NonvalidatingReader.parseUri(source) else: return NonvalidatingReader.parseUri(Uri.OsPathToUri(source))
def bind_file(fname, rules=None, binderobj=None, prefixes=None): """ Create a binding from XML read from a file rules - a list of bindery rule objects to fine-tune the binding binderobj - optional binder object to control binding details, the default is None, in which case a binder object will be created prefixes - dictionary mapping prefixes to namespace URIs the default is None """ #Create an input source for the XML isrc_factory = InputSource.DefaultFactory #Create a URI from a filename the right way file_uri = Uri.OsPathToUri(fname, attemptAbsolute=1) binding = bind_uri(file_uri, rules=rules, binderobj=binderobj, prefixes=prefixes) return binding
def render_documents(self): extras = {} for document in self.documents: # Find the stylesheet to render this document stylesheet = self.get_stylesheet_obj(document.stylesheet) filename = self.get_output_filename(document) destdir = os.path.dirname(filename) self.mkpath(destdir) try: target_mtime = os.path.getmtime(filename) except OSError: target_mtime = -1 document.uri = Uri.OsPathToUri(document.source) document_mtime = self.get_modification_time(document.uri) source_mtime = max(document_mtime, stylesheet.mtime) if document_mtime is None: self.announce('skipping %s (not documented)' % filename, 3) elif self.force or source_mtime > target_mtime: self.announce( "rendering %s -> %s" % (document.source, filename), 2) try: self.render_document(document, stylesheet, filename) except (KeyboardInterrupt, SystemExit): # Let "exitting" exceptions propagate through. raise except Exception, exc: if DEBUG: raise raise DistutilsFileError("could not render %s (%s)" % (document.source, exc)) else: self.announce('not rendering %s (up-to-date)' % filename, 1) # Copy any extra files for the stylesheet to destdir. # 'extra_outputs' is a list of URIs. for uri in stylesheet.extra_outputs: pathname = Uri.Relativize(uri, stylesheet.uri) target = os.path.join(destdir, *pathname.split('/')) if target not in extras: extras[target] = True self.copy_uri(uri, target)
def Transform(source, stylesheet, params=None, output=None): """ Convenience function for applying an XSLT transform. Returns a string. source - XML source document in the form of a a string (not Unicode object), file-like object (stream), file path, URI or Ft.Xml.InputSource.InputSource instance. If string or stream it must be self-contained XML (i.e. not requiring access to any other resource such as external entities or includes) stylesheet - XSLT document in the form of a string, stream, URL, file path or Ft.Xml.InputSource.InputSource instance params - optional dictionary of stylesheet parameters, the keys of which may be given as unicode objects if they have no namespace, or as (uri, localname) tuples if they do. output - optional file-like object to which output is written (incrementally, as processed) """ #do the imports within the function: a tad bit less efficient, but #avoid circular crap from Ft.Xml.Xslt import Processor from Ft.Xml import InputSource from Ft.Lib import Uri, Uuid from Ft.Xml.Lib.XmlString import IsXml params = params or {} processor = Processor.Processor() _AttachStylesheetToProcessor(stylesheet, processor) if isinstance(source, InputSource.InputSource): pass elif hasattr(source, 'read'): #Create dummy Uri to use as base dummy_uri = 'urn:uuid:' + Uuid.UuidAsString(Uuid.GenerateUuid()) source = InputSource.DefaultFactory.fromStream(source, dummy_uri) elif IsXml(source): dummy_uri = 'urn:uuid:' + Uuid.UuidAsString(Uuid.GenerateUuid()) source = InputSource.DefaultFactory.fromString(source, dummy_uri) elif Uri.IsAbsolute(source): # or not os.path.isfile(source): source = InputSource.DefaultFactory.fromUri(source) else: source = InputSource.DefaultFactory.fromUri(Uri.OsPathToUri(source)) return processor.run(source, topLevelParams=params, outputStream=output)
def loadFromFile(self, filename): #rngParser = libxml2.relaxNGNewMemParserCtxt(VALIDATION_SCHEMA, len(VALIDATION_SCHEMA)) #rngContext = rngParser.relaxNGParse().relaxNGNewValidCtxt() #file = fileRead(filename, 'r') #doc = libxml2.parseDoc(file) #if doc.relaxNGValidateDoc(rngContext) != 0: # raise InvalidLibraryException(filename) #for item in doc.xpathEval('/library/item'): # self.addItem({'authors': map(lambda x: x.content, item.xpathEval('authors/author')), # 'title': item.xpathEval('title')[0].content, # 'type': item.xpathEval('type')[0].content, # 'date': item.xpathEval('date')[0].content, # 'language': item.xpathEval('language')[0].content}) factory = InputSource.DefaultFactory schema = factory.fromString(VALIDATION_SCHEMA) validator = RelaxNgValidator(schema) file = Uri.OsPathToUri(filename, attemptAbsolute=1) # validate file if not(validator.isValid(factory.fromUri(file))): raise InvalidLibraryException(filename) doc = NonvalidatingReader.parse(factory.fromUri(file)) # read items from document for item in doc.xpath('/library/item'): self.addItem({'authors': map(lambda x: x.childNodes[0].nodeValue.strip(), item.xpath('authors/author')), 'title': item.xpath('title')[0].childNodes[0].nodeValue.strip(), 'type': item.xpath('type')[0].childNodes[0].nodeValue.strip(), 'date': item.xpath('date')[0].childNodes[0].nodeValue.strip(), 'language': item.xpath('language')[0].childNodes[0].nodeValue.strip()})
def OsPath2Uri(context, path): """ Returns the given OS path as a URI. The result varies depending on the underlying operating system. """ return Uri.OsPathToUri(Conversions.StringValue(path))
def Test(tester): base_uri = Uri.OsPathToUri(__file__, attemptAbsolute=True) tester.startTest('Creating test environment') from Ft.Xml import XPointer from Ft.Xml import Domlette r = Domlette.NonvalidatingReader doc_uri = Uri.Absolutize('addrbook.xml', base_uri) doc = r.parseUri(doc_uri) ADDRBOOK = doc.documentElement elementType = lambda n, nt=Node.ELEMENT_NODE: n.nodeType == nt ENTRIES = filter(elementType, ADDRBOOK.childNodes) PA = ENTRIES[0] children = filter(elementType, PA.childNodes) PA_NAME = children[0] PA_ADDR = children[1] PA_WORK = children[2] PA_FAX = children[3] PA_PAGER = children[4] PA_EMAIL = children[5] EN = ENTRIES[1] children = filter(elementType, EN.childNodes) EN_NAME = children[0] EN_ADDR = children[1] EN_WORK = children[2] EN_FAX = children[3] EN_PAGER = children[4] EN_EMAIL = children[5] VZ = ENTRIES[2] tester.testDone() # Just one; it can be too confusing to compare much else # because the documents are different uri = Uri.Absolutize('addrbook.xml#element(/1)', base_uri) tester.startTest('SelectUri(%s)' % uri) result = XPointer.SelectUri(uri) tester.compare(1, len(result)) tester.compare(ADDRBOOK.nodeName, result[0].nodeName) tester.testDone() tester.startTest('SelectNode()') frag = 'element(pa/2)' result = XPointer.SelectNode(doc, frag) tester.compare([PA_ADDR], result, 'frag=%s' % frag) frag = 'xpointer(//ENTRY[@ID="en"]/EMAIL)' result = XPointer.SelectNode(doc, frag) tester.compare([EN_EMAIL], result, 'frag=%s' % frag) tester.testDone() if tester.offline: # No further testing return tester.startTest('Testing remote lookup') nss = {'xsl': 'http://www.w3.org/1999/XSL/Transform'} uri = "http://www.w3.org/Style/XSL/stylesheets/public2html.xsl#xpointer(//xsl:template[@match='/'])" try: result = XPointer.SelectUri(uri, nss=nss) except UriException, error: if error.errorCode != UriException.RESOURCE_ERROR: raise tester.warning("No internet connection available")
def Test(tester): tester.startGroup('UriDict') tester.startGroup('file:/// and file://localhost/ equivalence') tester.startTest('equivalent key in UriDict') uris = Uri.UriDict() uris['file:///path/to/resource'] = 0 tester.compare(True, 'file://localhost/path/to/resource' in uris, 'RFC 1738 localhost support failed') tester.testDone() tester.startTest('value of 2 equivalent keys') uris = Uri.UriDict() uris['file:///path/to/resource'] = 1 uris['file://localhost/path/to/resource'] = 2 tester.compare(2, uris['file:///path/to/resource'], 'RFC 1738 localhost support failed') tester.testDone() tester.groupDone() tester.startGroup('case equivalence') for uri, expected, junk in caseNormalizationTests: tester.startTest('%s and %s equivalence' % (uri, expected)) uris[uri] = 1 uris[expected] = 2 tester.compare(2, uris[uri]) tester.testDone() tester.groupDone() tester.startGroup('percent-encoding equivalence') for uri, expected in pctEncNormalizationTests: tester.startTest('%s and %s equivalence' % (uri, expected)) uris[uri] = 1 uris[expected] = 2 tester.compare(2, uris[uri]) tester.testDone() tester.groupDone() tester.groupDone() tester.startGroup("PercentEncode and PercentDecode") for unencoded, encoded in percentEncodeTests: if len(unencoded) > 10: test_title = unencoded[:11] + '...' else: test_title = unencoded tester.startTest(repr(test_title)) tester.compare(encoded, Uri.PercentEncode(unencoded)) tester.compare(unencoded, Uri.PercentDecode(encoded)) tester.testDone() # non-BMP tests: # a couple of random chars from U+10000 to U+10FFFD. # # This string will be length 2 or 4 depending on how Python # was built. Either way, it should result in the same percent- # encoded sequence, which should decode back to the original # representation. unencoded = u'\U00010000\U0010FFFD' encoded = u'%F0%90%80%80%F4%8F%BF%BD' tester.startTest("u'\U00010000\U0010FFFD'") tester.compare(encoded, Uri.PercentEncode(unencoded)) tester.compare(unencoded, Uri.PercentDecode(encoded)) tester.testDone() # # This string will be length 4, regardless of how Python was # built. However, if Python was built with wide (UCS-4) chars, # PercentDecode will generate an optimal string (length: 2). unencoded_in = u'\ud800\udc00\udbff\udffd' encoded = u'%F0%90%80%80%F4%8F%BF%BD' unencoded_out = u'\U00010000\U0010FFFD' tester.startTest("u'\ud800\udc00\udbff\udffd'") tester.compare(encoded, Uri.PercentEncode(unencoded_in)) tester.compare(unencoded_out, Uri.PercentDecode(encoded)) tester.testDone() # test a few iso-8859-n variations just to make sure # iso-8859-1 isn't special unencoded = ''.join(map(chr, range(256))) encoded = '%00%01%02%03%04%05%06%07%08%09%0A%0B%0C%0D%0E%0F' \ '%10%11%12%13%14%15%16%17%18%19%1A%1B%1C%1D%1E%1F' \ '%20%21%22%23%24%25%26%27%28%29%2A%2B%2C-.%2F' \ '0123456789%3A%3B%3C%3D%3E%3F%40' \ 'ABCDEFGHIJKLMNOPQRSTUVWXYZ%5B%5C%5D%5E_%60' \ 'abcdefghijklmnopqrstuvwxyz%7B%7C%7D~' \ '%7F%80%81%82%83%84%85%86%87%88%89%8A%8B%8C%8D%8E%8F' \ '%90%91%92%93%94%95%96%97%98%99%9A%9B%9C%9D%9E%9F' \ '%A0%A1%A2%A3%A4%A5%A6%A7%A8%A9%AA%AB%AC%AD%AE%AF' \ '%B0%B1%B2%B3%B4%B5%B6%B7%B8%B9%BA%BB%BC%BD%BE%BF' \ '%C0%C1%C2%C3%C4%C5%C6%C7%C8%C9%CA%CB%CC%CD%CE%CF' \ '%D0%D1%D2%D3%D4%D5%D6%D7%D8%D9%DA%DB%DC%DD%DE%DF' \ '%E0%E1%E2%E3%E4%E5%E6%E7%E8%E9%EA%EB%EC%ED%EE%EF' \ '%F0%F1%F2%F3%F4%F5%F6%F7%F8%F9%FA%FB%FC%FD%FE%FF' for part in (1, 2, 3, 15): enc_name = 'iso-8859-%d' % part tester.startTest(enc_name) try: codecs.lookup(enc_name) except LookupError: tester.warning('Not supported on this platform') tester.testDone() continue tester.compare(encoded, Uri.PercentEncode(unencoded, encoding=enc_name)) tester.compare(unencoded, Uri.PercentDecode(encoded, encoding=enc_name)) tester.testDone() # utf-16be: why not? unencoded = u'a test string...\x00\xe9...\x20\x22...\xd8\x00\xdc\x00' encoded = u'a%20test%20string...\u00e9...%20%22...%D8%00%DC%00' tester.groupDone() tester.startGroup("PublicIdToUrn") for publicid, urn in publicIdTests: tester.startTest(publicid) tester.compare(urn, Uri.PublicIdToUrn(publicid)) tester.testDone() tester.groupDone() tester.startGroup("UrnToPublicId") for publicid, urn in publicIdTests: tester.startTest(urn) tester.compare(publicid, Uri.UrnToPublicId(urn)) tester.testDone() tester.groupDone() tester.startGroup("URI reference syntax") for testuri in good_URI_references: tester.startTest("Good URI ref: %s" % repr(testuri)) tester.compare(1, Uri.MatchesUriRefSyntax(testuri), "Mistakenly tests as invalid") tester.testDone() for testuri in bad_URI_references: tester.startTest("Bad URI ref: %s" % repr(testuri)) tester.compare(0, Uri.MatchesUriRefSyntax(testuri), "Mistakenly tests as valid") tester.testDone() tester.groupDone() tester.startGroup('Absolutize') for uriRef, baseUri, expectedUri in absolutize_test_cases: tester.startTest('base=%r ref=%r' % (baseUri, uriRef)) res = Uri.Absolutize(uriRef, baseUri) # in a couple cases, there's more than one correct result if isinstance(expectedUri, tuple): tester.compare(1, res in expectedUri, 'Invalid result') else: tester.compare(expectedUri, res, 'Invalid result') tester.testDone() tester.groupDone() tester.startGroup('Relativize') for targetUri, againstUri, relativeUri, subPathUri in relativize_test_cases: tester.startTest('target=%r against=%r (subPathOnly=False)' % (targetUri, againstUri)) res = Uri.Relativize(targetUri, againstUri) tester.compare(relativeUri, res, 'Invalid result') tester.testDone() if res is not None: tester.startTest( 'target=%r against=%r (subPathOnly=False, Absolutize)' % (targetUri, againstUri)) res = Uri.Absolutize(res, againstUri) tester.compare(res, targetUri, 'Invalid result') tester.testDone() tester.startTest('target=%r against=%r (subPathOnly=True)' % (targetUri, againstUri)) res = Uri.Relativize(targetUri, againstUri, True) tester.compare(subPathUri, res, 'Invalid result') tester.testDone() if res is not None: tester.startTest( 'target=%r against=%r (subPathOnly=True, Absolutize)' % (targetUri, againstUri)) res = Uri.Absolutize(res, againstUri) tester.compare(res, targetUri, 'Invalid result') tester.testDone() tester.groupDone() tester.startGroup('BaseJoin') for base, relative, expectedUri in basejoin_test_cases: tester.startTest('base=%r rel=%r' % (base, relative)) res = Uri.BaseJoin(base, relative) tester.compare(expectedUri, res, 'Invalid result') tester.testDone() tester.groupDone() tester.startGroup('UriToOsPath') for osname in ('posix', 'nt'): tester.startGroup(osname) for subgroupname in ('absolute', 'relative'): tester.startGroup(subgroupname) for uri, nt_path, posix_path in fileUris: if subgroupname == 'relative': if uri[:5] == 'file:': uri = uri[5:] else: break if isinstance(uri, unicode): testname = repr(uri) else: testname = uri tester.startTest(testname) if osname == 'nt': path = nt_path elif osname == 'posix': path = posix_path else: break if path is None: tester.testException(Uri.UriToOsPath, (uri, ), Uri.UriException, kwargs={ 'attemptAbsolute': False, 'osname': osname }) else: tester.compare( path, Uri.UriToOsPath(uri, attemptAbsolute=False, osname=osname)) tester.testDone() tester.groupDone() tester.groupDone() tester.groupDone() tester.startGroup('OsPathToUri') for osname in ('posix', 'nt'): tester.startGroup(osname) for path, nt_uri, posix_uri in filePaths: if isinstance(path, unicode): testname = repr(path) else: testname = path tester.startTest(testname) if osname == 'nt': uri = nt_uri elif osname == 'posix': uri = posix_uri else: break if uri is None: tester.testException(Uri.OsPathToUri, (path, ), Uri.UriException, kwargs={ 'attemptAbsolute': False, 'osname': osname }) else: tester.compare( uri, Uri.OsPathToUri(path, attemptAbsolute=False, osname=osname)) tester.testDone() tester.groupDone() tester.groupDone() tester.startGroup('NormalizeCase') for uri, expected0, expected1 in caseNormalizationTests: testname = uri uri = Uri.SplitUriRef(uri) tester.startTest(testname) tester.compare(expected0, Uri.UnsplitUriRef(Uri.NormalizeCase(uri))) tester.testDone() tester.startTest(testname + ' (host too)') tester.compare(expected1, Uri.UnsplitUriRef(Uri.NormalizeCase(uri, doHost=1))) tester.testDone() tester.groupDone() tester.startGroup('NormalizePercentEncoding') for uri, expected in pctEncNormalizationTests: testname = uri tester.startTest(testname) tester.compare(expected, Uri.NormalizePercentEncoding(uri)) tester.testDone() tester.groupDone() tester.startGroup('NormalizePathSegments') for path, expected in pathSegmentNormalizationTests: testname = path tester.startTest(testname) tester.compare(expected, Uri.NormalizePathSegments(path)) tester.testDone() tester.groupDone() tester.startGroup('NormalizePathSegmentsInUri') for path, expectedpath in pathSegmentNormalizationTests: # for non-hierarchical scheme, no change expected in every case uri = 'urn:bogus:%s?a=1&b=2#frag' % path expected = 'urn:bogus:%s?a=1&b=2#frag' % path testname = uri tester.startTest(testname) tester.compare(expected, Uri.NormalizePathSegmentsInUri(uri)) tester.testDone() for path, expectedpath in pathSegmentNormalizationTests: if path[:1] == '/': # hierarchical scheme uri = 'file://*****:*****@host%s?a=1&b=2#frag' % path expected = 'file://*****:*****@host%s?a=1&b=2#frag' % expectedpath testname = uri tester.startTest(testname) tester.compare(expected, Uri.NormalizePathSegmentsInUri(uri)) tester.testDone() tester.groupDone() tester.startGroup("MakeUrllibSafe") tests = makeUrllibSafeTests if os.name == 'nt': tests += winMakeUrllibSafeTests for uri, expected in makeUrllibSafeTests: if isinstance(uri, unicode): test_title = repr(uri) else: test_title = uri tester.startTest(test_title) res = Uri.MakeUrllibSafe(uri) tester.compare(expected, res) tester.testDone() tester.groupDone() tester.startGroup("Basic Uri Resolver") data = [ ('http://foo.com/root/', 'path', 'http://foo.com/root/path'), ('http://foo.com/root', 'path', 'http://foo.com/path'), ] for base, uri, exp in data: tester.startTest("normalize: %s %s" % (base, uri)) res = Uri.BASIC_RESOLVER.normalize(uri, base) tester.compare(exp, res) tester.testDone() base = 'foo:foo.com' uri = 'path' tester.startTest("normalize: %s %s" % (base, uri)) tester.testException(Uri.BASIC_RESOLVER.normalize, (uri, base), Uri.UriException) tester.testDone() tester.startTest('resolve') base = os.getcwd() if base[-1] != os.sep: base += os.sep stream = Uri.BASIC_RESOLVER.resolve('test.py', Uri.OsPathToUri(base)) tester.compare(TEST_DOT_PY_BANGPATH, string.rstrip(stream.readline())) stream.close() tester.testDone() tester.startTest('generate') uuid = Uri.BASIC_RESOLVER.generate() tester.compare('urn:uuid:', uuid[:9]) tester.testDone() tester.groupDone() tester.startGroup("SchemeRegistryResolver") def evalSchemeHandler(uri, base=None): if base: uri = base + uri uri = uri[5:] return str(eval(uri)) def shiftSchemeHandler(uri, base=None): if base: uri = base + uri uri = uri[6:] return ''.join([chr(ord(c) + 1) for c in uri]) resolver = SchemeRegistryResolver({ 'eval': evalSchemeHandler, 'shift': shiftSchemeHandler, }) scheme_cases = [ (None, 'eval:150-50', '100'), (None, 'shift:abcde', 'bcdef'), ('eval:150-', '50', '100'), ('shift:ab', 'cde', 'bcdef'), ] for base, relative, expected in scheme_cases: tester.startTest("URI: base=%s uri=%s" % (base, relative)) res = resolver.resolve(relative, base) tester.compare(expected, res) tester.testDone() resolver.handlers[None] = shiftSchemeHandler del resolver.handlers['shift'] for base, relative, expected in scheme_cases: tester.startTest("URI: base=%s uri=%s" % (base, relative)) res = resolver.resolve(relative, base) tester.compare(expected, res) 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
raise op.error('You need to provide a database type (MySQL or PostgreSQL).') store.open() factGraph = ConjunctiveGraph(store, identifier=options.graphName) if not options.reuse: if options.directory: if not options.format: op.error('You need to provide the format with `--format`\n' + 'when loading from a directory') dirparts = os.walk(options.directory).next() for entry in dirparts[2]: graphRef = os.path.join(dirparts[0], entry) factGraph.parse(graphRef, publicID=Uri.OsPathToUri(graphRef), format=options.format) for graphRef in options.xml: factGraph.parse(graphRef, publicID=Uri.OsPathToUri(graphRef), format='xml') for graphRef in options.trix: factGraph.parse(graphRef, publicID=Uri.OsPathToUri(graphRef), format='trix') for graphRef in options.n3: factGraph.parse(graphRef, publicID=Uri.OsPathToUri(graphRef), format='n3') for graphRef in options.nt:
import os from Xml.Xslt import test_harness from Ft.Lib import Uri from Ft.Xml.InputSource import DefaultFactory from Ft.Xml.Lib import TreeCompare from Ft.Xml.Xslt import Processor, Error uri = Uri.OsPathToUri(os.path.abspath(__file__)) tests = [] title = 'PI after prolog' source = """<?xml version="1.0" encoding="utf-8"?><dummy/><?xml-stylesheet href="mb_20030915.xslt"?>""" result = None tests.append((title, source, result, Error.NO_STYLESHEET)) title = 'PI with no type' source = """<?xml version="1.0" encoding="utf-8"?><?xml-stylesheet href="mb_20030915.xslt"?><dummy/>""" result = None tests.append((title, source, result, Error.NO_STYLESHEET)) title = 'PI with type="text/xsl"' source = """<?xml version="1.0" encoding="utf-8"?> <?xml-stylesheet type="text/xsl" href="mb_20030915.xslt"?> <dummy/>""" result = None tests.append((title, source, result, Error.NO_STYLESHEET)) title = 'PI with type="application/xslt+xml"' source = """<?xml version="1.0" encoding="utf-8"?> <?xml-stylesheet type="application/xslt+xml" href="mb_20030915.xslt"?>
from Ft.Xml.Xslt import Error from Xml.Xslt import test_harness from Ft.Lib import Uri import os base = os.getcwd() if base[-1] != os.sep: base += os.sep base = Uri.OsPathToUri(base) SHEET_1 = """\ <?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" > <xsl:import href="Xml/Xslt/Core/test-apply-imports-1.xslt"/> <xsl:template match="example"> <div style="border: solid red"> <xsl:apply-imports/> </div> </xsl:template> </xsl:stylesheet> """ SHEET_URI = base + 'sheet_string' SOURCE_1 = "<example>This is an example</example>"
def test_dom_builder(tester): tester.startGroup("DomBuilder") test_builder_basic(tester) test_builder_content(tester) test_builder_ns(tester) tester.groupDone() return ### XMLReader Interface ################################################ BASE_PATH = os.path.dirname(os.path.abspath(__file__)) CONTENT_PATH = os.path.join(BASE_PATH, 'data.xml') CONTENT_URI = Uri.OsPathToUri(CONTENT_PATH) f = open(CONTENT_PATH) try: XMLREADER_CONTENT = f.read() finally: f.close() del f def test_xmlreader_sax(tester): tester.startTest("SAX InputSource") parser = CreateParser() builder = DomBuilder() parser.setContentHandler(builder) parser.parse(prepare_input_source(CONTENT_PATH))
# document() # key() and xsl:key # format-number() and xsl:decimal-format # current() # unparsed-entity-uri() # generate-id() # system-property(), including these required properties: # system-property('xsl:version') # system-property('xsl:vendor') # system-property('xsl:vendor-url') tests = [] DUMMY_XML_STR = "<dummy/>" DUMMY_XML_URI = Uri.OsPathToUri( os.path.join(os.path.abspath(os.getcwd()), Uuid.UuidAsString(Uuid.GenerateUuid()))) TEST_XML_STR = """<?xml version="1.0"?> <doc> <elem a1="1" a2="6">one</elem> <elem a1="2" a2="4">two</elem> <elem a1="3" a2="2">three</elem> </doc>""" TEST_XML_URI = None # (one will be generated) TEST_DIR_URI_BASE = Uri.OsPathToUri(os.path.abspath(os.getcwd())) if TEST_DIR_URI_BASE[-1] != '/': TEST_DIR_URI_BASE += '/' f = open(os.path.normpath('Xml/Xslt/Core/addr_book1.xml'), 'r')
sheet_1 = """<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:import href="mb_20020527-1.xsl"/> <xsl:template name="i"> <foo>import.xsl</foo> </xsl:template> </xsl:stylesheet> """ expected = """<?xml version="1.0" encoding="UTF-8"?>\n<out><foo>import.xsl</foo></out>""" from Ft.Lib import Uri INC_PATH = Uri.OsPathToUri('Xml/Xslt/Borrowed/etc/', attemptAbsolute=1) def Test(tester): source = test_harness.FileInfo(string="<foo/>") sheet = test_harness.FileInfo(string=sheet_1) test_harness.XsltTest( tester, source, [sheet], expected, stylesheetAltUris=[INC_PATH], title="Nested imports with names that clash across import precedence") return
def main(): from optparse import OptionParser parser = OptionParser() parser.add_option( '--stdin', type="choice", choices=['xml', 'trix', 'n3', 'nt', 'rdfa'], help='Parse RDF from STDIN (useful for piping) with given format') parser.add_option('-x', '--xml', action='append', help='Append to the list of RDF/XML documents to parse') parser.add_option('-t', '--trix', action='append', help='Append to the list of TriX documents to parse') parser.add_option('-n', '--n3', action='append', help='Append to the list of N3 documents to parse') parser.add_option('--nt', action='append', help='Append to the list of NT documents to parse') parser.add_option('-a', '--rdfa', action='append', help='Append to the list of RDFa documents to parse') parser.add_option( '-o', '--output', type="choice", choices=['n3', 'xml', 'pretty-xml', 'TriX', 'turtle', 'nt'], help='Format of the final serialized RDF graph') parser.add_option( '-m', '--ns', action='append', help='Register a namespace binding (QName prefix to a base URI)') parser.add_option( '-r', '--rules', action='append', help='Append to the list of fact files to use to perform reasoning') parser.add_option( '-i', '--inferred', help='URI to use for the graph containing any inferred triples') parser.set_defaults(xml=[], trix=[], n3=[], nt=[], rdfa=[], ns=[], output='n3') (options, args) = parser.parse_args() store = plugin.get(RDFLIB_STORE, Store)() store.open(RDFLIB_CONNECTION) namespace_manager = NamespaceManager(Graph()) for prefixDef in options.ns: prefix, uri = prefixDef.split('=') namespace_manager.bind(prefix, uri, override=False) factGraph = ConjunctiveGraph(store) for graphRef in options.xml: factGraph.parse(graphRef, publicID=Uri.OsPathToUri(graphRef), format='xml') for graphRef in options.trix: factGraph.parse(graphRef, publicID=Uri.OsPathToUri(graphRef), format='trix') for graphRef in options.n3: factGraph.parse(graphRef, publicID=Uri.OsPathToUri(graphRef), format='n3') for graphRef in options.nt: factGraph.parse(graphRef, publicID=Uri.OsPathToUri(graphRef), format='nt') for graphRef in options.rdfa: factGraph.parse(graphRef, publicID=Uri.OsPathToUri(graphRef), format='rdfa') if options.stdin: factGraph.parse(sys.stdin, format=options.stdin) if options.inferred and len(options.rules) > 0: inferredURI = URIRef(options.inferred) ruleStore = N3RuleStore() ruleGraph = Graph(ruleStore) for ruleFile in options.rules: ruleGraph.parse(ruleFile, format='n3') tokenSet = generateTokenSet(factGraph) deltaGraph = Graph(store=factGraph.store, identifier=inferredURI) network = ReteNetwork(ruleStore, inferredTarget=deltaGraph) network.feedFactsToAdd(tokenSet) print factGraph.serialize(destination=None, format=options.output, base=None) store.rollback()