class WirisPlugin::TextServiceImpl

Attributes

data[RW]
error[RW]
plugin[RW]
serviceName[RW]
status[RW]

Public Class Methods

getDigestExtension(serviceName, provider) click to toggle source
# File lib/com/wiris/plugin/impl/TextServiceImpl.rb, line 145
def self.getDigestExtension(serviceName, provider)
    lang = provider::getParameter("lang","en")
    if (lang != nil) && (lang::length() == 0)
        return "en"
    end
    return lang
end
hasCache(serviceName) click to toggle source
# File lib/com/wiris/plugin/impl/TextServiceImpl.rb, line 133
def self.hasCache(serviceName)
    if (serviceName == "mathml2accessible")
        return true
    end
    return false
end
hasStats(serviceName) click to toggle source
# File lib/com/wiris/plugin/impl/TextServiceImpl.rb, line 139
def self.hasStats(serviceName)
    if (serviceName == "latex2mathml")
        return true
    end
    return false
end
new(plugin) click to toggle source
Calls superclass method
# File lib/com/wiris/plugin/impl/TextServiceImpl.rb, line 22
def initialize(plugin)
    super()
    self.plugin = plugin
end

Public Instance Methods

filter(str, prop) click to toggle source
# File lib/com/wiris/plugin/impl/TextServiceImpl.rb, line 152
def filter(str, prop)
    return TextFilter.new(@plugin)::filter(str,prop)
end
getMathML(digest, latex) click to toggle source
# File lib/com/wiris/plugin/impl/TextServiceImpl.rb, line 106
def getMathML(digest, latex)
    if digest != nil
        content = @plugin::getStorageAndCache()::decodeDigest(digest)
        if content != nil
            if StringTools::startsWith(content,"<")
                breakline = content::indexOf("\n",0)
                return Std::substr(content,0,breakline)
            else 
                iniFile = IniFile::newIniFileFromString(content)
                mathml = iniFile::getProperties()::get("mml")
                if mathml != nil
                    return mathml
                else 
                    return "Error: mathml not found."
                end
            end
        else 
            return "Error: formula not found."
        end
    else 
        if latex != nil
            return latex2mathml(latex)
        else 
            return "Error: no digest or latex has been sent."
        end
    end
end
jsonResponse(serviceName, provider) click to toggle source
# File lib/com/wiris/plugin/impl/TextServiceImpl.rb, line 45
def jsonResponse(serviceName, provider)
    renderParams = provider::getRenderParameters(@plugin::getConfiguration())
    digest = @plugin::newRender()::computeDigest(nil,renderParams)
    self.serviceName = serviceName
    url = @plugin::getImageServiceURL(serviceName,TextServiceImpl.hasStats(serviceName))
    h = HttpImpl.new(url,self)
    @plugin::addReferer(h)
    @plugin::addProxy(h)
    ha = PropertiesTools::fromProperties(provider::getServiceParameters())
    iter = ha::keys()
    while iter::hasNext()
        k = iter::next()
        h::setParameter(k,ha::get(k))
    end
    h::setParameter("httpstatus","true")
    begin
    h::request(true)
    end
    r = self.data != nil ? self.data : h::getData()
    response = JsonAPIResponse.new()
    if self.status == JsonAPIResponse::STATUS_ERROR
        response::setStatus(JsonAPIResponse::STATUS_ERROR)
        response::addError(self.error)
    else 
        response::setStatus(JsonAPIResponse::STATUS_OK)
        response::addResult("text",r)
    end
    if digest != nil
        store = @plugin::getStorageAndCache()
        ext = TextServiceImpl.getDigestExtension(serviceName,provider)
        store::storeData(digest,ext,Utf8::toBytes(response::getResponse()))
    end
    return response
end
latex2mathml(latex) click to toggle source
# File lib/com/wiris/plugin/impl/TextServiceImpl.rb, line 99
def latex2mathml(latex)
    param = PropertiesTools::newProperties()
    PropertiesTools::setProperty(param,"latex",latex)
    provider = @plugin::newGenericParamsProvider(param)
    mathml = service("latex2mathml",provider)
    return mathml::indexOf("Error converting") != -1 ? mathml : latex
end
mathml2accessible(mml, lang, param) click to toggle source
# File lib/com/wiris/plugin/impl/TextServiceImpl.rb, line 79
def mathml2accessible(mml, lang, param)
    if lang != nil
        PropertiesTools::setProperty(param,"lang",lang)
    end
    PropertiesTools::setProperty(param,"mml",mml)
    provider = @plugin::newGenericParamsProvider(param)
    reponse = jsonResponse("mathml2accessible",provider)
    if reponse::getStatus() == JsonAPIResponse::STATUS_OK
        result = reponse::getResult()
        return (result::get("text"))
    else 
        return "Error converting from mathml to text"
    end
end
mathml2latex(mml) click to toggle source
# File lib/com/wiris/plugin/impl/TextServiceImpl.rb, line 93
def mathml2latex(mml)
    param = PropertiesTools::newProperties()
    PropertiesTools::setProperty(param,"mml",mml)
    provider = @plugin::newGenericParamsProvider(param)
    return service("mathml2latex",provider)
end
onData(msg) click to toggle source
# File lib/com/wiris/plugin/impl/TextServiceImpl.rb, line 155
def onData(msg)
    self.status = JsonAPIResponse::STATUS_OK
end
onError(msg) click to toggle source
# File lib/com/wiris/plugin/impl/TextServiceImpl.rb, line 158
def onError(msg)
    if self.serviceName == "mathml2accessible"
        self.status = JsonAPIResponse::STATUS_WARNING
        self.data = "Error converting from MathML to accessible text."
    else 
        self.error = msg
        self.status = JsonAPIResponse::STATUS_ERROR
    end
end
service(serviceName, provider) click to toggle source
# File lib/com/wiris/plugin/impl/TextServiceImpl.rb, line 26
def service(serviceName, provider)
    self.serviceName = serviceName
    digest = nil
    renderParams = provider::getRenderParameters(@plugin::getConfiguration())
    if TextServiceImpl.hasCache(serviceName)
        digest = @plugin::newRender()::computeDigest(nil,renderParams)
        store = @plugin::getStorageAndCache()
        ext = TextServiceImpl.getDigestExtension(serviceName,provider)
        s = store::retreiveData(digest,ext)
        if s != nil
            cachedServiceText = Utf8::fromBytes(s)
            begin
            JSon::decode(cachedServiceText)
            end
            return cachedServiceText
        end
    end
    return jsonResponse(serviceName,provider)::getResponse()
end