Skip to content

Commit

Permalink
Added commented oddb2xml.xsd. Include rspec tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ngiger committed May 14, 2014
1 parent 953218a commit d6e1672
Show file tree
Hide file tree
Showing 4 changed files with 1,168 additions and 102 deletions.
11 changes: 7 additions & 4 deletions README.md
Expand Up @@ -100,14 +100,17 @@ See also http://bugs.ruby-lang.org/projects/ruby/wiki/ReleaseEngineering


## XSD files
If you need the XSD files, generate them yourself using the javabeans tool:

The file oddb2xml.xsd was manually created by merging the output of the xmlbeans tools inst2xsd and trang

* http://xmlbeans.apache.org/docs/2.0.0/guide/tools.html#inst2xsd
* http://www.thaiopensource.com/relaxng/trang.html

Running rake spec will validated the XML-files generated during the tests using the Nokogiri validator.

this will generate you a valid XSD file that can be used to validate against the XML file.
Manually you can also validate (assuming that you have installed the xmlbeans tools) all generated XML-files using

i.e.:
* /home/zeno/.software/xmlbeans-2.6.0/bin/inst2xsd oddb_article.xml -outPrefix oddb_article
* xsdvalidate oddb2xml.xsd *.xml

## XML files

Expand Down
110 changes: 28 additions & 82 deletions lib/oddb2xml/builder.rb
Expand Up @@ -15,12 +15,21 @@ def create_element name, *args, &block
end

module Oddb2xml
XML_OPTIONS = {
'xmlns:xsd' => 'http://www.w3.org/2001/XMLSchema',
'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
'xmlns' => 'http://wiki.oddb.org/wiki.php?pagename=Swissmedic.Datendeklaration',
'CREATION_DATETIME' => Time.new.strftime('%FT%T%z'),
'PROD_DATE' => Time.new.strftime('%FT%T%z'),
'VALID_DATE' => Time.new.strftime('%FT%T%z')
}
class Builder
attr_accessor :subject, :index, :items, :flags, :lppvs,
:actions, :migel, :orphans, :fridges,
:infos, :packs, :prices,
:ean14, :tag_suffix,
:companies, :people
:companies, :people,
:xsd
def initialize(args = {})
@options = args
@subject = nil
Expand Down Expand Up @@ -265,12 +274,7 @@ def build_substance
xml.doc.tag_suffix = @tag_suffix
datetime = Time.new.strftime('%FT%T%z')
xml.SUBSTANCE(
'xmlns:xsd' => 'http://www.w3.org/2001/XMLSchema',
'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
'xmlns' => 'http://wiki.oddb.org/wiki.php?pagename=Swissmedic.Datendeklaration',
'CREATION_DATETIME' => datetime,
'PROD_DATE' => datetime,
'VALID_DATE' => datetime
XML_OPTIONS
) {
Oddb2xml.log "build_substance #{@substances.size} substances"
exit 2 if @options[:extended] and @substances.size == 0
Expand Down Expand Up @@ -299,14 +303,7 @@ def build_limitation
_builder = Nokogiri::XML::Builder.new(:encoding => 'utf-8') do |xml|
xml.doc.tag_suffix = @tag_suffix
datetime = Time.new.strftime('%FT%T%z')
xml.LIMITATION(
'xmlns:xsd' => 'http://www.w3.org/2001/XMLSchema',
'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
'xmlns' => 'http://wiki.oddb.org/wiki.php?pagename=Swissmedic.Datendeklaration',
'CREATION_DATETIME' => datetime,
'PROD_DATE' => datetime,
'VALID_DATE' => datetime
) {
xml.LIMITATION(XML_OPTIONS) {
@limitations.each do |lim|
xml.LIM('DT' => '') {
case lim[:key]
Expand Down Expand Up @@ -340,20 +337,16 @@ def build_limitation
end
_builder.to_xml
end
def xml_and_comment(xml, *args, &block)
$stderr.puts "xml_and_comment #{args[0]}"
end
def build_interaction
prepare_interactions
prepare_codes
_builder = Nokogiri::XML::Builder.new(:encoding => 'utf-8') do |xml|
xml.doc.tag_suffix = @tag_suffix
datetime = Time.new.strftime('%FT%T%z')
xml.INTERACTION(
'xmlns:xsd' => 'http://www.w3.org/2001/XMLSchema',
'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
'xmlns' => 'http://wiki.oddb.org/wiki.php?pagename=Swissmedic.Datendeklaration',
'CREATION_DATETIME' => datetime,
'PROD_DATE' => datetime,
'VALID_DATE' => datetime
) {
xml.INTERACTION(XML_OPTIONS) {
Oddb2xml.log "build_interaction #{@interactions.size} interactions"
@interactions.sort_by{|ix| ix[:ixno] }.each do |ix|
xml.IX('DT' => '') {
Expand Down Expand Up @@ -411,14 +404,7 @@ def build_code
_builder = Nokogiri::XML::Builder.new(:encoding => 'utf-8') do |xml|
xml.doc.tag_suffix = @tag_suffix
datetime = Time.new.strftime('%FT%T%z')
xml.CODE(
'xmlns:xsd' => 'http://www.w3.org/2001/XMLSchema',
'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
'xmlns' => 'http://wiki.oddb.org/wiki.php?pagename=Swissmedic.Datendeklaration',
'CREATION_DATETIME' => datetime,
'PROD_DATE' => datetime,
'VALID_DATE' => datetime
) {
xml.CODE(XML_OPTIONS) {
@codes.each_pair do |val, definition|
xml.CD('DT' => '') {
xml.CDTYP definition[:int]
Expand Down Expand Up @@ -451,20 +437,13 @@ def build_product
_builder = Nokogiri::XML::Builder.new(:encoding => 'utf-8') do |xml|
xml.doc.tag_suffix = @tag_suffix
datetime = Time.new.strftime('%FT%T%z')
xml.PRODUCT(
'xmlns:xsd' => 'http://www.w3.org/2001/XMLSchema',
'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
'xmlns' => 'http://wiki.oddb.org/wiki.php?pagename=Swissmedic.Datendeklaration',
'CREATION_DATETIME' => datetime,
'PROD_DATE' => datetime,
'VALID_DATE' => datetime
) {
xml.PRODUCT(XML_OPTIONS) {
list = []
length = 0
@products.each do |obj|
seq = obj[:seq]
length += 1
xml.PRD('DT' => '') {
xml.PRD('DT' => '') {
ean = obj[:ean].to_s
xml.GTIN ean
ppac = ((_ppac = @packs[ean[4..11].intern] and !_ppac[:is_tier]) ? _ppac : {})
Expand Down Expand Up @@ -605,14 +584,7 @@ def build_article
_builder = Nokogiri::XML::Builder.new(:encoding => 'utf-8') do |xml|
xml.doc.tag_suffix = @tag_suffix
datetime = Time.new.strftime('%FT%T%z')
xml.ARTICLE(
'xmlns:xsd' => 'http://www.w3.org/2001/XMLSchema',
'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
'xmlns' => 'http://wiki.oddb.org/wiki.php?pagename=Swissmedic.Datendeklaration',
'CREATION_DATETIME' => datetime,
'PROD_DATE' => datetime,
'VALID_DATE' => datetime
) {
xml.ARTICLE(XML_OPTIONS) {
@articles.each do |obj|
idx += 1
Oddb2xml.log "build_article #{idx} of #{@articles.size} articles" if idx % 500 == 0
Expand All @@ -636,9 +608,9 @@ def build_article
if !@prices.empty? && ean && @prices[ean]
price = @prices[ean] # zurrose
end
xml.ART('DT' => '') {
xml.PHAR de_idx[:pharmacode] unless de_idx[:pharmacode].empty?
#xml.GRPCD
xml.ART('DT' => '') {
xml.PHAR de_idx[:pharmacode] unless de_idx[:pharmacode].empty?
#xml.GRPCD
#xml.CDS01
#xml.CDS02
if ppac
Expand All @@ -660,7 +632,7 @@ def build_article
end

if de_idx
xml.SALECD(de_idx[:status].empty? ? 'N' : de_idx[:status])
xml.SALECD(de_idx[:status].empty? ? 'N' : de_idx[:status]) # XML_OPTIONS
end
if pac and pac[:limitation_points]
#xml.INSLIM
Expand Down Expand Up @@ -804,14 +776,7 @@ def build_fi
_builder = Nokogiri::XML::Builder.new(:encoding => 'utf-8') do |xml|
xml.doc.tag_suffix = @tag_suffix
datetime = Time.new.strftime('%FT%T%z')
xml.KOMPENDIUM(
'xmlns:xsd' => 'http://www.w3.org/2001/XMLSchema',
'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
'xmlns' => 'http://wiki.oddb.org/wiki.php?pagename=Swissmedic.Datendeklaration',
'CREATION_DATETIME' => datetime,
'PROD_DATE' => datetime,
'VALID_DATE' => datetime
) {
xml.KOMPENDIUM(XML_OPTIONS) {
length = 0
%w[de fr].each do |lang|
infos = @infos[lang].uniq {|i| i[:monid] }
Expand Down Expand Up @@ -848,14 +813,7 @@ def build_fi_product
_builder = Nokogiri::XML::Builder.new(:encoding => 'utf-8') do |xml|
xml.doc.tag_suffix = @tag_suffix
datetime = Time.new.strftime('%FT%T%z')
xml.KOMPENDIUM_PRODUCT(
'xmlns:xsd' => 'http://www.w3.org/2001/XMLSchema',
'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
'xmlns' => 'http://wiki.oddb.org/wiki.php?pagename=Swissmedic.Datendeklaration',
'CREATION_DATETIME' => datetime,
'PROD_DATE' => datetime,
'VALID_DATE' => datetime
) {
xml.KOMPENDIUM_PRODUCT(XML_OPTIONS) {
length = 0
info_index = {}
%w[de fr].each do |lang|
Expand Down Expand Up @@ -893,13 +851,7 @@ def build_company
_builder = Nokogiri::XML::Builder.new(:encoding => 'utf-8') do |xml|
xml.doc.tag_suffix = @tag_suffix
datetime = Time.new.strftime('%FT%T%z')
xml.Betriebe(
'xmlns:xsd' => 'http://www.w3.org/2001/XMLSchema',
'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
'xmlns' => 'http://wiki.oddb.org/wiki.php?pagename=Swissmedic.Datendeklaration',
'CREATION_DATETIME' => datetime,
'VALID_DATE' => datetime
) {
xml.Betriebe(XML_OPTIONS) {
@companies.each do |c|
xml.Betrieb('DT' => '') {
xml.GLN_Betrieb c[:gln] unless c[:gln].empty?
Expand Down Expand Up @@ -930,13 +882,7 @@ def build_person
_builder = Nokogiri::XML::Builder.new(:encoding => 'utf-8') do |xml|
xml.doc.tag_suffix = @tag_suffix
datetime = Time.new.strftime('%FT%T%z')
xml.Personen(
'xmlns:xsd' => 'http://www.w3.org/2001/XMLSchema',
'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
'xmlns' => 'http://wiki.oddb.org/wiki.php?pagename=Swissmedic.Datendeklaration',
'CREATION_DATETIME' => datetime,
'VALID_DATE' => datetime
) {
xml.Personen(XML_OPTIONS) {
@people.each do |p|
xml.Person('DT' => '') {
xml.GLN_Person p[:gln] unless p[:gln].empty?
Expand Down

0 comments on commit d6e1672

Please sign in to comment.