Class: TaliaCore::DataTypes::XmlData

Inherits:
FileRecord show all
Defined in:
lib/talia_core/data_types/xml_data.rb

Overview

Class to manage XML and HTML data type

Instance Attribute Summary

Attributes inherited from DataRecord

#temp_path

Instance Method Summary collapse

Methods inherited from FileRecord

#all_bytes, #get_byte, #position, #reset, #seek, #size

Methods included from FileStore::ClassMethods

#find_or_create_and_assign_file

Methods included from PathHelpers::ClassMethods

#data_path, #extract_filename, #tempfile_path

Methods included from TempFileHandling::ClassMethods

#copy_to_temp_file, #create_tempfile_path, #write_to_temp_file

Methods included from DataLoader::ClassMethods

#create_from_url

Methods included from IipLoader

#convert_original?, #create_from_files, #create_from_stream, #create_iip, #open_original_image, #open_original_image_file, #open_original_image_stream, #orig_location, #prepare_image_from_existing!

Methods included from TaliaUtil::IoHelper

#base_for, #file_url, #open_from_url, #open_generic

Methods included from TempFileHandling

#copy_to_temp_file, #random_tempfile_filename, #temp_data, #temp_data=, #temp_path, #temp_path=, #temp_paths, #write_to_temp_file

Methods included from PathHelpers

#data_directory, #data_path, #extract_filename, #file_path, #full_filename, #static_path, #tempfile_path

Methods included from FileStore

#all_text, #assign_type, #create_from_file, #file, #file=, #is_file_open?, #write_file_after_save

Methods inherited from DataRecord

#all_bytes, #content_string, find_by_type_and_location!, find_data_records, #get_byte, #mime_type, #position, #reset, #seek, #size

Instance Method Details

#create_from_data(location, data, options = {:tidy => true}) ⇒ Object

Add data as string into file

  • location: location

  • data: data to write

  • options: options

  • options: enable or disable tidy (convert html into xhtml). Default value is true



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/talia_core/data_types/xml_data.rb', line 100

def create_from_data(location, data, options = {:tidy => true})
  # check tidy option
  if (((options[:tidy] == true) and (Tidy_enable == true)) and 
        ((File.extname(location) == '.htm') or (File.extname(location) == '.html') or (File.extname(location) == '.xhtml')))        
  
    # apply tidy on data
    data_to_write = Tidy.open(:show_warnings => false) do |tidy|
      tidy.options.output_xhtml = true
      tidy.options.tidy_mark = false
      xhtml = tidy.clean(data)
      xhtml
    end
  else
    data_to_write = data
  end

  # write data
  super(location, data_to_write, options)
end

#extract_mime_type(location) ⇒ Object

return the mime_type for a file



34
35
36
37
38
39
40
41
42
43
# File 'lib/talia_core/data_types/xml_data.rb', line 34

def extract_mime_type(location)
  case File.extname(location).downcase
  when '.htm', '.html','.xhtml'
    'text/html'
  when '.hnml'
    'text/hnml'
  when '.xml'
    'text/xml'
  end
end

#get_content(options = nil) ⇒ Object

return contect of the object as REXML::Elements

  • options: Options for getting context. Default nil.

  • options: xsl file path for transformation.



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
# File 'lib/talia_core/data_types/xml_data.rb', line 53

def get_content(options = nil)
  text_to_parse = all_text

  if (!options.nil?)
    # if xsl_file option is specified, execute transformation
    if (!options[:xsl_file].nil?)
      text_to_parse = xslt_transform(file_path, options[:xsl_file])
    end
  end

  # create document object
  document = REXML::Document.new text_to_parse

  # get content
  if ((mime_subtype == "html") or 
        ((mime_subtype == "xml") and (!options.nil?) and (!options[:xsl_file].nil?)))
    content = document.elements['//body'].elements
  elsif ((mime_subtype == "xml") or (mime_subtype == "hnml"))
    content = document.root.elements
  end

  # adjust/replace items path
  content.each { |i| wrapItem i }

  # return content
  return content
end

#get_content_string(options = nil) ⇒ Object

Returns an xml string of the elements returned by get_content



82
83
84
85
86
87
88
# File 'lib/talia_core/data_types/xml_data.rb', line 82

def get_content_string(options = nil)
  xml_str = ''
  get_content(options).each do |element|
    xml_str << element.to_s
  end
  xml_str
end

#get_escaped_content_string(options = nil) ⇒ Object

Returns an xml string that is escaped for HTML inclusing



91
92
93
# File 'lib/talia_core/data_types/xml_data.rb', line 91

def get_escaped_content_string(options = nil)
  get_content_string(options).gsub(/</, "&lt;").gsub(/>/, "&gt;")
end

#mime_subtypeObject

return the mime subtype for this specified class



46
47
48
# File 'lib/talia_core/data_types/xml_data.rb', line 46

def mime_subtype
  mime_type.split(/\//)[1]
end