T285 Added Javadoc

This commit is contained in:
2021-05-06 16:11:09 +02:00
parent 7a7834fb06
commit 4413405b7d
5 changed files with 95 additions and 21 deletions

View File

@@ -18,6 +18,7 @@ public class SparkInitializer {
* Initializes spark framework
*/
public static void run(){
// TODO: Port value as property
Spark.port(8081);
Spark.after((Filter) (request, response) -> {

View File

@@ -7,29 +7,52 @@ 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
*/
//przeskanowuje obiekt dom i zwraca mapę ns używając extract ns
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
*/
//rozpakowanie ns mapy i deklaracja w kompilatorze
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) {
@@ -40,6 +63,7 @@ public class NewNamespaceResolver {
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");
@@ -47,6 +71,7 @@ public class NewNamespaceResolver {
NamespaceMap nsTMP= rNode.getUnderlyingNode().getAllNamespaces();
System.out.println();
}
// end
extractNamespace(rNode);
}
}

View File

@@ -6,7 +6,19 @@ 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
*/
public static String processXSLT(String data, String transform) throws SaxonApiException {
Processor processor = new Processor(false);
XsltCompiler compiler = processor.newXsltCompiler();
@@ -21,14 +33,20 @@ public class Saxon {
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 {
Processor p = new Processor(false);
XPathCompiler compiler = p.newXPathCompiler();
DocumentBuilder builder = p.newDocumentBuilder();
XdmNode doc = builder.build(new StreamSource(new StringReader(data)));
// System.out.println(version);
compiler.setLanguageVersion(version);
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() {
return new Processor(false).getSaxonProductVersion();
}

View File

@@ -1,5 +1,6 @@
package r11.mltx.restxslt.processors;
import net.sf.saxon.s9api.SaxonApiException;
import org.w3c.dom.Document;
import javax.xml.XMLConstants;
@@ -21,22 +22,28 @@ import java.io.StringWriter;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
/**
* 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
*/
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);
@@ -45,6 +52,15 @@ public class Xalan {
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{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
@@ -57,31 +73,30 @@ public class Xalan {
String result = exp.evaluate(new InputSource(new StringReader(data)));
return result;
}
/**
* 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
*/
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);
// try {
Schema schema = schemaFactory.newSchema(xsdSource);
Validator validator = schema.newValidator();
validator.validate(dataSource);
// System.out.println(dataSource.getSystemId() + " 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();
// }
}
}

View File

@@ -8,6 +8,12 @@ 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>();
@@ -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) {
prefix2Uri.put(prefix, uri);
uri2Prefix.put(uri, prefix);