PLATFORM_IS_JAVA=!(RUBY_PLATFORM.index('java').nil?) OS_IS_WINDOWS=!(ENV['OS'].nil? ? true : ENV['OS'].index('Windows').nil?) if OS_IS_WINDOWS module Mtk_xslt class XsltProcessor def name return "msxml" end def transform(xslt,infile,outfile) param="#{infile} -o #{outfile} #{xslt}" cmd="#{File.dirname(__FILE__)}\\converter-ea\\msxsl.exe "+param.tr('/','\\\\') ret=system(cmd) raise Exception.new("\n***\n***Error running command\n***\n***#{cmd}\n***Please check that no msxml process is running in background\n***\n") unless ret end end # XsltProcessor end elsif PLATFORM_IS_JAVA # Jruby version #from: # http://kfahlgren.com/blog/2007/03/02/borrowing-javas-xslt-support-for-ruby/ require 'java' module Mtk_xslt include_class "javax.xml.transform.TransformerFactory" include_class "javax.xml.transform.Transformer" include_class "javax.xml.transform.stream.StreamSource" include_class "javax.xml.transform.stream.StreamResult" include_class "java.lang.System" class Saxon def transform(xslt,infile,outfile) transformer = @tf.newTransformer(StreamSource.new(xslt)) transformer.transform(StreamSource.new(infile), StreamResult.new(outfile)) end def name return "jruby/saxon" end TRANSFORMER_FACTORY_IMPL = "net.sf.saxon.TransformerFactoryImpl" def initialize System.setProperty("javax.xml.transform.TransformerFactory", TRANSFORMER_FACTORY_IMPL) @tf = TransformerFactory.newInstance raise Exception.new("\n*** *******\n*** ERROR loading saxon into jruby.\n*** This error may be caused by missing Saxon jars in jruby lib directory\n*** *******") if @tf.nil? end end class Xalan def transform(xslt,infile,outfile) transformer = @tf.newTransformer(StreamSource.new(xslt)) transformer.transform(StreamSource.new(infile), StreamResult.new(outfile)) end def name return "jruby/xalanl" end TRANSFORMER_FACTORY_IMPL = "org.apache.xalan.processor.TransformerFactoryImpl" def initialize System.setProperty("javax.xml.transform.TransformerFactory", TRANSFORMER_FACTORY_IMPL) @tf = TransformerFactory.newInstance raise Exception.new("\n*** *******\n*** ERROR loading saxon into jruby.\n*** This error may be caused by missing xalan jars in jruby lib directory\n*** *******") if @tf.nil? end end #class XsltProcessor < Saxon ; end class XsltProcessor < Xalan ; end end # if you wanted to run this from the command line, do something like # $ jruby lib/jxslt.rb a.xsl in.xml out.xml #xalan = Mtk_xslt::XsltProcessor.new #xalan.transform(*ARGV) #saxon = JXslt::Saxon.new #saxon.transform(*ARGV) else # XSLT is not available module Mtk_xslt class XsltProcessor def initialize raise Exception.new("\n***\n***XSLT is not available on your platform. Currently supported platform are JRUBY and Windows\n***\n***") end end end end