I18nDictionaryTranslator.java
9.47 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.granite.translation.api.TranslationException
* com.adobe.granite.translation.api.TranslationService
* javax.jcr.Node
* javax.jcr.NodeIterator
* javax.jcr.Property
* javax.jcr.PropertyIterator
* javax.jcr.RepositoryException
* org.apache.commons.lang3.StringUtils
* 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.I18nDictionary;
import com.adobe.cq.wcm.translation.impl.I18nStringExtractor;
import com.adobe.cq.wcm.translation.impl.TranslationRuleConfigurationFile;
import com.adobe.cq.wcm.translation.impl.TranslationUtils;
import com.adobe.granite.translation.api.TranslationException;
import com.adobe.granite.translation.api.TranslationService;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.Property;
import javax.jcr.PropertyIterator;
import javax.jcr.RepositoryException;
import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
class I18nDictionaryTranslator {
private static final Logger logger = LoggerFactory.getLogger(I18nDictionaryTranslator.class);
protected TranslationService tSvc;
protected TranslationRuleConfigurationFile ruleFile;
protected ResourceResolver resourceResolver;
protected I18nStringExtractor i18nStringExtractor;
protected HashSet<String> existingI18nStringSet;
protected HashSet<String> componentPathSet = new HashSet();
public I18nDictionaryTranslator(TranslationRuleConfigurationFile ruleFile, TranslationService tSvc, ResourceResolver resourceResolver) {
this.tSvc = tSvc;
logger.trace("In function: I18nDictionaryTranslator");
this.ruleFile = ruleFile;
this.resourceResolver = resourceResolver;
this.existingI18nStringSet = I18nDictionary.getAllI18nDictKeys(resourceResolver, ruleFile.getDestinationLanguage(), null);
this.i18nStringExtractor = new I18nStringExtractor(this.existingI18nStringSet, ruleFile, resourceResolver);
}
protected boolean processI18nDictTranslationNode(Resource i18NDict) throws RepositoryException, TranslationException, IOException {
logger.trace("In function: processI18nDictTranslationNode");
if (TranslationUtils.checkFileExtension((Node)i18NDict.adaptTo(Node.class), ".json")) {
return this.processI18nJson(i18NDict);
}
return this.processI18nJcrNodes(i18NDict);
}
private void processChildNodesForComponents(Node node) {
try {
NodeIterator nodesIterator = node.getNodes();
if (nodesIterator != null) {
while (nodesIterator.hasNext()) {
Node childNode = nodesIterator.nextNode();
if (childNode.isNodeType("cq:Page")) continue;
this.processSlingResourceTypeForComponents(childNode);
}
}
}
catch (Exception ignored) {
// empty catch block
}
}
private static boolean isResourceStringExtractionRequired(Resource resource) {
return resource.getPath().startsWith("/apps/");
}
private void processSlingResourceTypeForComponents(Node node) {
try {
if (I18nStringExtractor.isI18nContentStringExtractionRequired(node)) {
this.i18nStringExtractor.addFileForStringExtraction(node);
} else {
Resource resource;
String resourceType = TranslationUtils.getStringAttribute(logger, node, "sling:resourceType", "");
if (!StringUtils.isEmpty((CharSequence)resourceType) && I18nDictionaryTranslator.isResourceStringExtractionRequired(resource = this.resourceResolver.getResource(resourceType))) {
if (logger.isTraceEnabled()) {
logger.trace("Resource Type: {}", (Object)resourceType);
logger.trace("Path: {}", (Object)resource.getPath());
}
if (this.componentPathSet.add(resource.getPath())) {
this.processSlingResourceTypeForComponents((Node)resource.adaptTo(Node.class));
}
}
}
}
catch (Exception ignored) {
// empty catch block
}
this.processSlingResourceSuperTypeForComponents(node);
this.processChildNodesForComponents(node);
}
protected HashMap<String, String> extractComponentStringMapFromResourceList(ArrayList<Resource> resourceList) {
logger.trace("In function: getI18nComponentStringDict");
if (resourceList.size() > 0) {
try {
this.extractTranslatableComponentListFromResourceList(resourceList);
if (logger.isTraceEnabled()) {
logger.trace("Components:{}", (Object)this.componentPathSet.toString());
}
HashMap<String, String> componentStringMap = new HashMap<String, String>();
for (String componentPath : this.componentPathSet) {
Resource componentResource = this.resourceResolver.getResource(componentPath);
if (componentResource == null) continue;
this.extractTranslatableStringsFromNodeProperties((Node)componentResource.adaptTo(Node.class), componentStringMap, this.existingI18nStringSet);
}
this.i18nStringExtractor.extractAllI18nStrings(componentStringMap);
return componentStringMap;
}
catch (Exception e) {
logger.error("Can't get component strings");
return null;
}
}
return null;
}
private void processSlingResourceSuperTypeForComponents(Node node) {
try {
Resource resource;
String resourceSuperType = TranslationUtils.getStringAttribute(logger, node, "sling:resourceSuperType", "");
if (!StringUtils.isEmpty((CharSequence)resourceSuperType) && I18nDictionaryTranslator.isResourceStringExtractionRequired(resource = this.resourceResolver.getResource(resourceSuperType))) {
if (logger.isTraceEnabled()) {
logger.trace("Resource Super Type: {}", (Object)resourceSuperType);
logger.trace("Path: {}", (Object)resource.getPath());
}
if (this.componentPathSet.add(resource.getPath())) {
this.processSlingResourceTypeForComponents((Node)resource.adaptTo(Node.class));
}
}
}
catch (Exception ignored) {
// empty catch block
}
}
private void extractTranslatableComponentListFromResourceList(ArrayList<Resource> resourceList) throws RepositoryException, TranslationException, IOException {
if (resourceList.size() > 0) {
for (Resource resource : resourceList) {
this.processSlingResourceTypeForComponents((Node)resource.adaptTo(Node.class));
}
}
}
private boolean extractTranslatableStringsFromNodeProperties(Node node, HashMap<String, String> extractedStringMap, HashSet<String> existingI18nStringSet) throws TranslationException, IOException {
boolean bSave = false;
try {
PropertyIterator propIterator = node.getProperties();
ArrayList<String> translationList = this.ruleFile.getTranslatablePropertyListForNode(node);
while (propIterator.hasNext()) {
String componentString;
Property prop = propIterator.nextProperty();
String strPropertyName = prop.getName();
if (translationList == null || !translationList.contains(strPropertyName) || extractedStringMap.containsKey(componentString = prop.getString()) || existingI18nStringSet.contains(componentString)) continue;
extractedStringMap.put(componentString, componentString);
bSave = true;
}
bSave = this.extractComponentStringsFromChildNodes(node, extractedStringMap, existingI18nStringSet);
}
catch (RepositoryException e) {
logger.error("extractComponentStringsFromNode: Error processing component strings", (Throwable)e);
}
return bSave;
}
private boolean extractComponentStringsFromChildNodes(Node node, HashMap<String, String> componentStringMap, HashSet<String> existingI18nStringSet) throws TranslationException, IOException {
boolean bSave = false;
try {
NodeIterator nodesIterator = node.getNodes();
if (nodesIterator != null) {
while (nodesIterator.hasNext()) {
Node childNode = nodesIterator.nextNode();
this.extractTranslatableStringsFromNodeProperties(childNode, componentStringMap, existingI18nStringSet);
bSave = true;
}
}
}
catch (Exception ignored) {
// empty catch block
}
return bSave;
}
protected boolean processI18nJson(Resource i18nJsonDict) {
return false;
}
protected boolean processI18nJcrNodes(Resource i18nJcrDict) {
return false;
}
}