xml_schema 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/Rakefile +2 -0
- data/lib/xml_schema/ns.rb +29 -0
- data/lib/xml_schema/version.rb +3 -0
- data/lib/xml_schema.rb +80 -0
- data/xml_schema.gemspec +24 -0
- metadata +54 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'uri'
|
2
|
+
|
3
|
+
module NS
|
4
|
+
|
5
|
+
ALL = {
|
6
|
+
'XMLSchema' => 'http://www.w3.org/2001/XMLSchema',
|
7
|
+
'OWL' => 'http://www.w3.org/2002/07/owl',
|
8
|
+
'RDF' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns',
|
9
|
+
'RDFS' => 'http://www.w3.org/2000/01/rdf-schema'
|
10
|
+
}
|
11
|
+
|
12
|
+
ALL.each do |name, url|
|
13
|
+
ns_module = self.const_set(name, Module.new)
|
14
|
+
ns_module.module_eval <<-HERE
|
15
|
+
def self.uri
|
16
|
+
\"#{url}\"
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.to_s
|
20
|
+
\"\#{uri}#\"
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.[](value)
|
24
|
+
URI.parse(\"\#{self}\#{value}\")
|
25
|
+
end
|
26
|
+
HERE
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
data/lib/xml_schema.rb
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
$:.unshift(File.dirname(__FILE__)) unless
|
2
|
+
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
|
3
|
+
|
4
|
+
require 'xml_schema/ns'
|
5
|
+
require 'time'
|
6
|
+
|
7
|
+
# Slightly modified xml_schema from Redlander project
|
8
|
+
module XmlSchema
|
9
|
+
|
10
|
+
TYPES = %w(int boolean float dateTime time date string decimal double duration gYearMonth gYear gMonthDay gDay gMonth hexBinary base64Binary anyURI QName NOTATION)
|
11
|
+
|
12
|
+
|
13
|
+
# Obtain XML Schema datatype (as RDF node) for a Ruby object.
|
14
|
+
def self.datatype_of(object)
|
15
|
+
#--
|
16
|
+
# TODO: decimal, double, duration (Range?), gYearMonth, gYear, gMonthDay, gDay, gMonth, hexBinary, base64Binary, anyURI, QName, NOTATION
|
17
|
+
# TODO: language tag?
|
18
|
+
#++
|
19
|
+
case object.class.name
|
20
|
+
when "Fixnum", "Integer"
|
21
|
+
NS::XMLSchema['int'].to_s
|
22
|
+
when "TrueClass", "FalseClass"
|
23
|
+
NS::XMLSchema['boolean'].to_s
|
24
|
+
when "Float"
|
25
|
+
NS::XMLSchema['float'].to_s
|
26
|
+
when "DateTime"
|
27
|
+
NS::XMLSchema['dateTime'].to_s
|
28
|
+
when "Time"
|
29
|
+
NS::XMLSchema['time'].to_s
|
30
|
+
when "Date"
|
31
|
+
NS::XMLSchema['date'].to_s
|
32
|
+
when "String"
|
33
|
+
NS::XMLSchema['string'].to_s
|
34
|
+
else
|
35
|
+
raise RedlandError.new("#{object.class} cannot be coerced into any XMLSchema datatype")
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
# Instantiate a Ruby object from a literal string and datatype URI.
|
40
|
+
# If +datatype_uri+ is not specified,
|
41
|
+
# +literal+ is interpreted as RDF typed literal (with datatype postfix).
|
42
|
+
def self.instantiate(literal, datatype_uri = nil)
|
43
|
+
# NOTE: removing language tag (e.g. "@en")
|
44
|
+
full_literal = datatype_uri ? literal.sub(/@\w+$/, '') + "^^<#{datatype_uri}>" : literal.sub(/@\w+$/, '')
|
45
|
+
literal_value, literal_type = full_literal.split('^^')
|
46
|
+
datatype = if literal_type
|
47
|
+
ns, l_type = literal_type.delete("<>").split('#')
|
48
|
+
if ns == NS::XMLSchema.uri && TYPES.include?(l_type)
|
49
|
+
l_type
|
50
|
+
else
|
51
|
+
raise "Incompatible datatype URI! (#{ns})"
|
52
|
+
end
|
53
|
+
else
|
54
|
+
'string'
|
55
|
+
end
|
56
|
+
|
57
|
+
# clean-up the literal_value
|
58
|
+
literal_value.sub!(/^["']/, '')
|
59
|
+
literal_value.sub!(/["']$/, '')
|
60
|
+
|
61
|
+
case datatype
|
62
|
+
when 'int'
|
63
|
+
literal_value.to_i
|
64
|
+
when 'boolean'
|
65
|
+
%w(1 true).include?(literal_value)
|
66
|
+
when 'dateTime'
|
67
|
+
DateTime.parse(literal_value)
|
68
|
+
when 'date'
|
69
|
+
Date.parse(literal_value)
|
70
|
+
when 'time'
|
71
|
+
Time.parse(literal_value)
|
72
|
+
when 'float'
|
73
|
+
literal_value.to_f
|
74
|
+
else
|
75
|
+
# FIXME: fallback for unknown datatypes and 'string'
|
76
|
+
literal_value
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
data/xml_schema.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
$:.unshift(File.dirname(__FILE__)) unless
|
4
|
+
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
|
5
|
+
|
6
|
+
require "lib/xml_schema/version"
|
7
|
+
|
8
|
+
Gem::Specification.new do |s|
|
9
|
+
s.name = "xml_schema"
|
10
|
+
s.version = XmlSchema::VERSION
|
11
|
+
s.platform = Gem::Platform::RUBY
|
12
|
+
s.authors = ["Slava Kravchenko"]
|
13
|
+
s.email = ["slava.kravchenko@gmail.com"]
|
14
|
+
s.homepage = "https://github.com/cordawyn/xml_schema"
|
15
|
+
s.summary = %q{Helper methods and constants to handle XmlSchema}
|
16
|
+
s.description = %q{Helper methods and constants to handle XmlSchema}
|
17
|
+
|
18
|
+
s.rubyforge_project = "xml_schema"
|
19
|
+
|
20
|
+
s.files = `git ls-files`.split("\n")
|
21
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
22
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
23
|
+
s.require_paths = ["lib"]
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: xml_schema
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Slava Kravchenko
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-06-17 00:00:00.000000000 +03:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
description: Helper methods and constants to handle XmlSchema
|
16
|
+
email:
|
17
|
+
- slava.kravchenko@gmail.com
|
18
|
+
executables: []
|
19
|
+
extensions: []
|
20
|
+
extra_rdoc_files: []
|
21
|
+
files:
|
22
|
+
- .gitignore
|
23
|
+
- Gemfile
|
24
|
+
- Rakefile
|
25
|
+
- lib/xml_schema.rb
|
26
|
+
- lib/xml_schema/ns.rb
|
27
|
+
- lib/xml_schema/version.rb
|
28
|
+
- xml_schema.gemspec
|
29
|
+
has_rdoc: true
|
30
|
+
homepage: https://github.com/cordawyn/xml_schema
|
31
|
+
licenses: []
|
32
|
+
post_install_message:
|
33
|
+
rdoc_options: []
|
34
|
+
require_paths:
|
35
|
+
- lib
|
36
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
37
|
+
none: false
|
38
|
+
requirements:
|
39
|
+
- - ! '>='
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
43
|
+
none: false
|
44
|
+
requirements:
|
45
|
+
- - ! '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
requirements: []
|
49
|
+
rubyforge_project: xml_schema
|
50
|
+
rubygems_version: 1.6.2
|
51
|
+
signing_key:
|
52
|
+
specification_version: 3
|
53
|
+
summary: Helper methods and constants to handle XmlSchema
|
54
|
+
test_files: []
|