tilia-xml 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +19 -0
  3. data/.rubocop.yml +32 -0
  4. data/.simplecov +4 -0
  5. data/.travis.yml +3 -0
  6. data/CHANGELOG.sabre.md +167 -0
  7. data/CONTRIBUTING.md +25 -0
  8. data/Gemfile +15 -0
  9. data/Gemfile.lock +56 -0
  10. data/LICENSE +27 -0
  11. data/LICENSE.sabre +27 -0
  12. data/README.md +30 -0
  13. data/Rakefile +17 -0
  14. data/lib/tilia/xml/context_stack_trait.rb +99 -0
  15. data/lib/tilia/xml/element/base.rb +73 -0
  16. data/lib/tilia/xml/element/cdata.rb +53 -0
  17. data/lib/tilia/xml/element/elements.rb +109 -0
  18. data/lib/tilia/xml/element/key_value.rb +110 -0
  19. data/lib/tilia/xml/element/uri.rb +98 -0
  20. data/lib/tilia/xml/element/xml_fragment.rb +128 -0
  21. data/lib/tilia/xml/element.rb +22 -0
  22. data/lib/tilia/xml/lib_xml_exception.rb +9 -0
  23. data/lib/tilia/xml/parse_exception.rb +7 -0
  24. data/lib/tilia/xml/reader.rb +240 -0
  25. data/lib/tilia/xml/service.rb +151 -0
  26. data/lib/tilia/xml/version.rb +9 -0
  27. data/lib/tilia/xml/writer.rb +261 -0
  28. data/lib/tilia/xml/xml_deserializable.rb +29 -0
  29. data/lib/tilia/xml/xml_serializable.rb +27 -0
  30. data/lib/tilia/xml.rb +23 -0
  31. data/test/test_helper.rb +4 -0
  32. data/test/xml/context_stack_test.rb +40 -0
  33. data/test/xml/element/cdata_test.rb +37 -0
  34. data/test/xml/element/eater.rb +60 -0
  35. data/test/xml/element/elements_test.rb +113 -0
  36. data/test/xml/element/key_value_test.rb +187 -0
  37. data/test/xml/element/mock.rb +52 -0
  38. data/test/xml/element/uri_test.rb +55 -0
  39. data/test/xml/element/xml_fragment_test.rb +121 -0
  40. data/test/xml/infite_loop_test.rb +47 -0
  41. data/test/xml/reader_test.rb +407 -0
  42. data/test/xml/service_test.rb +156 -0
  43. data/test/xml/writer_test.rb +260 -0
  44. data/tilia-xml.gemspec +15 -0
  45. metadata +132 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a3fb4181ed07ce2430cb07b7fb523b084bf745a2
4
+ data.tar.gz: c47a873a3d33f0605a0f5427b42beae183a8b2c6
5
+ SHA512:
6
+ metadata.gz: e67f3310cad179234b9539320a0930ab2f0056438d9f49fda16cabb0640ee9f12bb51f64e108e72f2bf9687186213aef77227bacbe9bd6bad0b98e99f5629d2b
7
+ data.tar.gz: 35c16b92d5fabb186167abcb33c47c8d4f35c0866326724585fa41b3c656d0bbdac050bc48164a601879fbec5f9ed4c564655014d49f1dd84c091e561a0f2c1b
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ # Gem files
2
+ /*.gem
3
+
4
+ # backup files
5
+ *~
6
+
7
+ # swap files
8
+ *.swp
9
+ *.kate-swp
10
+
11
+ # file manager files
12
+ *.directory
13
+
14
+ # coverage
15
+ /coverage
16
+
17
+ # documentation
18
+ /doc
19
+ /.yardoc
data/.rubocop.yml ADDED
@@ -0,0 +1,32 @@
1
+ # Disable metrics - we stick to the sabre coding
2
+ Metrics/AbcSize:
3
+ Enabled: false
4
+
5
+ Metrics/BlockNesting:
6
+ Enabled: false
7
+
8
+ Metrics/ClassLength:
9
+ Enabled: false
10
+
11
+ Metrics/ModuleLength:
12
+ Enabled: false
13
+
14
+ Metrics/CyclomaticComplexity:
15
+ Enabled: false
16
+
17
+ Metrics/LineLength:
18
+ Enabled: false
19
+
20
+ Metrics/MethodLength:
21
+ Enabled: false
22
+
23
+ Metrics/ParameterLists:
24
+ Enabled: false
25
+
26
+ Metrics/PerceivedComplexity:
27
+ Enabled: false
28
+
29
+ # ['word', 'array'] looks better when aligned with other arrays that can't use
30
+ # %w() syntax
31
+ Style/WordArray:
32
+ Enabled: false
data/.simplecov ADDED
@@ -0,0 +1,4 @@
1
+ SimpleCov.start do
2
+ add_filter "/spec/"
3
+ end
4
+
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.3
@@ -0,0 +1,167 @@
1
+ ChangeLog
2
+ =========
3
+
4
+ 1.2.0 (2015-08-30)
5
+ ------------------
6
+
7
+ * #53: Added `parseGetElements`, a function like `parseInnerTree`, except
8
+ that it always returns an array of elements, or an empty array.
9
+
10
+
11
+ 1.1.0 (2015-06-29)
12
+ ------------------
13
+
14
+ * #44, #45: Catching broken and invalid XML better and throwing
15
+ `Sabre\Xml\LibXMLException` whenever we encounter errors. (@stefanmajoor,
16
+ @DaanBiesterbos)
17
+
18
+
19
+ 1.0.0 (2015-05-25)
20
+ ------------------
21
+
22
+ * No functional changes since 0.4.3. Marking it as 1.0.0 as a promise for
23
+ API stability.
24
+ * Using php-cs-fixer for automated CS enforcement.
25
+
26
+
27
+ 0.4.3 (2015-04-01)
28
+ -----------------
29
+
30
+ * Minor tweaks for the public release.
31
+
32
+
33
+ 0.4.2 (2015-03-20)
34
+ ------------------
35
+
36
+ * Removed `constants.php` again. They messed with PHPUnit and don't really
37
+ provide a great benefit.
38
+ * #41: Correctly handle self-closing xml elements.
39
+
40
+
41
+ 0.4.1 (2015-03-19)
42
+ ------------------
43
+
44
+ * #40: An element with an empty namespace (xmlns="") is not allowed to have a
45
+ prefix. This is now fixed.
46
+
47
+
48
+ 0.4.0 (2015-03-18)
49
+ ------------------
50
+
51
+ * Added `Sabre\Xml\Service`. This is intended as a simple way to centrally
52
+ configure xml applications and easily parse/write things from there. #35, #38.
53
+ * Renamed 'baseUri' to 'contextUri' everywhere.
54
+ * #36: Added a few convenience constants to `lib/constants.php`.
55
+ * `Sabre\Xml\Util::parseClarkNotation` is now in the `Sabre\Xml\Service` class.
56
+
57
+
58
+ 0.3.1 (2015-02-08)
59
+ ------------------
60
+
61
+ * Added `XmlDeserializable` to match `XmlSerializable`.
62
+
63
+
64
+ 0.3.0 (2015-02-06)
65
+ ------------------
66
+
67
+ * Added `$elementMap` argument to parseInnerTree, for quickly overriding
68
+ parsing rules within an element.
69
+
70
+
71
+ 0.2.2 (2015-02-05)
72
+ ------------------
73
+
74
+ * Now depends on sabre/uri 1.0.
75
+
76
+
77
+ 0.2.1 (2014-12-17)
78
+ ------------------
79
+
80
+ * LibXMLException now inherits from ParseException, so it's easy for users to
81
+ catch any exception thrown by the parser.
82
+
83
+
84
+ 0.2.0 (2014-12-05)
85
+ ------------------
86
+
87
+ * Major BC Break: method names for the Element interface have been renamed
88
+ from `serializeXml` and `deserializeXml` to `xmlSerialize` and
89
+ `xmlDeserialize`. This is so that it matches PHP's `JsonSerializable`
90
+ interface.
91
+ * #25: Added `XmlSerializable` to allow people to write serializers without
92
+ having to implement a deserializer in the same class.
93
+ * #26: Renamed the `Sabre\XML` namespace to `Sabre\Xml`. Due to composer magic
94
+ and the fact that PHP namespace are case-insensitive, this should not affect
95
+ anyone, unless you are doing exact string matches on class names.
96
+ * #23: It's not possible to automatically extract or serialize Xml fragments
97
+ from documents using `Sabre\Xml\Element\XmlFragment`.
98
+
99
+
100
+ 0.1.0 (2014-11-24)
101
+ ------------------
102
+
103
+ * #16: Added ability to override `elementMap`, `namespaceMap` and `baseUri` for
104
+ a fragment of a document during reading an writing using `pushContext` and
105
+ `popContext`.
106
+ * Removed: `Writer::$context` and `Reader::$context`.
107
+ * #15: Added `Reader::$baseUri` to match `Writer::$baseUri`.
108
+ * #20: Allow callbacks to be used instead of `Element` classes in the `Reader`.
109
+ * #25: Added `readText` to quickly grab all text from a node and advance the
110
+ reader to the next node.
111
+ * #15: Added `Sabre\XML\Element\Uri`.
112
+
113
+
114
+ 0.0.6 (2014-09-26)
115
+ ------------------
116
+
117
+ * Added: `CData` element.
118
+ * #13: Better support for xml with no namespaces. (@kalmas)
119
+ * Switched to PSR-4 directory structure.
120
+
121
+
122
+ 0.0.5 (2013-03-27)
123
+ ------------------
124
+
125
+ * Added: baseUri property to the Writer class.
126
+ * Added: The writeElement method can now write complex elements.
127
+ * Added: Throwing exception when invalid objects are written.
128
+
129
+
130
+ 0.0.4 (2013-03-14)
131
+ ------------------
132
+
133
+ * Fixed: The KeyValue parser was skipping over elements when there was no
134
+ whitespace between them.
135
+ * Fixed: Clearing libxml errors after parsing.
136
+ * Added: Support for CDATA.
137
+ * Added: Context properties.
138
+
139
+
140
+ 0.0.3 (2013-02-22)
141
+ ------------------
142
+
143
+ * Changed: Reader::parse returns an array with 1 level less depth.
144
+ * Added: A LibXMLException is now thrown if the XMLReader comes across an error.
145
+ * Fixed: Both the Elements and KeyValue parsers had severe issues with
146
+ nesting.
147
+ * Fixed: The reader now detects when the end of the document is hit before it
148
+ should (because we're still parsing an element).
149
+
150
+
151
+ 0.0.2 (2013-02-17)
152
+ ------------------
153
+
154
+ * Added: Elements parser.
155
+ * Added: KeyValue parser.
156
+ * Change: Reader::parseSubTree is now named parseInnerTree, and returns either
157
+ a string (in case of a text-node), or an array (in case there were child
158
+ elements).
159
+ * Added: Reader::parseCurrentElement is now public.
160
+
161
+
162
+ 0.0.1 (2013-02-07)
163
+ ------------------
164
+
165
+ * First alpha release
166
+
167
+ Project started: 2012-11-13. First experiments in June 2009.
data/CONTRIBUTING.md ADDED
@@ -0,0 +1,25 @@
1
+ # Contributing
2
+
3
+ This project adheres to the [Open Code of Conduct][code-of-conduct]. By participating, you are expected to honor this code.
4
+ [code-of-conduct]: http://todogroup.org/opencodeofconduct/#tilia-uri/tilia@jakobsack.de
5
+
6
+ This library is a port of [sabre/uri](http://github.com/fruux/sabre-uri). The ruby code should match the php code as good as possible. For more information refer to the [coding guidelines](https://tilia.github.io/coding_guidelines).
7
+
8
+ If you are having an issue [search the open issues](https://github.com/tilia/tilia-uri/issues) or create an issue and we'll help point you in the right direction.
9
+
10
+ ## Submitting a Pull Request
11
+
12
+ * Fork the project on Github
13
+ * Install development dependencies (`bundle install`)
14
+ * Create a topic branch for your changes
15
+ * Ensure that you provide *documentation* and *test coverage* for your changes (patches won't be accepted without)
16
+ * Ensure that all tests pass (`bundle exec rake`)
17
+ * Create a [pull request](https://github.com/tilia/tilia-uri/pulls) on Github (these are also a great place to start a conversation around a patch as early as possible)
18
+
19
+ ## Testing
20
+
21
+ To run the tests:
22
+
23
+ $ rake
24
+
25
+ If nothing complains, congratulations!
data/Gemfile ADDED
@@ -0,0 +1,15 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Internal dependencies
4
+ gem 'tilia-uri', '~> 1.0'
5
+
6
+ # External dependencies
7
+ gem 'activesupport', '~> 4.2'
8
+ gem 'libxml-ruby', '~> 2.8'
9
+
10
+ # Testing
11
+ gem 'rake'
12
+ gem 'minitest', '~> 5.8'
13
+ gem 'simplecov', '~> 0.10'
14
+ gem 'rubocop', '~> 0.34'
15
+ gem 'yard', '~> 0.8'
data/Gemfile.lock ADDED
@@ -0,0 +1,56 @@
1
+ GEM
2
+ remote: https://rubygems.org/
3
+ specs:
4
+ activesupport (4.2.4)
5
+ i18n (~> 0.7)
6
+ json (~> 1.7, >= 1.7.7)
7
+ minitest (~> 5.1)
8
+ thread_safe (~> 0.3, >= 0.3.4)
9
+ tzinfo (~> 1.1)
10
+ ast (2.1.0)
11
+ astrolabe (1.3.1)
12
+ parser (~> 2.2)
13
+ docile (1.1.5)
14
+ i18n (0.7.0)
15
+ json (1.8.3)
16
+ libxml-ruby (2.8.0)
17
+ minitest (5.8.1)
18
+ parser (2.2.3.0)
19
+ ast (>= 1.1, < 3.0)
20
+ powerpack (0.1.1)
21
+ rainbow (2.0.0)
22
+ rake (10.4.2)
23
+ rubocop (0.34.2)
24
+ astrolabe (~> 1.3)
25
+ parser (>= 2.2.2.5, < 3.0)
26
+ powerpack (~> 0.1)
27
+ rainbow (>= 1.99.1, < 3.0)
28
+ ruby-progressbar (~> 1.4)
29
+ ruby-progressbar (1.7.5)
30
+ simplecov (0.10.0)
31
+ docile (~> 1.1.0)
32
+ json (~> 1.8)
33
+ simplecov-html (~> 0.10.0)
34
+ simplecov-html (0.10.0)
35
+ thread_safe (0.3.5)
36
+ tilia-uri (1.0.1)
37
+ activesupport (~> 4.2)
38
+ tzinfo (1.2.2)
39
+ thread_safe (~> 0.1)
40
+ yard (0.8.7.6)
41
+
42
+ PLATFORMS
43
+ ruby
44
+
45
+ DEPENDENCIES
46
+ activesupport (~> 4.2)
47
+ libxml-ruby (~> 2.8)
48
+ minitest (~> 5.8)
49
+ rake
50
+ rubocop (~> 0.34)
51
+ simplecov (~> 0.10)
52
+ tilia-uri (~> 1.0)
53
+ yard (~> 0.8)
54
+
55
+ BUNDLED WITH
56
+ 1.10.6
data/LICENSE ADDED
@@ -0,0 +1,27 @@
1
+ Copyright (C) 2015 Jakob Sack (tilia@jakobsack.de)
2
+
3
+ All rights reserved.
4
+
5
+ Redistribution and use in source and binary forms, with or without modification,
6
+ are permitted provided that the following conditions are met:
7
+
8
+ * Redistributions of source code must retain the above copyright notice,
9
+ this list of conditions and the following disclaimer.
10
+ * Redistributions in binary form must reproduce the above copyright notice,
11
+ this list of conditions and the following disclaimer in the documentation
12
+ and/or other materials provided with the distribution.
13
+ * Neither the name Tilia nor the names of its contributors
14
+ may be used to endorse or promote products derived from this software
15
+ without specific prior written permission.
16
+
17
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
+ ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21
+ LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22
+ CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23
+ SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24
+ INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25
+ CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26
+ ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
+ POSSIBILITY OF SUCH DAMAGE.
data/LICENSE.sabre ADDED
@@ -0,0 +1,27 @@
1
+ Copyright (C) 2009-2015 fruux GmbH (https://fruux.com/)
2
+
3
+ All rights reserved.
4
+
5
+ Redistribution and use in source and binary forms, with or without modification,
6
+ are permitted provided that the following conditions are met:
7
+
8
+ * Redistributions of source code must retain the above copyright notice,
9
+ this list of conditions and the following disclaimer.
10
+ * Redistributions in binary form must reproduce the above copyright notice,
11
+ this list of conditions and the following disclaimer in the documentation
12
+ and/or other materials provided with the distribution.
13
+ * Neither the name Sabre nor the names of its contributors
14
+ may be used to endorse or promote products derived from this software
15
+ without specific prior written permission.
16
+
17
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
+ ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21
+ LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22
+ CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23
+ SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24
+ INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25
+ CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26
+ ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
+ POSSIBILITY OF SUCH DAMAGE.
data/README.md ADDED
@@ -0,0 +1,30 @@
1
+ tilia/xml
2
+ =========
3
+
4
+ [![Build Status](https://travis-ci.org/tilia/tilia-xml.svg?branch=master)](https://travis-ci.org/tilia/tilia-xml)
5
+
6
+ **tilia/xml is a port of [sabre/xml](https://github.com/fruux/sabre-xml)**
7
+
8
+ The sabre/xml library is a specialized XML reader and writer.
9
+
10
+
11
+ Installation
12
+ ------------
13
+
14
+ Simply add tilia-xml to your Gemfile and bundle it up:
15
+
16
+ ```ruby
17
+ gem 'tilia-xml', '~> 1.2'
18
+ ```
19
+
20
+
21
+ Contributing
22
+ ------------
23
+
24
+ See [Contributing](CONTRIBUTING.md)
25
+
26
+
27
+ License
28
+ -------
29
+
30
+ tilia-xml is licensed under the terms of the [three-clause BSD-license](LICENSE).
data/Rakefile ADDED
@@ -0,0 +1,17 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rubocop/rake_task'
4
+ require 'yard'
5
+
6
+ Rake::TestTask.new do |t|
7
+ t.test_files = Dir.glob('test/**/*_test.rb')
8
+ t.libs << 'test'
9
+ end
10
+ RuboCop::RakeTask.new do |t|
11
+ t.options = ['--format', 'html', '--out', 'coverage/rubocop.html']
12
+ end
13
+ YARD::Rake::YardocTask.new do |t|
14
+ t.files = ['lib/**/*.rb', '-', 'README.md']
15
+ end
16
+
17
+ task(default: :test)
@@ -0,0 +1,99 @@
1
+ module Tilia
2
+ module Xml
3
+ # Context Stack
4
+ #
5
+ # The Context maintains information about a document during either reading or
6
+ # writing.
7
+ #
8
+ # During this process, it may be neccesary to override this context
9
+ # information.
10
+ #
11
+ # This trait allows easy access to the context, and allows the end-user to
12
+ # override its settings for document fragments, and easily restore it again
13
+ # later.
14
+ module ContextStackTrait
15
+ # This is the element map. It contains a list of XML elements (in clark
16
+ # notation) as keys and PHP class names as values.
17
+ #
18
+ # The PHP class names must implement Sabre\Xml\Element.
19
+ #
20
+ # Values may also be a callable. In that case the function will be called
21
+ # directly.
22
+ #
23
+ # @return [Hash]
24
+ attr_accessor :element_map
25
+
26
+ # A context_uri pointing to the document being parsed / written.
27
+ # This uri may be used to resolve relative urls that may appear in the
28
+ # document.
29
+ #
30
+ # The reader and writer don't use this property, but as it's an extremely
31
+ # common use-case for parsing XML documents, it's added here as a
32
+ # convenience.
33
+ #
34
+ # @return [String]
35
+ attr_accessor :context_uri
36
+
37
+ # This is a list of namespaces that you want to give default prefixes.
38
+ #
39
+ # You must make sure you create this entire list before starting to write.
40
+ # They should be registered on the root element.
41
+ #
42
+ # @return [Hash]
43
+ attr_accessor :namespace_map
44
+
45
+ protected
46
+
47
+ # Backups of previous contexts.
48
+ #
49
+ # @return [Array]
50
+ attr_accessor :context_stack
51
+
52
+ public
53
+
54
+ # Create a new "context".
55
+ #
56
+ # This allows you to safely modify the element_map, context_uri or
57
+ # namespace_map. After you're done, you can restore the old data again
58
+ # with popContext.
59
+ #
60
+ # @return [void]
61
+ def push_context
62
+ dup_or_obj = lambda do |obj|
63
+ if obj.nil?
64
+ nil
65
+ elsif obj.is_a? Fixnum
66
+ obj
67
+ else
68
+ obj.dup
69
+ end
70
+ end
71
+
72
+ @context_stack << [
73
+ dup_or_obj.call(@element_map),
74
+ dup_or_obj.call(@context_uri),
75
+ dup_or_obj.call(@namespace_map)
76
+ ]
77
+ end
78
+
79
+ # Restore the previous "context".
80
+ #
81
+ # @return [void]
82
+ def pop_context
83
+ (
84
+ @element_map,
85
+ @context_uri,
86
+ @namespace_map
87
+ ) = @context_stack.pop
88
+ end
89
+
90
+ # TODO: document
91
+ def initialize_context_stack_attributes
92
+ @element_map = {}
93
+ @namespace_map = {}
94
+ @context_uri = ''
95
+ @context_stack = []
96
+ end
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,73 @@
1
+ module Tilia
2
+ module Xml
3
+ module Element
4
+ # The Base XML element is the standard parser & generator that's used by the
5
+ # XML reader and writer.
6
+ #
7
+ # It spits out a simple PHP array structure during deserialization, that can
8
+ # also be directly injected back into Writer::write.
9
+ class Base
10
+ include Element
11
+
12
+ protected
13
+
14
+ # PHP value to serialize.
15
+ attr_accessor :value
16
+
17
+ public
18
+
19
+ # Constructor
20
+ #
21
+ # @param value
22
+ def initialize(value = nil)
23
+ @value = value
24
+ end
25
+
26
+ # The xml_serialize metod is called during xml writing.
27
+ #
28
+ # Use the writer argument to write its own xml serialization.
29
+ #
30
+ # An important note: do _not_ create a parent element. Any element
31
+ # implementing XmlSerializable should only ever write what's considered
32
+ # its 'inner xml'.
33
+ #
34
+ # The parent of the current element is responsible for writing a
35
+ # containing element.
36
+ #
37
+ # This allows serializers to be re-used for different element names.
38
+ #
39
+ # If you are opening new elements, you must also close them again.
40
+ #
41
+ # @param [Writer] writer
42
+ # @return [void]
43
+ def xml_serialize(writer)
44
+ writer.write(@value)
45
+ end
46
+
47
+ # The deserialize method is called during xml parsing.
48
+ #
49
+ # This method is called statictly, this is because in theory this method
50
+ # may be used as a type of constructor, or factory method.
51
+ #
52
+ # Often you want to return an instance of the current class, but you are
53
+ # free to return other data as well.
54
+ #
55
+ # Important note 2: You are responsible for advancing the reader to the
56
+ # next element. Not doing anything will result in a never-ending loop.
57
+ #
58
+ # If you just want to skip parsing for this element altogether, you can
59
+ # just call reader->next();
60
+ #
61
+ # reader->parseInnerTree() will parse the entire sub-tree, and advance to
62
+ # the next element.
63
+ #
64
+ # @param [Reader] reader
65
+ # @return mixed
66
+ def self.xml_deserialize(reader)
67
+ sub_tree = reader.parse_inner_tree
68
+ sub_tree
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,53 @@
1
+ module Tilia
2
+ module Xml
3
+ module Element
4
+ # CDATA element.
5
+ #
6
+ # This element allows you to easily inject CDATA.
7
+ #
8
+ # Note that we strongly recommend avoiding CDATA nodes, unless you definitely
9
+ # know what you're doing, or you're working with unchangable systems that
10
+ # require CDATA.
11
+ class Cdata
12
+ include XmlSerializable
13
+
14
+ protected
15
+
16
+ # CDATA element value.
17
+ #
18
+ # @return [String]
19
+ attr_accessor :value
20
+
21
+ public
22
+
23
+ # Constructor
24
+ #
25
+ # @param [String] value
26
+ def initialize(value)
27
+ @value = value
28
+ end
29
+
30
+ # The xml_serialize metod is called during xml writing.
31
+ #
32
+ # Use the writer argument to write its own xml serialization.
33
+ #
34
+ # An important note: do _not_ create a parent element. Any element
35
+ # implementing XmlSerializble should only ever write what's considered
36
+ # its 'inner xml'.
37
+ #
38
+ # The parent of the current element is responsible for writing a
39
+ # containing element.
40
+ #
41
+ # This allows serializers to be re-used for different element names.
42
+ #
43
+ # If you are opening new elements, you must also close them again.
44
+ #
45
+ # @param [Writer] writer
46
+ # @return [void]
47
+ def xml_serialize(writer)
48
+ writer.write_cdata(@value)
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end