CQPageHumanTranslator.java
13.1 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*
* 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.PathNotFoundException
* javax.jcr.Property
* javax.jcr.RepositoryException
* javax.jcr.Value
* 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.CQPageTranslator;
import com.adobe.cq.wcm.translation.impl.TranslationObjectImpl;
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.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import javax.jcr.PathNotFoundException;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Value;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class CQPageHumanTranslator
extends CQPageTranslator {
private Element currentPropertiesElement = null;
private Document currentDoc = null;
private static final Logger log = LoggerFactory.getLogger(CQPageHumanTranslator.class);
public CQPageHumanTranslator(TranslationRuleConfigurationFile ruleFile) {
super(ruleFile, null);
}
protected boolean translateNow() throws RepositoryException, TranslationException {
return false;
}
@Override
protected boolean processTagElement(javax.jcr.Node tagNode, String strTagAttributeName, String strSourceTagTitle) throws TranslationException, RepositoryException {
Boolean bMultiValue = false;
Element propElement = this.currentDoc.createElement(TranslationUtils.TRANSLATION_OBJECT_PROPERTY_NODE);
propElement.setAttribute(TranslationUtils.ATTRIBUTE_TRANSLATION_PROPERTY_NAME, strTagAttributeName);
propElement.setAttribute(TranslationUtils.ATTRIBUTE_TRANSLATION_NODE_PATH, tagNode.getPath());
propElement.setAttribute(TranslationUtils.ATTRIBUTE_TRANSLATION_MULTI_VALUE, bMultiValue.toString());
propElement.setTextContent(strSourceTagTitle);
this.currentPropertiesElement.appendChild(propElement);
return true;
}
@Override
protected boolean processNodeProperty(javax.jcr.Node node, Property prop, String strPropertyName) {
log.debug("FUNCTION: translateNodeProperty ", (Object)strPropertyName);
boolean bSave = false;
try {
Element propElement = this.currentDoc.createElement(TranslationUtils.TRANSLATION_OBJECT_PROPERTY_NODE);
Boolean bMultiValue = prop.isMultiple();
propElement.setAttribute(TranslationUtils.ATTRIBUTE_TRANSLATION_PROPERTY_NAME, strPropertyName);
propElement.setAttribute(TranslationUtils.ATTRIBUTE_TRANSLATION_NODE_PATH, node.getPath());
propElement.setAttribute(TranslationUtils.ATTRIBUTE_TRANSLATION_MULTI_VALUE, bMultiValue.toString());
if (bMultiValue.booleanValue()) {
Value[] val_array = prop.getValues();
if (val_array != null && val_array.length > 0) {
bSave = false;
for (int index = 0; index < val_array.length; ++index) {
String strStringToTranslate;
Value value = val_array[index];
if (value == null || value.getType() != 1 || (strStringToTranslate = value.getString()) == null || strStringToTranslate.isEmpty()) continue;
Element valueElement = this.currentDoc.createElement(TranslationUtils.TRANSLATION_OBJECT_VALUE_NODE);
valueElement.setTextContent(strStringToTranslate);
valueElement.setAttribute(TranslationUtils.ATTRIBUTE_TRANSLATION_ORDER, new Integer(index).toString());
propElement.appendChild(valueElement);
bSave = true;
}
}
} else {
String strStringToTranslate;
Value value = prop.getValue();
if (value != null && value.getType() == 1 && (strStringToTranslate = value.getString()) != null && !strStringToTranslate.isEmpty()) {
propElement.setTextContent(strStringToTranslate);
bSave = true;
}
}
if (bSave) {
this.currentPropertiesElement.appendChild(propElement);
}
}
catch (RepositoryException ex) {
// empty catch block
}
return bSave;
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public static String getSourcePathOrFileTypeFromFile(File file) {
String strSourcePathOrFileType;
strSourcePathOrFileType = null;
if (file != null && file.exists()) {
FileInputStream is = null;
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
is = new FileInputStream(file);
Document doc = db.parse(is);
Node rootNode = doc.getFirstChild();
Element rootElement = (Element)rootNode;
if (TranslationUtils.TRANSLATION_OBJECT_FILE_NODE.equals(rootElement.getNodeName())) {
if (rootElement.hasAttribute("sourcePath")) {
strSourcePathOrFileType = rootElement.getAttribute("sourcePath");
} else if (rootElement.hasAttribute("fileType")) {
strSourcePathOrFileType = rootElement.getAttribute("fileType");
}
}
}
catch (Exception e) {
log.trace("getSourcePathOrFileTypeFromFile Error while parsing file " + file.getAbsolutePath());
}
finally {
if (is != null) {
try {
is.close();
}
catch (IOException e) {}
}
}
}
return strSourcePathOrFileType;
}
public void importInputStream(InputStream inputStream, Resource cqPageResource, ResourceResolver resourceResolver, String strObjectType, ArrayList<String> sourceFilePath, boolean bTagMetadata) throws ParserConfigurationException, SAXException, IOException, PathNotFoundException, RepositoryException {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(inputStream);
Node rootNode = doc.getFirstChild();
Element rootElement = (Element)rootNode;
this.rootPage = cqPageResource;
if (TranslationUtils.TRANSLATION_OBJECT_FILE_NODE.equals(rootElement.getNodeName())) {
String strSourcePath = rootElement.getAttribute("sourcePath");
String strFileType = rootElement.getAttribute("fileType");
boolean bValid = false;
if (this.rootPage != null && this.rootPage.getPath().equals(strSourcePath)) {
bValid = true;
} else if (strFileType != null && !strFileType.isEmpty() && strFileType.equals(strObjectType)) {
bValid = true;
}
if (bValid) {
NodeList rootChildNodeList = rootNode.getChildNodes();
for (int rootIndex = 0; rootIndex < rootChildNodeList.getLength(); ++rootIndex) {
Node propertiesNode = rootChildNodeList.item(rootIndex);
if (propertiesNode == null || !TranslationUtils.TRANSLATION_OBJECT_PROPERTIES_NODE.equals(propertiesNode.getNodeName())) continue;
NodeList nodeList = propertiesNode.getChildNodes();
for (int index = 0; index < nodeList.getLength(); ++index) {
Node childNode = nodeList.item(index);
this.importChildProperty(childNode, resourceResolver, sourceFilePath, bTagMetadata);
}
}
}
}
}
private void importChildProperty(Node childNode, ResourceResolver resourceResolver, ArrayList<String> sourceFilePath, boolean bTagMetadata) throws PathNotFoundException, RepositoryException {
javax.jcr.Node currentNode;
Element childElement;
String nodePath;
Resource resource;
if (childNode != null && TranslationUtils.TRANSLATION_OBJECT_PROPERTY_NODE.equals(childNode.getNodeName()) && (this.isNodePathPresentInSourceList(sourceFilePath, nodePath = (childElement = (Element)childNode).getAttribute(TranslationUtils.ATTRIBUTE_TRANSLATION_NODE_PATH)) || bTagMetadata && TranslationUtils.isETCTagsPath(nodePath)) && (resource = resourceResolver.getResource(nodePath)) != null && (currentNode = (javax.jcr.Node)resource.adaptTo(javax.jcr.Node.class)) != null) {
String propertyName = childElement.getAttribute(TranslationUtils.ATTRIBUTE_TRANSLATION_PROPERTY_NAME);
String isMultiValue = childElement.getAttribute(TranslationUtils.ATTRIBUTE_TRANSLATION_MULTI_VALUE);
boolean bMultiValue = "true".equals(isMultiValue);
if (currentNode.hasProperty(propertyName)) {
Property prop = currentNode.getProperty(propertyName);
if (prop != null && bMultiValue == prop.isMultiple()) {
if (bMultiValue) {
NodeList nodeList = childNode.getChildNodes();
ArrayList<String> strValArray = new ArrayList<String>();
for (int index = 0; index < nodeList.getLength(); ++index) {
Node valueNode = nodeList.item(index);
if (!TranslationUtils.TRANSLATION_OBJECT_VALUE_NODE.equals(valueNode.getNodeName())) continue;
strValArray.add(valueNode.getTextContent());
}
String[] valueArray = TranslationUtils.getStringArray(strValArray);
prop.setValue(valueArray);
} else {
prop.setValue(childElement.getTextContent());
}
}
} else if (bTagMetadata) {
currentNode.setProperty(propertyName, childElement.getTextContent());
}
}
}
private boolean isNodePathPresentInSourceList(ArrayList<String> sourceFilePath, String nodePath) {
boolean bRetVal = false;
if (sourceFilePath != null) {
for (String strSourcePath : sourceFilePath) {
if (nodePath.indexOf(strSourcePath) != 0) continue;
bRetVal = true;
break;
}
}
return bRetVal;
}
public Document generateXMLForResourceList(ArrayList<Resource> resourceList, TranslationObjectImpl.TranslationObjectType objectType, String strSourcePath) throws ParserConfigurationException, RepositoryException, TranslationException, TransformerException, IOException {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = null;
this.currentDoc = db.newDocument();
Element rootElement = this.currentDoc.createElement(TranslationUtils.TRANSLATION_OBJECT_FILE_NODE);
if (objectType != null) {
rootElement.setAttribute("fileType", objectType.toString());
}
if (strSourcePath != null) {
rootElement.setAttribute("sourcePath", strSourcePath);
}
this.currentDoc.appendChild(rootElement);
this.currentPropertiesElement = this.currentDoc.createElement(TranslationUtils.TRANSLATION_OBJECT_PROPERTIES_NODE);
ArrayList<String> tagList = new ArrayList<String>();
for (Resource currentRoot : resourceList) {
if (objectType == TranslationObjectImpl.TranslationObjectType.TAGMETADATA) {
this.processTagTranslationNode((javax.jcr.Node)currentRoot.adaptTo(javax.jcr.Node.class), tagList);
continue;
}
this.processTranslationOrTemporaryUpdateNode((javax.jcr.Node)currentRoot.adaptTo(javax.jcr.Node.class));
}
if (objectType == TranslationObjectImpl.TranslationObjectType.TAGMETADATA) {
this.processTagListNow(tagList);
}
rootElement.appendChild(this.currentPropertiesElement);
document = this.currentDoc;
this.currentPropertiesElement = null;
this.currentDoc = null;
return document;
}
}