-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathparse.go
172 lines (157 loc) · 4.25 KB
/
parse.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
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
99
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
164
165
166
167
168
169
170
171
172
// Copyright (c) 2014 Soichiro Kashima
// Licensed under MIT license.
package main
import (
"encoding/xml"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"
)
func parse(opt *Options) {
// Clean and create output directory if needed
if opt.Clean {
// Discard all files in the output directory
os.RemoveAll(opt.OutDir)
}
os.MkdirAll(opt.OutDir, 0777)
// Parse all of the files in res/values/*.xml
var res Resources
if opt.Localize {
resSubDirs, _ := ioutil.ReadDir(opt.ResDir)
for _, valuesDir := range resSubDirs {
// Get only values directories
if matched, _ := regexp.MatchString("^values", valuesDir.Name()); !matched {
continue
}
var lang string
var r Resources
if valuesDir.Name() == "values" {
// Base language
lang = "Base"
r = parseLang(opt, filepath.Join(opt.ResDir, valuesDir.Name()))
// Output only base language to Objective-C source
res = r
} else {
re := regexp.MustCompile("values-([a-zA-Z]+)")
groups := re.FindStringSubmatch(valuesDir.Name())
if groups == nil {
// Not supported
continue
} else {
// Maybe supported language
lang = groups[1]
}
r = parseLang(opt, filepath.Join(opt.ResDir, valuesDir.Name()))
}
// Create R.strings
printLocalizableStrings(&r, opt, lang)
}
} else {
valuesDir := filepath.Join(opt.ResDir, "values")
res = parseLang(opt, valuesDir)
}
resD := parseDrawables(opt)
if 0 < len(resD.Drawables) {
res.Drawables = append(res.Drawables, resD.Drawables...)
}
printAsObjectiveC(&res, opt)
}
func parseLang(opt *Options, valuesDir string) (res Resources) {
files, _ := ioutil.ReadDir(valuesDir)
// Regular expressions for format replacement
expStr := regexp.MustCompile("%([^\\$]*\\$?)s")
for _, entry := range files {
if matched, _ := regexp.MatchString(".xml$", entry.Name()); !matched {
continue
}
entryPath := filepath.Join(valuesDir, entry.Name())
r := parseXml(entryPath)
if opt.Types["string"] {
if 0 < len(r.Strings) {
// Replacing Android format to that of Objective-C
for _, s := range r.Strings {
// Usually, we use NSString so %1$s should be converted to '%@'
s.Value = expStr.ReplaceAllString(s.Value, "%$1@")
res.Strings = append(res.Strings, s)
}
}
}
if opt.Types["integer"] {
if 0 < len(r.Integers) {
res.Integers = append(res.Integers, r.Integers...)
}
}
if opt.Types["color"] {
if 0 < len(r.Colors) {
res.Colors = append(res.Colors, r.Colors...)
}
}
if 0 < len(r.Items) {
for _, i := range r.Items {
if opt.Types["integer"] {
if i.Type == "integer" {
res.Integers = append(res.Integers, Integer{Name: i.Name, Value: i.Value})
}
}
}
}
if opt.Types["integer-array"] {
if 0 < len(r.IntegerArrays) {
res.IntegerArrays = append(res.IntegerArrays, r.IntegerArrays...)
}
}
if opt.Types["string-array"] {
if 0 < len(r.StringArrays) {
res.StringArrays = append(res.StringArrays, r.StringArrays...)
}
}
}
return res
}
func parseDrawables(opt *Options) (res Resources) {
if !opt.Types["drawable"] {
return
}
resSubDirs, _ := ioutil.ReadDir(opt.ResDir)
drawables := make(map[string]string)
for _, drawableDir := range resSubDirs {
// Get only drawable directory
if matched, _ := regexp.MatchString("^drawable", drawableDir.Name()); !matched {
continue
}
files, _ := ioutil.ReadDir(filepath.Join(opt.ResDir, drawableDir.Name()))
for _, entry := range files {
if matched, _ := regexp.MatchString(".(png|jpeg|jpg)$", strings.ToLower(entry.Name())); !matched {
continue
}
// Identify drawables without modifiers(@*)
basename := regexp.MustCompile("(@[^@]+)?\\.[a-zA-Z]+$").ReplaceAllString(entry.Name(), "")
if _, ok := drawables[basename]; ok {
// Already found
continue
}
drawables[basename] = basename
// Append new drawable name
res.Drawables = append(res.Drawables, Drawable{Name: basename})
}
}
return res
}
func parseXml(filename string) (res Resources) {
xmlFile, err := os.Open(filename)
if err != nil {
fmt.Println("Error opening file", err)
return
}
defer xmlFile.Close()
b, _ := ioutil.ReadAll(xmlFile)
err = xml.Unmarshal(b, &res)
if err != nil {
fmt.Println("Error unmarshaling XML file", err)
return
}
return res
}