lyber-core 1.3.0 → 3.2.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,116 +0,0 @@
1
- require 'nokogiri'
2
-
3
- #<oai_dc:dc xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:oai_dc="http://www.openarchives.org/OAI/2.0/oai_dc/"
4
- #xmlns:srw_dc="info:srw/schema/1/dc-schema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5
- #xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/oai_dc/ http://cosimo.stanford.edu/standards/oai_dc/v2/oai_dc.xsd">
6
- # <dc:title>Life of Abraham Lincoln, sixteenth president of the United States: Containing his early
7
- #history and political career; together with the speeches, messages, proclamations and other official
8
- #documents illus. of his eventful administration</dc:title>
9
- # <dc:creator>Crosby, Frank.</dc:creator>
10
- # <dc:format>text</dc:format>
11
- # <dc:language>eng</dc:language>
12
- # <dc:subject>E457 .C94</dc:subject>
13
- # <dc:identifier>lccn:11030686</dc:identifier>
14
- # <dc:identifier>callseq:1</dc:identifier>
15
- # <dc:identifier>shelfseq:973.7111 .L731CR</dc:identifier>
16
- # <dc:identifier>catkey:1206382</dc:identifier>
17
- # <dc:identifier>barcode:36105005459602</dc:identifier>
18
- # <dc:identifier>uuid:ddcf5f1a-0331-4345-beca-e66f7db276eb</dc:identifier>
19
- # <dc:identifier>google:STANFORD_36105005459602</dc:identifier>
20
- # <dc:identifier>druid:ng786kn0371</dc:identifier>
21
- #</oai_dc:dc>
22
-
23
- class DublinCore
24
-
25
-
26
- attr_accessor :xml
27
-
28
- attr_accessor :title
29
- attr_accessor :creator
30
- attr_accessor :subject
31
- attr_accessor :description
32
- attr_accessor :publisher
33
- attr_accessor :contributor
34
- attr_accessor :date
35
- attr_accessor :type
36
- attr_accessor :format
37
- attr_accessor :identifier
38
- attr_accessor :source
39
- attr_accessor :language
40
- attr_accessor :relation
41
- attr_accessor :coverage
42
- attr_accessor :rights
43
-
44
-
45
-
46
-
47
- def initialize(xml = nil)
48
-
49
- @title ||= []
50
- @creator ||= []
51
- @subject ||= []
52
- @description ||= []
53
- @publisher ||= []
54
- @contributor ||= []
55
- @date ||= []
56
- @type ||= []
57
- @format ||= []
58
- @identifier ||= []
59
- @source ||= []
60
- @language ||= []
61
- @relation ||= []
62
- @coverage ||= []
63
- @rights ||= []
64
-
65
- # if the new is given an xml string, store that in the xml attr_accessor and don't rebuild.
66
- # this will allow users to access the raw unprocessed XML string via @xml.
67
- if xml.nil?
68
- build_xml()
69
- else
70
- @xml = xml
71
- end
72
-
73
- end #initalize
74
-
75
-
76
- def build_xml()
77
- builder = Nokogiri::XML::Builder.new do |xml|
78
- xml.dc('xmlns:dc' => 'http://purl.org/dc/elements/1.1/', 'xmlns:srw_dc' => 'info:srw/schema/1/dc-schema', "xmlns:xsi" => "http://www.w3.org/2001/XMLSchema-instance" ) {
79
- xml.parent.namespace = xml.parent.add_namespace_definition('oai_dc','http://www.openarchives.org/OAI/2.0/oai_dc/')
80
- xml.parent.add_namespace_definition("xsi:schemaLocation", "http://www.openarchives.org/OAI/2.0/oai_dc/ http://cosimo.stanford.edu/standards/oai_dc/v2/oai_dc.xsd")
81
- self.instance_variables.each do |var|
82
- unless var == "@xml"
83
- self.instance_variable_get(var).each { |v| xml['dc'].send("#{var.gsub('@','')}_", v) }
84
- end #unless
85
- end #instance_Variables.each
86
- }
87
- end
88
- @xml = builder.to_xml
89
- end
90
-
91
-
92
-
93
- # This method rebuilds the xml attr_accesor and returns it as a string.
94
- def to_xml
95
- build_xml
96
- return self.xml
97
- end #to_xml
98
-
99
-
100
- # This method takes DC XML as a string, and maps the root child node to their proper attr_accesor.
101
-
102
- def self.from_xml(xml="")
103
- dc = DublinCore.new(xml)
104
- doc = Nokogiri::XML(xml)
105
- children = doc.root.element_children
106
- children.each do |c|
107
- if dc.instance_variables.include?("@#{c.name}")
108
- dc.send("#{c.name}").send("<<", c.text.strip)
109
- end #if
110
- end #each
111
- return dc
112
- end #from_xml
113
-
114
-
115
-
116
- end #dublin_core
@@ -1,264 +0,0 @@
1
- require 'nokogiri'
2
- #<identityMetadata>
3
- # <objectId>druid:rt923jk342</objectId>
4
- # <objectType>item</objectType>
5
- # <objectLabel>google download barcode 36105049267078</objectLabel>
6
- # <objectCreator>DOR</objectCreator>
7
- # <citationTitle>Squirrels of North America</citationTitle>
8
- # <citationCreator>Eder, Tamara, 1974-</citationCreator>
9
- # <sourceId source="google">STANFORD_342837261527</sourceId>
10
- # <otherId name="barcode">342837261527</otherId>
11
- # <otherId name="catkey">129483625</otherId>
12
- # <otherId name="uuid">7f3da130-7b02-11de-8a39-0800200c9a66</otherId>
13
- # <agreementId>druid:yh72ms9133</agreementId>
14
- # <tag>Google Books : Phase 1</tag>
15
- # <tag>Google Books : Scan source STANFORD</tag>
16
- #</identityMetadata>
17
-
18
-
19
- # this just maps the #value method to return the "text", "source", "name" values form the hash
20
- class SourceId
21
- attr_accessor :source
22
- attr_accessor :value
23
-
24
- def xml_values
25
- [self.value, {:source => self.source}]
26
- end
27
-
28
- def to_s
29
- "#{self.source}:#{self.value}"
30
- end
31
-
32
- def empty?
33
- (self.source.nil? || self.source.empty?) && (self.value.nil? || self.value.empty?)
34
- end
35
-
36
- end
37
-
38
- class OtherId
39
- attr_accessor :name
40
- attr_accessor :value
41
-
42
- def xml_values
43
- [self.value, {:name => self.name}]
44
- end
45
-
46
- def to_s
47
- "#{self.name}:#{self.value}"
48
- end
49
-
50
- def empty?
51
- (self.name.nil? || self.name.empty?) && (self.value.nil? || self.value.empty?)
52
- end
53
-
54
- end
55
-
56
- class Tag
57
- attr_accessor :value
58
-
59
- def xml_values
60
- [self.value]
61
- end
62
- end
63
-
64
-
65
- class IdentityMetadata
66
-
67
-
68
-
69
- # these are single values
70
- attr_accessor :objectId
71
- attr_reader :sourceId, :tags
72
- # these instance vars map to nodes in the identityMetadata XML
73
- attr_accessor :objectTypes, :objectLabels, :objectCreators, :citationCreators, :citationTitle,
74
- :otherIds, :agreementIds, :objectAdminClass
75
- # this stores the xml string
76
- attr_accessor :xml
77
-
78
-
79
- def initialize(xml = nil)
80
-
81
- @objectId, @citationTitle, @objectAdminClass = "", "", "" #there can only be one of these values
82
- @sourceId = SourceId.new #there can be only one.
83
- @otherIds, @tags = [], [] # this is an array that will be filled with OtherId and Tag objects
84
- @objectTypes, @objectLabels, @objectCreators, @citationCreators, @agreementIds = [], [], [], [], [], []
85
-
86
-
87
- # if the new is given an xml string, store that in the xml attr_accessor and don't rebuild.
88
- # this will allow users to access the raw unprocessed XML string via @xml.
89
- if xml.nil?
90
- build_xml()
91
- else
92
- @xml = xml
93
- end
94
- end #def init
95
-
96
- #this builds the xml based on the instance variables in the object. If it's a hash, this assumes that we want to
97
- #use attributes ==> {"text"=> "7f3da130-7b02-11de-8a39-0800200c9a66", "name" => "uuid" }. If the instance var is
98
- # an array, we assume we don't need attrs, so all values get put into the text node.
99
- def build_xml()
100
- builder = Nokogiri::XML::Builder.new do |xml|
101
- xml.identityMetadata {
102
- self.instance_variables.each do |var_name|
103
- unless var_name == "@xml"
104
- var = self.instance_variable_get(var_name)
105
- tag_name = var_name[1..-1]
106
- if var.is_a?(Array)
107
- tag_name.chomp!('s')
108
- end
109
- # wrap the singleton properties in a one-element array
110
- var = [var] unless var.respond_to?(:each)
111
- var.each do |v|
112
- if v.respond_to?(:xml_values)
113
- unless (v.respond_to?(:empty?) && v.empty?)
114
- xml.send(tag_name, *(v.xml_values))
115
- end
116
- else
117
- xml.send(tag_name, v.to_s)
118
- end
119
- end #var.each
120
- end #unless
121
- end #instance_variables.each
122
- }
123
- end
124
- @xml = builder.to_xml
125
- end
126
-
127
- # This method rebuilds the xml attr_accesor and returns it as a string.
128
- def to_xml
129
- build_xml
130
- return self.xml
131
- end #to_xml
132
-
133
- #
134
- # The following methods are convience methods
135
- #
136
-
137
-
138
- # Add a new tag to the IdentityMetadata instance
139
- def add_tag(new_tag_value)
140
- # Make sure tag is not already present
141
- unless self.get_tags.include?(new_tag_value)
142
- tag = Tag.new
143
- tag.value = new_tag_value
144
- self.tags << tag
145
- end
146
- return self.get_tags
147
- end
148
-
149
- alias :tag :add_tag
150
-
151
- # Returns an array of tag values
152
- def get_tags()
153
- self.tags.collect { |t| t.value }
154
- end
155
-
156
- # Convenience method to return the first agreementId. There's usually only one.
157
- # Returns nil if there are no agreementIds.
158
- def agreementId
159
- @agreementIds.first
160
- end
161
-
162
-
163
- # Return the OtherId hash for the specified identier name
164
- def get_other_id(name)
165
- self.otherIds.each do |oi|
166
- if oi.name == name
167
- return oi
168
- end
169
- end
170
- return nil
171
- end
172
-
173
- # Return the identifier value for the specified identier name
174
- def get_identifier_value(key)
175
- other_id = self.get_other_id(key)
176
- if other_id != nil && other_id.value != nil
177
- return other_id.value
178
- end
179
- raise "No #{key} indentifier found for druid #{@objectId}"
180
- end
181
-
182
- # Add a new name,value pair to the set of identifiers
183
- def add_identifier(*args)
184
- (key,value) = args
185
- if value.nil? and key =~ /:/
186
- (key,value) = key.split(/:/,2)
187
- end
188
-
189
- other_id = self.get_other_id(key)
190
- if (other_id != nil)
191
- other_id.value = value
192
- else
193
- other_id = OtherId.new
194
- other_id.name = key
195
- other_id.value = value
196
- self.otherIds << other_id
197
- end
198
- end
199
-
200
- def sourceId=(*args)
201
- (source,value) = args
202
- if value.nil? and source =~ /:/
203
- (source,value) = source.split(/:/,2)
204
- end
205
-
206
- self.sourceId.source = source
207
- self.sourceId.value = value
208
- end
209
-
210
- # Return an array of strings where each entry consists of name:value
211
- def get_id_pairs
212
- pairs=Array.new
213
- self.otherIds.collect do |other_id|
214
- other_id.to_s
215
- end
216
- end
217
-
218
- #another convience method to allow citationCreator=
219
- def citationCreator=(creator)
220
- if creator.is_a?(Array)
221
- self.citationCreators = creator
222
- elsif creator.is_a?(String)
223
- self.citationCreators = [creator]
224
- else
225
- raise "Identity_metadata.citationCreator requires either a string or array. "
226
- end
227
- end
228
-
229
- #takes a string of XML and constructs the object with all the instance variables added to the correct location.
230
- def self.from_xml(xml="")
231
- if xml.is_a?(File)
232
- xml = xml.read
233
- end
234
-
235
- im = self.new(xml)
236
- doc = Nokogiri::XML(xml)
237
-
238
- children = doc.root.element_children #iterate through the nodes and map them to instance vars in the object.
239
- children.each do |c|
240
- if im.instance_variables.include?("@#{c.name}") or im.instance_variables.include?("@#{c.name}s")
241
- if c.name == "sourceId" #SourceID already has a SourceID object made
242
- im.sourceId.source = c["source"]
243
- im.sourceId.value = c.text.strip
244
- elsif c.name == "otherId" #otherID needs to be cast as an object and stored in an array
245
- im.add_identifier(c['name'],c.text.strip)
246
- elsif c.name == "tag" #tags also need to have objects created and stored in an array
247
- im.add_tag(c.text.strip)
248
- elsif c.name == "objectId" # objectId needs to be mapped to objectId attr_access
249
- im.objectId = c.text.strip
250
- elsif c.name == "citationTitle" #citationTitle also needs to be mapped to citationTitle attr_accessor
251
- im.citationTitle = c.text.strip
252
- elsif c.name == "objectAdminClass" #objectAdminClass also needs to be mapped to objectAdminClass attr_accessor
253
- im.objectAdminClass = c.text.strip
254
- else # everything else gets put into an attr_accessor array (note the added 's' on the attr_accessor.)
255
- im.send("#{c.name}s").send("<<", c.text.strip)
256
- end #if
257
- end #if
258
- end #each
259
-
260
- return im
261
-
262
- end #from_xml
263
-
264
- end #class IdentityMetadata