Class: Applocale::CompareStringFile

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(platform, file1, file2, lang = nil, print_result = true) ⇒ CompareStringFile

Returns a new instance of CompareStringFile.



8
9
10
11
12
13
14
15
16
# File 'lib/applocale/Core/CompareStringFile/compare_string_file.rb', line 8

def initialize(platform, file1, file2, lang = nil, print_result = true)
  @platform = platform
  @file1 = file1
  @file2 = file2
  @errorlist = Array.new()
  @platform = platform
  @print_result = print_result
  @lang = lang
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

#langObject (readonly)

Returns the value of attribute lang.



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

def lang
  @lang
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

Returns the value of attribute print_result.



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

def print_result
  @print_result
end

Class Method Details

.compare(lang_path_obj) ⇒ Object



18
19
20
# File 'lib/applocale/Core/CompareStringFile/compare_string_file.rb', line 18

def self.compare(lang_path_obj)
  Applocale::CompareStringFile.new(lang_path_obj.platform, lang_path_obj.filepath1, lang_path_obj.filepath2, lang_path_obj.lang, false).compare
end

Instance Method Details

#compareObject



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

def compare
  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


  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
  missingKeyInObj1 = nobj2.keys
  notSameKeys = notSame.keys
  comparison_result = Applocale::Config::LangPathComparisonResult.init(@platform, @lang, @file1, @file2, notSameKeys, duplicateKey, mismatch, missingKeyInObj1, missingkeyInObj2)

  if @print_result
    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 missingKeyInObj1
  end

  comparison_result
end

#parse_aos_file(strings_path) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/applocale/Core/CompareStringFile/compare_string_file.rb', line 81

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



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/applocale/Core/CompareStringFile/compare_string_file.rb', line 114

def parse_ios_file(path)
  strings_keys = {}
  begin
  IO.foreach(path, mode: 'r:bom|utf-8') {|line|
    line.strip!
    match = line.match(/"(.*?)" = "(.*)";/)
    unless match.nil?
      key = match.captures[0]
      value = match.captures[1]
      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



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

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