/
XSLTTransformationHandler.java
124 lines (116 loc) · 4.82 KB
/
XSLTTransformationHandler.java
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
package org.jboss.windup.rules.apps.xml.confighandler;
import static org.joox.JOOX.$;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.jboss.windup.config.exception.ConfigurationException;
import org.jboss.windup.config.parser.ElementHandler;
import org.jboss.windup.config.parser.NamespaceElementHandler;
import org.jboss.windup.config.parser.ParserContext;
import org.jboss.windup.rules.apps.xml.operation.xslt.XSLTTransformation;
import org.jboss.windup.util.exception.WindupException;
import org.ocpsoft.rewrite.config.Condition;
import org.w3c.dom.Element;
/**
* Represents an {@link XSLTTransformation} {@link Condition}.
*
* Example:
*
* <pre>
* <xslt description="weblogic.xml converted to jboss.xml" extension="-transformed-file.xml" xsltFile="path/to/xsltfile"/>
* </pre>
*
* @author jsightler <jesse.sightler@gmail.com>
*
*/
@NamespaceElementHandler(elementName = "xslt", namespace = "http://windup.jboss.org/v1/xml")
public class XSLTTransformationHandler implements ElementHandler<XSLTTransformation>
{
@Override
public XSLTTransformation processElement(ParserContext handlerManager, Element element)
throws ConfigurationException
{
String description = $(element).attr("description");
String extension = $(element).attr("extension");
String template = $(element).attr("template");
String of = $(element).attr("of");
if (StringUtils.isBlank(description))
{
throw new WindupException("Error, 'xslt' element must have a non-empty 'description' attribute");
}
if (StringUtils.isBlank(template))
{
throw new WindupException("Error, 'xslt' element must have a non-empty 'template' attribute");
}
if (StringUtils.isBlank(extension))
{
throw new WindupException("Error, 'xslt' element must have a non-empty 'extension' attribute");
}
Map<String, String> parameters = new HashMap<String, String>();
List<Element> children = $(element).children("xslt-parameter").get();
for (Element child : children)
{
XSLTParameter param = handlerManager.processElement(child);
parameters.put(param.getKey(), param.getValue());
}
Path pathContainingXml = handlerManager.getXmlInputRootPath();
if (pathContainingXml != null)
{
String fullPath;
if (template.startsWith("/") || template.startsWith("\\"))
{
fullPath = template;
}
else
{
Path path = pathContainingXml.resolve(template).toAbsolutePath();
// If we can't find it at the default location, try relative to the rule xml file itself
if (!Files.exists(path))
{
// This contains the parent of the rule itself (eg, /path/to/rules/rule.windup.xml would result in /path/to/rules/)
Path rulesParentPath = handlerManager.getXmlInputPath().getParent();
fullPath = rulesParentPath.resolve(template).normalize().toAbsolutePath().toString();
}
else
{
fullPath = path.normalize().toString();
}
}
if (of != null)
{
return (XSLTTransformation) XSLTTransformation
.of(of)
.usingFilesystem(fullPath)
.withDescription(description)
.withExtension(extension)
.withParameters(parameters);
}
return (XSLTTransformation) XSLTTransformation
.usingFilesystem(fullPath)
.withDescription(description)
.withExtension(extension)
.withParameters(parameters);
}
else
{
ClassLoader xmlFileAddonClassLoader = handlerManager.getAddonContainingInputXML().getClassLoader();
if (of != null)
{
return (XSLTTransformation) XSLTTransformation
.of(of)
.using(template, xmlFileAddonClassLoader)
.withDescription(description)
.withExtension(extension)
.withParameters(parameters);
}
return (XSLTTransformation) XSLTTransformation
.using(template, xmlFileAddonClassLoader)
.withDescription(description)
.withExtension(extension)
.withParameters(parameters);
}
}
}