yaxml 0.0.7

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.
@@ -0,0 +1,208 @@
1
+
2
+ body {
3
+ font-family: Verdana,Arial,Helvetica,sans-serif;
4
+ font-size: 90%;
5
+ margin: 0;
6
+ margin-left: 40px;
7
+ padding: 0;
8
+ background: white;
9
+ }
10
+
11
+ h1,h2,h3,h4 { margin: 0; color: #efefef; background: transparent; }
12
+ h1 { font-size: 150%; }
13
+ h2,h3,h4 { margin-top: 1em; }
14
+
15
+ a { background: #eef; color: #039; text-decoration: none; }
16
+ a:hover { background: #039; color: #eef; }
17
+
18
+ /* Override the base stylesheet's Anchor inside a table cell */
19
+ td > a {
20
+ background: transparent;
21
+ color: #039;
22
+ text-decoration: none;
23
+ }
24
+
25
+ /* and inside a section title */
26
+ .section-title > a {
27
+ background: transparent;
28
+ color: #eee;
29
+ text-decoration: none;
30
+ }
31
+
32
+ /* === Structural elements =================================== */
33
+
34
+ div#index {
35
+ margin: 0;
36
+ margin-left: -40px;
37
+ padding: 0;
38
+ font-size: 90%;
39
+ }
40
+
41
+
42
+ div#index a {
43
+ margin-left: 0.7em;
44
+ }
45
+
46
+ div#index .section-bar {
47
+ margin-left: 0px;
48
+ padding-left: 0.7em;
49
+ background: #ccc;
50
+ font-size: small;
51
+ }
52
+
53
+
54
+ div#classHeader, div#fileHeader {
55
+ width: auto;
56
+ color: white;
57
+ padding: 0.5em 1.5em 0.5em 1.5em;
58
+ margin: 0;
59
+ margin-left: -40px;
60
+ border-bottom: 3px solid #006;
61
+ }
62
+
63
+ div#classHeader a, div#fileHeader a {
64
+ background: inherit;
65
+ color: white;
66
+ }
67
+
68
+ div#classHeader td, div#fileHeader td {
69
+ background: inherit;
70
+ color: white;
71
+ }
72
+
73
+
74
+ div#fileHeader {
75
+ background: #057;
76
+ }
77
+
78
+ div#classHeader {
79
+ background: #048;
80
+ }
81
+
82
+
83
+ .class-name-in-header {
84
+ font-size: 180%;
85
+ font-weight: bold;
86
+ }
87
+
88
+
89
+ div#bodyContent {
90
+ padding: 0 1.5em 0 1.5em;
91
+ }
92
+
93
+ div#description {
94
+ padding: 0.5em 1.5em;
95
+ background: #efefef;
96
+ border: 1px dotted #999;
97
+ }
98
+
99
+ div#description h1,h2,h3,h4,h5,h6 {
100
+ color: #125;;
101
+ background: transparent;
102
+ }
103
+
104
+ div#validator-badges {
105
+ text-align: center;
106
+ }
107
+ div#validator-badges img { border: 0; }
108
+
109
+ div#copyright {
110
+ color: #333;
111
+ background: #efefef;
112
+ font: 0.75em sans-serif;
113
+ margin-top: 5em;
114
+ margin-bottom: 0;
115
+ padding: 0.5em 2em;
116
+ }
117
+
118
+
119
+ /* === Classes =================================== */
120
+
121
+ table.header-table {
122
+ color: white;
123
+ font-size: small;
124
+ }
125
+
126
+ .type-note {
127
+ font-size: small;
128
+ color: #DEDEDE;
129
+ }
130
+
131
+ .xxsection-bar {
132
+ background: #eee;
133
+ color: #333;
134
+ padding: 3px;
135
+ }
136
+
137
+ .section-bar {
138
+ color: #333;
139
+ border-bottom: 1px solid #999;
140
+ margin-left: -20px;
141
+ }
142
+
143
+
144
+ .section-title {
145
+ background: #79a;
146
+ color: #eee;
147
+ padding: 3px;
148
+ margin-top: 2em;
149
+ margin-left: -30px;
150
+ border: 1px solid #999;
151
+ }
152
+
153
+ .top-aligned-row { vertical-align: top }
154
+ .bottom-aligned-row { vertical-align: bottom }
155
+
156
+ /* --- Context section classes ----------------------- */
157
+
158
+ .context-row { }
159
+ .context-item-name { font-family: monospace; font-weight: bold; color: black; }
160
+ .context-item-value { font-size: small; color: #448; }
161
+ .context-item-desc { color: #333; padding-left: 2em; }
162
+
163
+ /* --- Method classes -------------------------- */
164
+ .method-detail {
165
+ background: #efefef;
166
+ padding: 0;
167
+ margin-top: 0.5em;
168
+ margin-bottom: 1em;
169
+ border: 1px dotted #ccc;
170
+ }
171
+ .method-heading {
172
+ color: black;
173
+ background: #ccc;
174
+ border-bottom: 1px solid #666;
175
+ padding: 0.2em 0.5em 0 0.5em;
176
+ }
177
+ .method-signature { color: black; background: inherit; }
178
+ .method-name { font-weight: bold; }
179
+ .method-args { font-style: italic; }
180
+ .method-description { padding: 0 0.5em 0 0.5em; }
181
+
182
+ /* --- Source code sections -------------------- */
183
+
184
+ a.source-toggle { font-size: 90%; }
185
+ div.method-source-code {
186
+ background: #262626;
187
+ color: #ffdead;
188
+ margin: 1em;
189
+ padding: 0.5em;
190
+ border: 1px dashed #999;
191
+ overflow: hidden;
192
+ }
193
+
194
+ div.method-source-code pre { color: #ffdead; overflow: hidden; }
195
+
196
+ /* --- Ruby keyword styles --------------------- */
197
+
198
+ .standalone-code { background: #221111; color: #ffdead; overflow: hidden; }
199
+
200
+ .ruby-constant { color: #7fffd4; background: transparent; }
201
+ .ruby-keyword { color: #00ffff; background: transparent; }
202
+ .ruby-ivar { color: #eedd82; background: transparent; }
203
+ .ruby-operator { color: #00ffee; background: transparent; }
204
+ .ruby-identifier { color: #ffdead; background: transparent; }
205
+ .ruby-node { color: #ffa07a; background: transparent; }
206
+ .ruby-comment { color: #b22222; font-weight: bold; background: transparent; }
207
+ .ruby-regexp { color: #ffa07a; background: transparent; }
208
+ .ruby-value { color: #7fffd4; background: transparent; }
@@ -0,0 +1,12 @@
1
+ <root xmlns:yaml='http://yaml.org/xml'>
2
+ <three>
3
+ <_>apple</_>
4
+ <_>pear</_>
5
+ </three>
6
+ <two> 2.0 </two>
7
+ <one>1</one>
8
+ <four>
9
+ <bb>22</bb>
10
+ <aa>11</aa>
11
+ </four>
12
+ </root>
@@ -0,0 +1,9 @@
1
+ one: 1
2
+ two: 2.0
3
+ three:
4
+ - apple
5
+ - pear
6
+ four:
7
+ aa: 11
8
+ bb: 22
9
+
data/lib/yaxml.rb ADDED
@@ -0,0 +1,354 @@
1
+ ####################################################
2
+ #
3
+ # YAXML Module
4
+ #
5
+ # This code is released under LGPL.
6
+ #
7
+ # This module make the next conversions:
8
+ #
9
+ # * YAML -> YAXML
10
+ # * JSON -> YAXML
11
+ # * YAXML -> YAML
12
+ # * YAXML -> JSON
13
+ #
14
+ # All conversions can use a file or a string as source.
15
+ #
16
+ # You can use this module in two ways:
17
+ #
18
+ # 1) Creating a Yaxml object
19
+ # xml = Yaxml.new 'file.yml'
20
+ # xml.write( $stdout, 2)
21
+ # xml.to_json
22
+ # xml.to_yaml
23
+ #
24
+ # 2) Using static methods of Yaxml module
25
+ # xml = Yaxml::yaml2yaxml( 'file.yml' )
26
+ # xml.write( $stdout, 2)
27
+ #
28
+ # Also, you can use:
29
+ # xml = Yaxml::json2yaxml( { numbers: [ 1, 2, 3 ]} )
30
+ # yaml = Yaxml::yaxml2yaml( 'file.yaxml' )
31
+ # json = Yaxml::yaxml2json( 'file.yaxml' )
32
+ #
33
+ # Author:: Diego Moreno (dmoreno@dit.upm.es)
34
+ # License:: GNU Lesser General Public License (aka LGPL)
35
+ #
36
+
37
+ #require 'ruby-debug'
38
+
39
+ require "rexml/document"
40
+ require 'yaml'
41
+
42
+ module YAXML
43
+
44
+ # Default root name
45
+ DEFAULT_ROOT_NAME = "root"
46
+
47
+ # Namespace for YAXML
48
+ NAMESPACE = "http://yaml.org/xml"
49
+
50
+ class Yaxml < REXML::Document
51
+
52
+ # Constructor
53
+ # source::
54
+ # It can be a String or a file name.
55
+ # options::
56
+ # It is possible to pass the next options:
57
+ # * <tt>:root_name</tt> It is the name of the root tag in the YAXML document. Defaults to +DEFAULT_ROOT_NAME+
58
+ # * <tt>:from_yaxml</tt> If true, the source will be considered as YAXML input. If false, the source will be considered as YAML input. Defaults to :+false+
59
+ #
60
+ # Example of use:
61
+ # xml = Yaxml.new 'file.yml'
62
+ # xml.write( $stdout, 2)
63
+ # xml.to_json
64
+ # xml.to_yaml
65
+ def initialize( source, options={} )
66
+ super()
67
+
68
+ if options[:from_yaxml]
69
+ content = extract_content source
70
+ doc = REXML::Document.new content
71
+ self.add_element( doc.elements[1] ) if doc.elements[1]
72
+
73
+ else #default is from_yaml
74
+ options[:root_name] ||= DEFAULT_ROOT_NAME
75
+ doc = YAXML::yaml2yaxml( source, options )
76
+
77
+ self.add_element( doc.elements[1] ) if doc.elements[1]
78
+ end
79
+
80
+ end
81
+
82
+ # The output is a YAML representation of YAXML object.
83
+ def to_yaml
84
+ YAXML::yaxml2yaml self
85
+ end
86
+
87
+ # The output is a JSON representation of YAXML object.
88
+ def to_json
89
+ YAXML::yaxml2json self
90
+ end
91
+
92
+ # The output is a YAXML representation of YAXML object.
93
+ def to_yaxml
94
+ self.to_s
95
+ end
96
+
97
+ private
98
+ def extract_content source
99
+ if File.exist?source
100
+ File.open( source, "r" ).read # is a file
101
+ else
102
+ source # is a string
103
+ end
104
+ end
105
+
106
+ end
107
+
108
+
109
+ #################################################
110
+ # From ANY To YAXML
111
+ #################################################
112
+
113
+ # Conversion YAML -> YAXML
114
+ #
115
+ # source::
116
+ # It can be a YAML file in a String or a file name.
117
+ # options::
118
+ # * <tt>:root_name</tt> It is the name of the root tag in the YAXML document. Defaults to +DEFAULT_ROOT_NAME+
119
+ #
120
+ # Example of use:
121
+ # test.yml content
122
+ # one: 1
123
+ # two: 2.0
124
+ # three:
125
+ # - apple
126
+ # - pear
127
+ # four:
128
+ # aa: 11
129
+ # bb: 22
130
+ #
131
+ # doc = YAXML.yaml2yaxml "test.yml"
132
+ # doc.write( $stdout, 2 )
133
+ # #-> <root xmlns:yaml='http://yaml.org/xml'>
134
+ # <three>
135
+ # <_>apple</_>
136
+ # <_>pear</_>
137
+ # </three>
138
+ # <two>2.0</two>
139
+ # <one>1</one>
140
+ # <four>
141
+ # <bb>22</bb>
142
+ # <aa>11</aa>
143
+ # </four>
144
+ # </root>
145
+ def self.yaml2yaxml( source, options={} )
146
+
147
+ options[:root_name] ||= DEFAULT_ROOT_NAME
148
+
149
+ json = if source.is_a?String
150
+ if File.exist?source
151
+ YAML::load_file( source ) # is a file
152
+ else
153
+ YAML::load( source ) # is a string
154
+ end
155
+ elsif source.is_a?(Hash) || source.is_a?(Array)
156
+ source
157
+ end
158
+
159
+ el = self.json2yaxml( json, options )
160
+ doc = REXML::Document.new
161
+ doc.add_element el
162
+ doc
163
+
164
+ end
165
+
166
+ # Conversion JSON -> YAXML
167
+ #
168
+ # source::
169
+ # It can be a Array or Hash object or a file name.
170
+ # options::
171
+ # * <tt>:root_name</tt> It is the name of the root tag in the YAXML document. Defaults to +DEFAULT_ROOT_NAME+
172
+ #
173
+ # Example of use:
174
+ #
175
+ # json = {"one" => 1, "two" => 2.0, "three" => [ "apple", "pear"], "four" => {"aa" => 11, "bb" => 22}}
176
+ # doc = YAXML.json2yaxml json
177
+ # doc.write( $stdout, 2 )
178
+ # #-> <root xmlns:yaml='http://yaml.org/xml'>
179
+ # <three>
180
+ # <_>apple</_>
181
+ # <_>pear</_>
182
+ # </three>
183
+ # <two>2.0</two>
184
+ # <one>1</one>
185
+ # <four>
186
+ # <bb>22</bb>
187
+ # <aa>11</aa>
188
+ # </four>
189
+ # </root>
190
+ def self.json2yaxml( json_to_encode, options={} )
191
+ return nil unless json_to_encode
192
+
193
+ options[:root_name] ||= DEFAULT_ROOT_NAME
194
+ unless options[:parent]
195
+ options[:parent] = REXML::Element.new options[:root_name]
196
+ options[:parent].attributes["xmlns:yaml"] = NAMESPACE
197
+ end
198
+
199
+ parent = options[:parent]
200
+
201
+ if json_to_encode.is_a?Hash
202
+ json_to_encode.each_pair do | key, value |
203
+ key.to_s if key.is_a?Symbol
204
+ el = REXML::Element.new key.to_s
205
+ result = self.json2yaxml( value, {:parent => el} )
206
+ parent.add result if result
207
+ end
208
+
209
+ elsif json_to_encode.is_a?Array
210
+ json_to_encode.each do | x |
211
+ el = REXML::Element.new "_"
212
+ result = self.json2yaxml( x, {:parent => el})
213
+ parent.add result if result
214
+ end
215
+
216
+ elsif json_to_encode.is_a?String
217
+ parent.text = json_to_encode
218
+
219
+ elsif json_to_encode.is_a?(Integer) || json_to_encode.is_a?(Float) || json_to_encode.is_a?(Date) || json_to_encode.is_a?(Symbol)
220
+ parent.text = json_to_encode.to_s
221
+
222
+ end
223
+
224
+ parent
225
+ end
226
+
227
+
228
+ #################################################
229
+ # From YAXML To ANY
230
+ #################################################
231
+
232
+ # Conversion YAXML -> YAML
233
+ #
234
+ # source::
235
+ # It can be a YAXML file in a String or a file name.
236
+ #
237
+ # Example of use:
238
+ # test.xml content
239
+ # <root xmlns:yaml='http://yaml.org/xml'>
240
+ # <three>
241
+ # <_>apple</_>
242
+ # <_>pear</_>
243
+ # </three>
244
+ # <two>2.0</two>
245
+ # <one>1</one>
246
+ # <four>
247
+ # <bb>22</bb>
248
+ # <aa>11</aa>
249
+ # </four>
250
+ # </root>
251
+ #
252
+ # yaml = YAXML.yaxml2yaml "test.xml"
253
+ # puts yaml
254
+ # #-> one: 1
255
+ # two: 2.0
256
+ # three:
257
+ # - apple
258
+ # - pear
259
+ # four:
260
+ # aa: 11
261
+ # bb: 22
262
+ def self.yaxml2yaml( source )
263
+ self.yaxml2json( source ).to_yaml
264
+ end
265
+
266
+ # Conversion YAXML -> JSON
267
+ #
268
+ # source::
269
+ # It can be a YAXML file in a String or a file name.
270
+ #
271
+ # Example of use:
272
+ # test.xml content
273
+ # <root xmlns:yaml='http://yaml.org/xml'>
274
+ # <three>
275
+ # <_>apple</_>
276
+ # <_>pear</_>
277
+ # </three>
278
+ # <two>2.0</two>
279
+ # <one>1</one>
280
+ # <four>
281
+ # <bb>22</bb>
282
+ # <aa>11</aa>
283
+ # </four>
284
+ # </root>
285
+ #
286
+ # json = YAXML.yaxml2json "test.xml"
287
+ # puts json.inspect
288
+ # #-> {"one" => 1, "two" => 2.0, "three" => [ "apple", "pear"], "four" => {"aa" => 11, "bb" => 22}}
289
+ def self.yaxml2json( source )
290
+ return nil unless source
291
+
292
+ xml_node = if source.kind_of?String
293
+ if File.exist?source
294
+ yaxml_string = File.open( source, "r" ).read
295
+ else
296
+ yaxml_string = source
297
+ end
298
+ REXML::Document.new yaxml_string
299
+
300
+ elsif source.kind_of?REXML::Element
301
+ source.elements[1] if source.elements[1]
302
+
303
+ else
304
+ nil
305
+ end
306
+
307
+ self.yaxmlnode2json xml_node
308
+ end
309
+
310
+ private
311
+ def self.yaxmlnode2json xml_node
312
+ parent ||= if first_element = xml_node.elements[1]
313
+ if first_element.name == "_"
314
+ Array.new
315
+ else
316
+ Hash.new
317
+ end
318
+ end
319
+
320
+ xml_node.elements.each do |el|
321
+ new_element = (el.has_elements?)? self.yaxmlnode2json( el ) : el.text.strip
322
+ self.add_to_parent( new_element, parent, first_element, el.name )
323
+ end
324
+
325
+ parent
326
+ end
327
+
328
+ def self.add_to_parent( element_value, parent, first_element, element_key=nil )
329
+ is_array_list = ( first_element.name == "_" )
330
+ if is_array_list
331
+ parent << element_value
332
+ else
333
+ parent[element_key] = element_value
334
+ end
335
+ end
336
+
337
+ end
338
+
339
+ #doc = YAXML::yaml2yaxml "simple_test.yml"
340
+ #puts doc.to_s
341
+ #doc.write( $stdout, 2 )
342
+
343
+ #doc = YAXML.yaxml2yaml_from_file "test.xml"
344
+ #puts doc.inspect
345
+
346
+ #yaxml = YAXML::Yaxml.new 'test2.yml'
347
+ #puts yaxml.to_s
348
+
349
+ #doc = REXML::Document.new yaxml.to_s
350
+ #doc.write( $stdout, 2 )
351
+
352
+ #yaxml.write( $stdout, 2 )
353
+ #puts yaxml.to_json.inspect
354
+ #puts yaxml.to_yaml