From: Jan Provaznik <jprovazn(a)redhat.com>
also 'add_template_name' moved to the model and renamed to 'name='.
---
src/app/controllers/images_controller.rb | 51 ++++++++++++-----------------
src/app/models/template_xml.rb | 35 ++++++++++++++++++--
src/config/locales/en.yml | 5 +++
3 files changed, 58 insertions(+), 33 deletions(-)
diff --git a/src/app/controllers/images_controller.rb
b/src/app/controllers/images_controller.rb
index 38d9f72..72a00a3 100644
--- a/src/app/controllers/images_controller.rb
+++ b/src/app/controllers/images_controller.rb
@@ -16,8 +16,6 @@
# MA 02110-1301, USA. A copy of the GNU General Public License is
# also available at
http://www.gnu.org/copyleft/gpl.html.
-require 'app/util/temlate_xml'
-
class ImagesController < ApplicationController
before_filter :require_user
@@ -99,7 +97,7 @@ class ImagesController < ApplicationController
begin
xml_source = RestClient.get(url, :accept => :xml)
rescue RestClient::Exception, SocketError, URI::InvalidURIError
- flash.now[:error] = t('images.flash.error.invalid_url')
+ flash.now[:error] = t('template_xml.flash.error.invalid_url')
render :new and return
end
else
@@ -108,12 +106,17 @@ class ImagesController < ApplicationController
end
begin
- tpl = TemplateXML.new(xml_source)
- doc = Nokogiri::XML(xml_source) { |config| config.strict }
- add_template_name(doc, @name)
- @xml = doc.to_xml
+ doc = TemplateXML.new(xml_source)
rescue Nokogiri::XML::SyntaxError
- flash.now[:error] = t('images.flash.warning.invalid_xml')
+ errors = [t('template_xml.errors.xml_parse_error')]
+ else
+ doc.name = @name
+ @xml = doc.to_xml
+ errors = doc.validate
+ end
+
+ if errors.any?
+ flash.now[:error] = errors
@xml = xml_source
render :edit_xml and return
end
@@ -125,13 +128,10 @@ class ImagesController < ApplicationController
@name = params[:name]
@xml = params[:image_xml]
- begin
- doc = Nokogiri::XML(@xml) { |config| config.strict }
- xml_name = doc.xpath('/template/name').first
- @name = xml_name.content unless xml_name.blank?
- rescue Nokogiri::XML::SyntaxError
- flash.now[:error] = t('images.flash.warning.invalid_xml')
- render :edit_xml
+ errors = TemplateXML.validate(@xml)
+ if errors.any?
+ flash.now[:error] = errors
+ render :edit_xml and return
end
end
@@ -144,6 +144,12 @@ class ImagesController < ApplicationController
render :edit_xml and return
end
+ errors = TemplateXML.validate(@xml)
+ if errors.any?
+ flash.now[:error] = errors
+ render :edit_xml and return
+ end
+
uuid = UUIDTools::UUID.timestamp_create.to_s
@template = Aeolus::Image::Warehouse::Template.create!(uuid, @xml, {
:object_type => 'template',
@@ -191,19 +197,4 @@ class ImagesController < ApplicationController
end
redirect_to images_path, :notice =>
t("images.flash.notice.multiple_deleted", :count => selected_images.count)
end
-
- protected
- def add_template_name(doc, name)
- return unless doc
-
- if doc.root.nil? || doc.root.name != 'template'
- doc.root = doc.create_element('template')
- end
-
- if doc.xpath('/template/name').empty?
- doc.xpath('/template').first << doc.create_element('name')
- end
-
- doc.xpath('/template/name').first.content = name unless name.blank?
- end
end
diff --git a/src/app/models/template_xml.rb b/src/app/models/template_xml.rb
index f7cb7c8..5321e36 100644
--- a/src/app/models/template_xml.rb
+++ b/src/app/models/template_xml.rb
@@ -29,9 +29,38 @@ class TemplateXML
def validate
xsd = Nokogiri::XML::RelaxNG(File.read(@relax_file))
- @errors = xsd.validate((a)xml).map {|err| err.message}
- @errors << 'Name is not set' if
doc.xpath('/template/name').text.empty?
- @errors.empty?
+ errors = xsd.validate((a)xml).map {|err| err.message}
+ if errors.any?
+ @errors << I18n.t('template_xml.errors.invalid_xml')
+ @errors += errors
+ end
+ @errors << I18n.t('template_xml.errors.name_is_not_set') if
@xml.xpath('/template/name').text.empty?
+ @errors
+ end
+
+ def self.validate(xmlstr)
+ begin
+ doc = TemplateXML.new(xmlstr)
+ rescue Nokogiri::XML::SyntaxError
+ return [I18n.t('template_xml.errors.xml_parser_error')]
+ end
+ return doc.validate
+ end
+
+ def name=(name)
+ if @xml.root.nil? || @xml.root.name != 'template'
+ @xml.root = @xml.create_element('template')
+ end
+
+ if @xml.xpath('/template/name').empty?
+ @xml.xpath('/template').first << @xml.create_element('name')
+ end
+
+ @xml.xpath('/template/name').first.content = name unless name.blank?
+ end
+
+ def name
+ doc.xpath('/template/name').text
end
def to_xml
diff --git a/src/config/locales/en.yml b/src/config/locales/en.yml
index b6dc774..8b52a31 100644
--- a/src/config/locales/en.yml
+++ b/src/config/locales/en.yml
@@ -723,6 +723,11 @@ en:
make_deployable: Automatically make "%{name}" deployable.
back: Back
save_template: Save Template
+ template_xml:
+ errors:
+ xml_parse_error: Failed to parse XML.
+ invalid_xml: "XML is not valid:"
+ name_is_not_set: Name is not set.
provider_images:
flash:
notice:
--
1.7.6