| 1 |
# |
| 2 |
# 4Suite plugin |
| 3 |
# |
| 4 |
import sys |
| 5 |
import os |
| 6 |
from io import open |
| 7 |
|
| 8 |
from Ft.Xml.Xslt import Processor |
| 9 |
from Ft.Lib.Uri import OsPathToUri |
| 10 |
from Ft.Xml import Catalog, InputSource |
| 11 |
from Ft.Xml.Domlette import NonvalidatingReader |
| 12 |
|
| 13 |
class FourXslt: |
| 14 |
def __init__(self): |
| 15 |
self.verbose = 0 |
| 16 |
self.use_catalogs = 1 |
| 17 |
# Do it only once |
| 18 |
self.factory = self.get_factory() |
| 19 |
|
| 20 |
def get_deplist(self): |
| 21 |
return [] |
| 22 |
|
| 23 |
def get_factory(self): |
| 24 |
system_catalogs = ['/etc/xml/catalog', |
| 25 |
'/usr/local/share/xml/catalog'] |
| 26 |
|
| 27 |
system_catalogs = [p for p in system_catalogs if os.path.exists(p)] |
| 28 |
|
| 29 |
if system_catalogs: |
| 30 |
xml_catalog_files = os.getenv("XML_CATALOG_FILES") |
| 31 |
if xml_catalog_files: |
| 32 |
xml_catalog_files += " " + " ".join(system_catalogs) |
| 33 |
else: |
| 34 |
xml_catalog_files = " ".join(system_catalogs) |
| 35 |
os.environ["XML_CATALOG_FILES"] = xml_catalog_files |
| 36 |
|
| 37 |
# The factory now takes into account the new XSML_CATALOG_FILES |
| 38 |
factory = InputSource.InputSourceFactory(catalog=Catalog.GetDefaultCatalog()) |
| 39 |
return factory |
| 40 |
|
| 41 |
def run(self, xslfile, xmlfile, outfile, opts=None, params=None): |
| 42 |
proc = Processor.Processor() |
| 43 |
proc.msgPrefix = "" |
| 44 |
proc.msgSuffix = "\n" |
| 45 |
factory = self.factory |
| 46 |
|
| 47 |
uri = OsPathToUri(xmlfile) |
| 48 |
xml = factory.fromUri(uri) |
| 49 |
|
| 50 |
uri = OsPathToUri(xslfile) |
| 51 |
xslt = factory.fromUri(uri, processIncludes=False) |
| 52 |
|
| 53 |
o = open(outfile, "wb") |
| 54 |
proc.appendStylesheet(xslt) |
| 55 |
if params: |
| 56 |
rc = proc.run(xml, outputStream=o, topLevelParams=params) |
| 57 |
else: |
| 58 |
rc = proc.run(xml, outputStream=o) |
| 59 |
o.close() |
| 60 |
|
| 61 |
|
| 62 |
class Xslt(FourXslt): |
| 63 |
"Plugin Class to load" |
| 64 |
|
| 65 |
|
| 66 |
def main(): |
| 67 |
run(sys.argv[1], sys.argv[2], sys.argv[3]) |
| 68 |
|
| 69 |
if __name__ == "__main__": |
| 70 |
main() |