Class: OoxmlParser::OOXMLDocumentObject
- Inherits:
-
Object
- Object
- OoxmlParser::OOXMLDocumentObject
show all
- Defined in:
- lib/ooxml_parser/common_parser/common_data/ooxml_document_object.rb
Direct Known Subclasses
Background, Borders, CellProperties, CellStyle, Chart, ChartAxis, ChartAxisTitle, Columns, Comment, CommonDocumentStructure, DocumentGrid, DocumentProperties, DocumentStyle, DocxBlip, DocxColor, DocxColorScheme, DocxGradientColor, DocxGraphic, DocxGroupedDrawing, DocxParagraph, DocxParagraphRun, DocxPatternFill, DocxShape, DocxShapeLinePath, DocxShapeProperties, ExcelComment, ExcelCommentCharacterProperties, ForegroundColor, GradientStop, GraphicFrame, HeaderFooter, Hyperlink, ImageFill, LineEnd, NonVisualProperties, Note, NumberingProperties, OOXMLFont, OOXMLShapeBodyProperties, OOXMLTextBox, OldDocxShape, OldDocxShapeFill, Outline, PageProperties, ParagraphProperties, ParagraphRun, ParagraphTab, PresentationAlternateContent, PresentationComment, PresentationCommentAuthor, PresentationFill, PresentationPattern, PresentationShapeProperties, PresentationTheme, PresetTextWarp, RunProperties, Shape, ShapePlaceholder, Slide, Sound, Table, TableBorders, TableCell, TableCellLine, TableGrid, TableLook, TableProperties, TableRow, TableRowProperties, TableStyle, TableStyleElement, TextFill, TextOutline, Tile, Transition, Worksheet, XlsxCell, XlsxColumnProperties, XlsxDrawing, XlsxRow, XlsxTable
Constant Summary
collapse
- DEFAULT_DIRECTORY_FOR_MEDIA =
'/tmp'.freeze
Class Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Class Attribute Details
.namespace_prefix ⇒ Object
Returns the value of attribute namespace_prefix.
20
21
22
|
# File 'lib/ooxml_parser/common_parser/common_data/ooxml_document_object.rb', line 20
def namespace_prefix
@namespace_prefix
end
|
.path_to_folder ⇒ Object
Returns the value of attribute path_to_folder.
24
25
26
|
# File 'lib/ooxml_parser/common_parser/common_data/ooxml_document_object.rb', line 24
def path_to_folder
@path_to_folder
end
|
.root_subfolder ⇒ Object
Returns the value of attribute root_subfolder.
21
22
23
|
# File 'lib/ooxml_parser/common_parser/common_data/ooxml_document_object.rb', line 21
def root_subfolder
@root_subfolder
end
|
.theme ⇒ Object
Returns the value of attribute theme.
22
23
24
|
# File 'lib/ooxml_parser/common_parser/common_data/ooxml_document_object.rb', line 22
def theme
@theme
end
|
.xmls_stack ⇒ Object
Returns the value of attribute xmls_stack.
23
24
25
|
# File 'lib/ooxml_parser/common_parser/common_data/ooxml_document_object.rb', line 23
def xmls_stack
@xmls_stack
end
|
Class Method Details
.add_to_xmls_stack(path) ⇒ Object
72
73
74
75
76
77
78
|
# File 'lib/ooxml_parser/common_parser/common_data/ooxml_document_object.rb', line 72
def add_to_xmls_stack(path)
OOXMLDocumentObject.xmls_stack << if path.include?('..')
"#{File.dirname(OOXMLDocumentObject.xmls_stack.last)}/#{path}"
else
path
end
end
|
.copy_file_and_rename_to_zip(path) ⇒ Object
40
41
42
43
44
45
46
47
48
49
50
|
# File 'lib/ooxml_parser/common_parser/common_data/ooxml_document_object.rb', line 40
def copy_file_and_rename_to_zip(path)
file_name = File.basename(path)
tmp_folder = "/tmp/office_open_xml_parser_#{SecureRandom.uuid}"
file_path = "#{tmp_folder}/#{file_name}"
FileUtils.rm_rf(tmp_folder) if File.directory?(tmp_folder)
FileUtils.mkdir_p(tmp_folder)
path = "#{Dir.pwd}/#{path}" unless path[0] == '/'
raise "Cannot find file by path #{path}" unless File.exist?(path)
FileUtils.cp path, tmp_folder
file_path
end
|
101
102
103
104
105
106
107
108
109
110
111
112
|
# File 'lib/ooxml_parser/common_parser/common_data/ooxml_document_object.rb', line 101
def copy_media_file(path_to_file)
folder_to_save_media = '/tmp/media_from_' + File.basename(OOXMLDocumentObject.path_to_folder)
path_to_copied_file = folder_to_save_media + '/' + File.basename(path_to_file)
full_path_to_file = OOXMLDocumentObject.path_to_folder + path_to_file
unless File.exist?(full_path_to_file)
warn "Couldn't find #{full_path_to_file} file on filesystem. Possible problem in original document"
return nil
end
FileUtils.mkdir(folder_to_save_media) unless File.exist?(folder_to_save_media)
FileUtils.copy_file(full_path_to_file, path_to_copied_file)
path_to_copied_file
end
|
.current_xml ⇒ Object
68
69
70
|
# File 'lib/ooxml_parser/common_parser/common_data/ooxml_document_object.rb', line 68
def current_xml
OOXMLDocumentObject.path_to_folder + OOXMLDocumentObject.xmls_stack.last
end
|
.dir ⇒ Object
64
65
66
|
# File 'lib/ooxml_parser/common_parser/common_data/ooxml_document_object.rb', line 64
def dir
OOXMLDocumentObject.path_to_folder + File.dirname(OOXMLDocumentObject.xmls_stack.last) + '/'
end
|
.encrypted_file?(path_to_file) ⇒ True, False
Returns Check if file is protected by password on open.
28
29
30
31
32
33
34
35
36
37
38
|
# File 'lib/ooxml_parser/common_parser/common_data/ooxml_document_object.rb', line 28
def encrypted_file?(path_to_file)
file_result = `file "#{path_to_file}"`
if file_result.include?('Encrypted') || file_result.include?('Composite Document File V2 Document, No summary info')
warn("File #{path_to_file} is encrypted. Can't parse it")
return true
end
false
end
|
.get_link_from_rels(id) ⇒ Object
80
81
82
83
84
85
|
# File 'lib/ooxml_parser/common_parser/common_data/ooxml_document_object.rb', line 80
def get_link_from_rels(id)
rels_path = dir + "_rels/#{File.basename(OOXMLDocumentObject.xmls_stack.last)}.rels"
raise LoadError, "Cannot find .rels file by path: #{rels_path}" unless File.exist?(rels_path)
relationships = XmlSimple.xml_in(File.open(rels_path))
relationships['Relationship'].each { |relationship| return relationship['Target'] if id == relationship['Id'] }
end
|
87
88
89
90
91
|
# File 'lib/ooxml_parser/common_parser/common_data/ooxml_document_object.rb', line 87
def media_folder
path = "#{DEFAULT_DIRECTORY_FOR_MEDIA}/media_from_#{@file_name}"
FileUtils.mkdir(path) unless File.exist?(path)
path + '/'
end
|
.option_enabled?(node, attribute_name = 'val') ⇒ Boolean
93
94
95
96
97
98
99
|
# File 'lib/ooxml_parser/common_parser/common_data/ooxml_document_object.rb', line 93
def option_enabled?(node, attribute_name = 'val')
return true if node.to_s == '1'
return false if node.to_s == '0'
return false if node.attribute(attribute_name).nil?
status = node.attribute(attribute_name).value
status == 'true' || status == 'on' || status == '1'
end
|
.unzip_file(path_to_file, destination) ⇒ Object
52
53
54
55
56
57
58
59
60
61
62
|
# File 'lib/ooxml_parser/common_parser/common_data/ooxml_document_object.rb', line 52
def unzip_file(path_to_file, destination)
Zip.warn_invalid_date = false
Zip::File.open(path_to_file) do |zip_file|
raise LoadError, "There is no files in zip #{path_to_file}" if zip_file.entries.empty?
zip_file.each do |file|
file_path = File.join(destination, file.name)
FileUtils.mkdir_p(File.dirname(file_path))
zip_file.(file, file_path) unless File.exist?(file_path)
end
end
end
|
Instance Method Details
#==(other) ⇒ Object
10
11
12
13
14
15
16
17
|
# File 'lib/ooxml_parser/common_parser/common_data/ooxml_document_object.rb', line 10
def ==(other)
instance_variables.each do |current_attribute|
unless instance_variable_get(current_attribute) == other.instance_variable_get(current_attribute)
return false
end
end
true
end
|