Skip to content

Commit

Permalink
working on switching to ROXML
Browse files Browse the repository at this point in the history
- ROXML >= 2.0 uses libxml instead of REXML for parsing. Much faster
  • Loading branch information
yob committed Oct 12, 2008
1 parent f3808ef commit defa781
Show file tree
Hide file tree
Showing 11 changed files with 112 additions and 134 deletions.
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down
2 changes: 0 additions & 2 deletions data/9780194351898.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE ONIXMessage SYSTEM "http://www.editeur.org/onix/2.1/reference/onix-international.dtd">
<ONIXMessage>
<Header>
<FromCompany>TitlePage</FromCompany>
Expand Down
4 changes: 2 additions & 2 deletions examples/read.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
36 changes: 18 additions & 18 deletions lib/onix.rb
Original file line number Diff line number Diff line change
@@ -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")
Expand All @@ -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
30 changes: 15 additions & 15 deletions lib/onix/contributor.rb
Original file line number Diff line number Diff line change
@@ -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
46 changes: 22 additions & 24 deletions lib/onix/header.rb
Original file line number Diff line number Diff line change
@@ -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
46 changes: 22 additions & 24 deletions lib/onix/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
58 changes: 21 additions & 37 deletions lib/onix/product.rb
Original file line number Diff line number Diff line change
@@ -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
6 changes: 3 additions & 3 deletions lib/onix/product_identifier.rb
Original file line number Diff line number Diff line change
@@ -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
8 changes: 4 additions & 4 deletions lib/onix/title.rb
Original file line number Diff line number Diff line change
@@ -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
8 changes: 4 additions & 4 deletions lib/onix/website.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit defa781

Please sign in to comment.