Changed Homepage to Frontend and fixed typo
This commit is contained in:
21
Backend/xslt-rest/Dockerfile
Normal file
21
Backend/xslt-rest/Dockerfile
Normal file
@@ -0,0 +1,21 @@
|
||||
# Using maven image, based on java 8
|
||||
FROM maven:3.6.3-jdk-8 as target
|
||||
WORKDIR build
|
||||
COPY ./ ./
|
||||
RUN mvn -ntp package
|
||||
|
||||
|
||||
# Go to working directory in docker image
|
||||
# WORKDIR /usr/app/
|
||||
|
||||
FROM openjdk:slim-buster
|
||||
COPY --from=target /build/target/*jar-with-dependencies.jar /app/REST_XSLT.jar
|
||||
|
||||
# Download dependencies during image build
|
||||
# (RUN - works on image creation)
|
||||
# (CMD - works on container start)
|
||||
# RUN mvn dependency:resolve
|
||||
# RUN mvn dependency:go-offline
|
||||
|
||||
# On container creation - run maven tests
|
||||
CMD exec java -jar /app/REST_XSLT.jar
|
||||
140
Backend/xslt-rest/pom.xml
Normal file
140
Backend/xslt-rest/pom.xml
Normal file
@@ -0,0 +1,140 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.r11.tools</groupId>
|
||||
<artifactId>xslt-rest</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>11</maven.compiler.source>
|
||||
<maven.compiler.target>11</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<jackson.version>2.14.1</jackson.version>
|
||||
<slf4j.version>2.0.6</slf4j.version>
|
||||
</properties>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<!-- <plugin>-->
|
||||
<!-- <artifactId>maven-jar-plugin</artifactId>-->
|
||||
<!-- <version>3.1.0</version>-->
|
||||
<!-- <configuration>-->
|
||||
<!-- <archive>-->
|
||||
<!-- <manifest>-->
|
||||
<!-- <addClasspath>true</addClasspath>-->
|
||||
<!-- <classpathPrefix>lib/</classpathPrefix>-->
|
||||
<!-- <mainClass>com.r11.tools.xslt.Main</mainClass>-->
|
||||
<!-- </manifest>-->
|
||||
<!-- </archive>-->
|
||||
<!-- </configuration>-->
|
||||
<!-- </plugin>-->
|
||||
|
||||
<plugin>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.8.1</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<artifactId>maven-assembly-plugin</artifactId>
|
||||
<version>3.3.0</version>
|
||||
|
||||
<configuration>
|
||||
<archive>
|
||||
<manifest>
|
||||
<addClasspath>true</addClasspath>
|
||||
<classpathPrefix>lib/</classpathPrefix>
|
||||
<mainClass>com.r11.tools.xslt.Main</mainClass>
|
||||
</manifest>
|
||||
</archive>
|
||||
<descriptorRefs>
|
||||
<descriptorRef>jar-with-dependencies</descriptorRef>
|
||||
</descriptorRefs>
|
||||
</configuration>
|
||||
|
||||
<executions>
|
||||
<execution>
|
||||
<id>make-assembly</id>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>single</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<dependencies>
|
||||
<!-- Spark -->
|
||||
<dependency>
|
||||
<groupId>com.sparkjava</groupId>
|
||||
<artifactId>spark-core</artifactId>
|
||||
<version>2.9.3</version>
|
||||
</dependency>
|
||||
|
||||
<!-- JSON -->
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-core</artifactId>
|
||||
<version>${jackson.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
<version>${jackson.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- XSLT -->
|
||||
<dependency>
|
||||
<groupId>net.sf.saxon</groupId>
|
||||
<artifactId>Saxon-HE</artifactId>
|
||||
<version>11.4</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>xalan</groupId>
|
||||
<artifactId>xalan</artifactId>
|
||||
<version>2.7.2</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Logging -->
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-simple</artifactId>
|
||||
<version>${slf4j.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>jcl-over-slf4j</artifactId>
|
||||
<version>${slf4j.version}</version>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
<version>${slf4j.version}</version>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-log4j12</artifactId>
|
||||
<version>${slf4j.version}</version>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.logging.log4j</groupId>
|
||||
<artifactId>log4j-core</artifactId>
|
||||
<version>2.19.0</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
</project>
|
||||
15
Backend/xslt-rest/src/main/java/com/r11/tools/xslt/Main.java
Normal file
15
Backend/xslt-rest/src/main/java/com/r11/tools/xslt/Main.java
Normal file
@@ -0,0 +1,15 @@
|
||||
package com.r11.tools.xslt;
|
||||
|
||||
/**
|
||||
* Application initializer
|
||||
* @author Wojciech Czop
|
||||
*/
|
||||
public class Main {
|
||||
/**
|
||||
* Initializes the application
|
||||
* @param args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
SparkInitializer.run();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,240 @@
|
||||
package com.r11.tools.xslt;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonMappingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.r11.tools.xslt.processors.Saxon;
|
||||
import com.r11.tools.xslt.processors.Xalan;
|
||||
import spark.*;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Class contains spark configuration and method initializing spark framework
|
||||
* @author Wojciech Czop
|
||||
*/
|
||||
public class SparkInitializer {
|
||||
/**
|
||||
* Initializes spark framework
|
||||
*/
|
||||
public static void run(){
|
||||
// TODO: Port value as property
|
||||
Spark.port(8081);
|
||||
|
||||
Spark.after((request, response) -> {
|
||||
response.header("Access-Control-Allow-Origin", "*");
|
||||
response.header("access-control-allow-headers", "*");
|
||||
response.header("access-control-expose-headers", "*");
|
||||
response.header("Access-Control-Allow-Methods", "POST");
|
||||
});
|
||||
|
||||
Spark.post("/xsltpost", xsltHandler);
|
||||
Spark.post("/xpathpost", xpathHandler);
|
||||
Spark.post("/xsdpost", xsdHandler);
|
||||
Spark.get("/procinfo", procinfoHandler);
|
||||
System.out.println("Server is online");
|
||||
}
|
||||
|
||||
/**
|
||||
* Handler that returns processor version
|
||||
*/
|
||||
private static Route procinfoHandler = (Request req, Response resp) -> {
|
||||
try {
|
||||
resp.header("processor", "Saxon " + Saxon.getVersion() + " over s9api");
|
||||
return Saxon.getVersion();
|
||||
} catch (Exception ex) {
|
||||
return ex.getMessage();
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Handler that returns info if document is valid
|
||||
* Also provides info about request time and processor
|
||||
*/
|
||||
private static Route xsdHandler = (Request req, Response resp) -> {
|
||||
String body = req.body();
|
||||
System.out.println("Request: " + body);
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
Map<String, String> requestMap = null;
|
||||
Map<String, String> responseMap = new HashMap<>();
|
||||
try {
|
||||
requestMap = mapper.readValue(body, Map.class);
|
||||
} catch (JsonMappingException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
|
||||
String data = requestMap.get("data");
|
||||
String xsd = requestMap.get("process");
|
||||
String processor = requestMap.get("processor");
|
||||
String version = requestMap.get("version");
|
||||
|
||||
resp.header("processor", Xalan.getVersion());
|
||||
long timeStart = System.currentTimeMillis();
|
||||
String tmp;
|
||||
try {
|
||||
tmp = Xalan.validate(data, xsd).trim();
|
||||
responseMap.put("result", tmp);
|
||||
responseMap.put("status", "OK");
|
||||
} catch (Exception ex) {
|
||||
System.out.println("Exception: "+ex.getMessage());
|
||||
responseMap.put("result", ex.getMessage());
|
||||
responseMap.put("status", "ERR");
|
||||
resp.status(400);
|
||||
}
|
||||
long duration = System.currentTimeMillis() - timeStart;
|
||||
responseMap.put("processor", Xalan.getVersion());
|
||||
responseMap.put("time", "" + duration);
|
||||
resp.body(mapper.writeValueAsString(responseMap));
|
||||
return resp;
|
||||
};
|
||||
|
||||
/**
|
||||
* Handler that returns output of xpath query and processor data
|
||||
*/
|
||||
private static Route xpathHandler = (Request req, Response resp) -> {
|
||||
|
||||
String body = req.body();
|
||||
System.out.println("Request: " + body);
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
Map<String, String> requestMap = null;
|
||||
Map<String, String> responseMap = new HashMap<>();
|
||||
try {
|
||||
requestMap = mapper.readValue(body, Map.class);
|
||||
} catch (JsonMappingException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
|
||||
String data = requestMap.get("data");
|
||||
String query = requestMap.get("process");
|
||||
String processor = requestMap.get("processor");
|
||||
String version = requestMap.get("version");
|
||||
|
||||
String tmp = "";
|
||||
long timeStart;
|
||||
long duration;
|
||||
|
||||
if (processor == null) {
|
||||
return "saxon, xalan";
|
||||
}
|
||||
try {
|
||||
switch (processor) {
|
||||
case "saxon":
|
||||
resp.header("processor", "Saxon " + Saxon.getVersion() + " " + version + " over s9api");
|
||||
System.out.print("Processing start...");
|
||||
timeStart = System.currentTimeMillis();
|
||||
try {
|
||||
tmp = Saxon.processXPath(data, query, version).trim();
|
||||
responseMap.put("result", tmp);
|
||||
responseMap.put("status", "OK");
|
||||
} catch (Exception ex) {
|
||||
responseMap.put("result", ex.getMessage());
|
||||
responseMap.put("status", "ERR");
|
||||
resp.status(400);
|
||||
}
|
||||
duration = System.currentTimeMillis() - timeStart;
|
||||
System.out.println("end(" + duration + ")");
|
||||
responseMap.put("processor", "Saxon " + Saxon.getVersion() + " " + version + " over s9api");
|
||||
responseMap.put("time", "" + duration);
|
||||
resp.body(mapper.writeValueAsString(responseMap));
|
||||
return resp;
|
||||
|
||||
case "xalan":
|
||||
resp.header("processor", Xalan.getVersion());
|
||||
timeStart = System.currentTimeMillis();
|
||||
try {
|
||||
tmp = Xalan.processXPath(data, query).trim();
|
||||
responseMap.put("result", tmp);
|
||||
responseMap.put("status", "OK");
|
||||
} catch (Exception ex) {
|
||||
responseMap.put("result", ex.getMessage());
|
||||
responseMap.put("status", "ERR");
|
||||
resp.status(400);
|
||||
}
|
||||
duration = System.currentTimeMillis() - timeStart;
|
||||
responseMap.put("processor", Xalan.getVersion());
|
||||
responseMap.put("result", tmp);
|
||||
responseMap.put("time", Long.toString(duration));
|
||||
resp.body(mapper.writeValueAsString(responseMap));
|
||||
return resp;
|
||||
|
||||
default:
|
||||
return "saxon, xalan";
|
||||
}
|
||||
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
return ex.getMessage();
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Handler that returns outcome of xslt transformation and processor data
|
||||
*/
|
||||
private static Route xsltHandler = (Request req, Response resp) -> {
|
||||
String body = req.body();
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
Map<String, String> jsonMap = null;
|
||||
Map<String, String> responseMap = new HashMap<>();
|
||||
try {
|
||||
jsonMap = mapper.readValue(body, Map.class);
|
||||
System.out.println(jsonMap);
|
||||
} catch (JsonMappingException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
String data = jsonMap.get("data");
|
||||
String query = jsonMap.get("process");
|
||||
String processor = jsonMap.get("processor");
|
||||
String version = jsonMap.get("version");
|
||||
|
||||
if (processor == null) {
|
||||
return "saxon, xalan";
|
||||
}
|
||||
try {
|
||||
String tmp;
|
||||
long timeStart;
|
||||
long duration;
|
||||
switch (processor) {
|
||||
case "saxon":
|
||||
timeStart = System.currentTimeMillis();
|
||||
try {
|
||||
tmp = Saxon.processXSLT(data, query);
|
||||
responseMap.put("result", tmp);
|
||||
responseMap.put("status", "OK");
|
||||
} catch (Exception ex) {
|
||||
responseMap.put("result", ex.getMessage());
|
||||
responseMap.put("status", "ERR");
|
||||
resp.status(400);
|
||||
}
|
||||
|
||||
|
||||
duration = System.currentTimeMillis() - timeStart;
|
||||
responseMap.put("processor", "Saxon " + Saxon.getVersion() + " " + version);
|
||||
responseMap.put("time", Long.toString(duration));
|
||||
resp.body(mapper.writeValueAsString(responseMap));
|
||||
return resp;
|
||||
|
||||
case "xalan":
|
||||
timeStart = System.currentTimeMillis();
|
||||
try {
|
||||
tmp = Xalan.processXSLT(data, query);
|
||||
responseMap.put("result", tmp);
|
||||
responseMap.put("status", "OK");
|
||||
} catch (Exception ex) {
|
||||
responseMap.put("result", ex.getMessage());
|
||||
responseMap.put("status", "ERR");
|
||||
resp.status(400);
|
||||
}
|
||||
duration = System.currentTimeMillis() - timeStart;
|
||||
responseMap.put("processor", Xalan.getVersion());
|
||||
responseMap.put("time", Long.toString(duration));
|
||||
resp.body(mapper.writeValueAsString(responseMap));
|
||||
return resp;
|
||||
|
||||
default:
|
||||
return "saxon, xalan";
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
return ex.getMessage();
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
package com.r11.tools.xslt.processors;
|
||||
|
||||
import net.sf.saxon.om.NamespaceBinding;
|
||||
import net.sf.saxon.om.NamespaceMap;
|
||||
import net.sf.saxon.s9api.XPathCompiler;
|
||||
import net.sf.saxon.s9api.XdmNode;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
/**
|
||||
* Handler for saxon namespace scan engine.
|
||||
* All found namespaces are stored within {@link #namespaceMap}
|
||||
* @author Wojciech Czop
|
||||
*/
|
||||
public class NewNamespaceResolver {
|
||||
|
||||
private NamespaceMap namespaceMap;
|
||||
|
||||
/**
|
||||
* Initializes {@link #namespaceMap} with namespace values
|
||||
* @param doc dom structure object
|
||||
* @return map of namespaces
|
||||
*/
|
||||
public NamespaceMap process(XdmNode doc) {
|
||||
namespaceMap = NamespaceMap.emptyMap();
|
||||
Iterator<XdmNode> it = doc.children().iterator();
|
||||
// TODO: remove
|
||||
while (it.hasNext()) {
|
||||
XdmNode tmp = it.next();
|
||||
extractNamespace(tmp);
|
||||
}
|
||||
// end
|
||||
return namespaceMap;
|
||||
}
|
||||
|
||||
/**
|
||||
* Iterates through {@link #namespaceMap} and declares namespaces in {@link XPathCompiler}
|
||||
* @param compiler compiler used to compile xpath statements
|
||||
*/
|
||||
public void exportNamespaces(XPathCompiler compiler){
|
||||
Iterator<NamespaceBinding> it = namespaceMap.iterator();
|
||||
// TODO: remove
|
||||
while(it.hasNext()){
|
||||
System.out.println(it.next());
|
||||
}
|
||||
// end
|
||||
namespaceMap.forEach(namespaceBinding -> compiler.declareNamespace(namespaceBinding.getPrefix(), namespaceBinding.getURI()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Uses recurrency to dive deep dom structure and appends {@link #namespaceMap} with every found namespace
|
||||
* @param node dom structure object
|
||||
*/
|
||||
private void extractNamespace(XdmNode node) {
|
||||
NamespaceMap tmp;
|
||||
if ((tmp = node.getUnderlyingNode().getAllNamespaces()) != null) {
|
||||
namespaceMap = namespaceMap.putAll(tmp);
|
||||
}
|
||||
if (node.children().iterator().hasNext()) {
|
||||
|
||||
Iterator<XdmNode> it = node.children().iterator();
|
||||
while (it.hasNext()) {
|
||||
XdmNode rNode = it.next();
|
||||
// TODO: remove
|
||||
if(rNode.getUnderlyingNode().getPrefix().isEmpty() && !rNode.getParent().getUnderlyingNode().getPrefix().isEmpty()){
|
||||
|
||||
System.out.println("prefix missing, parent has "+rNode.getParent().getUnderlyingNode().getPrefix() + ", but child has none");
|
||||
|
||||
System.out.println();
|
||||
}
|
||||
// end
|
||||
extractNamespace(rNode);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
package com.r11.tools.xslt.processors;
|
||||
|
||||
import net.sf.saxon.s9api.*;
|
||||
import javax.xml.transform.stream.StreamSource;
|
||||
import java.io.StringReader;
|
||||
import java.io.StringWriter;
|
||||
|
||||
/**
|
||||
* Handler for Saxon engine
|
||||
* @author Wojciech Czop
|
||||
*/
|
||||
public class Saxon {
|
||||
|
||||
/**
|
||||
* Transforms string containing xml document via xslt
|
||||
* @param data xml to be transformed
|
||||
* @param transform xslt
|
||||
* @return transformed xml
|
||||
* @throws SaxonApiException thrown on stylesheet or transformation errors
|
||||
*/
|
||||
public static String processXSLT(String data, String transform) throws SaxonApiException {
|
||||
Processor processor = new Processor(false);
|
||||
XsltCompiler compiler = processor.newXsltCompiler();
|
||||
XsltExecutable stylesheet = compiler.compile(new StreamSource(new StringReader(transform)));
|
||||
StringWriter sw = new StringWriter();
|
||||
Serializer out = processor.newSerializer(sw);
|
||||
out.setOutputProperty(Serializer.Property.METHOD, "xml");
|
||||
out.setOutputProperty(Serializer.Property.INDENT, "yes");
|
||||
|
||||
Xslt30Transformer transformer = stylesheet.load30();
|
||||
transformer.transform(new StreamSource(new StringReader(data)), out);
|
||||
return sw.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Process xpath and return either node or wrapped atomic value
|
||||
* @param data xml to be querried
|
||||
* @param query xpath queryy
|
||||
* @param version processor version
|
||||
* @return string xml representation of the node
|
||||
* @throws Exception thrown on node building errors or invalid xpath
|
||||
*/
|
||||
public static String processXPath(String data, String query, String version) throws Exception {
|
||||
Processor p = new Processor(false);
|
||||
XPathCompiler compiler = p.newXPathCompiler();
|
||||
DocumentBuilder builder = p.newDocumentBuilder();
|
||||
XdmNode doc = builder.build(new StreamSource(new StringReader(data)));
|
||||
|
||||
compiler.setLanguageVersion(version);
|
||||
|
||||
NewNamespaceResolver resolver = new NewNamespaceResolver();
|
||||
|
||||
resolver.process(doc);
|
||||
|
||||
resolver.exportNamespaces(compiler);
|
||||
|
||||
XdmValue result = compiler.evaluate(query, doc);
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (XdmItem xdmItem : result) {
|
||||
sb.append(xdmItem);
|
||||
sb.append('\n');
|
||||
}
|
||||
return sb.toString();
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns version of the processor
|
||||
* @return version of the processor
|
||||
*/
|
||||
public static String getVersion() {
|
||||
return new Processor(false).getSaxonProductVersion();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
package com.r11.tools.xslt.processors;
|
||||
|
||||
import org.w3c.dom.Document;
|
||||
import org.xml.sax.InputSource;
|
||||
|
||||
import javax.xml.XMLConstants;
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.transform.*;
|
||||
import javax.xml.transform.dom.DOMSource;
|
||||
import javax.xml.transform.stream.StreamResult;
|
||||
import javax.xml.transform.stream.StreamSource;
|
||||
import javax.xml.validation.Schema;
|
||||
import javax.xml.validation.SchemaFactory;
|
||||
import javax.xml.validation.Validator;
|
||||
import javax.xml.xpath.XPath;
|
||||
import javax.xml.xpath.XPathConstants;
|
||||
import javax.xml.xpath.XPathExpression;
|
||||
import javax.xml.xpath.XPathFactory;
|
||||
import java.io.StringReader;
|
||||
import java.io.StringWriter;
|
||||
|
||||
/**
|
||||
* Handler for Xalan engine
|
||||
* @author Wojciech Czop
|
||||
*/
|
||||
public class Xalan {
|
||||
/**
|
||||
* Transforms string containing xml document via xslt
|
||||
* @param data xml to be transformed
|
||||
* @param transform xslt
|
||||
* @return transformed xml
|
||||
* @throws Exception thrown on stylesheet or transformation errors
|
||||
*/
|
||||
public static String processXSLT(String data, String transform) throws Exception{
|
||||
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
||||
DocumentBuilder builder = factory.newDocumentBuilder();
|
||||
Document document = builder.parse(new InputSource(new StringReader(data)));
|
||||
|
||||
StreamSource stylesource = new StreamSource(new StringReader(transform));
|
||||
Transformer transformer = TransformerFactory.newInstance().newTransformer(stylesource);
|
||||
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
|
||||
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
|
||||
|
||||
Source source = new DOMSource(document);
|
||||
StringWriter sw = new StringWriter();
|
||||
Result outputTarget = new StreamResult(sw);
|
||||
transformer.transform(source, outputTarget);
|
||||
|
||||
return sw.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Process xpath and return either node or wrapped atomic value
|
||||
* @deprecated
|
||||
* Xalan needs assumption of the outcome, which is not implemented. Therefore method is deprecated
|
||||
* @param data xml
|
||||
* @param transform xpath
|
||||
* @return xml processed using given xpath
|
||||
* @throws Exception thrown on node building errors or invalid xpath
|
||||
*/
|
||||
public static String processXPath(String data, String transform) throws Exception{
|
||||
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
||||
DocumentBuilder builder = factory.newDocumentBuilder();
|
||||
|
||||
XPath xpath = XPathFactory.newInstance().newXPath();
|
||||
|
||||
xpath.setNamespaceContext(new XalanNamespaceResolver(builder.parse(new InputSource(new StringReader(data))), true));
|
||||
XPathExpression exp = xpath.compile(transform);
|
||||
exp.evaluate(new InputSource(new StringReader(data)), XPathConstants.NODESET);
|
||||
return exp.evaluate(new InputSource(new StringReader(data)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns version of the processor
|
||||
* @return version of the processor
|
||||
*/
|
||||
public static String getVersion(){
|
||||
return org.apache.xalan.Version.getVersion();
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates string representation of the xml document against xsd schema
|
||||
* @param data xml document
|
||||
* @param xsd xsd schema
|
||||
* @return statement of validity
|
||||
* @throws Exception thrown on invalid xsd schema or xml
|
||||
*/
|
||||
public static String validate(String data, String xsd) throws Exception{
|
||||
Source dataSource = new StreamSource(new StringReader(data));
|
||||
Source xsdSource = new StreamSource(new StringReader(xsd));
|
||||
SchemaFactory schemaFactory = SchemaFactory
|
||||
.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
|
||||
Schema schema = schemaFactory.newSchema(xsdSource);
|
||||
Validator validator = schema.newValidator();
|
||||
validator.validate(dataSource);
|
||||
return "XML file is valid";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,132 @@
|
||||
package com.r11.tools.xslt.processors;
|
||||
|
||||
import org.w3c.dom.*;
|
||||
|
||||
import javax.xml.XMLConstants;
|
||||
import javax.xml.namespace.NamespaceContext;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Contains methods that scan document for namespaces and store them in map objects.
|
||||
* @deprecated
|
||||
* Class no longer in use. It has been replaced by {@link NewNamespaceResolver}
|
||||
* @author Wojciech Czop
|
||||
*/
|
||||
public class XalanNamespaceResolver implements NamespaceContext {
|
||||
private static final String DEFAULT_NS = "DEFAULT";
|
||||
private Map<String, String> prefix2Uri = new HashMap<String, String>();
|
||||
private Map<String, String> uri2Prefix = new HashMap<String, String>();
|
||||
|
||||
/**
|
||||
* This constructor parses the document and stores all namespaces it can
|
||||
* find. If toplevelOnly is true, only namespaces in the root are used.
|
||||
*
|
||||
* @param document
|
||||
* source document
|
||||
* @param toplevelOnly
|
||||
* restriction of the search to enhance performance
|
||||
*/
|
||||
public XalanNamespaceResolver(Document document, boolean toplevelOnly) {
|
||||
examineNode(document.getFirstChild(), toplevelOnly);
|
||||
System.out.println("The list of the cached namespaces:");
|
||||
for (String key : prefix2Uri.keySet()) {
|
||||
System.out.println("prefix " + key + ": uri " + prefix2Uri.get(key));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A single node is read, the namespace attributes are extracted and stored.
|
||||
*
|
||||
* @param node
|
||||
* to examine
|
||||
* @param attributesOnly,
|
||||
* if true no recursion happens
|
||||
*/
|
||||
private void examineNode(Node node, boolean attributesOnly) {
|
||||
NamedNodeMap attributes = node.getAttributes();
|
||||
for (int i = 0; i < attributes.getLength(); i++) {
|
||||
Node attribute = attributes.item(i);
|
||||
storeAttribute((Attr) attribute);
|
||||
}
|
||||
|
||||
if (!attributesOnly) {
|
||||
NodeList chields = node.getChildNodes();
|
||||
for (int i = 0; i < chields.getLength(); i++) {
|
||||
Node chield = chields.item(i);
|
||||
if (chield.getNodeType() == Node.ELEMENT_NODE){
|
||||
examineNode(chield, false);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This method looks at an attribute and stores it, if it is a namespace
|
||||
* attribute.
|
||||
*
|
||||
* @param attribute
|
||||
* to examine
|
||||
*/
|
||||
private void storeAttribute(Attr attribute) {
|
||||
// examine the attributes in namespace xmlns
|
||||
String uri = attribute.getNamespaceURI();
|
||||
String name = attribute.getLocalName();
|
||||
String name2 = attribute.getName();
|
||||
String value = attribute.getNodeValue();
|
||||
if (attribute.getNodeValue() != null
|
||||
/*&& attribute.getNamespaceURI().equals(
|
||||
XMLConstants.XMLNS_ATTRIBUTE_NS_URI)*/) {
|
||||
// Default namespace xmlns="uri goes here"
|
||||
if (attribute.getNodeName().equals(XMLConstants.XMLNS_ATTRIBUTE)) {
|
||||
putInCache(DEFAULT_NS, attribute.getNodeValue());
|
||||
} else {
|
||||
// The defined prefixes are stored here
|
||||
putInCache(attribute.getName().replace("xmlns:", ""), attribute.getNodeValue());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Stores namespace prefix ass well as its uri in map
|
||||
* @param prefix
|
||||
* @param uri
|
||||
*/
|
||||
private void putInCache(String prefix, String uri) {
|
||||
prefix2Uri.put(prefix, uri);
|
||||
uri2Prefix.put(uri, prefix);
|
||||
}
|
||||
|
||||
/**
|
||||
* This method is called by XPath. It returns the default namespace, if the
|
||||
* prefix is null or "".
|
||||
*
|
||||
* @param prefix
|
||||
* to search for
|
||||
* @return uri
|
||||
*/
|
||||
public String getNamespaceURI(String prefix) {
|
||||
if (prefix == null || prefix.equals(XMLConstants.DEFAULT_NS_PREFIX)) {
|
||||
return prefix2Uri.get(DEFAULT_NS);
|
||||
} else {
|
||||
return prefix2Uri.get(prefix);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This method is not needed in this context, but can be implemented in a
|
||||
* similar way.
|
||||
*/
|
||||
public String getPrefix(String namespaceURI) {
|
||||
return uri2Prefix.get(namespaceURI);
|
||||
}
|
||||
|
||||
public Iterator getPrefixes(String namespaceURI) {
|
||||
// Not implemented
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
9
Backend/xslt-rest/src/main/resources/log4j.properties
Normal file
9
Backend/xslt-rest/src/main/resources/log4j.properties
Normal file
@@ -0,0 +1,9 @@
|
||||
# Set root logger level to DEBUG and its only appender to A1.
|
||||
log4j.rootLogger=ERROR, A1
|
||||
|
||||
# A1 is set to be a ConsoleAppender.
|
||||
log4j.appender.A1=org.apache.log4j.ConsoleAppender
|
||||
|
||||
# A1 uses PatternLayout.
|
||||
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
|
||||
log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
|
||||
Reference in New Issue
Block a user