-------------------------------------------------------------------------------- -- Title: LWLink.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 LWObjectService = require( "LWObjectService" ) local LWString = require( "LWString" ) local LWStyle = require( "LWStyle" ) local LUList = require( "LUList" ) -- define the class local super = LWComponent local self = super() -- method to access this link object function self:object() return self:bindings():get( "object" ) end -- method to access this link value function self:value() local aValue = self:bindings():get( "value" ) if aValue == nil then local anObject = self:object() if anObject ~= nil then if self:isObject( anObject ) == true then local aService = LWObjectService:serviceWithObject( anObject ) if aService ~= nil then aValue = aService:pathWithObject( anObject ) end else aValue = tostring( anObject ) end end end return aValue end -- method to access this link title function self:title() local aTitle = self:bindings():get( "title" ) if aTitle == nil then local anObject = self:object() if anObject ~= nil then if self:isObject( anObject ) == true then local aService = LWObjectService:serviceWithObject( anObject ) if aService ~= nil then aTitle = aService:titleWithObject( anObject ) else aTitle = anObject:toString() end else aTitle = tostring( anObject ) end end end return aTitle end -- method to access this link content function self:content() local aContent = self:bindings():get( "content" ) if aContent == nil then local someBindings = self:bindings():copy() someBindings:remove( "value" ) someBindings:remove( "title" ) aContent = LWString( self:context(), self, someBindings ) end return aContent end -- method for string representation function self:toString() local aValue = self:value() if aValue ~= nil then local aBuffer = LUList() local aStyle = LWStyle:styleWithComponent( self ) aBuffer:add( ( "" ) aBuffer:add( self:content():toString() ) aBuffer:add( "" ) aValue = aBuffer:join( "" ) end return aValue end -- method to URL encode a given string function self:encode( aString ) if aString ~= nil then local aFunction = function( aValue ) return ( "%%%02x" ):format( aValue:byte() ) end local anotherFunction = function( aValue ) local aByte = aValue:byte() if aByte > 127 then aValue = ( "%%%02x" ):format( aValue:byte() ) end return aValue end aString = aString:gsub( "([%s<>&=+%c])", aFunction ) aString = aString:gsub( "([%W])", anotherFunction ) end return aString end return self