eadcodec 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,10 @@
1
+ require "EAD"
2
+
3
+ module EAD
4
+ class EADBiogHist < EADElement
5
+ elname "bioghist"
6
+
7
+ xmlsubel :head
8
+ xmlsubel_mult :p
9
+ end
10
+ end
data/lib/EADDate.rb ADDED
@@ -0,0 +1,24 @@
1
+ module EAD
2
+ class EADDate < EADElement
3
+ elname "date"
4
+ elwithvalue
5
+
6
+ xmlattr :altrender
7
+ xmlattr :audience
8
+ xmlattr :calendar
9
+ xmlattr :certainty
10
+ xmlattr :encodinganalog
11
+ xmlattr :era
12
+ xmlattr :id
13
+ xmlattr :normal
14
+ xmlattr :type
15
+ end
16
+
17
+ class EADUnitDate < EADDate
18
+ elname "unitdate"
19
+ elwithvalue
20
+
21
+ xmlattr :datechar
22
+ xmlattr :label
23
+ end
24
+ end
@@ -0,0 +1,21 @@
1
+ require "EADDate"
2
+
3
+ module EAD
4
+ class EADDescription < EADElement
5
+ elname "did"
6
+
7
+ xmlattr :altrender
8
+ xmlattr :audience
9
+ xmlattr :encodinganalog
10
+ xmlattr :id
11
+
12
+ xmlsubel :head
13
+ xmlsubel :origination
14
+ xmlsubel :unittitle
15
+ xmlsubel :unitid
16
+ xmlsubel :abstract
17
+ xmlsubel :physdesc
18
+ xmlsubel :physloc
19
+ xmlsubel :unitdate
20
+ end
21
+ end
@@ -0,0 +1,98 @@
1
+ require "EAD"
2
+ require "XMLUtils"
3
+
4
+ module EAD
5
+ class EADArchDesc < EADElement
6
+ elname "archdesc"
7
+
8
+ xmlattr :level
9
+ xmlsubel :did
10
+ xmlsubel :bioghist
11
+ xmlsubel :scopecontent
12
+ xmlsubel :phystech
13
+ xmlsubel_mult :dsc
14
+
15
+ def initialize
16
+ @level = "fonds"
17
+ end
18
+
19
+ def add_descelem(elem)
20
+ dsc << elem
21
+ end
22
+
23
+ def descelems
24
+ dsc.dup
25
+ end
26
+
27
+ def current_content
28
+ text = create_open_tag+"\n"
29
+ each_subel do |name|
30
+ if not subel_mult?(name)
31
+ value = send(name)
32
+ if value
33
+ doc = REXML::Document.new
34
+ xml = value.create_xml(doc)
35
+ doc.write(text,2)
36
+ end
37
+ end
38
+ end
39
+ text
40
+ end
41
+ end
42
+
43
+ class EADDocument < EADElement
44
+ elname "ead"
45
+
46
+ COUNTRY_CODE = "pt"
47
+ AGENCY_CODE = "iantt"
48
+
49
+ xmlsubel :eadheader
50
+ xmlsubel :archdesc
51
+
52
+ def self.from_file(file)
53
+ doc = REXML::Document.new(File.new(file))
54
+ self.import_xml(doc.elements[1])
55
+ end
56
+
57
+ def initialize(id, title)
58
+ self.eadheader = EADHeader.new(id, title)
59
+ self.archdesc = EADArchDesc.new
60
+ end
61
+
62
+ def add_description(description)
63
+ @archdesc.did = description
64
+ end
65
+
66
+ def ead_did
67
+ @archdesc.did
68
+ end
69
+
70
+ def add_descelem(elem)
71
+ @archdesc.add_descelem(elem)
72
+ end
73
+
74
+ def descelems
75
+ @archdesc.descelems
76
+ end
77
+
78
+ def export(filename)
79
+ doc = Document.new
80
+ doc << XMLDecl.new("1.0", "UTF-8")
81
+ doc << DocType.new("ead SYSTEM", '"file:///home/pedrocr/ead.dtd"')
82
+ create_xml(doc)
83
+ output = File.open(filename, "w")
84
+
85
+ doc.write(output, 0)
86
+ output.write "\n"
87
+ output.close
88
+ end
89
+
90
+ def start_partial_export(file)
91
+ if not already_partial_exported?
92
+ file << "<?xml version='1.0' encoding='UTF-8'?>\n"
93
+ file << "<!DOCTYPE ead SYSTEM \"file:///home/pedrocr/ead.dtd\">\n"
94
+ super
95
+ end
96
+ end
97
+ end
98
+ end
data/lib/EADHeader.rb ADDED
@@ -0,0 +1,70 @@
1
+ module EAD
2
+ class EADId < EADElement
3
+ elname 'eadid'
4
+ elwithvalue
5
+
6
+ xmlattr :countrycode
7
+ xmlattr :encodinganalog
8
+ xmlattr :identifier
9
+ xmlattr :mainagencycode
10
+ xmlattr :publicid
11
+ xmlattr :url
12
+ xmlattr :urn
13
+ end
14
+
15
+ class EADTitleStatement < EADElement
16
+ elname 'titlestmt'
17
+
18
+ xmlsubel :author
19
+ xmlsubel :sponsor
20
+ xmlsubel :subtitle
21
+ xmlsubel :titleproper
22
+
23
+ def initialize(title)
24
+ self.titleproper = title
25
+ end
26
+ end
27
+
28
+ class EADFileDesc < EADElement
29
+ elname 'filedesc'
30
+
31
+ xmlattr :altrender
32
+ xmlattr :audience
33
+ xmlattr :encodinganalog
34
+ xmlattr :id
35
+ xmlsubel :titlestmt
36
+
37
+ def initialize(title)
38
+ self.titlestmt = EADTitleStatement.new(title)
39
+ end
40
+ end
41
+
42
+ class EADHeader < EADElement
43
+ elname 'eadheader'
44
+
45
+ xmlattr :altrender
46
+ xmlattr :audience
47
+ xmlattr :countryenconding
48
+ xmlattr :dateencoding
49
+ xmlattr :encodinganalog
50
+ xmlattr :findaidstatus
51
+ xmlattr :id
52
+ xmlattr :langenconding
53
+ xmlattr :relatedencoding
54
+ xmlattr :repositoryencoding
55
+ xmlattr :scriptencoding
56
+
57
+ xmlsubel :eadid
58
+ xmlsubel :filedesc
59
+ xmlsubel :profiledesc
60
+
61
+ def initialize(id, title)
62
+ self.eadid = EADId.new(id)
63
+ self.filedesc = EADFileDesc.new(title)
64
+ end
65
+
66
+ def eadtitle
67
+ self.filedesc.titlestmt.titleproper
68
+ end
69
+ end
70
+ end
data/lib/EADLevel.rb ADDED
@@ -0,0 +1,72 @@
1
+ module EAD
2
+ class EADLevel < EADElement
3
+ include EADLevels
4
+
5
+ xmlattr :altrender
6
+ xmlattr :audience
7
+ xmlattr :encodinganalog
8
+ xmlattr :id
9
+ xmlattr :level
10
+ xmlattr :otherlevel
11
+ xmlattr :tpattern
12
+
13
+ xmlsubel :did
14
+ xmlsubel :phystech
15
+ xmlsubel :scopecontent
16
+ xmlsubel :note
17
+ xmlsubel :relatedmaterial
18
+ xmlsubel_mult :c
19
+
20
+ attr_reader :parent
21
+
22
+ elnames(*["c","c01","c02","c03","c04","c05","c06",
23
+ "c07","c08","c09","c10","c11","c12"])
24
+
25
+ def initialize
26
+ @unnumbered = false
27
+ end
28
+
29
+ def set_unnumbered
30
+ @unnumbered = true
31
+ end
32
+
33
+ def set_numbered
34
+ @unnumbered = false
35
+ end
36
+
37
+ def unnumbered?
38
+ if not instance_variables.index('@unnumbered')
39
+ @unnumbered = false
40
+ end
41
+ @unnumbered
42
+ end
43
+
44
+ def __parent=(parent)
45
+ super
46
+ if parent.unnumbered?
47
+ self.set_unnumbered
48
+ end
49
+ end
50
+
51
+ def description_level
52
+ if __parent
53
+ return __parent.description_level + 1
54
+ else
55
+ return 0
56
+ end
57
+ end
58
+
59
+ alias_method :oldelname, :elname # Just to shut up the warning
60
+ def elname
61
+ if unnumbered?
62
+ return "c"
63
+ end
64
+
65
+ if description_level < 10
66
+ return "c0" + description_level.to_s
67
+ else
68
+ return "c" + description_level.to_s
69
+ end
70
+ end
71
+ end
72
+ end
data/lib/EADNote.rb ADDED
@@ -0,0 +1,24 @@
1
+ module EAD
2
+ class EADNote < EADElement
3
+ elname "note"
4
+
5
+ xmlattr :actuate
6
+ xmlattr :altrender
7
+ xmlattr :audience
8
+ xmlattr :encodinganalog
9
+ xmlattr :id
10
+ xmlattr :label
11
+ xmlattr :show
12
+ xmlattr :type
13
+
14
+ xmlsubel_mult :p
15
+
16
+ def value
17
+ str = ""
18
+ p.each do |par|
19
+ str += "<p>"+par.value+"</p>\n"
20
+ end
21
+ str
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,16 @@
1
+ module EAD
2
+ class EADPhysTech < EADElement
3
+ elname "phystech"
4
+
5
+ xmlsubel :head
6
+ xmlsubel_mult :p
7
+
8
+ def value
9
+ str = ""
10
+ p.each do |par|
11
+ str += "<p>"+par.value+"</p>\n"
12
+ end
13
+ str
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,17 @@
1
+ module EAD
2
+ class EADCreation < EADElement
3
+ elname "creation"
4
+ xmlsubelements
5
+ end
6
+
7
+ class EADProfileDesc < EADElement
8
+ elname "profiledesc"
9
+
10
+ xmlattr :altrender
11
+ xmlattr :audience
12
+ xmlattr :encodinganalog
13
+ xmlattr :id
14
+
15
+ xmlsubel :creation
16
+ end
17
+ end
@@ -0,0 +1,21 @@
1
+ module EAD
2
+ class EADRelatedMaterial < EADElement
3
+ elname "relatedmaterial"
4
+
5
+ xmlattr :altrender
6
+ xmlattr :audience
7
+ xmlattr :encodinganalog
8
+ xmlattr :id
9
+
10
+ xmlsubel :head
11
+ xmlsubel_mult :p
12
+
13
+ def value
14
+ str = ""
15
+ p.each do |par|
16
+ str += "<p>"+par.value+"</p>\n"
17
+ end
18
+ str
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,21 @@
1
+ module EAD
2
+ class EADScopeContent < EADElement
3
+ elname "scopecontent"
4
+
5
+ xmlattr :altrender
6
+ xmlattr :audience
7
+ xmlattr :encodinganalog
8
+ xmlattr :id
9
+
10
+ xmlsubel :head
11
+ xmlsubel_mult :p
12
+
13
+ def value
14
+ str = ""
15
+ p.each do |par|
16
+ str += "<p>"+par.value+"</p>\n"
17
+ end
18
+ str
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,40 @@
1
+ require "XMLStreamParser"
2
+ require "rexml/document"
3
+ require "EAD"
4
+
5
+ module EAD
6
+ # This is a stream parser that will output EAD element objects. To use it
7
+ # all you have to do is create a listener that responds to methods called
8
+ # el_<eadelementname> with one argument. The argument passed will be the
9
+ # object with the content of the EAD element.
10
+ #
11
+ # The actual stream parsing is done with XMLUtils::XMLStreamParser.
12
+ class EADStreamParser
13
+ def initialize(listener=nil)
14
+ @listener = listener
15
+ end
16
+
17
+ # Parse the text of the EAD file. This can be any IO capable object, like a
18
+ # string or an open file.
19
+ def parse(text)
20
+ parser = XMLUtils::XMLStreamParser.new(self)
21
+ parser.parse(text)
22
+ end
23
+
24
+ # This is the element method that is called by XMLStreamParser everytime
25
+ # a XML element is finished.
26
+ def element(el) #:nodoc:
27
+ method_sym = ("el_"+el.name).to_sym
28
+ if @listener.respond_to?(method_sym)
29
+ cl = XMLCodec::XMLElement.get_element_class(el.name.to_sym)
30
+ eadelement = cl.import_xml_text(el.content)
31
+ eadelement.element_id = el.element_id
32
+ eadelement.parent_id = el.parent_id
33
+ eadelement.__xml_text = el.content
34
+
35
+ @listener.send(method_sym, eadelement)
36
+ el.consume
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,29 @@
1
+ require "EAD.rb"
2
+
3
+ module EAD
4
+ class EADSubordinates < EADElement
5
+ elname "dsc"
6
+
7
+ include EADLevels
8
+
9
+ xmlattr :altrender
10
+ xmlattr :audience
11
+ xmlattr :encondinganalog
12
+ xmlattr :id
13
+ xmlattr :othertype
14
+ xmlattr :tpattern
15
+ xmlattr :type
16
+
17
+ xmlsubel :head
18
+ xmlsubel_mult :p
19
+ xmlsubel_mult :c
20
+
21
+ def description_level
22
+ 0
23
+ end
24
+
25
+ def unnumbered?
26
+ false
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,8 @@
1
+ require "EAD"
2
+
3
+ module EAD
4
+ class EADSubtitle < EADElement
5
+ elname "subtitle"
6
+ xmlsubelements
7
+ end
8
+ end
data/lib/eadcodec.rb ADDED
@@ -0,0 +1 @@
1
+ require 'EAD'
data/test/TestEAD.rb ADDED
@@ -0,0 +1,50 @@
1
+ $:.unshift File.join(File.dirname(__FILE__), "..")
2
+ $-w = true
3
+
4
+ require 'EAD'
5
+ require 'test/unit'
6
+ require 'XMLUtils'
7
+
8
+ class TestEAD < Test::Unit::TestCase
9
+ def initialize(*args)
10
+ super(*args)
11
+ @test_file = nil
12
+ end
13
+
14
+ def setup
15
+ @ead = EAD::EADDocument.new("1", "Teste")
16
+ end
17
+
18
+ def export
19
+ @ead.export(@test_file)
20
+ end
21
+
22
+ def import
23
+ @ead = EAD::EADDocument.from_file(@test_file)
24
+ end
25
+
26
+ def validate_well_formed
27
+ assert(system("rxp", "-xs", @test_file))
28
+ end
29
+
30
+ def validate_dtd
31
+ assert(system("rxp", "-VVs", @test_file))
32
+ #assert(system("xmlstarlet", "val", "-d", "ead.dtd", @test_file))
33
+ end
34
+
35
+ def test_fake
36
+ #Just a fake test to fool Unit::TestCase
37
+ end
38
+
39
+ def compare_xpath(value, path)
40
+ assert_equal(value, select(path))
41
+ end
42
+
43
+ def element_exists(xpath, *args)
44
+ assert(XMLUtils::element_exists(xpath, @test_file), *args)
45
+ end
46
+
47
+ def select(xpath)
48
+ XMLUtils::select_path(xpath, @test_file)
49
+ end
50
+ end
@@ -0,0 +1,73 @@
1
+ require "TestEAD"
2
+
3
+ class TestEADDescription < TestEAD
4
+ def initialize(*args)
5
+ super(*args)
6
+
7
+ @test_file = "test_ead_description.xml"
8
+
9
+ @head = "HEADTest"
10
+ @origination = "somwhere"
11
+ @unittitle = "Some Title"
12
+ @unitid = "15235"
13
+ @abstract = "Some Abstract"
14
+ @physdesc = "Some Description"
15
+ @physloc = "Some Location"
16
+
17
+ @unitdate = "1/1/2001"
18
+ @unitdatelabel = "TestLabel"
19
+ @unitdatetype = "inclusive"
20
+ end
21
+
22
+ def test_description
23
+ did = EAD::EADDescription.new
24
+ did.head = @head
25
+ did.origination = @origination
26
+ did.unittitle = @unittitle
27
+ did.unitid = @unitid
28
+ did.abstract = @abstract
29
+ did.physdesc = @physdesc
30
+ did.physloc = @physloc
31
+
32
+ did.unitdate = EAD::EADUnitDate.new(@unitdate)
33
+ did.unitdate.label = @unitdatelabel
34
+ did.unitdate.type = @unitdatetype
35
+
36
+ @ead.add_description(did)
37
+ export
38
+
39
+ # Since we've added content it is now valid EAD
40
+ validate_dtd
41
+
42
+ element_exists("/ead/archdesc/did", "Test did exists")
43
+ compare_xpath(@head, "/ead/archdesc/did/head")
44
+ compare_xpath(@origination, "/ead/archdesc/did/origination")
45
+ compare_xpath(@unittitle, "/ead/archdesc/did/unittitle")
46
+ compare_xpath(@abstract, "/ead/archdesc/did/abstract")
47
+ compare_xpath(@physdesc, "/ead/archdesc/did/physdesc")
48
+ compare_xpath(@physloc, "/ead/archdesc/did/physloc")
49
+ compare_xpath(@unitid, "/ead/archdesc/did/unitid")
50
+
51
+ compare_xpath(@unitdate, "/ead/archdesc/did/unitdate")
52
+ compare_xpath(@unitdatelabel, "/ead/archdesc/did/unitdate/@label")
53
+ compare_xpath(@unitdatetype, "/ead/archdesc/did/unitdate/@type")
54
+ end
55
+
56
+
57
+ def test_import_description
58
+ import
59
+ did = @ead.ead_did
60
+
61
+ assert_equal(@head, did.head.value)
62
+ assert_equal(@origination, did.origination.value)
63
+ assert_equal(@unittitle, did.unittitle.value)
64
+ assert_equal(@unitid, did.unitid.value)
65
+ assert_equal(@abstract, did.abstract.value)
66
+ assert_equal(@physdesc, did.physdesc.value)
67
+ assert_equal(@physloc, did.physloc.value)
68
+
69
+ assert_equal(@unitdate, did.unitdate.value)
70
+ assert_equal(@unitdatelabel, did.unitdate.label)
71
+ assert_equal(@unitdatetype, did.unitdate.type)
72
+ end
73
+ end
@@ -0,0 +1,32 @@
1
+ require "TestEAD"
2
+
3
+ class TestEADDocument < TestEAD
4
+ def setup
5
+ super
6
+ @test_file = "test_ead_document.xml"
7
+ end
8
+
9
+ def test_base_export
10
+ export
11
+ # The base document has an empty archdesc and is thus not valid EAD
12
+ # Just check for well-formed XML, although REXML will probably never
13
+ # output anything else.
14
+ validate_well_formed
15
+
16
+ compare_xpath("1", "/ead/eadheader/eadid")
17
+ compare_xpath("Teste", "/ead/eadheader/filedesc/titlestmt/titleproper")
18
+ end
19
+
20
+ def test_base_import
21
+ import
22
+ assert_equal("1", @ead.eadheader.eadid.value)
23
+ assert_equal("Teste", @ead.eadheader.eadtitle.value)
24
+ end
25
+
26
+ #def test_import_export_2
27
+ # @test_file = "examples/example2.xml"
28
+ # import
29
+ # @test_file = "examples/example2_exported.xml"
30
+ # export
31
+ #end
32
+ end