File: xslt.rb

package info (click to toggle)
jruby 9.4.8.0%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 89,244 kB
  • sloc: ruby: 548,574; java: 276,189; yacc: 25,873; ansic: 6,178; xml: 6,111; sh: 1,855; sed: 94; makefile: 78; jsp: 48; tcl: 40; exp: 12
file content (49 lines) | stat: -rw-r--r-- 1,293 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
include Java
require 'optparse'
require 'ostruct'

import java.io.FileOutputStream
import javax.xml.transform.stream.StreamSource
import javax.xml.transform.TransformerFactory

class XSLTOptions
  def self.parse(args)
    options = OpenStruct.new
    options.parameters = {}
       
    opts = OptionParser.new do |opts|
      opts.banner = "Usage: [options] xslt {xml} {xslt} [{result}]"
      opts.separator ""
      opts.separator "Specific options:"
      
      opts.on("-p", "--parameters name=value,name1=value1", Array) do |n|
	n.collect do |v| 
	  name, value = v.split(/\s*=\s*/)
	  options.parameters[name] = value
	end
      end
    end  
    opts.parse!(args)
    options
  end
end

options = XSLTOptions.parse(ARGV)

if (ARGV.length < 2 || ARGV.length > 3) 
  puts "Usage: xslt {xml} {xslt} [{result}]"
  exit
end

document = StreamSource.new ARGV[0]
stylesheet = StreamSource.new ARGV[1]
output = ARGV.length < 3 ? java.lang.System::out : FileOutputStream.new(ARGV[2])
result = javax.xml.transform.stream.StreamResult.new output

begin
  transformer = TransformerFactory.newInstance.newTransformer(stylesheet)
  options.parameters.each {|name, value| transformer.setParameter(name, value) }
  transformer.transform(document, result)
rescue java.lang.Exception => e
  puts e
end