Class: Rng::RncParser
- Inherits:
-
Parslet::Parser
- Object
- Parslet::Parser
- Rng::RncParser
- Defined in:
- lib/rng/rnc_parser.rb
Class Method Summary collapse
- .convert_to_rng(tree) ⇒ Object
- .parse(input) ⇒ Object
- .process_content_item(xml, item) ⇒ Object
- .to_rnc(schema) ⇒ Object
Class Method Details
.convert_to_rng(tree) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/rng/rnc_parser.rb', line 109 def self.convert_to_rng(tree) builder = Nokogiri::XML::Builder.new(encoding: "UTF-8") do |xml| if tree[:root].key?(:start) # This is a grammar with named patterns xml.grammar(xmlns: "http://relaxng.org/ns/structure/1.0") do # Add datatype library if present xml.datatypeLibrary tree[:datatype_library][:uri][:string].to_s if tree[:datatype_library] # Process start pattern xml.start do process_content_item(xml, tree[:root][:start]) end # Process named patterns tree[:definitions]&.each do |def_item| next unless def_item.key?(:name) xml.define(name: def_item[:name][:identifier].to_s) do process_content_item(xml, def_item[:pattern]) end end end else # This is a simple element pattern process_content_item(xml, tree[:root]) end end builder.to_xml end |
.parse(input) ⇒ Object
97 98 99 100 101 |
# File 'lib/rng/rnc_parser.rb', line 97 def self.parse(input) parser = new tree = parser.parse(input.strip) convert_to_rng(tree) end |
.process_content_item(xml, item) ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/rng/rnc_parser.rb', line 140 def self.process_content_item(xml, item) if item.key?(:name) # Element definition attrs = {} attrs[:name] = item[:name][:local_name][:identifier].to_s attrs[:ns] = item[:name][:prefix][:identifier].to_s if item[:name][:prefix] xml.element(attrs) do if item[:content] item[:content][:items].each do |content_item| process_content_item(xml, content_item) end end end # Handle occurrence if item[:occurrence] case item[:occurrence].to_s when "*" xml.parent.name = "zeroOrMore" when "+" xml.parent.name = "oneOrMore" when "?" xml.parent.name = "optional" end end elsif item.key?(:attr_name) # Attribute definition attrs = {} attrs[:name] = item[:attr_name][:local_name][:identifier].to_s attrs[:ns] = item[:attr_name][:prefix][:identifier].to_s if item[:attr_name][:prefix] xml.attribute(attrs) do if item[:type] == "text" xml.text elsif item[:type].key?(:prefix) xml.data(type: item[:type][:type][:identifier].to_s, datatypeLibrary: "http://www.w3.org/2001/XMLSchema-datatypes") end end elsif item.key?(:text) xml.text elsif item.key?(:empty) xml.empty elsif item.key?(:group) xml.group do item[:group][:items].each do |group_item| process_content_item(xml, group_item) end end # Handle occurrence if item[:occurrence] case item[:occurrence].to_s when "*" xml.parent.name = "zeroOrMore" when "+" xml.parent.name = "oneOrMore" when "?" xml.parent.name = "optional" end end elsif item.key?(:first) && item.key?(:rest) # Choice definition xml.choice do process_content_item(xml, item[:first]) item[:rest].each do |choice_item| process_content_item(xml, choice_item[:second]) end end elsif item.key?(:ref) # Reference to a named pattern xml.ref(name: item[:ref][:identifier].to_s) end end |
.to_rnc(schema) ⇒ Object
103 104 105 106 107 |
# File 'lib/rng/rnc_parser.rb', line 103 def self.to_rnc(schema) # Convert RNG schema to RNC builder = RncBuilder.new builder.build(schema) end |