Class: HMSTools::XFDL

Inherits:
Object
  • Object
show all
Defined in:
lib/hmstools.rb

Instance Method Summary collapse

Constructor Details

#initializeXFDL

Returns a new instance of XFDL.



278
279
280
281
282
# File 'lib/hmstools.rb', line 278

def initialize
	require 'base64'
	require 'zlib'
	require 'stringio'
end

Instance Method Details

#b64decode_it(contents = nil) ⇒ Object

Base64 decodes a given string Example >> XFDL.b64decode_it “randommishmash”

> “Not So Random Mish Mash”

Arguments

contents: (String)


172
173
174
175
# File 'lib/hmstools.rb', line 172

def b64decode_it(contents = nil)
	x = Base64.decode64(contents)
	return x
end

#b64encode_it(contents = nil) ⇒ Object

Base64 encodes a given string Example >> XFDL.b64eecode_it “Not So Random Mish Mash”

> “randommishmash”

Arguments

contents: (String)


185
186
187
188
189
190
# File 'lib/hmstools.rb', line 185

def b64encode_it(contents = nil)
	unless contents.nil?
		x = Base64.encode64(contents)
		return x
	end
end

#build_and_write_xfdl_from_directory(xmldirectory, xfdldirectory) ⇒ Object

Searches for all xml files in a directory and turns them into xfdl files Example >> XFDL.build_and_write_xfdl_from_directory “myxmlfiles/*”, “myxfdlfiles”

> Packaging Form1.xml to Form1.xfdl - Complete

> 1 out of 1 were XML files and processed to the XFDL format!

Arguments

xmldirectory: (String)  A directory where all the xml files are stored
xfdldirectory: (String)  A directory where all the xfdl files will be written to


202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/hmstools.rb', line 202

def build_and_write_xfdl_from_directory(xmldirectory, xfdldirectory)
	@files = Dir.glob(xmldirectory)
	numfiles = 0
	numxmlfiles = 0

	for file in @files
		if File.extname(file) == ".xml"
			print "Packaging "+ File.basename(file) +" to " + File.basename(file, ".xml") +".xfdl"
			build_and_write_xfdl_from_xml(file, xfdldirectory+"/"+File.basename(file, ".xml")+".xfdl")
			print "  - Complete\n"
			numxmlfiles += 1
		end

		numfiles += 1
	end

	print "\n" + numxmlfiles.to_s + " out of " + numfiles.to_s + " were XML files and processed to the XFDL format!"
end

#build_and_write_xfdl_from_xml(xmlfile, xfdlfile) ⇒ Object

Turns an xml file into an xfdl file Example >> XFDL.build_and_write_xfdl_from_xml “myform.xml”, “myform.xfdl”

Arguments

xmlfile: (String)  The xml file you want to convert
xfdlfile: (String)  The name of the xfdl file you want to save.


229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/hmstools.rb', line 229

def build_and_write_xfdl_from_xml(xmlfile, xfdlfile)
	# 1. Gzip
	# 2. Base64
	# 3. Write string with mime stuff on the fist line
	# 4. Save as XFDL
	
	xml_file = ''
	b64_string = ''
	new_contents = ''

	if File.exists?(xmlfile)
		xml_file = IO.read(xmlfile).to_s
	end

	# Tell Zlib to use StringIO instead of a file.. Suck it Zlib!
	a = StringIO.new ""
	
	gz = Zlib::GzipWriter.new(a)
	gz.write xml_file
	gz.close

	b64_string = b64encode_it(a.string)
	new_contents = "application/vnd.xfdl;content-encoding=\"base64-gzip\"\n" + b64_string

	File.open(xfdlfile, "w") { |f| f.write(new_contents) }

end

#deconstruct_and_write_xml(old_filename, new_filename) ⇒ Object

Turns an xfdl file into an xml file Example >> XFDL.deconstruct_and_write_xml “myform.xfdl”, “myform.xml”

Arguments

old_filename: (String)  The xfdl file you want to convert
new_filename: (String)  The name of the xml file


264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/hmstools.rb', line 264

def deconstruct_and_write_xml(old_filename, new_filename)

	if File.exists?(old_filename)
		enc_file = ''
		enc_file = IO.read(old_filename).to_s
		content = get_xml enc_file
		File.open(new_filename, "w") { |f| f.write(content) }
	else
		puts "File does not exist!"
		return nil
	end

end

#get_xml(contents) ⇒ Object

Example >> XFDL.get_xml “Some Fun Stuff”

Arguments

contents: (String)  The base 64 encoded string after opening the xfdl file and stripping the content type information


291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/hmstools.rb', line 291

def get_xml(contents)
	b64_decoded = ''
	xml_contents = ''

	b64_decoded = b64decode_it(contents.lines.to_a[1..-1].join)

	# Tell Zlib to use StringIO instead of a file.. Suck it Zlib!
	a = StringIO.new b64_decoded

	gz = Zlib::GzipReader.new(a)
	xml_contents = gz.read
	gz.close

	return xml_contents
end