File: xslt.rb

package info (click to toggle)
jruby 1.5.6-9
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 45,088 kB
  • ctags: 77,093
  • sloc: ruby: 398,491; java: 170,202; yacc: 3,782; xml: 2,529; sh: 299; tcl: 40; makefile: 35; ansic: 23
file content (49 lines) | stat: -rw-r--r-- 1,293 bytes parent folder | download | duplicates (8)
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