clarus 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,6 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ *.swp
6
+ spec/output/*
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in junodoc.gemspec
4
+ gemspec
5
+
6
+ gem 'rake'
7
+ gem 'minitest'
8
+
9
+ group :development do
10
+ gem 'jruby-openssl'
11
+ end
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+ require 'bundler'
4
+
5
+ Bundler.setup
6
+
7
+ Rake::TestTask.new do |t|
8
+ t.pattern = "spec/*_spec.rb"
9
+ end
data/Readme ADDED
@@ -0,0 +1,22 @@
1
+ clarus - a jRuby interface to create documents, moof!
2
+
3
+ This is a gem to provide a programatic way to create word documents using jruby
4
+
5
+ LICENSE:
6
+
7
+ Copyright (c) 2011 Revelation, Inc.
8
+
9
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files
10
+ (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify,
11
+ merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
12
+ furnished to do so, subject to the following conditions:
13
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
16
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
17
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
18
+ IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19
+
20
+ This software includes libraries from java2word: http://code.google.com/p/java2word/
21
+
22
+ java2word is covered under the LGPL license: http://www.gnu.org/licenses/lgpl.html
data/clarus.gemspec ADDED
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "clarus/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "clarus"
7
+ s.version = Clarus::VERSION
8
+ s.authors = ["Dan Herrera"]
9
+ s.email = ["dan@revelationglobal.com"]
10
+ s.homepage = ""
11
+ s.summary = %q{Clarus is a way to create documents programmatically. Moof!}
12
+ s.description = %q{Clarus makes a jruby wrapper around the java2word library to create documents.}
13
+
14
+ s.rubyforge_project = "clarus"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+ end
Binary file
Binary file
@@ -0,0 +1,50 @@
1
+ module Clarus
2
+ require File.dirname(__FILE__) + '/../../jars/j2w-ejb-2.0_2011Jun06'
3
+ require File.dirname(__FILE__) + '/../../jars/xstream-1.3.1'
4
+
5
+ class Document
6
+ import 'java.io.PrintWriter'
7
+ import 'java.io.File'
8
+ import 'word.api.interfaces.IDocument'
9
+ import 'word.w2004.Document2004'
10
+ import 'word.w2004.elements.BreakLine'
11
+ import 'word.w2004.elements.Heading1'
12
+ import 'word.w2004.elements.Image'
13
+ import 'word.w2004.elements.ImageLocation'
14
+ import 'word.w2004.elements.Paragraph'
15
+ import 'word.w2004.elements.Table'
16
+
17
+ def initialize
18
+ @doc = create_document
19
+ end
20
+
21
+ def add_text(text)
22
+ @doc.getBody.addEle(Paragraph.new(text))
23
+ end
24
+
25
+
26
+ def add_image(image_url)
27
+ @doc.getBody.addEle(Image.from_WEB_URL(image_url))
28
+ end
29
+
30
+ def add_paragraph_break
31
+ @doc.getBody.addEle(BreakLine.new)
32
+ end
33
+
34
+ def write_document(path)
35
+ writer = PrintWriter.new(File.new(path))
36
+ writer.println(@doc.getContent)
37
+ writer.close
38
+ @doc = nil
39
+ end
40
+
41
+ def stream_document
42
+ @doc.getContent
43
+ end
44
+
45
+ private
46
+ def create_document
47
+ @doc = Document2004.new
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,3 @@
1
+ module Clarus
2
+ VERSION = "0.0.4"
3
+ end
data/lib/clarus.rb ADDED
@@ -0,0 +1,6 @@
1
+ require 'java'
2
+ require 'clarus/version'
3
+ require 'clarus/document'
4
+
5
+ module Clarus
6
+ end
@@ -0,0 +1,14 @@
1
+ require File.dirname(__FILE__) + "/spec_helper"
2
+ require 'benchmark'
3
+
4
+ Benchmark.benchmark do |x|
5
+ x.report("creating 1 docs with 500 images and paragraphs") do
6
+ @clarus = Clarus::Document.new
7
+ 100.times do
8
+ @clarus.add_text("istanbul, not constantinople")
9
+ @clarus.add_paragraph_break
10
+ @clarus.add_image("file://#{Dir.pwd}/spec/fixtures/image.jpg")
11
+ end
12
+ @clarus.write_document(Dir.pwd + '/spec/output/benchmark_test.doc')
13
+ end
14
+ end
@@ -0,0 +1,4 @@
1
+ require File.dirname(__FILE__) + "/spec_helper"
2
+
3
+ describe Clarus do
4
+ end
@@ -0,0 +1,38 @@
1
+ require File.dirname(__FILE__) + "/spec_helper"
2
+
3
+ describe Clarus::Document do
4
+ before do
5
+ @clarus = Clarus::Document.new
6
+ end
7
+
8
+ it "should be an instance of Clarus::Document" do
9
+ @clarus.class.must_equal(Clarus::Document)
10
+ end
11
+
12
+ it "should be able to add text to the document" do
13
+ @clarus.add_text("istanbul, not constantinople")
14
+ @clarus.stream_document.must_equal File.read(Dir.pwd + '/spec/fixtures/add_text.doc').strip
15
+ end
16
+
17
+ it "should be able to add a paragraph break" do
18
+ @clarus.add_text("istanbul, not constantinople")
19
+ @clarus.add_paragraph_break
20
+ @clarus.add_text("istanbul, not constantinople")
21
+ @clarus.stream_document.must_equal File.read(Dir.pwd + '/spec/fixtures/add_paragraph_break.doc').strip
22
+ end
23
+
24
+ it "should be able to add an image by url" do
25
+ @clarus.add_text("istanbul, not constantinople")
26
+ @clarus.add_paragraph_break
27
+ @clarus.add_image("file://#{Dir.pwd}/spec/fixtures/image.jpg")
28
+ @clarus.stream_document.must_match File.read(Dir.pwd + '/spec/fixtures/add_image.doc').strip
29
+ end
30
+
31
+ it "should be able to write out the document to disk" do
32
+ @clarus.add_text("istanbul, not constantinople")
33
+ full_file_path = Dir.pwd + '/spec/output/test.doc'
34
+ @clarus.write_document(full_file_path)
35
+ File.exists?(full_file_path).must_equal true
36
+ File.unlink(full_file_path)
37
+ end
38
+ end