I18nStringExtractor.java
7.98 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.granite.ui.clientlibs.ClientLibrary
* com.adobe.granite.ui.clientlibs.HtmlLibraryManager
* com.adobe.granite.ui.clientlibs.LibraryType
* javax.jcr.Binary
* javax.jcr.Node
* javax.jcr.Property
* javax.jcr.RepositoryException
* org.apache.commons.io.IOUtils
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.wcm.translation.impl;
import com.adobe.cq.wcm.translation.impl.I18nContentProcessor;
import com.adobe.cq.wcm.translation.impl.I18nDictionaryTranslator;
import com.adobe.cq.wcm.translation.impl.I18nHandlebarsProcessor;
import com.adobe.cq.wcm.translation.impl.I18nJSPProcessor;
import com.adobe.cq.wcm.translation.impl.I18nJavaScriptProcessor;
import com.adobe.cq.wcm.translation.impl.I18nSightlyProcessor;
import com.adobe.cq.wcm.translation.impl.TranslationRuleConfigurationFile;
import com.adobe.cq.wcm.translation.impl.TranslationUtils;
import com.adobe.granite.ui.clientlibs.ClientLibrary;
import com.adobe.granite.ui.clientlibs.HtmlLibraryManager;
import com.adobe.granite.ui.clientlibs.LibraryType;
import java.io.BufferedInputStream;
import java.io.InputStream;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.jcr.Binary;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import org.apache.commons.io.IOUtils;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class I18nStringExtractor {
private static final Logger logger = LoggerFactory.getLogger(I18nDictionaryTranslator.class);
private static final String JS_TXT = "js.txt";
private HashSet<String> filePathSet = new HashSet();
private HashSet<String> clientLibCategorySet = new HashSet();
private TranslationRuleConfigurationFile ruleFile = null;
private ResourceResolver resourceResolver = null;
private I18nContentProcessor jspProcessor = null;
private I18nContentProcessor sightlyProcessor = null;
private I18nContentProcessor javaScriptProcessor = null;
private I18nContentProcessor handlebarsProcessor = null;
public I18nStringExtractor(HashSet<String> existingI18nStringSet, TranslationRuleConfigurationFile ruleFile, ResourceResolver resourceResolver) {
this.ruleFile = ruleFile;
this.resourceResolver = resourceResolver;
this.jspProcessor = new I18nJSPProcessor(this, existingI18nStringSet, resourceResolver);
this.sightlyProcessor = new I18nSightlyProcessor(this, existingI18nStringSet, resourceResolver);
this.javaScriptProcessor = new I18nJavaScriptProcessor(this, existingI18nStringSet, resourceResolver);
this.handlebarsProcessor = new I18nHandlebarsProcessor(this, existingI18nStringSet, resourceResolver);
}
private I18nContentProcessor getContentProcessor(Node fileNode) {
if (TranslationUtils.checkFileExtension(fileNode, ".jsp")) {
return this.jspProcessor;
}
if (TranslationUtils.checkFileExtension(fileNode, ".html")) {
return this.sightlyProcessor;
}
if (TranslationUtils.checkFileExtension(fileNode, ".js")) {
return this.javaScriptProcessor;
}
if (TranslationUtils.checkFileExtension(fileNode, ".hbs")) {
return this.handlebarsProcessor;
}
return null;
}
public boolean extractAllI18nStrings(HashMap<String, String> i18nStringMap) {
boolean bSave = false;
this.getFilesFromClientLibCategories();
for (String file : this.filePathSet) {
try {
if (!this.extractI18nStringsFromFile((Node)this.resourceResolver.getResource(file).adaptTo(Node.class), i18nStringMap)) continue;
bSave = true;
}
catch (Exception ignored) {}
}
return bSave;
}
private void getFilesFromClientLibCategories() {
if (this.ruleFile != null && this.ruleFile.htmlLibraryManager != null) {
Collection clientLibraries = this.ruleFile.htmlLibraryManager.getLibraries(this.clientLibCategorySet.toArray(new String[this.clientLibCategorySet.size()]), LibraryType.JS, true, true);
for (ClientLibrary clientLibrary : clientLibraries) {
try {
String path = clientLibrary.getPath();
if (!path.startsWith("/apps/")) continue;
this.processClientLibForFiles(this.resourceResolver.getResource(path));
}
catch (Exception ignored) {}
}
this.clientLibCategorySet.clear();
}
}
private void processClientLibForFiles(Resource clientLib) {
Node jsTxtNode = (Node)clientLib.getChild("js.txt").adaptTo(Node.class);
if (jsTxtNode != null) {
String jsTxtContent = I18nStringExtractor.getFileContentsFromNode(jsTxtNode);
String jsRootPath = this.getJSRelRootPath(jsTxtContent);
if (jsRootPath == null) {
return;
}
String regEx = "\"^\\\\s*([^#].*)\"";
Pattern pattern = Pattern.compile(regEx);
Matcher matcher = pattern.matcher(jsTxtContent);
while (matcher.find()) {
String jsRelPath = matcher.group(1);
this.filePathSet.add(jsRootPath + "/" + jsRelPath);
}
}
}
private String getJSRelRootPath(String jsTxtContent) {
String regEx = "^\\s*#base\\s*=\\s*(.*)";
Pattern pattern = Pattern.compile(regEx);
Matcher matcher = pattern.matcher(jsTxtContent);
if (matcher.find()) {
return matcher.group(1);
}
return null;
}
protected boolean addClientCategory(String category) {
return this.clientLibCategorySet.add(category);
}
private boolean extractI18nStringsFromFile(Node fileNode, HashMap<String, String> i18nStringMap) {
logger.trace("In function: extractI18nStringsFromFile");
I18nContentProcessor i18NContentProcessor = this.getContentProcessor(fileNode);
return i18NContentProcessor != null && i18NContentProcessor.processFileForI18nStrings(fileNode, i18nStringMap);
}
protected boolean addFileForStringExtraction(Node node) {
boolean bSave = false;
try {
if (this.filePathSet.add(node.getPath())) {
bSave = true;
this.addRelatedFilesForStringExtraction(node);
}
}
catch (RepositoryException ignored) {
// empty catch block
}
return bSave;
}
private boolean addRelatedFilesForStringExtraction(Node fileNode) {
I18nContentProcessor i18NContentProcessor = this.getContentProcessor(fileNode);
return i18NContentProcessor != null && i18NContentProcessor.processFileForIncludes(fileNode);
}
public static boolean isI18nContentStringExtractionRequired(Node node) throws RepositoryException {
return node.getPath().startsWith("/apps/") && (TranslationUtils.checkFileExtension(node, ".jsp") || TranslationUtils.checkFileExtension(node, ".html") || TranslationUtils.checkFileExtension(node, ".js") || TranslationUtils.checkFileExtension(node, ".hbs"));
}
public static String getFileContentsFromNode(Node jspNode) {
String result = null;
try {
Node ntResourceNode = jspNode.getNode("jcr:content");
InputStream inputStream = ntResourceNode.getProperty("jcr:data").getBinary().getStream();
BufferedInputStream bin = new BufferedInputStream(inputStream);
result = IOUtils.toString((InputStream)bin, (String)"UTF-8");
bin.close();
inputStream.close();
}
catch (Exception ignored) {
// empty catch block
}
return result;
}
}