Class: Applocale::ParseStringsFile

Inherits:
Object
  • Object
show all
Defined in:
lib/applocale/Core/ParserStringFile/parse_strings_file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(platform, langpathobj_list, injectobj) ⇒ ParseStringsFile

Returns a new instance of ParseStringsFile.



11
12
13
14
15
16
17
18
# File 'lib/applocale/Core/ParserStringFile/parse_strings_file.rb', line 11

def initialize(platform, langpathobj_list, injectobj)
  @strings_keys = {}
  @keys_list = Array.new
  @errorlist = Array.new()
  @platform = platform
  @injectobj = injectobj
  self.to_parse_files(langpathobj_list)
end

Instance Attribute Details

#errorlistObject (readonly)

Returns the value of attribute errorlist.



9
10
11
# File 'lib/applocale/Core/ParserStringFile/parse_strings_file.rb', line 9

def errorlist
  @errorlist
end

#in_multiline_commentsObject (readonly)

Returns the value of attribute in_multiline_comments.



9
10
11
# File 'lib/applocale/Core/ParserStringFile/parse_strings_file.rb', line 9

def in_multiline_comments
  @in_multiline_comments
end

#injectobjObject (readonly)

Returns the value of attribute injectobj.



9
10
11
# File 'lib/applocale/Core/ParserStringFile/parse_strings_file.rb', line 9

def injectobj
  @injectobj
end

#keys_listObject (readonly)

Returns the value of attribute keys_list.



9
10
11
# File 'lib/applocale/Core/ParserStringFile/parse_strings_file.rb', line 9

def keys_list
  @keys_list
end

#platformObject (readonly)

Returns the value of attribute platform.



9
10
11
# File 'lib/applocale/Core/ParserStringFile/parse_strings_file.rb', line 9

def platform
  @platform
end

#strings_keysObject (readonly)

Returns the value of attribute strings_keys.



9
10
11
# File 'lib/applocale/Core/ParserStringFile/parse_strings_file.rb', line 9

def strings_keys
  @strings_keys
end

Instance Method Details

#parse_token(linenum, line, sep, lang, file) ⇒ Object



100
101
102
103
104
105
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/applocale/Core/ParserStringFile/parse_strings_file.rb', line 100

def parse_token(linenum, line, sep, lang, file)
  n = 0
  in_value = false
  in_quote = false
  in_escape = false
  value = ""

  for ch in line.chars
    prech = ""
    prech = line.chars[n-1] if n > 0
    n += 1
    if @in_multiline_comments
      if "#{prech}#{ch}" == "*/"
        @in_multiline_comments = false
        in_value = false
        value = ""
      end
      next
    end

    if not in_value
      if ch == "\""
        in_quote = true
        in_value = true
      elsif ch != " " and ch != "\t" and ch != sep
        in_value = true
        value << ch
      end
      next
    end

    if in_escape
      value << prech
      value << ch
      in_escape = false
    elsif ch == "\\"
      in_escape = true
    elsif in_quote
      if ch == "\""
        break
      else
        value << ch
      end
    else
      if ch == " " or ch == "\t" or ch == sep
        n -= 1
        break
      elsif "#{prech}#{ch}" == "/*"
        @in_multiline_comments = true
      elsif "#{prech}#{ch}" == "//"
        return value, ""
      elsif ch == "#"
        return value, ""
      elsif "#{prech}#{ch}".length > 1
        error = ErrorUtil::ParseLocalizedError::WrongFormat.new(file, lang, linenum)
        @errorlist.push(error)
        return value, ""
      else
        value << ch
      end
    end
  end
  return value, line[n..-1]
end

#remove_escape(lang, key, content) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/applocale/Core/ParserStringFile/parse_strings_file.rb', line 165

def remove_escape(lang, key, content)
  value = content
  if @injectobj.has_before_parse_from_locale
    value = @injectobj.load_before_parse_from_locale(lang.to_s, key,  value)
  end
  if @injectobj.has_parse_from_locale
    value = @injectobj.load_parse_from_locale(lang.to_s, key,  value)
  else
    value = ContentUtil.remove_escape(@platform, value)
  end
  if @injectobj.has_after_parse_from_locale
    value = @injectobj.load_after_parse_from_locale(lang.to_s, key,  value)
  end
  return value
end

#to_parse_files(langpathobj_list) ⇒ Object



20
21
22
23
24
# File 'lib/applocale/Core/ParserStringFile/parse_strings_file.rb', line 20

def to_parse_files(langpathobj_list)
  langpathobj_list.each do |langpathobj|
    self.to_parse_strings_file(langpathobj.lang, langpathobj.filepath)
  end
end

#to_parse_strings_file(lang, strings_filepath) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/applocale/Core/ParserStringFile/parse_strings_file.rb', line 26

def to_parse_strings_file(lang, strings_filepath)
  puts "Start to Parse strings file: \"#{strings_filepath}\" ...".green

  @in_multiline_comments = false
  keyrowno = {}
  linenum = 0
  begin

  IO.foreach(strings_filepath, mode: 'r:bom|utf-8') {|line|
    linenum += 1
    line.strip!
    if !@in_multiline_comments
      next if line.start_with?('#')
      next if line.start_with?('//')
    end
    if line.length <= 0
      next
    end
    while true

      key, line = parse_token(linenum, line, "=", lang, strings_filepath)
      line.strip!

      if not line.start_with?("=")
        if !@in_multiline_comments && line.length > 0
          error = ErrorUtil::ParseLocalizedError::WrongFormat.new(strings_filepath, lang, linenum)
          @errorlist.push(error)
        end
        break
      end
      line.slice!(0)

      value, line = parse_token(linenum, line, ";", lang, strings_filepath)
      line.strip!

      if line.start_with?(";")
        line.slice!(0)
      else
        error = ErrorUtil::ParseLocalizedError::WrongFormat.new(strings_filepath, lang, linenum)
        @errorlist.push(error)
        key = nil
        value = nil
        break
      end

      if !ValidKey.is_validkey(@platform, key)
        error = ErrorUtil::ParseLocalizedError::InvalidKey.new(key, strings_filepath, lang, linenum)
        @errorlist.push(error)
        break
      end
      if @strings_keys[key].nil?
        @strings_keys[key] = Hash.new
        @keys_list.push(key)
      end
      if @strings_keys[key][lang.to_s].nil?
        @strings_keys[key][lang.to_s] = Hash.new
        @strings_keys[key][lang.to_s][:rowno] = linenum
        @strings_keys[key][lang.to_s][:value] = self.remove_escape(lang, key, value)
        keyrowno[key] = linenum
      else
        error = ErrorUtil::ParseLocalizedError::DuplicateKey.new(key, keyrowno[key], strings_filepath, lang, linenum)
        @errorlist.push(error)
      end
      if line.length <= 0
        break
      end
    end
  }
  rescue Exception => e
    puts e.message
    ErrorUtil::ParseLocalizedError::InvalidFile.new(strings_filepath).raise
  end
end