#
# 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('xf')
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:: output to hold the view part of the document
# model_buffer:: output to hold the model data of the document
# Final document is view_buffer + model_buffer for some subclass transformer (xforms, etc.)
#
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. Style is optional:
#
#Ex:
# @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 << ""
doc << title
doc << ""
doc << ""
yield doc
doc << ""
end
#Produce a text string. To apply style for text, wrap the tag with xml tag:
#Ex:
# xml.i do |y|
# @xml.text(y, "my italic example")
# end
#Generates the following xhtml code:
#
# my italic example
#
def text(doc, txt="", attr=nil, xattr=nil)
doc << txt
end
#Produce a text string in 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 :style.
#Ex:
#
def textarea(doc, txt="", attr=nil, xattr=nil)
doc << "\n"
end
# Tag for user input field. Type can be "text" or "password". For type "submit",
# use submit tag.
def input(doc, id, value, type, args={})
doc << "\n\n"
end
def submit_tag()
end
def form(doc, id, action, method, args={})
doc << "\n'
end
def link(doc, href, txt="", args={})
doc << "\n" << txt << ''
end
def plain_view(doc, txt)
# do nothing
end
def plain_model(doc, txt)
# do nothing
end
def softBr(doc)
doc.br
end
def image(doc, id, src, args={})
doc << "\n'
end
def instance_tag(doc, id)
# do nothing, only implemented in xforms
yield doc
end
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 << "