#
# Copyright (C) 2007 Mobio Networks, Inc.
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program. If not, see .
#
require 'builder/xmlmarkup'
require 'rmobio/rxml/base_transformer'
module Rmobio
module Rxml
class XformsTransformer < BaseTransformer
def doctype(xml, style_src=nil)
xml << ''
@model_buffer = '
'
@model_buffer << '' if style_src
@model_buffer << "\n"
yield xml
@model_buffer << "\n"
xml << @model_buffer << @view_buffer
xml << ""
end
def body(doc, title, style=nil)
# xforms style is external style
@view_buffer << '' if style
@view_buffer << ''
yield doc
@view_buffer << ""
end
def text(doc, txt="", args=nil)
if args[:id]
@model_buffer << "\n" << '' << txt << "\n"
end
@view_buffer << "\n"
else
@view_buffer << ">" << txt
@view_buffer << ''
end
end
#
# def text(doc, txt="", id=nil, style=nil, height=nil, width=nil)
# if id
# @model_buffer << "\n" << '' << txt << "\n"
# end
#
# @view_buffer << "\n"
# else
# @view_buffer << ">" << txt
# @view_buffer << ''
# end
# end # text
def textarea(doc, txt="", height=nil, width=nil, attr=nil, id=nil)
if id
@model_buffer << "\n" << '' << txt << "\n"
end
@view_buffer << "\n"
else
@view_buffer << ">" << txt
@view_buffer << ''
end
end # textarea
def input(doc, name, value, type, attr=nil)
@model_buffer << "\n" << '' << value << "\n"
@view_buffer << "\n"
end
def submit_tag(id)
end
def form(doc, name, action, method, req_id=nil, replace_id=nil, attr=nil)
@model_buffer << "\n'
#yield doc
#@model_buffer << ''
end
def link(doc)
end
def softBr(doc)
@view_buffer << ''
end
# If we have height or width specified, use 'image' widget, otherwise,
# just use 'icon' widget.
def image(doc, src, name, height=nil, width=nil, attr=nil, alt=nil)
if width or height
@view_buffer << ""
else
@view_buffer << '>' << src
end
if height or width
@view_buffer << ''
else
@view_buffer << ''
end
end
def tag!(sym, *args)
end
# Create an instance in the model
def instance_tag(doc, name)
@model_buffer << "\n"
yield doc
@model_buffer << ''
end
# Beginning overwrite the builder methods and send all output to model_buffer
def method_missing(sym, *args, &block)
text = nil
attrs = nil
sym = "#{sym}:#{args.shift}" if args.first.kind_of?(Symbol)
args.each do |arg|
case arg
when Hash
attrs ||= {}
attrs.merge!(arg)
else
text ||= ''
text << arg.to_s
end
end
if block
unless text.nil?
raise ArgumentError, "XmlMarkup cannot mix a text argument with a block"
end
_indent
_start_tag(sym, attrs)
_newline
_nested_structures(block)
_indent
_end_tag(sym)
_newline
elsif text.nil?
_indent
_start_tag(sym, attrs, true)
_newline
else
_indent
_start_tag(sym, attrs)
text! text
_end_tag(sym)
_newline
end
end
def _text(text)
@model_buffer << text
end
# Start an XML tag. If end_too is true, then the start
# tag is also the end tag (e.g.
def _start_tag(sym, attrs, end_too=false)
@model_buffer << "<#{sym}"
_insert_attributes(attrs)
@model_buffer << "/" if end_too
@model_buffer << ">"
end
# Insert an ending tag.
def _end_tag(sym)
@model_buffer << "#{sym}>"
end
# Insert the attributes (given in the hash).
def _insert_attributes(attrs, order=[])
return if attrs.nil?
order.each do |k|
v = attrs[k]
@model_buffer << %{ #{k}="#{_attr_value(v)}"} if v # " WART
end
attrs.each do |k, v|
@model_buffer << %{ #{k}="#{_attr_value(v)}"} unless order.member?(k) # " WART
end
end
def _attr_value(value)
case value
when Symbol
value.to_s
else
_escape_quote(value.to_s)
end
end
def _nested_structures(block)
@level += 1
block.call(self)
ensure
@level -= 1
end
# End builder methods
end
end
end