/
hgv_trans_identifier.rb
212 lines (159 loc) · 6.63 KB
/
hgv_trans_identifier.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
class HGVTransIdentifier < HGVIdentifier
PATH_PREFIX = 'HGV_trans_EpiDoc'
IDENTIFIER_NAMESPACE = 'hgvtrans'
XML_VALIDATOR = JRubyXML::EpiDocP5Validator
FRIENDLY_NAME = "Translation"
BROKE_LEIDEN_MESSAGE = "Broken Leiden+ below saved to come back to later:\n"
# defined in vendor/plugins/rxsugar/lib/jruby_helper.rb
acts_as_translation
def to_path
if name =~ /#{self.class::TEMPORARY_COLLECTION}/
return self.temporary_path
else
path_components = [ PATH_PREFIX ]
# assume the name is e.g. hgv2302zzr
trimmed_name = self.to_components.last # 2302zzr
hgv_xml_path = trimmed_name + '.xml'
# HGV_trans_EpiDoc uses a flat hierarchy
path_components << hgv_xml_path
# e.g. HGV_trans_EpiDoc/2302zzr.xml
return File.join(path_components)
end
end
def id_attribute
return "hgv-TEMP"
end
def n_attribute
ddb = DDBIdentifier.find_by_publication_id(self.publication.id, :limit => 1)
return ddb.n_attribute
end
def xml_title_text
return " HGVTITLE (DDBTITLE) "
end
def is_valid?(content = nil)
#FIXME added here since trans is not P5 validable yet
return true
end
def self.new_from_template(publication)
new_identifier = super(publication)
new_identifier.stub_text_structure('en')
return new_identifier
end
def related_text
self.publication.identifiers.select{|i| (i.class == DDBIdentifier) && !i.is_reprinted?}.last
end
def before_commit(content)
JRubyXML.apply_xsl_transform(
JRubyXML.stream_from_string(content),
JRubyXML.stream_from_file(File.join(RAILS_ROOT,
%w{data xslt translation preprocess.xsl}))
)
end
def translation_already_in_language?(lang)
lang_path = '/TEI/text/body/div[@type = "translation" and @xml:lang = "' + lang + '"]'
doc = REXML::Document.new(self.xml_content)
result = REXML::XPath.match(doc, lang_path)
if result.length > 0
return true
else
return false
end
end
def stub_text_structure(lang)
translation_stub_xsl =
JRubyXML.apply_xsl_transform(
JRubyXML.stream_from_string(self.related_text.content),
JRubyXML.stream_from_file(File.join(RAILS_ROOT,
%w{data xslt translation ddb_to_translation_xsl.xsl}))
)
rewritten_xml =
JRubyXML.apply_xsl_transform(
JRubyXML.stream_from_string(self.content),
JRubyXML.stream_from_string(translation_stub_xsl),
#:lang => 'en'
#assumed that hard coded 'en' is remnant and should be
:lang => lang
)
self.set_xml_content(rewritten_xml, :comment => "Update translation with stub for @xml:lang='#{lang}'")
end
def preview
JRubyXML.apply_xsl_transform(
JRubyXML.stream_from_string(self.xml_content),
JRubyXML.stream_from_file(File.join(RAILS_ROOT,
%w{data xslt pn start-divtrans-portlet.xsl})))
end
def get_broken_leiden(original_xml = nil)
original_xml_content = original_xml || REXML::Document.new(self.xml_content)
brokeleiden_path = '/TEI/text/body/div[@type = "translation"]/div[@subtype = "brokeleiden"]/note'
brokeleiden_here = REXML::XPath.first(original_xml_content, brokeleiden_path)
if brokeleiden_here.nil?
return nil
else
brokeleiden = brokeleiden_here.get_text.value
return brokeleiden.sub(/^#{Regexp.escape(BROKE_LEIDEN_MESSAGE)}/,'')
end
end
def leiden_trans
original_xml = self.xml_content
original_xml_content = REXML::Document.new(original_xml)
# if XML does not contain broke Leiden send XML to be converted to Leiden and return that
# otherwise, return nil (client can then get_broken_leiden)
if get_broken_leiden(original_xml_content).nil?
body = HGVTransIdentifier.get_body(original_xml)
# transform XML to Leiden+
transformed = HGVTransIdentifier.xml2nonxml(body.to_s) #via jrubyHelper
return transformed
else
return nil
end
end
# Returns a String of the SHA1 of the commit
def set_leiden_translation_content(leiden_translation_content, comment)
# transform back to XML
xml_content = self.leiden_translation_to_xml(leiden_translation_content)
# commit xml to repo
self.set_xml_content(xml_content, :comment => comment)
end
def leiden_translation_to_xml(content)
# transform the Leiden Translation to XML
nonx2x = HGVTransIdentifier.nonxml2xml(content)
nonx2x.sub!(/ xmlns:xml="http:\/\/www.w3.org\/XML\/1998\/namespace"/,'')
transformed_xml_content = REXML::Document.new(nonx2x)
puts nonx2x
puts transformed_xml_content.to_s
# fetch the original content
original_xml_content = REXML::Document.new(self.xml_content)
#rip out the body so we can replace it with the new data
original_xml_content.delete_element('/TEI/text/body')
#add the new data
original_xml_content.elements.each('/TEI/text') { |text_element| text_element.add_element(transformed_xml_content) }
# write back to a string
modified_xml_content = ''
original_xml_content.write(modified_xml_content)
return modified_xml_content
end
def save_broken_leiden_trans_to_xml(brokeleiden, commit_comment = '')
# fetch the original content
original_xml_content = REXML::Document.new(self.xml_content)
#deletes XML with broke Leiden+ if it exists already so can add with updated data
original_xml_content.delete_element('/TEI/text/body/div[@type = "translation"]/div[@subtype = "brokeleiden"]')
#set in XML where to add new div tag to contain broken Leiden+ and add it
basepath = '/TEI/text/body/div[@type = "translation"]'
add_node_here = REXML::XPath.first(original_xml_content, basepath)
add_node_here.add_element 'div', {'type'=>'translation', 'subtype'=>'brokeleiden'}
#set in XML where to add new note tag to contain broken Leiden+ and add it
basepath = '/TEI/text/body/div[@type = "translation"]/div[@subtype = "brokeleiden"]'
add_node_here = REXML::XPath.first(original_xml_content, basepath)
add_node_here.add_element "note"
#set in XML where to add broken Leiden+ and add it
basepath = '/TEI/text/body/div[@type = "translation"]/div[@subtype = "brokeleiden"]/note'
add_node_here = REXML::XPath.first(original_xml_content, basepath)
brokeleiden = BROKE_LEIDEN_MESSAGE + brokeleiden
add_node_here.add_text brokeleiden
# write back to a string
modified_xml_content = ''
original_xml_content.write(modified_xml_content)
# commit xml to repo
self.set_xml_content(modified_xml_content, :comment => commit_comment)
end
end