Class: WirisPlugin::TextServiceImpl

Inherits:
Object
  • Object
show all
Extended by:
HttpListenerInterface, TextServiceInterface
Includes:
Wiris
Defined in:
lib/com/wiris/plugin/impl/TextServiceImpl.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from TextServiceInterface

TextService

Methods included from HttpListenerInterface

HttpListener

Constructor Details

#initialize(plugin) ⇒ TextServiceImpl

Returns a new instance of TextServiceImpl.



22
23
24
25
# File 'lib/com/wiris/plugin/impl/TextServiceImpl.rb', line 22

def initialize(plugin)
    super()
    self.plugin = plugin
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



21
22
23
# File 'lib/com/wiris/plugin/impl/TextServiceImpl.rb', line 21

def data
  @data
end

#errorObject

Returns the value of attribute error.



20
21
22
# File 'lib/com/wiris/plugin/impl/TextServiceImpl.rb', line 20

def error
  @error
end

#pluginObject

Returns the value of attribute plugin.



17
18
19
# File 'lib/com/wiris/plugin/impl/TextServiceImpl.rb', line 17

def plugin
  @plugin
end

#serviceNameObject

Returns the value of attribute serviceName.



18
19
20
# File 'lib/com/wiris/plugin/impl/TextServiceImpl.rb', line 18

def serviceName
  @serviceName
end

#statusObject

Returns the value of attribute status.



19
20
21
# File 'lib/com/wiris/plugin/impl/TextServiceImpl.rb', line 19

def status
  @status
end

Class Method Details

.getDigestExtension(serviceName, provider) ⇒ Object



145
146
147
148
149
150
151
# 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) ⇒ Object



133
134
135
136
137
138
# 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) ⇒ Object



139
140
141
142
143
144
# File 'lib/com/wiris/plugin/impl/TextServiceImpl.rb', line 139

def self.hasStats(serviceName)
    if (serviceName == "latex2mathml")
        return true
    end
    return false
end

Instance Method Details

#filter(str, prop) ⇒ Object



152
153
154
# 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) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# 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) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# 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) ⇒ Object



99
100
101
102
103
104
105
# 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) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
# 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) ⇒ Object



93
94
95
96
97
98
# 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) ⇒ Object



155
156
157
# File 'lib/com/wiris/plugin/impl/TextServiceImpl.rb', line 155

def onData(msg)
    self.status = JsonAPIResponse::STATUS_OK
end

#onError(msg) ⇒ Object



158
159
160
161
162
163
164
165
166
# 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) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# 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