saxaphone 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Matthew Higgins
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
File without changes
data/Rakefile ADDED
@@ -0,0 +1,23 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
7
+
8
+ desc 'Test.'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.libs << 'test'
12
+ t.pattern = 'test/**/*_test.rb'
13
+ t.verbose = true
14
+ end
15
+
16
+ desc 'Generate documentation.'
17
+ Rake::RDocTask.new(:rdoc) do |rdoc|
18
+ rdoc.rdoc_dir = 'rdoc'
19
+ rdoc.title = 'Saxaphone'
20
+ rdoc.options << '--line-numbers' << '--inline-source'
21
+ rdoc.rdoc_files.include('README')
22
+ rdoc.rdoc_files.include('lib/**/*.rb')
23
+ end
@@ -0,0 +1,34 @@
1
+ require 'nokogiri'
2
+
3
+ module Saxaphone
4
+ class Document < Nokogiri::XML::SAX::Document
5
+ class << self
6
+ def parse(io, root_element_class)
7
+ parser = Nokogiri::XML::SAX::Parser.new(new(root_element_class))
8
+ parser.parse(io)
9
+ end
10
+ end
11
+
12
+ attr_accessor :element_stack, :root_element_class
13
+ def initialize(root_element_class)
14
+ @element_stack = []
15
+ @root_element_class = root_element_class
16
+ end
17
+
18
+ def start_element(name, attributes = [])
19
+ new_element = element_stack.empty? ? root_element_class.new(name) : element_stack.last.new_element(name)
20
+ element_stack << new_element
21
+ end
22
+
23
+ def end_element(name)
24
+ ending_element = element_stack.pop
25
+ unless element_stack.empty?
26
+ element_stack.last.add_element(ending_element)
27
+ end
28
+ end
29
+
30
+ def characters(string)
31
+ element_stack.last.append_content(string)
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,74 @@
1
+ module Saxaphone
2
+ class Element
3
+ @@base_class_name = 'Saxaphone::Element'
4
+
5
+ class ElementHandler < Struct.new(:class_name, :proc)
6
+ end
7
+
8
+ class << self
9
+ def setup(&block)
10
+ define_method(:setup, &block)
11
+ end
12
+
13
+ def element_attributes(element_names)
14
+ element_names.each do |element_name|
15
+ element_attribute(element_name)
16
+ end
17
+ end
18
+
19
+ def element_attribute(element_name, options = {})
20
+ converted_name = options.delete(:as) || element_name
21
+ logic = proc { |element| attributes[converted_name] = element.content }
22
+ element_handler = ElementHandler.new(@@base_class_name, logic)
23
+
24
+ element_handlers[element_name] = element_handler
25
+ end
26
+
27
+ def has_element(element_name, class_name = @@base_class_name, &block)
28
+ element_handlers[element_name] = ElementHandler.new(class_name, block)
29
+ end
30
+
31
+ def has_attributes(attribute_names)
32
+ raise 'not implemented'
33
+ end
34
+
35
+ def element_handlers
36
+ @element_handlers ||= {}
37
+ end
38
+
39
+ def parse(xml)
40
+ Saxaphone::Document.parse(xml, self)
41
+ end
42
+ end
43
+
44
+ attr_accessor :name, :content, :attributes
45
+ def initialize(name = '', content = '')
46
+ self.name = name
47
+ self.content = content
48
+ self.attributes = {}
49
+ setup
50
+ end
51
+
52
+ def setup
53
+ end
54
+
55
+ def add_element(element)
56
+ if element_handler = self.class.element_handlers[element.name]
57
+ instance_exec(element, &element_handler.proc)
58
+ end
59
+ end
60
+
61
+ def new_element(element_name)
62
+ if element_handler = self.class.element_handlers[element_name]
63
+ klass = element_handler.class_name.constantize
64
+ else
65
+ klass = Saxaphone::Element
66
+ end
67
+ klass.new(element_name)
68
+ end
69
+
70
+ def append_content(string)
71
+ content << string
72
+ end
73
+ end
74
+ end
data/lib/saxaphone.rb ADDED
@@ -0,0 +1,4 @@
1
+ module Saxaphone
2
+ autoload :Document, 'saxaphone/document'
3
+ autoload :Element, 'saxaphone/element'
4
+ end
@@ -0,0 +1,22 @@
1
+ require 'test_helper'
2
+
3
+ class Saxaphone::DocumentTest < MiniTest::Spec
4
+ class TestDocument < Saxaphone::Document
5
+ class FooElement < Saxaphone::Element
6
+ end
7
+ end
8
+
9
+ def test_parse_xml
10
+ # assert false
11
+ end
12
+
13
+ private
14
+ def xml
15
+ StringIO.new %{<?xml version="1.0" encoding="utf-8"?>
16
+ <foo>
17
+ <hola>como estas</hola>
18
+ <omg>wtf</omg>
19
+ </foo>
20
+ }
21
+ end
22
+ end
@@ -0,0 +1,52 @@
1
+ require 'test_helper'
2
+
3
+ class Saxaphone::ElementTest < MiniTest::Spec
4
+ class TestChildElement < Saxaphone::Element
5
+ attr_accessor :special
6
+ end
7
+
8
+ class TestElement < Saxaphone::Element
9
+ attr_accessor :setup_attr
10
+ attr_accessor :child_special
11
+ setup do
12
+ self.setup_attr = 'hola'
13
+ end
14
+
15
+ element_attributes %w(foo bar)
16
+ element_attribute 'faz', as: 'baz'
17
+
18
+ has_element 'omg', 'TestChildElement' do |element|
19
+ self.child_special = element.special
20
+ end
21
+ end
22
+
23
+ def test_setup
24
+ element = TestElement.new
25
+ assert_equal 'hola', element.setup_attr
26
+ end
27
+
28
+ def test_element_attributes
29
+ element = TestElement.new
30
+ element.add_element(Saxaphone::Element.new('foo', 'value1'))
31
+ element.add_element(Saxaphone::Element.new('bar', 'value2'))
32
+ element.add_element(Saxaphone::Element.new('invalid', 'value3'))
33
+
34
+ assert_equal({'foo' => 'value1', 'bar' => 'value2'}, element.attributes)
35
+ end
36
+
37
+ def test_element_attribute
38
+ element = TestElement.new
39
+ element.add_element(Saxaphone::Element.new('faz', 'value'))
40
+
41
+ assert_equal({'baz' => 'value'}, element.attributes)
42
+ end
43
+
44
+ def test_has_element
45
+ element = TestElement.new
46
+ child_element = TestChildElement.new('omg')
47
+ child_element.special = 'weee'
48
+ element.add_element(child_element)
49
+
50
+ assert_equal 'weee', element.child_special
51
+ end
52
+ end
@@ -0,0 +1,2 @@
1
+ require 'saxaphone'
2
+ require 'minitest/autorun'
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: saxaphone
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors:
8
+ - Matthew Higgins
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-05-26 00:00:00 -07:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: nokogiri
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: "0"
25
+ type: :runtime
26
+ version_requirements: *id001
27
+ description: Use objects
28
+ email: developer@matthewhiggins.com
29
+ executables: []
30
+
31
+ extensions: []
32
+
33
+ extra_rdoc_files:
34
+ - README.rdoc
35
+ files:
36
+ - MIT-LICENSE
37
+ - Rakefile
38
+ - README.rdoc
39
+ - lib/saxaphone/document.rb
40
+ - lib/saxaphone/element.rb
41
+ - lib/saxaphone.rb
42
+ - test/document_test.rb
43
+ - test/element_test.rb
44
+ - test/test_helper.rb
45
+ has_rdoc: true
46
+ homepage: http://github.com/matthuhiggins/saxaphone
47
+ licenses: []
48
+
49
+ post_install_message:
50
+ rdoc_options: []
51
+
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: 1.9.2
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: 1.3.5
66
+ requirements: []
67
+
68
+ rubyforge_project:
69
+ rubygems_version: 1.6.2
70
+ signing_key:
71
+ specification_version: 3
72
+ summary: Sexy SAX Parsing
73
+ test_files: []
74
+