-------------------------------------------------------------------------------- -- Title: LWString.lua -- Description: Like a square peg in a round hole -- Author: Raphaël Szwarc http://alt.textdrive.com/lua/ -- Creation Date: February 1, 2005 -- Legal: Copyright (C) 2005 Raphaël Szwarc -------------------------------------------------------------------------------- -- import dependencies local LWComponent = require( "LWComponent" ) local LWStyle = require( "LWStyle" ) local LUBundle = require( "LUBundle" ) local LUList = require( "LUList" ) local LUMap = require( "LUMap" ) -- define the class local super = LWComponent local self = super() -- class variable(s) local _styles = nil local _tags = nil -- method to access this string object function self:object() return self:bindings():get( "object" ) end -- method to access this string value function self:value() local aValue = self:bindings():get( "value" ) if aValue == nil then local anObject = self:object() if anObject ~= nil then if self:isKindOf( anObject ) == true then local LWObjectService = require( "LWObjectService" ) local aService = LWObjectService:serviceWithObject( anObject ) if aService ~= nil then aValue = aService:valueWithObject( anObject ) else aValue = anObject:toString() end else aValue = tostring( anObject ) end end end return aValue end -- method to access this string style function self:style() local aStyle = self:bindings():get( "style" ) if aStyle == nil then local aBuffer = LUList() local someBindings = self:bindings() local someStyles = self:styles() for aKey, aValue in someStyles:iterator() do local aBinding = someBindings:get( aKey ) if aBinding ~= nil then aBuffer:add( ( "%s: %s" ):format( aValue, aBinding ) ) end end if aBuffer:hasData() == true then aStyle = aBuffer:join( "; " ) end end return aStyle end -- method for string representation function self:toString() local aValue = self:value() if aValue ~= nil then local aBuffer = LUList() local aStyle = LWStyle:styleWithComponent( self ) local someClosingTags = LUList() local someBindings = self:bindings() local someTags = self:tags() for aKey, aValue in someTags:iterator() do local aBinding = someBindings:get( aKey ) if aBinding == true then local aTag = aValue aBuffer:add( ( "<%s>" ):format( aTag ) ) someClosingTags:add( ( "" ):format( aTag ) ) end end if aStyle ~= nil then aBuffer:add( ( "" ):format( aStyle ) ) someClosingTags:add( "" ) end aBuffer:add( self:encode( aValue ) ) aBuffer:add( someClosingTags:reverse():join( "" ) ) aValue = aBuffer:join( "" ) end return aValue end -- method to HTML encode a given string function self:encode( aString ) if aString ~= nil then local aFunction = function( aValue ) return ( "&#%02d;" ):format( aValue:byte() ) end aString = tostring( aString ):gsub( "([<>&'\"])", aFunction ) end return aString end -- method to access the string styles function self:styles() if _styles == nil then local aBundle = LUBundle:bundleWithName( "LWString" ) local aFile = aBundle:fileWithName( "LWStringStyles", "txt" ) _styles = LUMap():load( aFile ) end return _styles end -- method to access the string tags function self:tags() if _tags == nil then local aBundle = LUBundle:bundleWithName( "LWString" ) local aFile = aBundle:fileWithName( "LWStringTags", "txt" ) _tags = LUMap():load( aFile ) end return _tags end return self