Class: Applocale::CompareStringFile

Inherits:
Object
  • Object
show all
Defined in:
lib/applocale/Core/CompareStringFile/compare_string_file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(platform, file1, file2) ⇒ CompareStringFile

Returns a new instance of CompareStringFile.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/applocale/Core/CompareStringFile/compare_string_file.rb', line 8

def initialize(platform, file1, file2)
  @platform = platform
  @file1 = file1
  @file12 = file2


  @errorlist = Array.new()
  @platform = platform

  obj1 = {}
  obj2 = {}
  if platform == Platform::IOS
    obj1 = parse_ios_file(file1)
    obj2 = parse_ios_file(file2)
  elsif platform == Platform::ANDROID
    obj1 = parse_aos_file(file1)
    obj2 = parse_aos_file(file2)
  end
  compare(obj1,obj2)
end

Instance Attribute Details

#errorlistObject (readonly)

Returns the value of attribute errorlist.



6
7
8
# File 'lib/applocale/Core/CompareStringFile/compare_string_file.rb', line 6

def errorlist
  @errorlist
end

#file1Object (readonly)

Returns the value of attribute file1.



6
7
8
# File 'lib/applocale/Core/CompareStringFile/compare_string_file.rb', line 6

def file1
  @file1
end

#file2Object (readonly)

Returns the value of attribute file2.



6
7
8
# File 'lib/applocale/Core/CompareStringFile/compare_string_file.rb', line 6

def file2
  @file2
end

#in_multiline_commentsObject (readonly)

Returns the value of attribute in_multiline_comments.



6
7
8
# File 'lib/applocale/Core/CompareStringFile/compare_string_file.rb', line 6

def in_multiline_comments
  @in_multiline_comments
end

#platformObject (readonly)

Returns the value of attribute platform.



6
7
8
# File 'lib/applocale/Core/CompareStringFile/compare_string_file.rb', line 6

def platform
  @platform
end

Instance Method Details

#compare(obj1, obj2) ⇒ Object



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
# File 'lib/applocale/Core/CompareStringFile/compare_string_file.rb', line 30

def compare(obj1, obj2)
  missingkeyInObj2 = Array.new
  mismatch = Array.new
  duplicateKey = Array.new
  notSame = {}
  nobj2 = obj2
  obj1.each do |key, value|
    if nobj2[key].nil?
      missingkeyInObj2.push(key)
    else
      obj1Value = value
      obj2Value = obj2[key]
      if obj1Value.length != obj2Value.length
        mismatch.push(key)
      elsif obj1Value.length != 1
        duplicateKey.push(key)
      elsif obj1Value[0] != obj2Value[0]
        notSame[key] = {obj1: obj1Value[0],obj2: obj2Value[0]}
      end
    end
    nobj2.delete(key)
  end
  puts "==> not Same value:"
  notSame.each do |key, value|
    puts "key = #{key}"
    puts "#{value[:obj1]}<"
    puts "#{value[:obj2]}<"
  end
  puts "==> duplicateKey"
  puts duplicateKey
  puts "==> mismatch"
  puts mismatch
  puts "==> missingkeyInObj2"
  puts missingkeyInObj2
  puts "==> missingKeyInObj1"
  puts nobj2
end

#parse_aos_file(strings_path) ⇒ Object



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
99
100
101
# File 'lib/applocale/Core/CompareStringFile/compare_string_file.rb', line 70

def parse_aos_file(strings_path)
  strings_keys = {}

  return if !File.exist? strings_path
  puts "Start to Parse xml file: \"#{strings_path}\" ...".green

  xml_doc = Nokogiri::XML(File.open(strings_path))
  string_nodes = xml_doc.xpath("//string")
  string_nodes.each do |node|
    key = node["name"]
    value = node.content
    if !key.nil? && key.strip.length > 0
      if strings_keys[key].nil?
        strings_keys[key] = Array.new
      end
      strings_keys[key].push(value)

      # 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][:value] = self.remove_escape(lang, key, value)
      # else
      #   error = ErrorUtil::ParseLocalizedError::DuplicateKey.new(key, -1, strings_path, lang, -1).raise
      # end
    end
  end
  return strings_keys

end

#parse_ios_file(path) ⇒ Object



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
# File 'lib/applocale/Core/CompareStringFile/compare_string_file.rb', line 103

def parse_ios_file(path)
  strings_keys = {}
  @in_multiline_comments = false
  linenum = 0
  begin
  IO.foreach(path, 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, "=", path)
      line.strip!

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

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

      if line.start_with?(";")
        line.slice!(0)
      else
        error = ErrorUtil::ParseLocalizedError::WrongFormat.new(path, "", linenum).raise
        key = nil
        value = nil
        break
      end
      if strings_keys[key].nil?
        strings_keys[key] = Array.new
      end
      strings_keys[key].push(value)
    end
  }
  rescue Exception => e
    puts e.message
    ErrorUtil::ParseLocalizedError::InvalidFile.new(file1).raise
  end

  return strings_keys
end

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



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/applocale/Core/CompareStringFile/compare_string_file.rb', line 157

def parse_token(linenum, line, sep, 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, "", linenum)
        @errorlist.push(error)
        return value, ""
      else
        value << ch
      end
    end
  end
  return value, line[n..-1]
end