xmlutils 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +6 -0
  3. data/Gemfile +4 -0
  4. data/LICENSE +22 -0
  5. data/README.md +77 -0
  6. data/bin/xmltogdl +88 -0
  7. data/docs/README.txt +20 -0
  8. data/lib/xmlutils/contextlistener.rb +432 -0
  9. data/lib/xmlutils/contextparser.rb +111 -0
  10. data/lib/xmlutils/dumplistener.rb +57 -0
  11. data/lib/xmlutils/gdlcontext.rb +305 -0
  12. data/lib/xmlutils/gdlcontextobjs.rb +286 -0
  13. data/lib/xmlutils/gdldoc.rb +547 -0
  14. data/lib/xmlutils/gdldocbuilder.rb +181 -0
  15. data/lib/xmlutils/gdllistener.rb +265 -0
  16. data/lib/xmlutils/gdltemplate.rb +488 -0
  17. data/lib/xmlutils/lineparser.rb +193 -0
  18. data/lib/xmlutils/listener.rb +459 -0
  19. data/lib/xmlutils/rulelistener.rb +414 -0
  20. data/lib/xmlutils/ruleparser.rb +112 -0
  21. data/lib/xmlutils/varlistener.rb +86 -0
  22. data/lib/xmlutils/version.rb +5 -0
  23. data/lib/xmlutils/xmlrulevisitor.rb +704 -0
  24. data/lib/xmlutils/xmltogdlcontroller.rb +93 -0
  25. data/lib/xmlutils/xmltogdltask.rb +23 -0
  26. data/lib/xmlutils/xmlvisitor.rb +690 -0
  27. data/lib/xmlutils.rb +40 -0
  28. data/rakefile.rb +34 -0
  29. data/unittests/2830.xml +1 -0
  30. data/unittests/chunks.rb +31 -0
  31. data/unittests/curDirTest.rb +2 -0
  32. data/unittests/data/2830-GENERATED.gdl +309 -0
  33. data/unittests/data/2830-GENERATED.xml +1 -0
  34. data/unittests/data/AUGuideline.xml +12 -0
  35. data/unittests/data/AUGuideline.xml.gdl +19190 -0
  36. data/unittests/data/BAKUP.DCTEST1.xml +1 -0
  37. data/unittests/data/DCTEST.xml +1 -0
  38. data/unittests/data/DCTEST1.xml +1 -0
  39. data/unittests/data/DCTEST1.xml.gdl +14890 -0
  40. data/unittests/data/DCTEST1.xml.rename.csv +180 -0
  41. data/unittests/data/DCTEST1wLookups.xml +174 -0
  42. data/unittests/data/DCTEST1wLookups.xml.gdl +127 -0
  43. data/unittests/data/GENERATED.gdl.xml +1 -0
  44. data/unittests/data/Message.xml +1 -0
  45. data/unittests/data/Message.xml.gdl +142 -0
  46. data/unittests/data/Pricing2ndGuideline.xml +1 -0
  47. data/unittests/data/Pricing2ndGuideline.xml.gdl +52670 -0
  48. data/unittests/data/Z-TEMP-Jeff.xml +1 -0
  49. data/unittests/data/Z-TEMP-Jeff.xml.gdl +288 -0
  50. data/unittests/tc_convert_xml_to_gdl.rb +23 -0
  51. data/unittests/ts_allTests.rb +2 -0
  52. data/xmlutils.gemspec +25 -0
  53. metadata +139 -0
@@ -0,0 +1,414 @@
1
+ #
2
+ # File: ruleListener.rb
3
+ #
4
+ # This class is an rule listener base class
5
+ #
6
+ #
7
+
8
+ require 'rexml/streamlistener'
9
+
10
+ include REXML
11
+
12
+
13
+
14
+
15
+ #################################################
16
+ #
17
+ # class RuleListener
18
+ #
19
+ #################################################
20
+ class RuleListener
21
+ include StreamListener
22
+
23
+ attr_writer :verbose
24
+
25
+ attr_writer :inRule
26
+ attr_writer :inIfMsgs
27
+ attr_writer :inElseMsgs
28
+ attr_writer :inMsg
29
+
30
+ attr_accessor :curRuleName
31
+ attr_accessor :curRuleSrc
32
+ attr_accessor :curIfMsgs
33
+ attr_accessor :curElseMsgs
34
+ attr_accessor :context
35
+
36
+
37
+
38
+ def inRule?
39
+ @inRule
40
+ end
41
+
42
+
43
+
44
+
45
+ def inIfMsgs?
46
+ @inIfMsgs
47
+ end
48
+
49
+
50
+
51
+
52
+ def inElseMsgs?
53
+ @inElseMsgs
54
+ end
55
+
56
+
57
+
58
+
59
+ def inMsg?
60
+ @inMsg
61
+ end
62
+
63
+
64
+
65
+
66
+ def verbose?
67
+ @verbose
68
+ end
69
+
70
+
71
+
72
+
73
+ #-------------------------------------------------------------------------------------------------------------#
74
+ # initialize - Ctor
75
+ #
76
+ #
77
+ #------------------------------------------------------------------------------------------------------------#
78
+ def initialize(ctx)
79
+ super()
80
+
81
+ @inRule = false
82
+ @inIfMsgs = false
83
+ @inElseMsgs = false
84
+ @inMsg = false
85
+
86
+ @curRuleSrc = ""
87
+ @curRuleName = nil
88
+ @curIfMsgs = Array.new
89
+ @curElseMsgs = Array.new
90
+
91
+ @context = ctx
92
+ end
93
+
94
+
95
+
96
+ #-------------------------------------------------------------------------------------------------------------#
97
+ # tag_start - A start tag has been parsed
98
+ #
99
+ # tag - name of tag (element name)
100
+ # attributes - element tag attributes
101
+ #------------------------------------------------------------------------------------------------------------#
102
+ def tag_start(tag, attributes)
103
+ case tag
104
+ when 'Rule'
105
+ openRule(attributes)
106
+
107
+ when 'IfMessages'
108
+ @inIfMsgs = true
109
+ openUnknown(tag, attributes)
110
+
111
+ when 'ElseMessages'
112
+ @inElseMsgs = true
113
+ openUnknown(tag, attributes)
114
+
115
+ when 'Message'
116
+ openMessage(attributes)
117
+
118
+ else
119
+ openUnknown(tag, attributes)
120
+ end # case
121
+
122
+ end
123
+
124
+
125
+
126
+
127
+ #-------------------------------------------------------------------------------------------------------------#
128
+ # tag_end - An ending tag has been parsed
129
+ #
130
+ # tag - name of tag (element name)
131
+ #------------------------------------------------------------------------------------------------------------#
132
+ def tag_end(tag)
133
+
134
+ case tag
135
+ when 'Rule'
136
+ closeRule()
137
+
138
+ when 'IfMessages'
139
+ @inIfMsgs = false
140
+ closeUnknown(tag)
141
+
142
+ when 'ElseMessages'
143
+ @inElseMsgs = false
144
+ closeUnknown(tag)
145
+
146
+
147
+ when 'Message'
148
+ closeMessage()
149
+
150
+ else
151
+ closeUnknown(tag)
152
+ end # case
153
+
154
+ end
155
+
156
+
157
+
158
+
159
+ #-------------------------------------------------------------------------------------------------------------#
160
+ # addMsg - Add a message to the proper message list
161
+ #
162
+ # msg - msg to add
163
+ #------------------------------------------------------------------------------------------------------------#
164
+ def addMsg(msg)
165
+ puts "addMsg: #{msg}" if $DEBUG
166
+
167
+ if (inIfMsgs?)
168
+ @curIfMsgs << msg
169
+ elsif (inElseMsgs?)
170
+ @curElseMsgs << msg
171
+ end
172
+ end
173
+
174
+
175
+
176
+
177
+ #-------------------------------------------------------------------------------------------------------------#
178
+ # entity - An entity has been parsed
179
+ #
180
+ # content - entity content
181
+ #------------------------------------------------------------------------------------------------------------#
182
+ def entity(content)
183
+ puts "entity: #{content}" if $DEBUG
184
+
185
+ if (inRule?)
186
+ @curRuleSrc += content
187
+ end
188
+ end
189
+
190
+
191
+
192
+
193
+ #-------------------------------------------------------------------------------------------------------------#
194
+ # text - A text node has been parsed
195
+ #
196
+ # txt - node text
197
+ #------------------------------------------------------------------------------------------------------------#
198
+ def text(txt)
199
+ puts "text: #{txt}" if $DEBUG
200
+
201
+ if (inRule?)
202
+ txt = cleanText(txt)
203
+ @curRuleSrc += txt
204
+ end
205
+ end
206
+
207
+
208
+
209
+
210
+ #-------------------------------------------------------------------------------------------------------------#
211
+ # cdata - A cdata node has been parsed
212
+ # Called when <![CDATA[ � ]]> is encountered in a document. @p content "�"
213
+ # txt - node text
214
+ #------------------------------------------------------------------------------------------------------------#
215
+ def cdata(txt)
216
+ if (inMsg?)
217
+ puts "cdata: #{txt}" unless !$DEBUG
218
+ @curRuleSrc += "<![CDATA[#{txt}]]>"
219
+ addMsg(txt)
220
+ end # if inMsg
221
+ end
222
+
223
+
224
+
225
+
226
+ #-------------------------------------------------------------------------------------------------------------#
227
+ # openRule - A Rule element has been started
228
+ #
229
+ # attributes - Rule element attributes
230
+ #
231
+ #------------------------------------------------------------------------------------------------------------#
232
+ def openRule(attributes)
233
+ if (inRule?)
234
+ closeRule()
235
+ end # if inRule
236
+
237
+ if ($DEBUG)
238
+ puts "openRule:"
239
+
240
+ if(!attributes.empty?)
241
+ puts " Attr:"
242
+ attributes.each do |attr|
243
+ puts " #{attr[0]}: #{attr[1]}"
244
+ end
245
+ end
246
+ end # if verbose
247
+
248
+ @inRule = true
249
+
250
+ @curRuleName = attributes['Name']
251
+
252
+ @curRuleSrc = String.new("<Rule")
253
+ @curRuleSrc += buildAttrString(attributes)
254
+ @curRuleSrc += ">"
255
+
256
+ puts "Collecting rule XML: #{@curRuleName}" if verbose?
257
+
258
+ end # openRule
259
+
260
+
261
+
262
+
263
+ #-------------------------------------------------------------------------------------------------------------#
264
+ # closeRule - A Rule element has been ended
265
+ #
266
+ #
267
+ #------------------------------------------------------------------------------------------------------------#
268
+ def closeRule()
269
+ puts "closeRule" if $DEBUG
270
+
271
+ if (inRule?)
272
+ @curRuleSrc += "</Rule>"
273
+ @context.rules[@curRuleName].xml = @curRuleSrc
274
+ @context.rules[@curRuleName].ifMsgs = @curIfMsgs
275
+ @context.rules[@curRuleName].elseMsgs = @curElseMsgs
276
+ @curRuleSrc = ""
277
+ @curRuleName = ""
278
+ @curIfMsgs = Array.new
279
+ @curElseMsgs = Array.new
280
+
281
+ @inRule = false
282
+ else
283
+ raise "Rule end tag encountered without preceeding start tag."
284
+ end # if inRule
285
+ end # closeRule
286
+
287
+
288
+
289
+
290
+ #-------------------------------------------------------------------------------------------------------------#
291
+ # openMessage - A Message element has been started
292
+ #
293
+ # attributes - Message element attributes
294
+ #
295
+ #------------------------------------------------------------------------------------------------------------#
296
+ def openMessage(attributes)
297
+ if ($DEBUG)
298
+ puts "openMessage"
299
+
300
+ if(!attributes.empty?)
301
+ puts " Attr:"
302
+ attributes.each do |attr|
303
+ puts " #{attr[0]}: #{attr[1]}"
304
+ end
305
+ end
306
+ end # if $DEBUG
307
+
308
+ @inMsg = true
309
+
310
+ @curRuleSrc += "<Message"
311
+ @curRuleSrc += buildAttrString(attributes)
312
+ @curRuleSrc += ">"
313
+
314
+ end # openMessage
315
+
316
+
317
+
318
+
319
+ #-------------------------------------------------------------------------------------------------------------#
320
+ # closeMessage - A Message element has been ended
321
+ #
322
+ #
323
+ #------------------------------------------------------------------------------------------------------------#
324
+ def closeMessage()
325
+ puts "closeMessage" if $DEBUG
326
+ if (inMsg?)
327
+ @curRuleSrc += "</Message>"
328
+ end # if inMsg
329
+ end # closeMessage
330
+
331
+
332
+
333
+
334
+ #-------------------------------------------------------------------------------------------------------------#
335
+ # openUnknown - A Unknown element has been started
336
+ #
337
+ # tag - Tag name of unknown element
338
+ # attributes - Unknown element attributes
339
+ #
340
+ #------------------------------------------------------------------------------------------------------------#
341
+ def openUnknown(tag, attributes)
342
+ if ($DEBUG)
343
+ puts "openUnknown: #{tag}"
344
+
345
+ if(!attributes.empty?)
346
+ puts " Attr:"
347
+ attributes.each do |attr|
348
+ puts " #{attr[0]}: #{attr[1]}"
349
+ end
350
+ end
351
+ end # if $DEBUG
352
+
353
+ if (inRule?)
354
+ @curRuleSrc += "<#{tag}"
355
+ @curRuleSrc += buildAttrString(attributes)
356
+ @curRuleSrc += ">"
357
+ end # if inRule
358
+ end # openUnknown
359
+
360
+
361
+
362
+
363
+ #-------------------------------------------------------------------------------------------------------------#
364
+ # closeUnknown - A Unknown element has been ended
365
+ #
366
+ # tag - Tag name of unknown element
367
+ #
368
+ #------------------------------------------------------------------------------------------------------------#
369
+ def closeUnknown(tag)
370
+ puts "closeUnknown: #{tag}" if $DEBUG
371
+ if (inRule?)
372
+ @curRuleSrc += "</#{tag}>"
373
+ end # if inRule
374
+ end # closeUnknown
375
+
376
+
377
+
378
+
379
+ #-------------------------------------------------------------------------------------------------------------#
380
+ # buildAttrString - Build an XML attribute string.
381
+ #
382
+ # attributes - Hash of attributes
383
+ #
384
+ #------------------------------------------------------------------------------------------------------------#
385
+ def buildAttrString(attributes)
386
+ str = ""
387
+
388
+ attributes.each do |key, val|
389
+ str += " #{key}='#{val}'"
390
+ end # attributes.each
391
+
392
+ puts "attrib str: #{str}" if $DEBUG
393
+ str
394
+ end # buildAttrString
395
+
396
+
397
+
398
+
399
+ #-------------------------------------------------------------------------------------------------------------#
400
+ # cleanText - Convert text into entities
401
+ #
402
+ # txt - text to convert
403
+ #
404
+ #------------------------------------------------------------------------------------------------------------#
405
+ def cleanText(txt)
406
+ clean = txt.gsub("<", "&lt;")
407
+ clean.gsub!(">", "&gt;")
408
+
409
+ puts "cleaned text: #{txt} -> #{clean}" if $DEBUG
410
+ clean
411
+
412
+ end # buildAttrString
413
+ end # class Listener
414
+
@@ -0,0 +1,112 @@
1
+ #
2
+ # File: ruleParser.rb
3
+ #
4
+ # This class will convert XML rule elements to GDL (Guideline Definition Language).
5
+ #
6
+ #
7
+
8
+ require 'rexml/document'
9
+ require 'rexml/streamlistener'
10
+ require 'xmlutils/xmlrulevisitor'
11
+
12
+ include REXML
13
+
14
+
15
+ #################################################
16
+ #
17
+ # class RuleParser
18
+ #
19
+ #################################################
20
+ class RuleParser
21
+ attr_accessor :context
22
+
23
+ #-------------------------------------------------------------------------------------------------------------#
24
+ # initialize - Constructor
25
+ #
26
+ #
27
+ #------------------------------------------------------------------------------------------------------------#
28
+ def initialize(ctx)
29
+ @verbose = false
30
+ @context = ctx
31
+
32
+ end
33
+
34
+
35
+
36
+
37
+ def verbose?
38
+ @verbose
39
+ end
40
+
41
+
42
+
43
+
44
+ #-------------------------------------------------------------------------------------------------------------#
45
+ # setFlag - Set configuration flags
46
+ #
47
+ # flg - Array of options or single string option. Currently, only -v: verbose is available
48
+ #
49
+ #------------------------------------------------------------------------------------------------------------#
50
+ def setFlag(flg)
51
+ if (flg.class == Array)
52
+ flg.each do |f|
53
+ case f
54
+ when '-v'
55
+ @verbose = true
56
+ end
57
+ end # flg.each
58
+
59
+ return
60
+ end # if flg
61
+
62
+ case flg
63
+ when '-v'
64
+ @verbose = true
65
+ end
66
+
67
+ end
68
+
69
+
70
+
71
+
72
+ #-------------------------------------------------------------------------------------------------------------#
73
+ # parse - Build a GDL rule definition from XML source.
74
+ #
75
+ # src - String containing XML source
76
+ # gdl - GDL rule definition.
77
+ #
78
+ #------------------------------------------------------------------------------------------------------------#
79
+ def parse(src)
80
+ doc = nil
81
+ doc = Document.new(src)
82
+ root = doc.root
83
+
84
+ return parseRuleXml(root)
85
+
86
+ end # def parse
87
+
88
+
89
+
90
+
91
+ #-------------------------------------------------------------------------------------------------------------#
92
+ # parseRuleXml - generate a GDL version of a rule definition based on Rule XML element
93
+ #
94
+ # rule - XML element to generate output for
95
+ #
96
+ # @returns GDL output for Rule definition
97
+ #
98
+ #------------------------------------------------------------------------------------------------------------#
99
+ def parseRuleXml(rule)
100
+ # arule = root.elements['//Rule'].to_a # returns all 1st level rule children elements
101
+ #ruleParts = rule.elements.to_a
102
+ puts "Parsing rule: #{rule.attributes['Name']}" if verbose?
103
+
104
+ visitor = XmlRuleVisitor.new(@context)
105
+ output = ""
106
+
107
+ return visitor.visit(rule, output)
108
+
109
+
110
+ end # parseRuleXml
111
+ end # class RuleParser
112
+
@@ -0,0 +1,86 @@
1
+ #
2
+ # File: varListener.rb
3
+ #
4
+ # This class is used to collect XML variables
5
+ #
6
+ #
7
+
8
+ require 'rexml/streamlistener'
9
+ require 'xmlutils/gdlcontext'
10
+ require 'xmlutils/gdlcontextobjs'
11
+ require 'xmlutils/listener'
12
+
13
+ include REXML
14
+
15
+
16
+
17
+
18
+ #################################################
19
+ #
20
+ # class VarListener
21
+ #
22
+ #################################################
23
+ class VarListener < Listener
24
+
25
+ attr_reader :context
26
+
27
+
28
+
29
+
30
+ #-------------------------------------------------------------------------------------------------------------#
31
+ # initialize - Ctor
32
+ #
33
+ # ctx - Context object to store variables in
34
+ #
35
+ #------------------------------------------------------------------------------------------------------------#
36
+ def initialize(ctx)
37
+ @verbose = false
38
+ @context = ctx
39
+ end
40
+
41
+
42
+
43
+
44
+ #-------------------------------------------------------------------------------------------------------------#
45
+ # openDPM - Add a DPM variable to the context object
46
+ #
47
+ # attributes - DPM element attributes
48
+ #
49
+ #------------------------------------------------------------------------------------------------------------#
50
+ def openDPM(attributes)
51
+ dpmAlias = attributes["Name"]
52
+ confName = @context.createValidName(dpmAlias)
53
+ varType = attributes["Type"]
54
+ dataType = attributes["DataType"] if attributes.has_key?("DataType")
55
+ prodType = attributes["ProductType"]
56
+
57
+ dataType = prodType if nil == dataType
58
+
59
+ dpm = Dpm.new(confName, dpmAlias, varType, dataType, prodType)
60
+
61
+ @context.dpms[dpmAlias] = dpm
62
+ end # openDPM
63
+
64
+
65
+
66
+
67
+ #-------------------------------------------------------------------------------------------------------------#
68
+ # openPPM - Add a PPM variable to the context object
69
+ #
70
+ # attributes - PPM element attributes
71
+ #
72
+ #------------------------------------------------------------------------------------------------------------#
73
+ def openPPM(attributes)
74
+ ppmAlias = attributes["Name"]
75
+ confName = @context.createValidName(ppmAlias)
76
+ varType = attributes["Type"]
77
+ dataType = attributes["DataType"] if attributes.has_key?("DataType")
78
+
79
+ dataType = "Text" if nil == dataType
80
+
81
+ ppm = Ppm.new(confName, ppmAlias, varType, dataType)
82
+
83
+ @context.ppms[ppmAlias] = ppm
84
+ end # openPPM
85
+ end # class VarListener
86
+
@@ -0,0 +1,5 @@
1
+ module XmlUtils
2
+ VERSION = "0.0.2" unless constants.include?("VERSION")
3
+ APPNAME = "XmlUtils" unless constants.include?("APPNAME")
4
+ COPYRIGHT = "Copyright (c) 2014, kTech Systems LLC. All rights reserved" unless constants.include?("COPYRIGHT")
5
+ end