Empact-roxml 2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. data/MIT-LICENSE +18 -0
  2. data/README.rdoc +122 -0
  3. data/Rakefile +104 -0
  4. data/lib/roxml.rb +362 -0
  5. data/lib/roxml/array.rb +15 -0
  6. data/lib/roxml/options.rb +175 -0
  7. data/lib/roxml/string.rb +35 -0
  8. data/lib/roxml/xml.rb +243 -0
  9. data/lib/roxml/xml/libxml.rb +63 -0
  10. data/lib/roxml/xml/rexml.rb +59 -0
  11. data/roxml.gemspec +78 -0
  12. data/test/fixtures/book_malformed.xml +5 -0
  13. data/test/fixtures/book_pair.xml +8 -0
  14. data/test/fixtures/book_text_with_attribute.xml +5 -0
  15. data/test/fixtures/book_valid.xml +5 -0
  16. data/test/fixtures/book_with_authors.xml +7 -0
  17. data/test/fixtures/book_with_contributions.xml +9 -0
  18. data/test/fixtures/book_with_contributors.xml +7 -0
  19. data/test/fixtures/book_with_contributors_attrs.xml +7 -0
  20. data/test/fixtures/book_with_default_namespace.xml +9 -0
  21. data/test/fixtures/book_with_depth.xml +6 -0
  22. data/test/fixtures/book_with_publisher.xml +7 -0
  23. data/test/fixtures/dictionary_of_attrs.xml +6 -0
  24. data/test/fixtures/dictionary_of_mixeds.xml +4 -0
  25. data/test/fixtures/dictionary_of_texts.xml +10 -0
  26. data/test/fixtures/library.xml +30 -0
  27. data/test/fixtures/library_uppercase.xml +30 -0
  28. data/test/fixtures/nameless_ageless_youth.xml +2 -0
  29. data/test/fixtures/person.xml +1 -0
  30. data/test/fixtures/person_with_guarded_mothers.xml +13 -0
  31. data/test/fixtures/person_with_mothers.xml +10 -0
  32. data/test/mocks/dictionaries.rb +56 -0
  33. data/test/mocks/mocks.rb +212 -0
  34. data/test/test_helper.rb +16 -0
  35. data/test/unit/options_test.rb +62 -0
  36. data/test/unit/roxml_test.rb +24 -0
  37. data/test/unit/string_test.rb +11 -0
  38. data/test/unit/to_xml_test.rb +75 -0
  39. data/test/unit/xml_attribute_test.rb +34 -0
  40. data/test/unit/xml_construct_test.rb +19 -0
  41. data/test/unit/xml_hash_test.rb +54 -0
  42. data/test/unit/xml_name_test.rb +14 -0
  43. data/test/unit/xml_namespace_test.rb +36 -0
  44. data/test/unit/xml_object_test.rb +94 -0
  45. data/test/unit/xml_text_test.rb +57 -0
  46. metadata +110 -0
@@ -0,0 +1,63 @@
1
+ require 'libxml'
2
+
3
+ module ROXML
4
+ module XML # ::nodoc::
5
+ Document = LibXML::XML::Document
6
+ Node = LibXML::XML::Node
7
+ Parser = LibXML::XML::Parser
8
+
9
+ module NamespacedSearch
10
+ def search(xpath)
11
+ if default_namespace && !xpath.include?(':')
12
+ find(namespaced(xpath),
13
+ in_default_namespace(default_namespace.href))
14
+ else
15
+ find(xpath)
16
+ end
17
+ end
18
+
19
+ private
20
+ def namespaced(xpath)
21
+ xpath.between('/') do |component|
22
+ if component =~ /\w+/ && !component.include?(':')
23
+ in_default_namespace(component)
24
+ else
25
+ component
26
+ end
27
+ end
28
+ end
29
+
30
+ def in_default_namespace(name)
31
+ "roxmldefaultnamespace:#{name}"
32
+ end
33
+ end
34
+
35
+ class Document
36
+ include NamespacedSearch
37
+
38
+ private
39
+ delegate :default_namespace, :to => :root
40
+ end
41
+
42
+ class Node
43
+ include NamespacedSearch
44
+
45
+ private
46
+ def default_namespace
47
+ @default_namespace ||= namespace && namespace.find {|n| n.to_s.nil? }
48
+ end
49
+ end
50
+
51
+ class Parser
52
+ class << self
53
+ def parse(str_data)
54
+ string(str_data).parse
55
+ end
56
+
57
+ def parse_file(path)
58
+ file(path).parse
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,59 @@
1
+ module ROXML
2
+ module XML # ::nodoc::
3
+ Document = REXML::Document
4
+ Node = REXML::Element
5
+
6
+ class Node
7
+ class << self
8
+ def new_cdata(content)
9
+ REXML::CData.new(content)
10
+ end
11
+
12
+ def new_element(name)
13
+ name = name.id2name if name.is_a? Symbol
14
+ REXML::Element.new(name)
15
+ end
16
+ end
17
+
18
+ alias_attribute :content, :text
19
+ alias :search :get_elements
20
+
21
+ def child_add(element)
22
+ if element.is_a?(REXML::CData)
23
+ REXML::CData.new(element, true, self)
24
+ else
25
+ add_element(element)
26
+ end
27
+ end
28
+
29
+ def ==(other)
30
+ to_s == other.to_s
31
+ end
32
+ end
33
+
34
+ class Parser
35
+ class << self
36
+ def parse(string)
37
+ REXML::Document.new(string)
38
+ end
39
+
40
+ def parse_file(path)
41
+ REXML::Document.new(open(path), :ignore_whitespace_nodes => :all)
42
+ end
43
+
44
+ def register_error_handler(&block)
45
+ end
46
+ end
47
+ ParseError = REXML::ParseException
48
+ end
49
+
50
+ class Document
51
+ delegate :search, :to => :root
52
+
53
+ def root=(node)
54
+ raise ArgumentError, "Root is already defined" if root
55
+ add(node)
56
+ end
57
+ end
58
+ end
59
+ end
data/roxml.gemspec ADDED
@@ -0,0 +1,78 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "roxml"
3
+ s.summary = "Ruby Object to XML mapping library"
4
+ s.version = "2.0"
5
+ s.homepage = "http://roxml.rubyforge.org"
6
+ s.platform = Gem::Platform::RUBY
7
+ s.authors = ["Ben Woosley", "Zak Mandhro", "Anders Engstrom", "Russ Olsen"]
8
+ s.email = "ben.woosley@gmail.com"
9
+ s.rubyforge_project = 'roxml'
10
+ s.files = [
11
+ 'README.rdoc',
12
+ 'MIT-LICENSE',
13
+ 'Rakefile',
14
+ 'roxml.gemspec',
15
+ 'lib/roxml.rb',
16
+ 'lib/roxml/array.rb',
17
+ 'lib/roxml/options.rb',
18
+ 'lib/roxml/string.rb',
19
+ 'lib/roxml/xml.rb',
20
+ 'lib/roxml/xml/libxml.rb',
21
+ 'lib/roxml/xml/rexml.rb',
22
+ 'test/fixtures/book_malformed.xml',
23
+ 'test/fixtures/book_pair.xml',
24
+ 'test/fixtures/book_text_with_attribute.xml',
25
+ 'test/fixtures/book_valid.xml',
26
+ 'test/fixtures/book_with_authors.xml',
27
+ 'test/fixtures/book_with_contributions.xml',
28
+ 'test/fixtures/book_with_contributors_attrs.xml',
29
+ 'test/fixtures/book_with_contributors.xml',
30
+ 'test/fixtures/book_with_default_namespace.xml',
31
+ 'test/fixtures/book_with_depth.xml',
32
+ 'test/fixtures/book_with_publisher.xml',
33
+ 'test/fixtures/dictionary_of_attrs.xml',
34
+ 'test/fixtures/dictionary_of_mixeds.xml',
35
+ 'test/fixtures/dictionary_of_texts.xml',
36
+ 'test/fixtures/library_uppercase.xml',
37
+ 'test/fixtures/library.xml',
38
+ 'test/fixtures/nameless_ageless_youth.xml',
39
+ 'test/fixtures/person_with_guarded_mothers.xml',
40
+ 'test/fixtures/person_with_mothers.xml',
41
+ 'test/fixtures/person.xml',
42
+ 'test/mocks/mocks.rb',
43
+ 'test/mocks/dictionaries.rb',
44
+ 'test/test_helper.rb',
45
+ 'test/unit/options_test.rb',
46
+ 'test/unit/roxml_test.rb',
47
+ 'test/unit/string_test.rb',
48
+ 'test/unit/to_xml_test.rb',
49
+ 'test/unit/xml_attribute_test.rb',
50
+ 'test/unit/xml_construct_test.rb',
51
+ 'test/unit/xml_hash_test.rb',
52
+ 'test/unit/xml_name_test.rb',
53
+ 'test/unit/xml_namespace_test.rb',
54
+ 'test/unit/xml_object_test.rb',
55
+ 'test/unit/xml_text_test.rb']
56
+ s.requirements << 'none'
57
+ s.require_path = 'lib'
58
+ s.test_files = [
59
+ 'test/unit/options_test.rb',
60
+ 'test/unit/roxml_test.rb',
61
+ 'test/unit/string_test.rb',
62
+ 'test/unit/to_xml_test.rb',
63
+ 'test/unit/xml_attribute_test.rb',
64
+ 'test/unit/xml_construct_test.rb',
65
+ 'test/unit/xml_hash_test.rb',
66
+ 'test/unit/xml_name_test.rb',
67
+ 'test/unit/xml_namespace_test.rb',
68
+ 'test/unit/xml_object_test.rb',
69
+ 'test/unit/xml_text_test.rb']
70
+ s.has_rdoc = true
71
+ s.description = <<EOF
72
+ ROXML is a Ruby library designed to make it easier for Ruby developers to work with XML.
73
+ Using simple annotations, it enables Ruby classes to be mapped to XML. ROXML takes care
74
+ of the marshalling and unmarshalling of mapped attributes so that developers can focus on
75
+ building first-class Ruby classes. As a result, ROXML simplifies the development of
76
+ RESTful applications, Web Services, and XML-RPC.
77
+ EOF
78
+ end
@@ -0,0 +1,5 @@
1
+ <book isbn="0201710897">
2
+ <title>The PickAxe<title>
3
+ <description><![CDATA[Probably the best Ruby book out there]]></description>
4
+ <author>David Thomas, Andrew Hunt, Dave Thomas</author>
5
+ </book>
@@ -0,0 +1,8 @@
1
+ <book isbn="0974514055">
2
+ <title>Programming Ruby - 2nd Edition</title>
3
+ <description>Second edition of the great book out there</description>
4
+ <book isbn="0974514055">
5
+ <title>Agile Web Development with Rails</title>
6
+ <description>Jolt winning original Ruby on Rails book</description>
7
+ </book>
8
+ </book>
@@ -0,0 +1,5 @@
1
+ <book ISBN="0201710897">
2
+ <title>The PickAxe</title>
3
+ <description><![CDATA[Probably the best Ruby book out there]]></description>
4
+ <author role="primary">David Thomas</author>
5
+ </book>
@@ -0,0 +1,5 @@
1
+ <book ISBN="0201710897">
2
+ <title>The PickAxe</title>
3
+ <description><![CDATA[Probably the best Ruby book out there]]></description>
4
+ <author>David Thomas, Andrew Hunt, Dave Thomas</author>
5
+ <pagecount>357</pagecount></book>
@@ -0,0 +1,7 @@
1
+ <book ISBN="0201710897">
2
+ <title>The PickAxe</title>
3
+ <description><![CDATA[Probably the best Ruby book out there]]></description>
4
+ <author>David Thomas</author>
5
+ <author>Andrew Hunt</author>
6
+ <author>Dave Thomas</author>
7
+ </book>
@@ -0,0 +1,9 @@
1
+ <book isbn="0974514055">
2
+ <title>Programming Ruby - 2nd Edition</title>
3
+ <description>Second edition of the great book out there</description>
4
+ <contributions>
5
+ <contributor role="author"><name>David Thomas</name></contributor>
6
+ <contributor role="supporting author"><name>Andrew Hunt</name></contributor>
7
+ <contributor role="supporting author"><name>Chad Fowler</name></contributor>
8
+ </contributions>
9
+ </book>
@@ -0,0 +1,7 @@
1
+ <book isbn="0974514055">
2
+ <title>Programming Ruby - 2nd Edition</title>
3
+ <description>Second edition of the great book out there</description>
4
+ <contributor role="author"><name>David Thomas</name></contributor>
5
+ <contributor role="supporting author"><name>Andrew Hunt</name></contributor>
6
+ <contributor role="supporting author"><name>Chad Fowler</name></contributor>
7
+ </book>
@@ -0,0 +1,7 @@
1
+ <book isbn="0974514055">
2
+ <title>Programming Ruby - 2nd Edition</title>
3
+ <description>Second edition of the great book out there</description>
4
+ <contributor role="author" name="David Thomas" />
5
+ <contributor role="supporting author" name="Andrew Hunt" />
6
+ <contributor role="supporting author" name="Chad Fowler" />
7
+ </book>
@@ -0,0 +1,9 @@
1
+ <book xmlns="http://booknamespace.org" isbn="0974514055">
2
+ <title>Programming Ruby - 2nd Edition</title>
3
+ <description>Second edition of the great book out there</description>
4
+ <contributions>
5
+ <contributor role="author"><name>David Thomas</name></contributor>
6
+ <contributor role="supporting author"><name>Andrew Hunt</name></contributor>
7
+ <contributor role="supporting author"><name>Chad Fowler</name></contributor>
8
+ </contributions>
9
+ </book>
@@ -0,0 +1,6 @@
1
+ <book ISDN="0201710897">
2
+ <title>The PickAxe</title>
3
+ <description><![CDATA[Probably the best Ruby book out there]]></description>
4
+ <author>David Thomas, Andrew Hunt, Dave Thomas</author>
5
+ <depth units="hundredths-meters">1130</depth>
6
+ </book>
@@ -0,0 +1,7 @@
1
+ <book isbn="0974514055">
2
+ <title>Programming Ruby - 2nd Edition</title>
3
+ <description>Second edition of the great book out there</description>
4
+ <publisher>
5
+ <name>Pragmatic Bookshelf</name>
6
+ </publisher>
7
+ </book>
@@ -0,0 +1,6 @@
1
+ <dictionary>
2
+ <definitions>
3
+ <definition dt="quaquaversally" dd="adjective: (of a geological formation) sloping downward from the center in all directions." />
4
+ <definition dt="tergiversate" dd="To use evasions or ambiguities; equivocate." />
5
+ </definitions>
6
+ </dictionary>
@@ -0,0 +1,4 @@
1
+ <dictionary>
2
+ <definition word="quaquaversally">adjective: (of a geological formation) sloping downward from the center in all directions.</definition>
3
+ <definition word="tergiversate">To use evasions or ambiguities; equivocate.</definition>
4
+ </dictionary>
@@ -0,0 +1,10 @@
1
+ <dictionary>
2
+ <definition>
3
+ <word>quaquaversally</word>
4
+ <meaning>adjective: (of a geological formation) sloping downward from the center in all directions.</meaning>
5
+ </definition>
6
+ <definition>
7
+ <word>tergiversate</word>
8
+ <meaning>To use evasions or ambiguities; equivocate.</meaning>
9
+ </definition>
10
+ </dictionary>
@@ -0,0 +1,30 @@
1
+ <library>
2
+ <name>Ruby library</name>
3
+ <book isbn="0974514055">
4
+ <title>Programming Ruby - 2nd Edition</title>
5
+ <description>Second edition of the great book out there</description>
6
+ <contributions>
7
+ <contributor role="author">
8
+ <name>David Thomas</name>
9
+ </contributor>
10
+ <contributor role="supporting author">
11
+ <name>Andrew Hunt</name>
12
+ </contributor>
13
+ <contributor role="supporting author">
14
+ <name>Chad Fowler</name>
15
+ </contributor>
16
+ </contributions>
17
+ </book>
18
+ <book isbn="0974514055">
19
+ <title>Agile Web Development with Rails</title>
20
+ <description>Jolt winning original Ruby on Rails book</description>
21
+ <contributions>
22
+ <contributor role="author">
23
+ <name>David Thomas</name>
24
+ </contributor>
25
+ <contributor role="author">
26
+ <name>David Heinemeier Hansson</name>
27
+ </contributor>
28
+ </contributions>
29
+ </book>
30
+ </library>
@@ -0,0 +1,30 @@
1
+ <library>
2
+ <NAME>Ruby library</NAME>
3
+ <BOOK isbn="0974514055">
4
+ <title>Programming Ruby - 2nd Edition</title>
5
+ <description>Second edition of the great book out there</description>
6
+ <contributions>
7
+ <contributor role="author">
8
+ <name>David Thomas</name>
9
+ </contributor>
10
+ <contributor role="supporting author">
11
+ <name>Andrew Hunt</name>
12
+ </contributor>
13
+ <contributor role="supporting author">
14
+ <name>Chad Fowler</name>
15
+ </contributor>
16
+ </contributions>
17
+ </BOOK>
18
+ <BOOK isbn="0974514055">
19
+ <title>Agile Web Development with Rails</title>
20
+ <description>Jolt winning original Ruby on Rails book</description>
21
+ <contributions>
22
+ <contributor role="author">
23
+ <name>David Thomas</name>
24
+ </contributor>
25
+ <contributor role="author">
26
+ <name>David Heinemeier Hansson</name>
27
+ </contributor>
28
+ </contributions>
29
+ </BOOK>
30
+ </library>
@@ -0,0 +1,2 @@
1
+ <person>
2
+ </person>
@@ -0,0 +1 @@
1
+ <person>Ben Franklin</person>
@@ -0,0 +1,13 @@
1
+ <person>
2
+ <name>Ben Franklin</name>
3
+ <mother>
4
+ <person>
5
+ <name>Abiah Folger</name>
6
+ <mother>
7
+ <person>
8
+ <name>Madeup Mother</name>
9
+ </person>
10
+ </mother>
11
+ </person>
12
+ </mother>
13
+ </person>
@@ -0,0 +1,10 @@
1
+ <person>
2
+ <name>Ben Franklin</name>
3
+ <mother>
4
+ <name>Abiah Folger</name>
5
+ <mother>
6
+ <name>Madeup Mother</name>
7
+ </mother>
8
+ </mother>
9
+ </person>
10
+
@@ -0,0 +1,56 @@
1
+ require 'lib/roxml'
2
+
3
+ class DictionaryOfAttrs
4
+ include ROXML
5
+
6
+ xml_name :dictionary
7
+ xml_reader :definitions, {:attrs => [:dt, :dd]}, :in => :definitions
8
+ end
9
+
10
+ class DictionaryOfTexts
11
+ include ROXML
12
+
13
+ xml_name :dictionary
14
+ xml_reader :definitions, {:key => :word,
15
+ :value => :meaning}
16
+ end
17
+
18
+ class DictionaryOfMixeds
19
+ include ROXML
20
+
21
+ xml_name :dictionary
22
+ xml_reader :definitions, {:key => {:attr => :word},
23
+ :value => :content}
24
+ end
25
+
26
+ class DictionaryOfNames
27
+ include ROXML
28
+
29
+ xml_name :dictionary
30
+ xml_reader :definitions, {:key => :name,
31
+ :value => :content}
32
+ end
33
+
34
+ class DictionaryOfGuardedNames
35
+ include ROXML
36
+
37
+ xml_name :dictionary
38
+ xml_reader :definitions, {:key => :name,
39
+ :value => :content}, :in => :definitions
40
+ end
41
+
42
+ class DictionaryOfNameClashes
43
+ include ROXML
44
+
45
+ xml_name :dictionary
46
+ xml_reader :definitions, {:key => 'name',
47
+ :value => 'content'}, :from => :definition
48
+ end
49
+
50
+ class DictionaryOfAttrNameClashes
51
+ include ROXML
52
+
53
+ xml_name :dictionary
54
+ xml_reader :definitions, {:key => {:attr => :name},
55
+ :value => 'content'}, :from => :definition
56
+ end