ufooar 0.1.1.140

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,167 @@
1
+ # Filename: document.rb
2
+ #
3
+ # Author:: Nathan Lane (mailto:nathamberlane@gmail.com)
4
+ # Copyright:: Copyright (c) 2007 Nathan Lane
5
+ # License:: GNU Lesser General Public License (http://www.gnu.org/licenses/lgpl.html)
6
+ #
7
+ # This program is free software; you can redistribute it and/or
8
+ # modify it under the terms of the GNU General Public License
9
+ # as published by the Free Software Foundation; either version 2
10
+ # of the License, or (at your option) any later version.
11
+ #
12
+ # This program is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ # GNU General Public License for more details.
16
+ #
17
+ # You should have received a copy of the GNU General Public License
18
+ # along with this program; if not, write to the Free Software
19
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20
+ #
21
+ # Documentation for this library may be found at http://code.google.com/p/ufooar/
22
+
23
+ # This module contains the Document class, which is a factory class for other classes used
24
+ # to automate OpenOffice.org.
25
+ # Author:: Nathan Lane
26
+ # Last Updated: 11/09/2007
27
+
28
+ module OpenOffice
29
+
30
+ class Document
31
+
32
+ # creates and opens a new document of the specified type
33
+ def self.create_document(doc_type, hidden = false)
34
+ case doc_type
35
+ when :calc
36
+ factory_uri = "private:factory/scalc"
37
+ when :writer
38
+ factory_uri = "private:factory/swriter"
39
+ when :impress
40
+ factory_uri = "private:factory/simpress"
41
+ when :draw
42
+ factory_uri = "private:factory/sdraw"
43
+ when :base, :math
44
+ msg = "#{doc_type.to_s} cannot be created."
45
+ raise(ArgumentError, msg)
46
+ else
47
+ msg = "#{doc_type.to_s} is not a type of Open Office document."
48
+ raise(ArgumentError, msg)
49
+ end
50
+
51
+ file_args = hidden ? UNO.create_property_values("Hidden", true) : []
52
+ load_document(factory_uri, file_args)
53
+ end
54
+
55
+ # opens a document
56
+ # require fully qualified file name, excluding uri portion
57
+ def self.open_document(file_name, hidden = false)
58
+ msg = "Document #{file_name} does not exists."
59
+ file_exist = File.exist?(file_name)
60
+ raise(ArgumentError, msg) unless File.exist?(file_name)
61
+
62
+ # build the uri
63
+ extension = File.basename(file_name).split('.')[-1]
64
+ factory_uri = 'file:///' + file_name
65
+
66
+ # determine the required propety values require for the document
67
+ case extension
68
+ when "odw", "doc", "ods", "txt"
69
+ names_values = hidden ? ["Hidden", true] : []
70
+ when "xls"
71
+ names_values = ["FilterName", "MS Excel 97"]
72
+ names_values += ["Hidden", true] if hidden
73
+ when "csv"
74
+ col_descr = csv_col_descr() # 1/10/2/10 .. /255/10/256/10
75
+ names_values = ["FilterName",
76
+ "Text - txt - csv (StarCalc)",
77
+ "FilterOptions",
78
+ "44, 34, SYSTEM, 1, #{col_descr}"]
79
+ names_values += ["Hidden", true] if hidden
80
+ when "htm", "html"
81
+ names_values = ["FilterName", "HTML (StarWriter)", "Overwrite", TRUE]
82
+ names_values += ["Hidden", true] if hidden
83
+ else
84
+ msg = "Cannot open #{file_name} document currently."
85
+ raise(ArgumentError, msg)
86
+ end
87
+
88
+ file_args = names_values.size > 0 ? UNO.create_property_values(names_values) : []
89
+ load_document(factory_uri, file_args)
90
+ end
91
+
92
+ # This method returns a WIN32OLE object to the document object requested.
93
+ # Author:: Nathan Lane
94
+ # Last Updated: 11/09/2007
95
+ def self.create_document_instance(document_type, file_uri = nil, file_args = [])
96
+ case document_type
97
+ when :calc
98
+ if file_uri.nil?
99
+ factory_uri = "private:factory/scalc"
100
+ elsif file_uri =~ /^(file:\/\/\/)[a-zA-Z]{1}[:]{1}[\/a-zA-Z\d\s._-]+(ods|xls|csv|html|htm){1}$/
101
+ factory_uri = file_uri
102
+ elsif file_uri =~ /^[a-zA-Z]{1}[:]{1}[\/a-zA-Z\d\s._-]+(ods|xls|csv|html|htm){1}$/
103
+ factory_uri = "file:///" + file_uri
104
+ else
105
+ msg = "Malformed URI only support for xls, ods, and csv files is currently available"
106
+ raise(ArgumentError, msg)
107
+ end # End: if file_uri.nil?
108
+
109
+ when :writer
110
+ if file_uri.nil?
111
+ factory_uri = "private:factory/swriter"
112
+ elsif file_uri =~ /^(file:\/\/\/)[a-zA-Z]{1}[:]{1}[\/a-zA-Z\d\s._-]+(odw|doc|txt){1}$/
113
+ factory_uri = file_uri
114
+ elsif file_uri =~ /^[a-zA-Z]{1}[:]{1}[\/a-zA-Z\d\s._-]+(odw|doc|txt){1}$/
115
+ factory_uri = "file:///" + file_uri
116
+ elsif file_uri =~ /^(https:\/\/)[a-zA-Z]{1}[:]{1}[\/a-zA-Z\d\s._-]+(htm|html){1}$/
117
+ factory_uri = file_uri
118
+ elsif file_uri =~ /^[a-zA-Z]{1}[:]{1}[\/a-zA-Z\d\s._-]+(htm|html){1}$/
119
+ factory_uri = "http://" + file_uri
120
+ else
121
+ raise "Malformed URI, only support for doc, odw, htm, and html files is currently available"
122
+ end # End: if file_uri.nil?
123
+ else
124
+ puts "document_type must be either :calc or :writer"
125
+ end # End: case document_type
126
+
127
+ if !factory_uri["csv"].nil?
128
+ # Need to create two Property objects and insert them into the file_args array
129
+ col_descr = csv_col_descr()
130
+ names_values = ["FilterName",
131
+ "Text - txt - csv (StarCalc)",
132
+ "FilterOptions",
133
+ "44, 34, SYSTEM, 1, #{col_descr}"]
134
+ # "44, 34, SYSTEM, 1, 1/10/2/10/3/10/4/10/5/10/6/10/7/10/8/10/9/10"]
135
+ file_args = UNO.create_property_values( names_values )
136
+ end # End: if !factory_uri["csv"].nil?
137
+
138
+ if !factory_uri["htm"].nil? || !factory_uri["html"].nil?
139
+ # Need to create two Property objects and insert them into the file_args array
140
+ names_values = ["FilterName", "HTML (StarWriter)", "Overwrite", TRUE]
141
+ file_args = UNO.create_property_values( names_values )
142
+ end # End: if !factory_uri["htm"].nil?
143
+
144
+ Document.load_document(factory_uri, file_args)
145
+ end # End: def self.create_document_instance document_type, file_uri = nil, args = []
146
+
147
+ # This method loads a document based on its URI and args array.
148
+ # Author:: Nathan Lane
149
+ # Last Updated: 11/09/2007
150
+ def self.load_document factory_uri, file_args
151
+ # puts [factory_uri, file_args].inspect
152
+ UNO.get_desktop_instance.loadComponentFromURL(factory_uri, "_blank", 0, file_args)
153
+ end # End: def load_document uri = nil, args = []
154
+
155
+ end # End: class Document
156
+
157
+ # ###############################################################
158
+ # support methods
159
+ # ###############################################################
160
+ # generates columns descriptor for the csv FilterOption
161
+ def csv_col_descr
162
+ cols = ""
163
+ (1..256).each { |i| cols += i.to_s + '/' + 10.to_s + '/' }
164
+ cols.chop!
165
+ end
166
+
167
+ end # End: module OpenOffice
@@ -0,0 +1,137 @@
1
+ # Filename: writer.rb
2
+ #
3
+ # Author:: Nathan Lane (mailto:nathamberlane@gmail.com)
4
+ # Copyright:: Copyright (c) 2007 Nathan Lane
5
+ # License:: GNU Lesser General Public License (http://www.gnu.org/licenses/lgpl.html)
6
+ #
7
+ # This program is free software; you can redistribute it and/or
8
+ # modify it under the terms of the GNU General Public License
9
+ # as published by the Free Software Foundation; either version 2
10
+ # of the License, or (at your option) any later version.
11
+ #
12
+ # This program is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ # GNU General Public License for more details.
16
+ #
17
+ # You should have received a copy of the GNU General Public License
18
+ # along with this program; if not, write to the Free Software
19
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20
+ #
21
+ # Documentation for this library may be found at http://code.google.com/p/ufooar/
22
+
23
+ # This module contains the Writer class, which wraps around Writer documents.
24
+ # Author:: Nathan Lane
25
+ # Last Updated: 11/13/2007
26
+
27
+ require 'openoffice/document'
28
+
29
+ module OpenOffice
30
+
31
+ # This class contains methods used to manipulate Writer documents.
32
+ # Author:: Nathan Lane
33
+ # Last Updated: 11/13/2007
34
+
35
+ class Writer
36
+
37
+ attr_reader :document, :file_uri, :file_args
38
+
39
+ # This constructor takes two arguments, file_path, which is a String and args, which is an
40
+ # Array. Args is not required to create a new document, but you must specify a file path
41
+ # as a URI in the form file:///driveletter:/path/to/file.ods.
42
+ # Author:: Nathan Lane
43
+ # Last Updated: 11/13/2007
44
+
45
+ def initialize file_path = nil, file_args = []
46
+ @document, @file_uri, @file_args = Document.create_document_instance :writer, file_path, file_args
47
+ end # End: def initialize file_path = nil, file_args = []
48
+
49
+ # This method saves the current document according to the URI you provide as the file path. The
50
+ # URI must be in the format file:///driveletter:/path/to/file.odw
51
+ # Author:: Nathan Lane
52
+ # Last Updated: 11/13/2007
53
+
54
+ def save file_path = nil, args = []
55
+ if file_path =~ /^(file:\/\/\/)[a-zA-Z]{1}[:]{1}[\/a-zA-Z\d\s._-]+(odw|doc){1}$/
56
+ file_uri = file_path
57
+ file_args = args
58
+ @document.storeAsURL file_uri, file_args
59
+ elsif file_path.nil? and !@file_uri.nil?
60
+ if @file_uri =~ /^[a-zA-Z]{1}[:]{1}[\/a-zA-Z\d\s._-]+(odw|doc){1}$/
61
+ full_file_uri = "file:///" + @file_uri
62
+ else
63
+ full_file_uri = @file_uri
64
+ end # End: if @file_uri =~ /^[a-zA-Z]{1}[:]{1}[\/a-zA-Z\d\s._-]+(odw|doc){1}$/
65
+
66
+ file_args = args
67
+ @document.storeAsURL full_file_uri, file_args
68
+ else
69
+ raise "Malformed URI: only support for xls and ods files is currently available or file path not supplied"
70
+ end # End: if filepath =~ /^(file:\/\/\/)[a-zA-Z]{1}[:]{1}[\/a-zA-Z\d\s._-]+(odw|doc){1}$/
71
+ end # End: def save file_path
72
+
73
+ # This method saves the current document in a different form according to what you provide as
74
+ # the format.
75
+ # Author:: Nathan Lane
76
+ # Last Updated: 11/13/2007
77
+
78
+ def save_as file_type = :odw, file_path = nil, args = []
79
+ case file_type
80
+ when :ods
81
+ args = args
82
+
83
+ # Fix the URI
84
+ if file_path.nil?
85
+ file_suffix = "odw"
86
+
87
+ if @file_uri["doc"]
88
+ len = @file_uri.length - 1
89
+ @file_uri = @file_uri[0..(len - 3)] + file_suffix
90
+ end
91
+ else
92
+ file_suffix = "doc"
93
+
94
+ if file_path["odw"]
95
+ len = @file_uri.length - 1
96
+ file_path = file_path[0..(len - 3)] + file_suffix
97
+ end
98
+ end # End: if file_path.nil?
99
+ when :word97
100
+ excel_filter = UNO.create_property_value "FilterName", "MS Word 97"
101
+ args = args + [ excel_filter ]
102
+
103
+ # Fix the URI
104
+ if file_path.nil?
105
+ file_suffix = "doc"
106
+
107
+ if @file_uri["odw"]
108
+ len = @file_uri.length - 1
109
+ @file_uri = @file_uri[0..(len - 3)] + file_suffix
110
+ end
111
+ else
112
+ file_suffix = "doc"
113
+
114
+ if file_path["odw"]
115
+ len = @file_uri.length - 1
116
+ file_path = file_path[0..(len - 3)] + file_suffix
117
+ end
118
+ end # End: if file_path.nil?
119
+ else
120
+ raise "Unsupported file type only :odw and :word97 are supported"
121
+ end # End: case file_type
122
+
123
+ save file_path, args
124
+ end # End: def save_as file_path, args = [], file_type = :ods
125
+
126
+ # This method closes the current document.
127
+ # Author:: Nathan Lane
128
+ # Last Updated: 11/13/2007
129
+
130
+ def close
131
+ @document.close(0)
132
+ @document
133
+ end # End: def close
134
+
135
+ end # End: class Writer
136
+
137
+ end # End: module OpenOffice
@@ -0,0 +1,45 @@
1
+ # Filename: ufooar.rb
2
+ #
3
+ # Author:: Nathan Lane (mailto:nathamberlane@gmail.com)
4
+ # Copyright:: Copyright (c) 2007 Nathan Lane
5
+ # License:: GNU Lesser General Public License (http://www.gnu.org/licenses/lgpl.html)
6
+ #
7
+ # This program is free software; you can redistribute it and/or
8
+ # modify it under the terms of the GNU General Public License
9
+ # as published by the Free Software Foundation; either version 2
10
+ # of the License, or (at your option) any later version.
11
+ #
12
+ # This program is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ # GNU General Public License for more details.
16
+ #
17
+ # You should have received a copy of the GNU General Public License
18
+ # along with this program; if not, write to the Free Software
19
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20
+ #
21
+ # Documentation for this library may be found at http://code.google.com/p/ufooar/
22
+
23
+ # This module contains the UFOOAR classes used to automate OpenOffice.org
24
+ # via its UNO component.
25
+ # Author:: Nathan Lane
26
+ # Last Updated: 11/09/2007
27
+
28
+ require 'win32ole'
29
+ require 'uno/uno'
30
+ require 'openoffice/calc'
31
+ require 'openoffice/writer'
32
+
33
+ module OpenOffice
34
+ end # End: module OpenOffice
35
+
36
+ module UFOOAR
37
+ # The revision number (according to Subversion)
38
+ REVISION_STRING = '$Revision: 140 $'
39
+ REVISION_STRING.scan(/Revision: (\d*)/)
40
+ REVISION = $1 or 'unknown'
41
+
42
+ # The Release number
43
+ VERSION_SHORT = '0.1.1'
44
+ VERSION = VERSION_SHORT + '.' + REVISION
45
+ end # End: module UFOOAR
@@ -0,0 +1,25 @@
1
+ # Author:: Nathan Lane (mailto:nathamberlane@gmail.com)
2
+ # Copyright:: Copyright (c) 2007 Nathan Lane
3
+ # License:: GNU Lesser General Public License (http://www.gnu.org/licenses/lgpl.html)
4
+ #
5
+ # This program is free software; you can redistribute it and/or
6
+ # modify it under the terms of the GNU General Public License
7
+ # as published by the Free Software Foundation; either version 2
8
+ # of the License, or (at your option) any later version.
9
+ #
10
+ # This program is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ # GNU General Public License for more details.
14
+ #
15
+ # You should have received a copy of the GNU General Public License
16
+ # along with this program; if not, write to the Free Software
17
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
+ #
19
+
20
+ # This unit test set tests all of the ufooar classes
21
+ # Author:: Nathan Lane
22
+ # Last Update: 11/08/2007
23
+
24
+ require 'spreadsheet_tests'
25
+ require 'uno_tests'
@@ -0,0 +1,56 @@
1
+ # Author:: Nathan Lane (mailto:nathamberlane@gmail.com)
2
+ # Copyright:: Copyright (c) 2007 Nathan Lane
3
+ # License:: GNU Lesser General Public License (http://www.gnu.org/licenses/lgpl.html)
4
+ #
5
+ # This program is free software; you can redistribute it and/or
6
+ # modify it under the terms of the GNU General Public License
7
+ # as published by the Free Software Foundation; either version 2
8
+ # of the License, or (at your option) any later version.
9
+ #
10
+ # This program is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ # GNU General Public License for more details.
14
+ #
15
+ # You should have received a copy of the GNU General Public License
16
+ # along with this program; if not, write to the Free Software
17
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
+ #
19
+
20
+ # This unit test set tests the Spreadsheet class
21
+ # Author:: Nathan Lane
22
+ # Last Update: 11/08/2007
23
+
24
+ require 'test/unit'
25
+ require 'ufooar'
26
+
27
+ include OpenOffice
28
+
29
+ class TestSpreadsheet < Test::Unit::TestCase
30
+
31
+ def test_spreadheet_hidden
32
+ test_object = Spreadsheet.HIDDEN
33
+ assert_equal test_object.class, WIN32OLE
34
+ assert_equal test_object.Name, "Hidden"
35
+ assert_equal test_object.Value, true
36
+ end # End: def test_spreadheet_hidden
37
+
38
+ def test_open_new_spreadsheet
39
+ test_object = Spreadsheet.new
40
+ assert_equal test_object.class, Spreadsheet
41
+ assert_equal test_object.spreadsheet.class, WIN32OLE
42
+
43
+ test_object.close
44
+ assert test_object.spreadsheet.nil?
45
+ end # End: def test_open_new_spreadsheet
46
+
47
+ def test_open_ods_spreadsheet
48
+ test_object = Spreadsheet.new ("file:///" + Dir.getwd + "/unittestresources/test.ods")
49
+ assert_equal test_object.class, Spreadsheet
50
+ assert_equals test_object.spreadsheet.class, WIN32OLE
51
+
52
+ test_object.close
53
+ assert test_object.spreadsheet.nil?
54
+ end # End: def test_open_ods_spreadsheet
55
+
56
+ end # End: class TestSpreadsheet < Test::Unit::TestCase
@@ -0,0 +1,5 @@
1
+ Name, Country, Age, Hobby
2
+ "George", "England", 24, "Guitar"
3
+ "Henry", "France", 28, "Ruling"
4
+ "Jack", "Italy", 19, "Stealing"
5
+ "Kevin", "United States", 21, "Hacking"
@@ -0,0 +1,54 @@
1
+ # Author:: Nathan Lane (mailto:nathamberlane@gmail.com)
2
+ # Copyright:: Copyright (c) 2007 Nathan Lane
3
+ # License:: GNU Lesser General Public License (http://www.gnu.org/licenses/lgpl.html)
4
+ #
5
+ # This program is free software; you can redistribute it and/or
6
+ # modify it under the terms of the GNU General Public License
7
+ # as published by the Free Software Foundation; either version 2
8
+ # of the License, or (at your option) any later version.
9
+ #
10
+ # This program is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ # GNU General Public License for more details.
14
+ #
15
+ # You should have received a copy of the GNU General Public License
16
+ # along with this program; if not, write to the Free Software
17
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
+ #
19
+
20
+ # This unit test set tests the UNO class
21
+ # Author:: Nathan Lane
22
+ # Last Update: 11/08/2007
23
+
24
+ require 'test/unit'
25
+ require 'ufooar'
26
+
27
+ include OpenOffice
28
+
29
+ class TestUNO < Test::Unit::TestCase
30
+
31
+ def test_get_service_manager
32
+ assert_equal UNO.get_service_manager.class, WIN32OLE
33
+ end # End: def test_get_service_manager
34
+
35
+ def test_get_core_reflection_instance
36
+ assert_equal UNO.get_core_reflection_instance.class, WIN32OLE
37
+ end # End: def test_get_core_reflection_instance
38
+
39
+ def test_get_desktop_instance
40
+ assert_equal UNO.get_desktop_instance.class, WIN32OLE
41
+ end # End: def test_get_desktop_instance
42
+
43
+ def test_property_value
44
+ assert_equal UNO.property_value.class, WIN32OLE
45
+ end # End: def test_property_value
46
+
47
+ def test_create_property_value
48
+ test_object = UNO.create_property_value "Hidden", true
49
+ assert_equal test_object.class, WIN32OLE
50
+ assert_equal test_object.Name, "Hidden"
51
+ assert_equal test_object.Value, true
52
+ end # End: def test_create_property_value
53
+
54
+ end # End: class TestUNO < Test::Unit::TestCase