# = Shader # # The equivalent of a 3d engine shader. Converts the page representation # to the html output. Actually converts it to a dynamic page ready for # evaluation. # # === Design: # # We follow the xp methodology: the shader as simple as possible. # In the future we may add mulitple xsls (xsl pipeline) but # we done need them now, so we wont code them! # # - when compiling get the default shader and keep it in the # script structure. # # noo better push them in a request stack! # # - when rendering: # if there is an override shader use it! (example: inject shader) # if there is a parent shader use it! # if there is a script shader use it! # if there is a user shader use it! # if there is an app shader use it! # no shading! # # === Think: # # - are inject override shaders possible ? (no they are # dynamic) # # === TODO: # # - add monitoring support for xsl's. # # code: # George Moschovitis # # (c) 2004 Navel, all rights reserved. # $Id: shaders.rb 89 2004-10-20 12:55:58Z gmosx $ module N; # = Shader # # One shader can utilize several xsls to render different pages. # the shader selection may be time consuming but is performed once # when transforming the script, so its essentially for free (and # anyway the xsl transoformation is orders of magnitude slower) # # There is no need to keep post xsl. I can reuse the same xsl # by calling transform again. # class Shader attr_accessor :name attr_accessor :xsl_filename # the xslt transoformer. attr_accessor :xslt attr_accessor :mtime def initialize(name, xsl_filename) # leave this require here. only inlcude the xslt # library if the project needs it. require "xml/xslt" @name = name @xsl_filename = xsl_filename @xslt = XML::XSLT.new parse_xsl! end # transform the given document # def xsl_transform(document) @xslt.xml = document return @xslt.serve() end alias_method :transform, :xsl_transform def xsl parse_xsl! if $reload_xsl return @xsl end def to_s @name end private # Parse the xsl. # def parse_xsl! $log.debug "Parsing xsl '#{@xsl_filename}'" if $DBG @mtime = File.stat(@xsl_filename).mtime @xslt.xsl = File.read(@xsl_filename) end end # = NilShader # # A shader that does absolutely nothing. # # === WARNING: not tested. # class NilShader < Shader def initialize @name = "nil" @mtime = Time.at(0) end # No transform, just convert to a String. def transform(document) return document.to_s end end # = ShaderManager # # Manages the shaders for the webapp. # # TODO: integrate with sitemap. # # === Design: # # - the shader selection may be time consuming but is performed once # when transforming the script, so its essentially for free (and # anyway the xsl transoformation is orders of magnitude slower) # class ShaderManager # corresponding arrays attr_accessor :regex, :shaders attr_accessor :default_shader def initialize @regex = [] @shaders = [] end def set_default(shader) @default_shader = shader end def add(regex, shader) @regex << regex @shaders << shader end def shader_for_path(path) @regex.each_with_index { |rx, idx| if path =~ rx return @shaders[idx] end } return @default_shader end def delete(regex) if idx = @regex.index(regex) @regex.delete_at(idx) @shaders.delete_at(idx) end end end end # module;