Module: IPXACT
- Defined in:
- lib/ipxact.rb
Overview
Root and commodity module for the IPXACT Ruby library. In particular, this module should be used for loading the component design files.
Defined Under Namespace
Modules: Parser Classes: Component, Identifier, Platform, Register
Constant Summary collapse
- DESIGN_SCHEMA_PATH =
Path to the
design.xsd
file File.join File.dirname(__FILE__), '../schemas/design.xsd'
- COMPONENT_SCHEMA_PATH =
Path to the
component.xsd
file File.join File.dirname(__FILE__), '../schemas/component.xsd'
Class Method Summary collapse
-
.identifier(path) ⇒ Hash
Returns the component’s identifier (i.e., name, library, vendor, version) from a design or component file.
-
.is_valid_component?(path) ⇒ Boolean
Returns whether the XML file is an IPXACT component (i.e., it is consistent with the
component.xsd
schema.. -
.is_valid_design?(path) ⇒ Boolean
Returns whether the XML file is an IPXACT design (i.e., it is consistent with the
design.xsd
schema). -
.load_components(components_path) ⇒ Hash
Loads the components from an IPXACT XML specification.
-
.load_designs(designs_path) ⇒ Hash
Loads the designs from an IPXACT XML specification.
Class Method Details
.identifier(path) ⇒ Hash
Returns the component’s identifier (i.e., name, library, vendor, version) from a design or component file.
87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/ipxact.rb', line 87 def self.identifier(path) if is_valid_design?(path) || is_valid_component?(path) ipxact_file = File.open(path) ipxact_doc = Nokogiri::XML(ipxact_file) ipxact_file.close IPXACT::Parser::IdentifierData.parse_identifier(ipxact_doc.xpath('./*')) else nil end end |
.is_valid_component?(path) ⇒ Boolean
Returns whether the XML file is an IPXACT component (i.e., it is consistent with the component.xsd
schema.
45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/ipxact.rb', line 45 def self.is_valid_component?(path) raise ArgumentError unless path.is_a? String component_schema_file = File.open(COMPONENT_SCHEMA_PATH) component_schema = Nokogiri::XML::Schema(component_schema_file) component_schema_file.close component_file = File.open(path) component_doc = Nokogiri::XML(component_file) component_file.close component_schema.validate(component_doc).size == 0 end |
.is_valid_design?(path) ⇒ Boolean
Returns whether the XML file is an IPXACT design (i.e., it is consistent with the design.xsd
schema).
66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/ipxact.rb', line 66 def self.is_valid_design?(path) raise ArgumentError unless path.is_a? String design_schema_file = File.open(DESIGN_SCHEMA_PATH) design_schema = Nokogiri::XML::Schema design_schema_file design_schema_file.close design_file = File.open(path) design_doc = Nokogiri::XML(design_file) design_file.close design_schema.validate(design_doc).size == 0 end |
.load_components(components_path) ⇒ Hash
Loads the components from an IPXACT XML specification.
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/ipxact.rb', line 116 def self.load_components(components_path) component_schema_file = File.open(COMPONENT_SCHEMA_PATH) component_schema = Nokogiri::XML::Schema(component_schema_file) component_schema_file.close xml_files = Dir.glob(File.join(components_path, "**", "*.xml")) component_docs = xml_files.inject({}) do |acc, file| component_file = File.open(file) component_doc = Nokogiri::XML(component_file) component_file.close version_tag = component_doc.xpath("/spirit:component/spirit:version") version = version_tag.first.nil? ? "0" : version_tag.first.text name_tag = component_doc.xpath("/spirit:component/spirit:name") name = name_tag.first.nil? ? nil : name_tag.first.text unless name.nil? key = [component_doc.xpath("/spirit:component/spirit:name").first.text, version] acc[key] = component_doc \ if component_schema.validate(component_doc).size == 0 end acc end raise 'No valid component was found!' if component_docs.keys.empty? component_docs end |
.load_designs(designs_path) ⇒ Hash
Loads the designs from an IPXACT XML specification.
with the IPXACT XML (Nokogiri) structure of the design. Should not be used, as this is ‘raw’ IPXACT data.
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 |
# File 'lib/ipxact.rb', line 160 def self.load_designs(designs_path) design_schema_file = File.open(DESIGN_SCHEMA_PATH) design_schema = Nokogiri::XML::Schema design_schema_file design_schema_file.close xml_files = Dir.glob(File.join(designs_path, "**", "*.xml")) design_docs = xml_files.inject({}) do |acc, xml_file| design_file = File.open(xml_file) design_doc = Nokogiri::XML(design_file) design_file.close if design_schema.validate(design_doc).size == 0 design_name = design_doc.xpath("./spirit:design/spirit:name").text design_version = design_doc.xpath("./spirit:design/spirit:version").text acc[[design_name, design_version]] = design_doc end acc end raise 'No valid design could be found!' if design_docs.keys.empty? design_docs end |