T285 Added Javadoc
This commit is contained in:
@@ -18,6 +18,7 @@ public class SparkInitializer {
|
|||||||
* Initializes spark framework
|
* Initializes spark framework
|
||||||
*/
|
*/
|
||||||
public static void run(){
|
public static void run(){
|
||||||
|
// TODO: Port value as property
|
||||||
Spark.port(8081);
|
Spark.port(8081);
|
||||||
|
|
||||||
Spark.after((Filter) (request, response) -> {
|
Spark.after((Filter) (request, response) -> {
|
||||||
|
|||||||
@@ -7,29 +7,52 @@ import net.sf.saxon.s9api.XdmNode;
|
|||||||
|
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handler for saxon namespace scan engine.
|
||||||
|
* All found namespaces are stored within {@link #namespaceMap}
|
||||||
|
* @author Wojciech Czop
|
||||||
|
*/
|
||||||
public class NewNamespaceResolver {
|
public class NewNamespaceResolver {
|
||||||
|
|
||||||
private NamespaceMap namespaceMap;
|
private NamespaceMap namespaceMap;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initializes {@link #namespaceMap} with namespace values
|
||||||
|
* @param doc dom structure object
|
||||||
|
* @return map of namespaces
|
||||||
|
*/
|
||||||
|
//przeskanowuje obiekt dom i zwraca mapę ns używając extract ns
|
||||||
public NamespaceMap process(XdmNode doc) {
|
public NamespaceMap process(XdmNode doc) {
|
||||||
namespaceMap = NamespaceMap.emptyMap();
|
namespaceMap = NamespaceMap.emptyMap();
|
||||||
Iterator<XdmNode> it = doc.children().iterator();
|
Iterator<XdmNode> it = doc.children().iterator();
|
||||||
|
// TODO: remove
|
||||||
while (it.hasNext()) {
|
while (it.hasNext()) {
|
||||||
XdmNode tmp = it.next();
|
XdmNode tmp = it.next();
|
||||||
extractNamespace(tmp);
|
extractNamespace(tmp);
|
||||||
}
|
}
|
||||||
|
// end
|
||||||
return namespaceMap;
|
return namespaceMap;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Iterates through {@link #namespaceMap} and declares namespaces in {@link XPathCompiler}
|
||||||
|
* @param compiler compiler used to compile xpath statements
|
||||||
|
*/
|
||||||
|
//rozpakowanie ns mapy i deklaracja w kompilatorze
|
||||||
public void exportNamespaces(XPathCompiler compiler){
|
public void exportNamespaces(XPathCompiler compiler){
|
||||||
Iterator<NamespaceBinding> it = namespaceMap.iterator();
|
Iterator<NamespaceBinding> it = namespaceMap.iterator();
|
||||||
|
// TODO: remove
|
||||||
while(it.hasNext()){
|
while(it.hasNext()){
|
||||||
System.out.println(it.next());
|
System.out.println(it.next());
|
||||||
}
|
}
|
||||||
|
// end
|
||||||
namespaceMap.forEach(namespaceBinding -> compiler.declareNamespace(namespaceBinding.getPrefix(), namespaceBinding.getURI()));
|
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) {
|
private void extractNamespace(XdmNode node) {
|
||||||
NamespaceMap tmp;
|
NamespaceMap tmp;
|
||||||
if ((tmp = node.getUnderlyingNode().getAllNamespaces()) != null) {
|
if ((tmp = node.getUnderlyingNode().getAllNamespaces()) != null) {
|
||||||
@@ -40,6 +63,7 @@ public class NewNamespaceResolver {
|
|||||||
Iterator<XdmNode> it = node.children().iterator();
|
Iterator<XdmNode> it = node.children().iterator();
|
||||||
while (it.hasNext()) {
|
while (it.hasNext()) {
|
||||||
XdmNode rNode = it.next();
|
XdmNode rNode = it.next();
|
||||||
|
// TODO: remove
|
||||||
if(rNode.getUnderlyingNode().getPrefix().isEmpty() && !rNode.getParent().getUnderlyingNode().getPrefix().isEmpty()){
|
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("prefix missing, parent has "+rNode.getParent().getUnderlyingNode().getPrefix() + ", but child has none");
|
||||||
@@ -47,6 +71,7 @@ public class NewNamespaceResolver {
|
|||||||
NamespaceMap nsTMP= rNode.getUnderlyingNode().getAllNamespaces();
|
NamespaceMap nsTMP= rNode.getUnderlyingNode().getAllNamespaces();
|
||||||
System.out.println();
|
System.out.println();
|
||||||
}
|
}
|
||||||
|
// end
|
||||||
extractNamespace(rNode);
|
extractNamespace(rNode);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,19 @@ import javax.xml.transform.stream.StreamSource;
|
|||||||
import java.io.StringReader;
|
import java.io.StringReader;
|
||||||
import java.io.StringWriter;
|
import java.io.StringWriter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handler for Saxon engine
|
||||||
|
* @author Wojciech Czop
|
||||||
|
*/
|
||||||
public class Saxon {
|
public class Saxon {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Transforms string containing xml document via xslt
|
||||||
|
* @param data xml to be transformed
|
||||||
|
* @param transform xslt
|
||||||
|
* @return transformed xml
|
||||||
|
* @throws SaxonApiException
|
||||||
|
*/
|
||||||
public static String processXSLT(String data, String transform) throws SaxonApiException {
|
public static String processXSLT(String data, String transform) throws SaxonApiException {
|
||||||
Processor processor = new Processor(false);
|
Processor processor = new Processor(false);
|
||||||
XsltCompiler compiler = processor.newXsltCompiler();
|
XsltCompiler compiler = processor.newXsltCompiler();
|
||||||
@@ -21,14 +33,20 @@ public class Saxon {
|
|||||||
return sw.toString();
|
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
|
||||||
|
*/
|
||||||
public static String processXPath(String data, String query, String version) throws Exception {
|
public static String processXPath(String data, String query, String version) throws Exception {
|
||||||
Processor p = new Processor(false);
|
Processor p = new Processor(false);
|
||||||
XPathCompiler compiler = p.newXPathCompiler();
|
XPathCompiler compiler = p.newXPathCompiler();
|
||||||
DocumentBuilder builder = p.newDocumentBuilder();
|
DocumentBuilder builder = p.newDocumentBuilder();
|
||||||
XdmNode doc = builder.build(new StreamSource(new StringReader(data)));
|
XdmNode doc = builder.build(new StreamSource(new StringReader(data)));
|
||||||
|
|
||||||
// System.out.println(version);
|
|
||||||
|
|
||||||
compiler.setLanguageVersion(version);
|
compiler.setLanguageVersion(version);
|
||||||
|
|
||||||
NewNamespaceResolver resolver = new NewNamespaceResolver();
|
NewNamespaceResolver resolver = new NewNamespaceResolver();
|
||||||
@@ -47,6 +65,10 @@ public class Saxon {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns version of the processor
|
||||||
|
* @return version of the processor
|
||||||
|
*/
|
||||||
public static String getVersion() {
|
public static String getVersion() {
|
||||||
return new Processor(false).getSaxonProductVersion();
|
return new Processor(false).getSaxonProductVersion();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package r11.mltx.restxslt.processors;
|
package r11.mltx.restxslt.processors;
|
||||||
|
|
||||||
|
import net.sf.saxon.s9api.SaxonApiException;
|
||||||
import org.w3c.dom.Document;
|
import org.w3c.dom.Document;
|
||||||
|
|
||||||
import javax.xml.XMLConstants;
|
import javax.xml.XMLConstants;
|
||||||
@@ -21,22 +22,28 @@ import java.io.StringWriter;
|
|||||||
import org.w3c.dom.NodeList;
|
import org.w3c.dom.NodeList;
|
||||||
import org.xml.sax.InputSource;
|
import org.xml.sax.InputSource;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handler for Xalan engine
|
||||||
|
* @author Wojciech Czop
|
||||||
|
*/
|
||||||
public class Xalan {
|
public class Xalan {
|
||||||
|
/**
|
||||||
|
* Transforms string containing xml document via xslt
|
||||||
|
* @param data xml to be transformed
|
||||||
|
* @param transform xslt
|
||||||
|
* @return transformed xml
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
public static String processXSLT(String data, String transform) throws Exception{
|
public static String processXSLT(String data, String transform) throws Exception{
|
||||||
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
||||||
DocumentBuilder builder = factory.newDocumentBuilder();
|
DocumentBuilder builder = factory.newDocumentBuilder();
|
||||||
Document document = builder.parse(new InputSource(new StringReader(data)));
|
Document document = builder.parse(new InputSource(new StringReader(data)));
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
StreamSource stylesource = new StreamSource(new StringReader(transform));
|
StreamSource stylesource = new StreamSource(new StringReader(transform));
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Transformer transformer = TransformerFactory.newInstance().newTransformer(stylesource);
|
Transformer transformer = TransformerFactory.newInstance().newTransformer(stylesource);
|
||||||
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
|
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
|
||||||
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
|
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
|
||||||
|
|
||||||
Source source = new DOMSource(document);
|
Source source = new DOMSource(document);
|
||||||
StringWriter sw = new StringWriter();
|
StringWriter sw = new StringWriter();
|
||||||
Result outputTarget = new StreamResult(sw);
|
Result outputTarget = new StreamResult(sw);
|
||||||
@@ -45,6 +52,15 @@ public class Xalan {
|
|||||||
return sw.toString();
|
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
|
||||||
|
* @param transform
|
||||||
|
* @return
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
public static String processXPath(String data, String transform) throws Exception{
|
public static String processXPath(String data, String transform) throws Exception{
|
||||||
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
||||||
DocumentBuilder builder = factory.newDocumentBuilder();
|
DocumentBuilder builder = factory.newDocumentBuilder();
|
||||||
@@ -57,31 +73,30 @@ public class Xalan {
|
|||||||
String result = exp.evaluate(new InputSource(new StringReader(data)));
|
String result = exp.evaluate(new InputSource(new StringReader(data)));
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns version of the processor
|
||||||
|
* @return version of the processor
|
||||||
|
*/
|
||||||
public static String getVersion(){
|
public static String getVersion(){
|
||||||
return org.apache.xalan.Version.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
|
||||||
|
*/
|
||||||
public static String validate(String data, String xsd) throws Exception{
|
public static String validate(String data, String xsd) throws Exception{
|
||||||
|
|
||||||
|
|
||||||
Source dataSource = new StreamSource(new StringReader(data));
|
Source dataSource = new StreamSource(new StringReader(data));
|
||||||
Source xsdSource = new StreamSource(new StringReader(xsd));
|
Source xsdSource = new StreamSource(new StringReader(xsd));
|
||||||
SchemaFactory schemaFactory = SchemaFactory
|
SchemaFactory schemaFactory = SchemaFactory
|
||||||
.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
|
.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
|
||||||
// try {
|
|
||||||
Schema schema = schemaFactory.newSchema(xsdSource);
|
Schema schema = schemaFactory.newSchema(xsdSource);
|
||||||
Validator validator = schema.newValidator();
|
Validator validator = schema.newValidator();
|
||||||
validator.validate(dataSource);
|
validator.validate(dataSource);
|
||||||
// System.out.println(dataSource.getSystemId() + " is valid");
|
|
||||||
return "XML file is valid";
|
return "XML file is valid";
|
||||||
// } catch (SAXException e) {
|
|
||||||
// System.out.println("Invalid: "+e.getMessage());
|
|
||||||
// return "XML file is NOT valid: " + e.getMessage();
|
|
||||||
//// System.out.println(dataSource.getSystemId() + " is NOT valid reason:" + e);
|
|
||||||
// } catch (IOException e) {
|
|
||||||
// return "IO error: "+e.getMessage();
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,6 +8,12 @@ import java.util.HashMap;
|
|||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.Map;
|
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 {
|
public class XalanNamespaceResolver implements NamespaceContext {
|
||||||
private static final String DEFAULT_NS = "DEFAULT";
|
private static final String DEFAULT_NS = "DEFAULT";
|
||||||
private Map<String, String> prefix2Uri = new HashMap<String, String>();
|
private Map<String, String> prefix2Uri = new HashMap<String, String>();
|
||||||
@@ -84,6 +90,11 @@ public class XalanNamespaceResolver implements NamespaceContext {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stores namespace prefix ass well as its uri in map
|
||||||
|
* @param prefix
|
||||||
|
* @param uri
|
||||||
|
*/
|
||||||
private void putInCache(String prefix, String uri) {
|
private void putInCache(String prefix, String uri) {
|
||||||
prefix2Uri.put(prefix, uri);
|
prefix2Uri.put(prefix, uri);
|
||||||
uri2Prefix.put(uri, prefix);
|
uri2Prefix.put(uri, prefix);
|
||||||
|
|||||||
Reference in New Issue
Block a user