# # 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 attr_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 #and style path is relative to public/stylesheets. # #==== Examples # @xml.body(x, 'mobio', "css/mobio_twitter.css") #Xhtml output: # # # mobio # def body(doc, title="",style=nil) doc << "\n" if style #doc << "" # user internal style sheet doc << "' 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 #Itentical to text except a line break at the end of the text def text_line(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' #==== Examples # @xml.form(@doc, "f1", "login", "post") {} #generates the following xhtml: #
#
def form(doc, id, action, method) doc << "\n
" yield doc doc << '
' end #Create a html link #1. url: the link url #2. txt: text displayed in the link #==== Options #* :style -- specifies html, xhtml style attributes as a string #==== Examples # args= {:xstyle=>'height="1ex" width="20em"', :style=>'class="btm-menu"'} # @xml.link(@doc, "index", "Friends", args) #generates the following xhtml: # Friends def link(doc, url, txt="", options={}, &block) doc << "\n" << txt << '' end # To be implemented def button(doc, url, label, options={}, &block) doc << "\n" << label << '' 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. src: the url of the image #==== Options #* :style -- specifies html, xhtml style attributes as a string #* :alt -- html alt attribute #==== Examples # img = {:alt=>"Rails", :xstyle=>'height="5ex" width="100%"'} # @xml.image(body, 'http://homer.qa.mobiolabs.com/cms/images/default/glp/bn/til/top-wcric.png', img) #generates the following xhtml: # Rails def image(doc, src, options={}) doc << "\n\""' end #Not implemented for html client, only impemented for xforms client def instance_tag(doc, id, src=nil, &block) if block yield doc end end def vstack(doc, xstyle='height="0"') yield doc end def hstack(doc, xstyle='height="0"') yield doc end #Create table tag #==== Options #* :style -- specifies html, xhtml style attributes as a string #* :xstyle -- specifies xforms style attributes as a string def table(doc, options={}) doc << "\n" yield doc doc << '
' end def row(doc, id=nil, nodeset=nil, options={}) doc << "" yield doc doc << '' end def row_list(doc, id=nil, options={}) doc << "" yield doc doc << '' end def itemset(doc, id, nodeset, options={}) yield doc end def itemlist(doc, id, options={}) yield doc end def cell(doc, options={}) doc << "" yield doc doc << '' end def list(doc, options={}) yield doc end # The following tags are only supported by xforms client def tab(doc, xstyle="") yield doc end def switch(doc) yield doc end def xcase(doc, id, label=nil, xstyle=nil) yield doc end def softkey(doc, position="1", label="", refid=nil, &block) if block yield doc end end #Implement menus with links def menus(doc, id, label="Options", xstyle=nil) doc << "
" if block yield doc end doc << "
" end # def menu(doc, label, accesskey=nil, &block) doc << "" << label << ' ' end def load_tag(doc, resource) doc << resource end def syscall(doc, name, params={}) end def click2call(doc, phone) end def action_tag(doc, event="DOMActivate") end def toggle_tag(doc, name="main") end def exit_tag(doc) end def back_tag(doc) end end end end include Rmobio::Rxml