xqruby 1.0.0-java

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem 'blankslate', '~>2.1.2.4'
4
+
5
+ group :development do
6
+ gem "bundler", "~> 1.0.0"
7
+ gem "jeweler", "~> 1.6.2"
8
+ gem "rcov", ">= 0"
9
+ gem "rdoc", ">= 3.7"
10
+ end
File without changes
data/LICENSE ADDED
File without changes
@@ -0,0 +1,10 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.txt
4
+ Rakefile
5
+ LICENSE
6
+ lib/xqruby.rb
7
+ lib/xqruby/xqruby.rb
8
+ lib/module/lang.rb
9
+ lib/module/xom.rb
10
+ lib/java/xom-1.2.7.jar
File without changes
@@ -0,0 +1,78 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ #--
4
+ # Copyright 2012 Rajesh Cherukuri
5
+ # All Rights Reserved.
6
+ #
7
+ # Permission is granted for use, copying, modification, distribution,
8
+ # and distribution of modified versions of this work as long as the
9
+ # above copyright notice is included.
10
+ #++
11
+
12
+ # encoding: utf-8
13
+
14
+ # --------------------------------------------------------------------
15
+
16
+ require 'rubygems'
17
+ require 'bundler'
18
+ begin
19
+ Bundler.setup(:default, :development)
20
+ rescue Bundler::BundlerError => e
21
+ $stderr.puts e.message
22
+ $stderr.puts "Run `bundle install` to install missing gems"
23
+ exit e.status_code
24
+ end
25
+ require 'rake'
26
+
27
+ # --------------------------------------------------------------------
28
+
29
+ require 'jeweler'
30
+ Jeweler::Tasks.new do |gem|
31
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
32
+ gem.name = "xqruby"
33
+ gem.homepage = "http://newton.case.edu"
34
+ gem.license = "MIT"
35
+ gem.summary = "JRuby DSL for XOM"
36
+ gem.description = "JRuby DSL for XML Building and Manipulation with XPath & XQuery"
37
+ gem.email = "rxc178@case.edu"
38
+ gem.authors = ["Rajesh Cherukuri"]
39
+ gem.platform = "java"
40
+ gem.files = FileList["History.txt", "Manifest.txt", "README.rdoc", "Gemfile", "Rakefile", "LICENSE", "lib/**/*.rb", "lib/java/*.jar"]
41
+ # dependencies defined in Gemfile
42
+ end
43
+ Jeweler::RubygemsDotOrgTasks.new
44
+
45
+ # --------------------------------------------------------------------
46
+
47
+ require 'rake/testtask'
48
+ Rake::TestTask.new(:test) do |test|
49
+ test.libs << 'lib' << 'test'
50
+ test.pattern = 'test/**/test_*.rb'
51
+ test.verbose = true
52
+ end
53
+
54
+ # --------------------------------------------------------------------
55
+
56
+ require 'rcov/rcovtask'
57
+ Rcov::RcovTask.new do |test|
58
+ test.libs << 'test'
59
+ test.pattern = 'test/**/test_*.rb'
60
+ test.verbose = true
61
+ test.rcov_opts << '--exclude "gems/*"'
62
+ end
63
+
64
+ task :default => :test
65
+
66
+ # --------------------------------------------------------------------
67
+
68
+ require 'rdoc/task'
69
+ RDoc::Task.new do |rdoc|
70
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
71
+
72
+ rdoc.rdoc_dir = 'rdoc'
73
+ rdoc.title = "xqruby #{version}"
74
+ rdoc.rdoc_files.include('README*')
75
+ rdoc.rdoc_files.include('lib/**/*.rb')
76
+ end
77
+
78
+ # --------------------------------------------------------------------
Binary file
@@ -0,0 +1,32 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ #--
4
+ # Copyright 2012 Rajesh Cherukuri
5
+ # All Rights Reserved.
6
+ #
7
+ # Permission is hereby granted, free of charge, to any person obtaining
8
+ # a copy of this software and associated documentation files (the
9
+ # "Software"), to deal in the Software without restriction, including
10
+ # without limitation the rights to use, copy, modify, merge, publish,
11
+ # distribute, sublicense, and/or sell copies of the Software, and to
12
+ # permit persons to whom the Software is furnished to do so, subject to
13
+ # the following conditions:
14
+ #
15
+ # The above copyright notice and this permission notice shall be
16
+ # included in all copies or substantial portions of the Software.
17
+ #
18
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
+ #++
26
+
27
+ require 'java'
28
+
29
+ module Lang
30
+ include_package 'java.lang'
31
+ include_package 'java.io'
32
+ end
@@ -0,0 +1,34 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ #--
4
+ # Copyright 2012 Rajesh Cherukuri
5
+ # All Rights Reserved.
6
+ #
7
+ # Permission is hereby granted, free of charge, to any person obtaining
8
+ # a copy of this software and associated documentation files (the
9
+ # "Software"), to deal in the Software without restriction, including
10
+ # without limitation the rights to use, copy, modify, merge, publish,
11
+ # distribute, sublicense, and/or sell copies of the Software, and to
12
+ # permit persons to whom the Software is furnished to do so, subject to
13
+ # the following conditions:
14
+ #
15
+ # The above copyright notice and this permission notice shall be
16
+ # included in all copies or substantial portions of the Software.
17
+ #
18
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
+ #++
26
+
27
+ require 'java'
28
+ require 'java/xom-1.2.7.jar'
29
+
30
+ module XOM
31
+ include_package 'nu.xom'
32
+ include_package 'nu.xom.canonical'
33
+ include_package 'nu.xom.xinclude'
34
+ end
@@ -0,0 +1,27 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ #--
4
+ # Copyright 2012 Rajesh Cherukuri
5
+ # All Rights Reserved.
6
+ #
7
+ # Permission is hereby granted, free of charge, to any person obtaining
8
+ # a copy of this software and associated documentation files (the
9
+ # "Software"), to deal in the Software without restriction, including
10
+ # without limitation the rights to use, copy, modify, merge, publish,
11
+ # distribute, sublicense, and/or sell copies of the Software, and to
12
+ # permit persons to whom the Software is furnished to do so, subject to
13
+ # the following conditions:
14
+ #
15
+ # The above copyright notice and this permission notice shall be
16
+ # included in all copies or substantial portions of the Software.
17
+ #
18
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
+ #++
26
+
27
+ require 'xqruby/xqruby'
@@ -0,0 +1,286 @@
1
+ #--
2
+ # Copyright 2012 Rajesh Cherukuri
3
+ # All Rights Reserved.
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining
6
+ # a copy of this software and associated documentation files (the
7
+ # "Software"), to deal in the Software without restriction, including
8
+ # without limitation the rights to use, copy, modify, merge, publish,
9
+ # distribute, sublicense, and/or sell copies of the Software, and to
10
+ # permit persons to whom the Software is furnished to do so, subject to
11
+ # the following conditions:
12
+ #
13
+ # The above copyright notice and this permission notice shall be
14
+ # included in all copies or substantial portions of the Software.
15
+ #
16
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+ #++
24
+
25
+ require 'rubygems'
26
+ require 'blankslate'
27
+ require 'java'
28
+ require 'module/lang'
29
+ require 'module/xom'
30
+
31
+ module Xqruby
32
+
33
+ class Document < BlankSlate
34
+
35
+ # Create an Xqruby Document
36
+ # The method expects a hash that contain the possible elements
37
+ # :root => value : The name of the root or top-most element of the document
38
+ # :xml => value : A string containing an XML document
39
+ # :url => value : A URL pointing to an XML document
40
+ # :file => filename : A filename pointing to an XML file
41
+ # :validate => true|false : whether to validate the document being read from
42
+ # an XML string, URL or file
43
+ # :resolve_includes => true|false replaces xi:include elements by the content they refer to
44
+ # :namespace => provides a namespace prefix to the elements
45
+ def initialize(options)
46
+ # extract options
47
+ validate = options[:validate] ? options[:validate] : false
48
+ root = options[:root]
49
+ xml = options[:xml]
50
+ url = options[:url]
51
+ file = options[:file]
52
+ namespace = options[:namespace]
53
+ resolve_includes = options[:resolve_includes] ? options[:resolve_includes] : false
54
+
55
+ if root
56
+ unless namespace
57
+ @root = XOM::Element.new "#{root}"
58
+ else
59
+ @root = XOM::Element.new "#{root}", namespace
60
+ prefix = root.to_s.split(":").first if root.include? ":"
61
+ (@namespaces ||= {})[prefix] = namespace if prefix
62
+ end
63
+
64
+ @doc = XOM::Document.new @root
65
+ else
66
+ builder = XOM::Builder.new validate
67
+ end
68
+
69
+ if xml
70
+ @doc = builder.build(xml, nil)
71
+ elsif url
72
+ @doc = builder.build url
73
+ elsif file
74
+ java_file = Lang::File.new file
75
+ @doc = builder.build java_file
76
+ end
77
+
78
+ if resolve_includes
79
+ @doc = XOM::XIncluder.resolve(@doc)
80
+ end
81
+
82
+ @root = @doc.get_root_element unless @root
83
+ @target = @root
84
+ end
85
+
86
+ # Dynamic Proxy behavior
87
+ # Add XML elements (tags) based on the name of the method called on the
88
+ # Document instance, blocks passed get processed recursively
89
+ def method_missing(sym, *args, &block)
90
+ if sym.to_s != 'class' && sym.to_s != 'to_s' && sym.to_s != 'inspect' # WTF? If I don't do this I
91
+ text = nil # end up with extraneous tags
92
+ attrs = nil # in the resulting document!!
93
+ namespace = nil
94
+ sym = "#{sym}:#{args.shift}" if args.first.kind_of?(Symbol)
95
+ prefix = sym.to_s.split(":").first if sym.to_s.include? ":"
96
+ args.each do |arg|
97
+ case arg
98
+ when Hash
99
+ if arg.has_key? :namespace
100
+ namespace = arg[:namespace]
101
+ prefix = sym.to_s.split(":").first
102
+ else
103
+ attrs ||= {}
104
+ attrs.merge!(arg)
105
+ end
106
+ else
107
+ text ||= ''
108
+ text << arg.to_s
109
+ end
110
+ end
111
+
112
+ # try to get the namespace from the saved namespaces
113
+ if prefix != nil && namespace.nil? && @namespaces != nil
114
+ namespace = "#{@namespaces[prefix]}"
115
+ end
116
+
117
+ unless namespace
118
+ tag = XOM::Element.new sym.to_s
119
+ else
120
+ tag = XOM::Element.new sym.to_s, namespace
121
+ end
122
+
123
+ _add_attributes(tag, attrs)
124
+
125
+ if block
126
+ _nested_structures(block, tag)
127
+ elsif !text.nil?
128
+ tag.append_child text
129
+ end
130
+
131
+ @target.append_child tag
132
+ end
133
+
134
+ self
135
+ end
136
+
137
+ # Returns the XML document as a single line, e.g. no formatting
138
+ def to_xml
139
+ @doc.to_xml
140
+ end
141
+
142
+ # Appends a comment node at the current position in the document
143
+ def comment!(string)
144
+ comment = XOM::Comment.new string
145
+ @target.append_child(comment)
146
+ self
147
+ end
148
+
149
+ # Appends a text node at the current position in the document
150
+ def text!(string)
151
+ text = XOM::Text.new string
152
+ @target.append_child(text)
153
+ self
154
+ end
155
+
156
+ # Appends a processing instruction at the current position in the document
157
+ def processing_instruction!(target, data)
158
+ pi = XOM::ProcessingInstruction.new(target, data)
159
+ @target.append_child(pi)
160
+ self
161
+ end
162
+
163
+ # Attempts to reposition the document pointer based on the first match of
164
+ # and XQuery expression. Return true is the pointer was successfully moved
165
+ # and false otherwise
166
+ def target!(xpath_query)
167
+ nodes = @doc.query xpath_query
168
+ (0..nodes.size-1).each do |i|
169
+ node = nodes.get(i)
170
+ if node.class == XOM::Element
171
+ @target = node
172
+ return true
173
+ end
174
+ end
175
+ return false
176
+ end
177
+
178
+ # Create a tag named +sym+. Other than the first argument which
179
+ # is the tag name, the arguments are the same as the tags
180
+ # implemented via <tt>method_missing</tt>.
181
+ # XOM will throw an exception if you pass something with a prefix but no
182
+ # associated namespace
183
+ def tag!(sym, *args, &block)
184
+ method_missing(sym.to_sym, *args, &block)
185
+ self
186
+ end
187
+
188
+ # Returns a pretty-formatted document with the given indent, max line length
189
+ # and encoding. The preserve_base_uri determines whether preserves the
190
+ # original base URIs by inserting extra xml:base attributes.
191
+ def to_pretty_xml(indent=2, line_length=0, encoding='utf-8', preserve_base_uri=true)
192
+ baos = Lang::ByteArrayOutputStream.new
193
+ serializer = XOM::Serializer.new(baos, encoding)
194
+ serializer.indent = indent
195
+ serializer.max_length = line_length
196
+ serializer.write @doc
197
+ baos.to_string
198
+ end
199
+
200
+ # Appends an XML document type declaration at the current position in the
201
+ # document
202
+ def doc_type!(root_element_name, public_id='', system_id='')
203
+ doctype = XOM::DocType(root_element_name, public_id, system_id)
204
+ @doc.insert_child(doctype, 0)
205
+ self
206
+ end
207
+
208
+ # Returns the value of the first child element with the specified name in
209
+ # no namespace. If there is no such element, it returns nill.
210
+ def find_first_tag_by_name(tag_name)
211
+ element = @root.get_first_child_element tag_name
212
+ element ? element.get_value : nil
213
+ end
214
+
215
+ # Returns the values of the nodes selected by the XPath expression in the
216
+ # context of this node in document order as defined by XSLT. This XPath
217
+ # expression must not contain any namespace prefixes.
218
+ def query(xpath_query)
219
+ nodes = @doc.query xpath_query
220
+ result = Array.new
221
+ (0..nodes.size-1).each do |i| result << nodes.get(i).get_value end
222
+ result
223
+ end
224
+
225
+ # See Jim Weirich's comment on builder (plus all the test frameworks seem
226
+ # to call nil? on objects being tested
227
+ def nil?
228
+ false
229
+ end
230
+
231
+ # Returns the value of the document as defined by XPath 1.0. This is the
232
+ # same as the value of the root element, which is the complete PCDATA
233
+ # content of the root element, without any tags, comments, or processing
234
+ # instructions after all entity and character references have been resolved.
235
+ def extract_text
236
+ @doc.get_value
237
+ end
238
+
239
+ # Returns XML in the format specified by the Canonica XML Version 1.0
240
+ # (w3.org/TR/2001/REC-xml-c14n-20010315 or Exclusive XML)
241
+ # Canonicalization Version 1.0 (w3.org/TR/2002/REC-xml-exc-c14n-20020718/)
242
+ def to_canonical_form
243
+ baos = Lang::ByteArrayOutputStream.new
244
+ outputter = XOM::Canonicalizer.new baos
245
+ outputter.write(@doc)
246
+ baos.to_string
247
+ end
248
+
249
+ # Adds attributes to the current target element (pointer to an element in the document
250
+ # by default the root of the document)
251
+ def attributes(attrs)
252
+ _add_attributes(@target, attrs)
253
+ end
254
+
255
+ private
256
+
257
+ # Adds attributes to the given Element (tag) as define by the parameter
258
+ # target, passing an attribute with a value of nil, removes the attribute if
259
+ # it exists
260
+ def _add_attributes(target, attrs)
261
+ if attrs
262
+ attrs.each do |key, value|
263
+ unless value.nil?
264
+ attribute = XOM::Attribute.new(key.to_s, value.to_s)
265
+ target.add_attribute(attribute)
266
+ else
267
+ attribute = target.get_attribute(key.to_s)
268
+ target.remove_attribute(attribute) if attribute
269
+ end
270
+ end
271
+ end
272
+ end
273
+
274
+ # Recursively processed blocks passed to document in the context of a method
275
+ # call
276
+ def _nested_structures(block, new_target)
277
+ old_target = @target
278
+ @target = new_target
279
+ block.call(self)
280
+ ensure
281
+ @target = old_target
282
+ end
283
+
284
+ end
285
+
286
+ end
metadata ADDED
@@ -0,0 +1,121 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: xqruby
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 1.0.0
6
+ platform: java
7
+ authors:
8
+ - Rajesh Cherukuri
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2012-01-05 00:00:00 -05:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: blankslate
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ~>
23
+ - !ruby/object:Gem::Version
24
+ version: 2.1.2.4
25
+ type: :runtime
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ prerelease: false
30
+ requirement: &id002 !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ~>
34
+ - !ruby/object:Gem::Version
35
+ version: 1.0.0
36
+ type: :development
37
+ version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
39
+ name: jeweler
40
+ prerelease: false
41
+ requirement: &id003 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ~>
45
+ - !ruby/object:Gem::Version
46
+ version: 1.6.2
47
+ type: :development
48
+ version_requirements: *id003
49
+ - !ruby/object:Gem::Dependency
50
+ name: rcov
51
+ prerelease: false
52
+ requirement: &id004 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "0"
58
+ type: :development
59
+ version_requirements: *id004
60
+ - !ruby/object:Gem::Dependency
61
+ name: rdoc
62
+ prerelease: false
63
+ requirement: &id005 !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: "3.7"
69
+ type: :development
70
+ version_requirements: *id005
71
+ description: JRuby DSL for XML Building and Manipulation with XPath & XQuery
72
+ email: rxc178@case.edu
73
+ executables: []
74
+
75
+ extensions: []
76
+
77
+ extra_rdoc_files:
78
+ - LICENSE
79
+ - README.rdoc
80
+ files:
81
+ - Gemfile
82
+ - History.txt
83
+ - LICENSE
84
+ - Manifest.txt
85
+ - README.rdoc
86
+ - Rakefile
87
+ - lib/xqruby.rb
88
+ - lib/xqruby/xqruby.rb
89
+ - lib/java/xom-1.2.7.jar
90
+ - lib/module/lang.rb
91
+ - lib/module/xom.rb
92
+ has_rdoc: true
93
+ homepage:
94
+ licenses:
95
+ - MIT
96
+ post_install_message:
97
+ rdoc_options: []
98
+
99
+ require_paths:
100
+ - lib
101
+ required_ruby_version: !ruby/object:Gem::Requirement
102
+ none: false
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: "0"
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ none: false
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: "0"
113
+ requirements: []
114
+
115
+ rubyforge_project:
116
+ rubygems_version: 1.5.1
117
+ signing_key:
118
+ specification_version: 3
119
+ summary: JRuby DSL for XOM
120
+ test_files: []
121
+