#
# 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 'singleton'
module Rmobio
module Rxml
=begin rdoc
Provide a base transformer to translate rxml document to xhtml markup.
It subclasses the builder library XmlMarkup class so method not defined in this
transformer class will inherit methods or method missing from XmlMarkup class.
See XmlMarkup for documentation.
Example:
In the controller, get the proper transformer class by passing a client type,
See TransformerFactory class for supported client types:
require 'rmobio/rxml/transformer_factory'
@xml = TransformerFactory.get_transformer('xhtml')
Here is the view that uses methods to output xhtml document header, an
image and some text:
@xml.doctype(xml) do |x|
@xml.body(x, 'mobio') do|body|
@xml.image(body, "img1", 'http://localhost:3000/images/rails.png')
@xml.softBr(body)
body.b do |y|
@xml.text(y, 'My test app')
end
end
end
The above code generates the following xhtml in Firefox:
mobio
My test app
=end
class BaseTransformer < Builder::XmlMarkup
cattr_accessor :view_buffer, :model_buffer
include Singleton
# Create a base rxml transformer to transform rxml document to xhtml markup
#
# out:: Object receiving the markup in xhtml
# @view_buffer:: buffer to hold the view part of the document
# @model_buffer:: buffer to hold the model data of the document
# Final document is view_buffer + model_buffer for some subclass transformer (XformsTransformer)
#
def initialize
@view_buffer = ""
@model_buffer = ""
super
end
def cattr_reader(*syms)
syms.flatten.each do |sym|
class_eval(<<-EOS, __FILE__, __LINE__)
unless defined? @@#{sym}
@@#{sym} = nil
end
def self.#{sym}
@@#{sym}
end
def #{sym}
@@#{sym}
end
EOS
end
end
def cattr_writer(*syms)
syms.flatten.each do |sym|
class_eval(<<-EOS, __FILE__, __LINE__)
unless defined? @@#{sym}
@@#{sym} = nil
end
def self.#{sym}=(obj)
@@#{sym} = obj
end
def #{sym}=(obj)
@@#{sym} = obj
end
EOS
end
end
def cattr_accessor(*syms)
cattr_reader(*syms)
cattr_writer(*syms)
end
# To be implemented.
def transform(xml, client=nil)
end
#Generate standdard xhtml document header
def doctype(xml)
xml << '
'
yield xml
xml << ""
end
#Generate xthml head and body tag. The document title and style src are optional.
#
#==== Examples
# @xml.body(x, 'mobio', "../stylesheets/css/mobio_twitter.css")
#Xhtml output:
#
#
# mobio
#
def body(doc, title="",style=nil)
doc << "\n"
if style
doc << "\r"
end
doc << "#{title}"
yield doc
doc << ""
end
#Produce a text string. To apply style for text, wrap the tag with xml tag:
#==== Examples
# xml.i do |y|
# @xml.text(y, "my italic example")
# end
#Generates the following xhtml code:
#
# my italic example
#
def text(doc, txt="", options={})
doc << txt
end
#Create a text box. To apply style for text, wrap the tag with xml tag.
#Extra html style (cols, rows, etc.) can be specified in the hash variable options.
#===== Options
#
#* :style -- specifies html, xhtml style attributes like cols, rows, etc. as a string
#* :xstyle -- specifies xforms style attributes like height, width, etc. as a string.
#
#==== Examples
#
# @xml.textare(body, "some text",
# :style=>'class="white"', :xstyle=>'height="3ex" style="white"')
#generates the following xhtml code:
#
def textarea(doc, txt="", options={})
doc << "\n"
end
#Create user input field.
#1. id: not used in xhtml client
#2. value: initial value that will be displayed when ui is loaded
#3. type: html input type attriute, "text", "password", "submit", etc.
#==== Options
#* :style -- specifies html, xhtml style attributes like cols, rows, etc. as a string
#* :xstyle -- specifies xforms style attributes like height, width, etc. as a string.
#* :xpath -- specifies the xpath of the input data in the model, xforms only.
#==== Examples
#* a text input box
# @xml.input(f, "name", "john", "text")
#generates the xhtml:
#
#* a password input box
# @xml.input(f, "password", "", "password")
#generates the xhtml:
#
#* a submit button
# @xml.input(f, "submit", "Login", "submit")
#generates a submit button:
#
#
def input(doc, id, value, type, options={})
doc << "\n\n"
end
def submit_tag()
# do nothing, only implemented in xforms
end
#Create form tag for submission
#1. id: name of the form
#2. action: the action url that is invoked
#3. method: http 'get' or 'post'
def form(doc, id, action, method)
doc << "\n'
end
#Create a html link
#1. href: the link url
#2. txt: text displayed in the link
#==== Options
#* :style -- specifies html, xhtml style attributes as a string
def link(doc, href, txt="", options={}, &block)
doc << "\n" << txt << ''
end
#Not implemented for xhtml client
def model_tag(doc, txt)
# do nothing
end
#Not implemented for xhtml client
def view_tag(doc, txt)
# do nothing
end
#Line break
def softBr(doc)
doc.br
end
#Create html img tag.
#1. id: name of the widget
#2. src: the url of the image
#==== Options
#* :style -- specifies html, xhtml style attributes as a string
#* :alt -- html alt attribute
def image(doc, id, src, options={})
doc << "\n'
end
#Not implemented for html client
def instance_tag(doc, id)
# do nothing, only implemented in xforms
end
#Create table tag
#1. style: html styles
#2. xstyle: xforms styles
def table(doc, style="", xstyle="")
doc << "\n
"
yield doc
doc << '
'
end
def table_row(doc, style="", xstyle="")
doc << "\n
"
yield doc
doc << '
'
end
def table_cell(doc, style="", xstyle="")
doc << "