From defa7810817b5976806c25428f5a050aa710a3b5 Mon Sep 17 00:00:00 2001 From: James Healy Date: Mon, 13 Oct 2008 01:07:39 +1100 Subject: [PATCH] working on switching to ROXML - ROXML >= 2.0 uses libxml instead of REXML for parsing. Much faster --- Rakefile | 2 +- data/9780194351898.xml | 2 -- examples/read.rb | 4 +-- lib/onix.rb | 36 ++++++++++----------- lib/onix/contributor.rb | 30 +++++++++--------- lib/onix/header.rb | 46 +++++++++++++-------------- lib/onix/message.rb | 46 +++++++++++++-------------- lib/onix/product.rb | 58 ++++++++++++---------------------- lib/onix/product_identifier.rb | 6 ++-- lib/onix/title.rb | 8 ++--- lib/onix/website.rb | 8 ++--- 11 files changed, 112 insertions(+), 134 deletions(-) diff --git a/Rakefile b/Rakefile index 383268d..f35ee7b 100644 --- a/Rakefile +++ b/Rakefile @@ -62,7 +62,7 @@ spec = Gem::Specification.new do |s| "tasks/**/*.rb", "tasks/**/*.rake" ] - s.add_dependency('xml-mapping', '>= 0.8.1') + s.add_dependency('roxml', '2.1') end Rake::GemPackageTask.new(spec) do |p| diff --git a/data/9780194351898.xml b/data/9780194351898.xml index 298dfc5..3b6aa9d 100644 --- a/data/9780194351898.xml +++ b/data/9780194351898.xml @@ -1,5 +1,3 @@ - -
TitlePage diff --git a/examples/read.rb b/examples/read.rb index 2b3dfdc..472398e 100644 --- a/examples/read.rb +++ b/examples/read.rb @@ -2,9 +2,9 @@ require 'onix' -filename = File.join(File.dirname(__FILE__), "..","data","jul.xml") +filename = File.join(File.dirname(__FILE__), "..","data","9780194351898.xml") -msg = ONIX::Message.load_from_file(filename) +msg = ONIX::Message.parse(File.read(filename)) puts msg.header.inspect puts puts "#{msg.products.size} products" diff --git a/lib/onix.rb b/lib/onix.rb index b13112d..94ec7ea 100644 --- a/lib/onix.rb +++ b/lib/onix.rb @@ -1,30 +1,30 @@ require 'rubygems' -gem 'xml-mapping', '0.8.1' -require 'xml/mapping' +gem 'roxml', '2.1' +require 'roxml' # custom xml-mapping node types -require File.join(File.dirname(__FILE__), "onix", "two_digit_node") -require File.join(File.dirname(__FILE__), "onix", "date_node") +#require File.join(File.dirname(__FILE__), "onix", "two_digit_node") +#require File.join(File.dirname(__FILE__), "onix", "date_node") # core files # - ordering is important, classes need to be defined before any # other class can use them -require File.join(File.dirname(__FILE__), "onix", "sender_identifier") -require File.join(File.dirname(__FILE__), "onix", "addressee_identifier") +#require File.join(File.dirname(__FILE__), "onix", "sender_identifier") +#require File.join(File.dirname(__FILE__), "onix", "addressee_identifier") require File.join(File.dirname(__FILE__), "onix", "header") require File.join(File.dirname(__FILE__), "onix", "product_identifier") require File.join(File.dirname(__FILE__), "onix", "title") require File.join(File.dirname(__FILE__), "onix", "website") require File.join(File.dirname(__FILE__), "onix", "contributor") -require File.join(File.dirname(__FILE__), "onix", "subject") -require File.join(File.dirname(__FILE__), "onix", "other_text") -require File.join(File.dirname(__FILE__), "onix", "media_file") -require File.join(File.dirname(__FILE__), "onix", "imprint") -require File.join(File.dirname(__FILE__), "onix", "publisher") -require File.join(File.dirname(__FILE__), "onix", "sales_restriction") -require File.join(File.dirname(__FILE__), "onix", "stock") -require File.join(File.dirname(__FILE__), "onix", "price") -require File.join(File.dirname(__FILE__), "onix", "supply_detail") +#require File.join(File.dirname(__FILE__), "onix", "subject") +#require File.join(File.dirname(__FILE__), "onix", "other_text") +#require File.join(File.dirname(__FILE__), "onix", "media_file") +#require File.join(File.dirname(__FILE__), "onix", "imprint") +#require File.join(File.dirname(__FILE__), "onix", "publisher") +#require File.join(File.dirname(__FILE__), "onix", "sales_restriction") +#require File.join(File.dirname(__FILE__), "onix", "stock") +#require File.join(File.dirname(__FILE__), "onix", "price") +#require File.join(File.dirname(__FILE__), "onix", "supply_detail") require File.join(File.dirname(__FILE__), "onix", "product") require File.join(File.dirname(__FILE__), "onix", "message") require File.join(File.dirname(__FILE__), "onix", "stream_reader") @@ -41,9 +41,9 @@ module ONIX module Version #:nodoc: Major = 0 - Minor = 2 - Tiny = 8 - + Minor = 4 + Tiny = 0 + String = [Major, Minor, Tiny].join('.') end end diff --git a/lib/onix/contributor.rb b/lib/onix/contributor.rb index ad00f1e..9667bdf 100644 --- a/lib/onix/contributor.rb +++ b/lib/onix/contributor.rb @@ -1,20 +1,20 @@ module ONIX class Contributor - include XML::Mapping + include ROXML - numeric_node :sequence_number, "SequenceNumber", :optional => true - text_node :contributor_role, "ContributorRole", :optional => true - text_node :language_code, "LanguageCode", :optional => true - numeric_node :sequence_number_within_role, "SequenceNumberWithinRole", :optional => true - text_node :person_name, "PersonName", :optional => true - text_node :person_name_inverted, "PersonNameInverted", :optional => true - text_node :titles_before_name, "TitlesBeforeName", :optional => true - text_node :names_before_key, "NamesBeforeKey", :optional => true - text_node :prefix_to_key, "PrefixToKey", :optional => true - text_node :key_names, "KeyNames", :optional => true - text_node :names_after_key, "NamesArterKey", :optional => true - text_node :suffix_to_key, "SuffixToKey", :optional => true - text_node :letters_after_names, "LettersAfterNames", :optional => true - text_node :titles_after_names, "TitlesAfterNames", :optional => true + xml_accessor :sequence_number, :from => "SequenceNumber" + xml_accessor :contributor_role, :from => "ContributorRole" + xml_accessor :language_code, :from => "LanguageCode" + xml_accessor :sequence_number_within_role, :from => "SequenceNumberWithinRole" + xml_accessor :person_name, :from => "PersonName" + xml_accessor :person_name_inverted, :from => "PersonNameInverted" + xml_accessor :titles_before_name, :from => "TitlesBeforeName" + xml_accessor :names_before_key, :from => "NamesBeforeKey" + xml_accessor :prefix_to_key, :from => "PrefixToKey" + xml_accessor :key_names, :from => "KeyNames" + xml_accessor :names_after_key, :from => "NamesArterKey" + xml_accessor :suffix_to_key, :from => "SuffixToKey" + xml_accessor :letters_after_names, :from => "LettersAfterNames" + xml_accessor :titles_after_names, :from => "TitlesAfterNames" end end diff --git a/lib/onix/header.rb b/lib/onix/header.rb index 2a443be..8a586a9 100644 --- a/lib/onix/header.rb +++ b/lib/onix/header.rb @@ -1,31 +1,29 @@ module ONIX class Header - include XML::Mapping + include ROXML - root_element_name "Header" - - text_node :sender_ean_number, "SenderEANNumber", :optional => true - text_node :from_san, "FromSAN", :optional => true - array_node :sender_identifiers, "SenderIdentifier", :optional => true, :class => ONIX::SenderIdentifier - text_node :from_company, "FromCompany", :optional => true - text_node :from_person, "FromPerson", :optional => true - text_node :from_email, "FromEmail", :optional => true - text_node :to_ean_number, "ToEANNumber", :optional => true - text_node :to_san, "ToSAN", :optional => true - array_node :addressee_identifier, "AddresseeIdentifier", :optional => true, :class => ONIX::AddresseeIdentifier - text_node :to_company, "ToCompany", :optional => true - text_node :to_person, "ToPerson", :optional => true - text_node :message_number, "MessageNumber", :optional => true - text_node :message_repeat, "MessageRepeat", :optional => true - date_node :sent_date, "SentDate", :optional => true - text_node :message_note, "MessageNote", :optional => true + xml_accessor :from_person, :from => "FromPerson" + xml_accessor :sender_ean_number, :from => "SenderEANNumber" + xml_accessor :from_san, :from => "FromSAN" + #array_node :sender_identifiers, :from => "SenderIdentifier", :class => ONIX::SenderIdentifier + xml_accessor :from_company, :from => "FromCompany" + xml_accessor :from_email, :from => "FromEmail" + xml_accessor :to_ean_number, :from => "ToEANNumber" + xml_accessor :to_san, :from => "ToSAN" + #array_node :addressee_identifier, "AddresseeIdentifier", :class => ONIX::AddresseeIdentifier + xml_accessor :to_company, :from => "ToCompany" + xml_accessor :to_person, :from => "ToPerson" + xml_accessor :message_number, :from => "MessageNumber" + xml_accessor :message_repeat, :from => "MessageRepeat" + #date_node :sent_date, "SentDate" + xml_accessor :message_note, :from => "MessageNote" # defaults - text_node :default_language_of_text, "DefaultLanguageOfText", :optional => true - text_node :default_price_type_code, "DefaultPriceTypeCode", :optional => true - text_node :default_currency_code, "DefaultCurrencyCode", :optional => true - text_node :default_linear_unit, "DefaultLinearUnit", :optional => true # TODO deprecated. make read only - text_node :default_weight_unit, "DefaultWeightUnit", :optional => true # TODO deprecated. make read only - text_node :default_class_of_trade, "DefaultClassOfTrade", :optional => true + xml_accessor :default_language_of_text, :from => "DefaultLanguageOfText" + xml_accessor :default_price_type_code, :from => "DefaultPriceTypeCode" + xml_accessor :default_currency_code, :from => "DefaultCurrencyCode" + xml_reader :default_linear_unit, :from => "DefaultLinearUnit" # deprecated + xml_reader :default_weight_unit, :from => "DefaultWeightUnit" # deprecated + xml_accessor :default_class_of_trade, :from => "DefaultClassOfTrade" end end diff --git a/lib/onix/message.rb b/lib/onix/message.rb index 4be7201..20926a2 100644 --- a/lib/onix/message.rb +++ b/lib/onix/message.rb @@ -42,46 +42,44 @@ module ONIX # # puts msg.to_s class Message - include XML::Mapping + include ROXML ONIX_DTD_URL = "http://www.editeur.org/onix/2.1/reference/onix-international.dtd" - root_element_name "ONIXMessage" - - object_node :header, "Header", :class => ONIX::Header - array_node :products, "Product", :class => ONIX::Product + xml_accessor :header, ONIX::Header, :from => "Header" + xml_accessor :products, [ONIX::Product], :from => "Product"#, :in => "products" # create a new onix message - def initialize - self.products = [] - end + #def initialize + #self.products = [] + #end # build this message into an XML string - def to_s - to_stringio.string - end + #def to_s + # to_stringio.string + #end # build this message into an XML StringIO object - def to_stringio - write(StringIO.new) - end + #def to_stringio + # write(StringIO.new) + #end # build this message into an XML and write it to the specified IO stream # # File.open("somefile.xml","w") do |output| # msg.write(output) # end - def write(io) - formatter = REXML::Formatters::Default.new(2) + #def write(io) + # formatter = REXML::Formatters::Default.new(2) - decl = REXML::XMLDecl.new - doctype = REXML::DocType.new('ONIXMessage', "SYSTEM \"#{ONIX::Message::ONIX_DTD_URL}\"") - decl.encoding = "utf-8" - io.write(decl.to_s+"\n") - io.write(doctype.to_s+"\n") + # decl = REXML::XMLDecl.new + # doctype = REXML::DocType.new('ONIXMessage', "SYSTEM \"#{ONIX::Message::ONIX_DTD_URL}\"") + # decl.encoding = "utf-8" + # io.write(decl.to_s+"\n") + # io.write(doctype.to_s+"\n") - formatter.write(self.save_to_xml, io) - io - end + # formatter.write(self.save_to_xml, io) + # io + #end end end diff --git a/lib/onix/product.rb b/lib/onix/product.rb index ff8f3d0..1d1ae94 100644 --- a/lib/onix/product.rb +++ b/lib/onix/product.rb @@ -1,43 +1,27 @@ module ONIX class Product - include XML::Mapping + include ROXML - root_element_name "Product" + xml_accessor :record_reference, :from => "RecordReference" + xml_accessor :notification_type, :from => "NotificationType" + xml_accessor :product_identifiers, [ONIX::ProductIdentifier], :from => "ProductIdentifier" + xml_accessor :product_form, :from => "ProductForm" + xml_accessor :series, :from => "Series" + xml_accessor :edition, :from => "Edition" + xml_accessor :titles, [ONIX::Title], :from => "Title" + xml_accessor :websites, [ONIX::Website], :from => "Website" + xml_accessor :contributors, [ONIX::Contributor], :from => "Contributor" + xml_accessor :number_of_pages, :from => "NumberOfPages" + xml_accessor :bic_main_subject, :from => "BICMainSubject" + #array_node :subjects, "Subject", , :class => ONIX::Subject + #array_node :text, "OtherText", , :class => ONIX::OtherText + #array_node :media_files, "MediaFile", , :class => ONIX::MediaFile + #array_node :imprints, "Imprint", , :class => ONIX::Imprint + #array_node :publishers, "Publisher", , :class => ONIX::Publisher + xml_accessor :publishing_status, :from => "PublishingStatus" + xml_accessor :publication_date, :from => "PublicationDate" + #array_node :sales_restrictions, "SalesRestriction", , :class => ONIX::SalesRestriction + #array_node :supply_details, "SupplyDetail", , :class => ONIX::SupplyDetail - text_node :record_reference, "RecordReference" - two_digit_node :notification_type, "NotificationType" - array_node :product_identifiers, "ProductIdentifier", :class => ONIX::ProductIdentifier - text_node :product_form, "ProductForm", :optional => true - text_node :series, "Series", :optional => true - text_node :edition, "Edition", :optional => true - array_node :titles, "Title", :optional => true, :class => ONIX::Title - array_node :websites, "Website", :optional => true, :class => ONIX::Website - array_node :contributors, "Contributor", :optional => true, :class => ONIX::Contributor - numeric_node :number_of_pages, "NumberOfPages", :optional => true - text_node :bic_main_subject, "BICMainSubject", :optional => true - array_node :subjects, "Subject", :optional => true, :class => ONIX::Subject - array_node :text, "OtherText", :optional => true, :class => ONIX::OtherText - array_node :media_files, "MediaFile", :optional => true, :class => ONIX::MediaFile - array_node :imprints, "Imprint", :optional => true, :class => ONIX::Imprint - array_node :publishers, "Publisher", :optional => true, :class => ONIX::Publisher - two_digit_node :publishing_status, "PublishingStatus", :optional => true - text_node :publication_date, "PublicationDate", :optional => true - array_node :sales_restrictions, "SalesRestriction", :optional => true, :class => ONIX::SalesRestriction - array_node :supply_details, "SupplyDetail", :optional => true, :class => ONIX::SupplyDetail - - def initialize - # setup some default values for when the object is created from scratch - self.product_identifiers = [] - self.titles = [] - self.websites = [] - self.contributors = [] - self.subjects = [] - self.text = [] - self.media_files = [] - self.imprints = [] - self.publishers = [] - self.sales_restrictions = [] - self.supply_details = [] - end end end diff --git a/lib/onix/product_identifier.rb b/lib/onix/product_identifier.rb index f03b1c7..2bac2c0 100644 --- a/lib/onix/product_identifier.rb +++ b/lib/onix/product_identifier.rb @@ -1,8 +1,8 @@ module ONIX class ProductIdentifier - include XML::Mapping + include ROXML - two_digit_node :product_id_type, "ProductIDType" - text_node :id_value, "IDValue" + xml_accessor :product_id_type, :from => "ProductIDType" + xml_accessor :id_value, :from => "IDValue" end end diff --git a/lib/onix/title.rb b/lib/onix/title.rb index dd1f1be..ab358c8 100644 --- a/lib/onix/title.rb +++ b/lib/onix/title.rb @@ -1,10 +1,10 @@ module ONIX class Title - include XML::Mapping + include ROXML - two_digit_node :title_type, "TitleType" - text_node :title_text, "TitleText" - text_node :subtitle, "Subtitle", :optional => true + xml_accessor :title_type, :from => "TitleType" + xml_accessor :title_text, :from => "TitleText" + xml_accessor :subtitle, :from => "Subtitle" end end diff --git a/lib/onix/website.rb b/lib/onix/website.rb index 8819faf..0015181 100644 --- a/lib/onix/website.rb +++ b/lib/onix/website.rb @@ -1,9 +1,9 @@ module ONIX class Website - include XML::Mapping + include ROXML - numeric_node :website_role, "WebsiteRole", :optional => true - text_node :website_description, "WebsiteDescription", :optional => true - text_node :website_link, "WebsiteLink" + xml_accessor :website_role, :from => "WebsiteRole" + xml_accessor :website_description, :from => "WebsiteDescription" + xml_accessor :website_link, :from => "WebsiteLink" end end