Class: AemLookout::SlingInitialContentConverter
- Inherits:
-
Object
- Object
- AemLookout::SlingInitialContentConverter
- Defined in:
- lib/aem_lookout/sling_initial_content_converter.rb
Class Method Summary collapse
- .add_children(children, builder) ⇒ Object
- .convert(json_string) ⇒ Object
- .convert_package(package_path) ⇒ Object
- .convert_to_serialized_jcr_value(value) ⇒ Object
- .default_attributes ⇒ Object
- .generate_content_xml_path(json_path) ⇒ Object
-
.group_data(node_data) ⇒ Object
Divide node data into serialized attributes and children.
- .json_files_within(path) ⇒ Object
-
.namespaces ⇒ Object
All known namespaces.
Class Method Details
.add_children(children, builder) ⇒ Object
40 41 42 43 44 45 46 47 |
# File 'lib/aem_lookout/sling_initial_content_converter.rb', line 40 def self.add_children(children, builder) children.each do |name, node_data| serialized_attributes, children = group_data(node_data) builder.tag!(name, serialized_attributes) do |b| add_children(children, b) end end end |
.convert(json_string) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/aem_lookout/sling_initial_content_converter.rb', line 6 def self.convert(json_string) node_data = JSON.parse(json_string) builder = Builder::XmlMarkup.new serialized_attributes, children = group_data(node_data) builder.tag!("jcr:root", namespaces.merge(serialized_attributes)) do |b| add_children(children, b) end rescue JSON::ParserError => e raise SlingInitialContentConverterError, "A problem occurred while parsing JSON descriptor file: #{e.}" end |
.convert_package(package_path) ⇒ Object
18 19 20 21 22 23 24 25 26 27 |
# File 'lib/aem_lookout/sling_initial_content_converter.rb', line 18 def self.convert_package(package_path) json_files = json_files_within(package_path) json_files.each do |json_file| content_xml_path = generate_content_xml_path(json_file) FileUtils.mkdir_p(content_xml_path.parent) xml_string = convert(File.read(json_file)) File.open(content_xml_path, 'w') {|f| f.write(xml_string) } FileUtils.rm(json_file) end end |
.convert_to_serialized_jcr_value(value) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/aem_lookout/sling_initial_content_converter.rb', line 81 def self.convert_to_serialized_jcr_value(value) if value == true || value == false "{Boolean}#{value}" elsif value.is_a?(Array) values = value.map {|el| convert_to_serialized_jcr_value(el) } "[#{values.join(",")}]" elsif value.is_a?(Time) "{Date}#{value.strftime("%Y-%m-%dT%H:%M:%S.%L%:z")}" elsif value.is_a?(String) value else raise "Unknown type, cannot serialize #{value.class} value: #{value}" end end |
.default_attributes ⇒ Object
64 65 66 67 68 |
# File 'lib/aem_lookout/sling_initial_content_converter.rb', line 64 def self.default_attributes { "jcr:primaryType" => "nt:unstructured" } end |
.generate_content_xml_path(json_path) ⇒ Object
34 35 36 37 38 |
# File 'lib/aem_lookout/sling_initial_content_converter.rb', line 34 def self.generate_content_xml_path(json_path) raise ArgumentError, "#{json_path} must end in .json" unless json_path.end_with?(".json") node_name = File.basename(json_path, ".json") Pathname(json_path).parent + node_name + ".content.xml" end |
.group_data(node_data) ⇒ Object
Divide node data into serialized attributes and children
50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/aem_lookout/sling_initial_content_converter.rb', line 50 def self.group_data(node_data) serialized_attributes = {} children = {} node_data.each do |key,value| if !value.is_a?(Hash) serialized_attributes[key] = convert_to_serialized_jcr_value(value) else children[key] = value end end [default_attributes.merge(serialized_attributes), children] end |
.json_files_within(path) ⇒ Object
29 30 31 32 |
# File 'lib/aem_lookout/sling_initial_content_converter.rb', line 29 def self.json_files_within(path) glob_path = Pathname(path).to_s + "/**/*" Dir.glob(glob_path.to_s).delete_if {|path| !path.match(/\.json$/) } end |
.namespaces ⇒ Object
All known namespaces
71 72 73 74 75 76 77 78 79 |
# File 'lib/aem_lookout/sling_initial_content_converter.rb', line 71 def self.namespaces { "xmlns:cq" => "http://www.day.com/jcr/cq/1.0", "xmlns:sling" => "http://sling.apache.org/jcr/sling/1.0", "xmlns:jcr" => "http://www.jcp.org/jcr/1.0", "xmlns:vlt" => "http://www.day.com/jcr/vault/1.0", "xmlns:nt" => "http://www.jcp.org/jcr/nt/1.0" } end |