Class: WirisPlugin::TextServiceImpl

Inherits:
Object
  • Object
show all
Extended by:
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

Constructor Details

#initialize(plugin) ⇒ TextServiceImpl

Returns a new instance of TextServiceImpl.



13
14
15
16
# File 'lib/com/wiris/plugin/impl/TextServiceImpl.rb', line 13

def initialize(plugin)
  super()
  @plugin = plugin
end

Instance Attribute Details

#pluginObject

Returns the value of attribute plugin.



12
13
14
# File 'lib/com/wiris/plugin/impl/TextServiceImpl.rb', line 12

def plugin
  @plugin
end

Class Method Details

.getDigestExtension(serviceName, provider) ⇒ Object



107
108
109
110
111
112
113
# File 'lib/com/wiris/plugin/impl/TextServiceImpl.rb', line 107

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



95
96
97
98
99
100
# File 'lib/com/wiris/plugin/impl/TextServiceImpl.rb', line 95

def self.hasCache(serviceName)
  if (serviceName=="mathml2accessible")
    return true
  end
  return false
end

.hasStats(serviceName) ⇒ Object



101
102
103
104
105
106
# File 'lib/com/wiris/plugin/impl/TextServiceImpl.rb', line 101

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

Instance Method Details

#filter(str, prop) ⇒ Object



114
115
116
# File 'lib/com/wiris/plugin/impl/TextServiceImpl.rb', line 114

def filter(str,prop)
  return TextFilter.new(@plugin)::filter(str,prop)
end

#getMathML(digest, latex) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/com/wiris/plugin/impl/TextServiceImpl.rb', line 68

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

#latex2mathml(latex) ⇒ Object



62
63
64
65
66
67
# File 'lib/com/wiris/plugin/impl/TextServiceImpl.rb', line 62

def latex2mathml(latex)
  param = PropertiesTools::newProperties()
  PropertiesTools::setProperty(param,"latex",latex)
  provider = @plugin::newGenericParamsProvider(param)
  return service("latex2mathml",provider)
end

#mathml2accessible(mml, lang, param) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/com/wiris/plugin/impl/TextServiceImpl.rb', line 48

def mathml2accessible(mml,lang,param)
  if lang!=nil
    PropertiesTools::setProperty(param,"lang",lang)
  end
  PropertiesTools::setProperty(param,"mml",mml)
  provider = @plugin::newGenericParamsProvider(param)
  return service("mathml2accessible",provider)
end

#mathml2latex(mml) ⇒ Object



56
57
58
59
60
61
# File 'lib/com/wiris/plugin/impl/TextServiceImpl.rb', line 56

def mathml2latex(mml)
  param = PropertiesTools::newProperties()
  PropertiesTools::setProperty(param,"mml",mml)
  provider = @plugin::newGenericParamsProvider(param)
  return service("mathml2latex",provider)
end

#service(serviceName, provider) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/com/wiris/plugin/impl/TextServiceImpl.rb', line 17

def service(serviceName,provider)
  digest = nil
  renderParams = provider::getRenderParameters(@plugin::getConfiguration())
  if self.class.hasCache(serviceName)
    digest = @plugin::newRender()::computeDigest(nil,renderParams)
    store = @plugin::getStorageAndCache()
    ext = self.class.getDigestExtension(serviceName,provider)
    s = store::retreiveData(digest,ext)
    if s!=nil
      return Utf8::fromBytes(s)
    end
  end
  url = @plugin::getImageServiceURL(serviceName,self.class.hasStats(serviceName))
  h = HttpImpl.new(url,nil)
  @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::request(true)
  r = h::getData()
  if digest!=nil
    store = @plugin::getStorageAndCache()
    ext = self.class.getDigestExtension(serviceName,provider)
    store::storeData(digest,ext,Utf8::toBytes(r))
  end
  return r
end