1 package edu.jiangxin.apktoolbox.android.i18n;
2
3 import java.awt.HeadlessException;
4 import java.awt.event.ActionEvent;
5 import java.awt.event.ActionListener;
6 import java.io.File;
7 import java.io.FileFilter;
8 import java.io.IOException;
9 import java.io.Serial;
10 import java.util.ArrayList;
11 import java.util.List;
12 import java.util.regex.Matcher;
13 import java.util.regex.Pattern;
14
15 import javax.swing.Box;
16 import javax.swing.BoxLayout;
17 import javax.swing.JButton;
18 import javax.swing.JLabel;
19 import javax.swing.JPanel;
20 import javax.swing.JTextField;
21
22 import edu.jiangxin.apktoolbox.swing.extend.listener.SelectDirectoryListener;
23 import org.apache.commons.io.FileUtils;
24 import org.apache.commons.lang3.StringUtils;
25
26 import edu.jiangxin.apktoolbox.swing.extend.EasyPanel;
27 import edu.jiangxin.apktoolbox.utils.Constants;
28
29
30
31
32
33
34 public class I18nRemovePanel extends EasyPanel {
35 @Serial
36 private static final long serialVersionUID = 1L;
37
38 List<I18nFindLongestPanel.I18nInfo> infos = new ArrayList<>();
39
40 private static final String CHARSET = "UTF-8";
41
42 private JTextField srcTextField;
43
44 private JTextField itemTextField;
45
46 public I18nRemovePanel() throws HeadlessException {
47 super();
48 }
49
50 @Override
51 public void initUI() {
52 BoxLayout boxLayout = new BoxLayout(this, BoxLayout.Y_AXIS);
53 setLayout(boxLayout);
54
55 createSourcePanel();
56 add(Box.createVerticalStrut(Constants.DEFAULT_Y_BORDER));
57 createItemPanel();
58 add(Box.createVerticalStrut(Constants.DEFAULT_Y_BORDER));
59 createOperationPanel();
60 }
61
62 private void createOperationPanel() {
63 JPanel operationPanel = new JPanel();
64 operationPanel.setLayout(new BoxLayout(operationPanel, BoxLayout.X_AXIS));
65 add(operationPanel);
66
67 JButton removeButton = new JButton(bundle.getString("android.i18n.remove.title"));
68 removeButton.addActionListener(new ActionListener() {
69 @Override
70 public void actionPerformed(ActionEvent e) {
71 infos.clear();
72
73 String srcPath = checkAndGetDirContent(srcTextField, "android.i18n.remove.src.dir", "Source directory is invalid");
74 if (StringUtils.isEmpty(srcPath)) {
75 return;
76 }
77
78 String item = checkAndGetStringContent(itemTextField, "android.i18n.remove.items", "Items is empty");
79 if (StringUtils.isEmpty(item)) {
80 return;
81 }
82
83 remove(srcPath, itemTextField.getText());
84 }
85 });
86
87 operationPanel.add(removeButton);
88 }
89
90 private void createItemPanel() {
91 JPanel itemPanel = new JPanel();
92 itemPanel.setLayout(new BoxLayout(itemPanel, BoxLayout.X_AXIS));
93 add(itemPanel);
94
95 itemTextField = new JTextField();
96 itemTextField.setText(conf.getString("android.i18n.remove.items"));
97
98 JLabel itemLabel = new JLabel("Items");
99
100 itemPanel.add(itemTextField);
101 itemPanel.add(Box.createHorizontalGlue());
102 itemPanel.add(itemLabel);
103 }
104
105 private void createSourcePanel() {
106 JPanel sourcePanel = new JPanel();
107 sourcePanel.setLayout(new BoxLayout(sourcePanel, BoxLayout.X_AXIS));
108 add(sourcePanel);
109
110 srcTextField = new JTextField();
111 srcTextField.setText(conf.getString("android.i18n.remove.src.dir"));
112
113 JButton srcButton = new JButton("Source Directory");
114 srcButton.addActionListener(new SelectDirectoryListener("select a directory", srcTextField));
115
116 sourcePanel.add(srcTextField);
117 sourcePanel.add(Box.createHorizontalGlue());
118 sourcePanel.add(srcButton);
119 }
120
121 private void remove(String sourceBaseStr, String itemName) {
122 File[] sourceParentFiles = new File(sourceBaseStr).listFiles(new FileFilter() {
123 @Override
124 public boolean accept(File pathname) {
125 return pathname.getName().startsWith("values");
126 }
127 });
128 if (sourceParentFiles == null) {
129 logger.error("None valid directory found");
130 return;
131 }
132 int count = 0;
133 for (File sourceParentFile : sourceParentFiles) {
134 File sourceFile = new File(sourceParentFile, "strings.xml");
135 if (sourceFile.exists()) {
136 try {
137 System.out.println("read from: " + sourceFile.getCanonicalPath());
138 String content = FileUtils.readFileToString(sourceFile, CHARSET);
139 Pattern pattern = Pattern.compile("\\s*<string name=\"" + itemName + "\".*>.*</string>");
140 Matcher matcher = pattern.matcher(content);
141 String resultString = matcher.replaceAll("");
142 FileUtils.writeStringToFile(sourceFile, resultString, CHARSET);
143 logger.info("remove success, count: {}, and file: {}", ++count, sourceFile);
144 } catch (IOException e) {
145 logger.error("remove exception: {}", sourceFile);
146 }
147 }
148 }
149 }
150 }