/* * de.superx.etl - a package for controlling ETL routines * Copyright (C) 2021 Daniel Quathamer * * This package is licensed under the CampusSource License; * http://www.campussource.de/org/license/ */ package de.superx.etl; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.StringReader; import java.io.StringWriter; import java.io.UnsupportedEncodingException; import java.net.URISyntaxException; import java.util.Enumeration; import java.util.NoSuchElementException; import java.util.Properties; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathExpressionException; import javax.xml.xpath.XPathFactory; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import de.superx.servlet.SuperXManager; /** */ public class EtlUtils { public static final String NEWLINE=System.getProperty("line.separator"); public static final String PATHSEP=File.separator; public static String WEBINFDIR=SuperXManager.getWEB_INFPfad(); public static String SUPERX_DIR=(System.getProperties().containsKey("SUPERX_DIR")? System.getProperty("SUPERX_DIR"):""); public static void main(String args[]) { try { String tidInXmlFile=""; Document mydomres =de.superx.etl.EtlUtils.buildDocumentFromXmlFile("/home/superx/devel_module/community/tomcat/temp/myTempFile6460222908896375059.xml"); XPathFactory factory = new net.sf.saxon.xpath.XPathFactoryImpl(); XPath xPath = factory.newXPath(); //String searchPath="/etlAction[@name=\"sx_select_mask\"]/unload/row/fld[@name=\"tid\"]"; String searchPath="/etlAction[@name=\"sx_select_mask\"]/unload[@name=\"unload_maskeninfo\"]/rs/row/fld[@name=\"tid\"]"; Node tidNode=(Node) xPath.compile(searchPath).evaluate( mydomres, XPathConstants.NODE); if(tidNode!=null) tidInXmlFile=de.memtext.util.XMLUtils.getTheValue(tidNode); else System.out.println("node not found"); System.out.println(tidInXmlFile); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (XPathExpressionException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ParserConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SAXException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static Properties convertStringToProperty(String inp) throws IOException { Properties myProps = new Properties(); myProps.load(new StringReader(inp)); return myProps; } public static Properties mergeParamProperties(Properties params, Properties runTimeParams) { String parsedParam; Enumeration runTimeParamNames = runTimeParams.propertyNames(); while (runTimeParamNames.hasMoreElements()) { String runTimeParamName = (String)runTimeParamNames.nextElement(); String runTimeParamValue = runTimeParams.getProperty(runTimeParamName); Enumeration paramNames = params.propertyNames(); while (paramNames.hasMoreElements()) { String paramName=(String)paramNames.nextElement(); String paramValue=params.getProperty(paramName); if(paramName.equals(runTimeParamName) ) { paramValue=runTimeParamValue; params.setProperty(paramName, paramValue); } if(paramValue.indexOf("$"+runTimeParamName)>-1) { paramValue=de.memtext.util.StringUtils.replace(paramValue,"$"+runTimeParamName, runTimeParamValue); params.setProperty(paramName, paramValue); } } } return params; } public static void initJobEnvironment() { String initVar=""; if(System.getProperty("WEBINFDIR") ==null) { try { initVar=de.superx.servlet.SuperXManager.getWEB_INFPfad(); if(initVar==null || initVar.equals(".")) { //ermittle webinfdir initVar=getWebinfDirectory(); } } catch (Exception e) { // do nothing, try another } WEBINFDIR=initVar; } if(System.getProperty("SUPERX_DIR") ==null) { SUPERX_DIR=WEBINFDIR+PATHSEP+"conf"+PATHSEP+"edustore"; } } private static String getJarName() { return new File(EtlUtils.class.getProtectionDomain() .getCodeSource() .getLocation() .getPath()) .getName(); } private static boolean runningFromJar() { String jarName = getJarName(); return jarName.contains(".jar"); } public static String getWebinfDirectory() throws URISyntaxException { if (runningFromJar()) { return getWebinfDirectoryFromJar(); } else { return getWebinfDirectoryFromClass(); } } private static String getWebinfDirectoryFromClass() { File f= new File(EtlUtils.class.getProtectionDomain() .getCodeSource() .getLocation() .getPath()+PATHSEP+".."); String class_path=f.getAbsolutePath(); return class_path; } private static String getWebinfDirectoryFromJar() throws URISyntaxException { String pathOfJarFile=new File(EtlUtils.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath()).getParent(); String webinfDir=new File(pathOfJarFile+PATHSEP+"..").getAbsolutePath(); return webinfDir; } /* die folgenden 2 Methoden * getFileContentsWithEncoding * saveFileContentsWithEncoding * sind in kern5.0 in de.superx.util.FileUtils * wg. abwärtskompatiblität hierhin kopiert, sollten langfristig wieder weg */ public static String getFileContentsWithEncoding(String filePath, String encoding) { File f = new File(filePath); if (!f.exists()) { System.out.println("Fehler: Datei " + filePath + " existiert nicht."); return null; } String fileContents = ""; if (encoding == null || encoding.trim().equals("")) { encoding = System.getProperty("file.encoding"); } try { // --- IputStream und OutputStream generieren ---// FileInputStream fis = new FileInputStream(f); // Wenn Quelldatei Unicode, dann speziellen Reader nutzen BufferedReader in; //BufferedReader ist schneller bei großen Dateien in = new BufferedReader(new InputStreamReader(fis, encoding)); // --- Output-Stream der temporären Datei erzeugen ---// StringWriter out = new StringWriter(); // --- Verarbeiten der Datei ---// String text; text = in.readLine(); while (text != null) { // Datei nicht leer out.write(text); out.write(System.getProperty("line.separator")); text = in.readLine(); } if (!(out == null)) { fileContents = out.toString(); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return fileContents; } public static void saveFileContentsWithEncoding(String filename, String contents, String encoding) throws FileNotFoundException, IOException { File f = new File(filename); BufferedReader in; BufferedWriter out; //Default encoding ist utf-8 if (encoding == null) encoding = System.getProperty("file.encoding"); // --- Output-Stream der temporären Datei erzeugen ---// out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f), encoding)); out.write(contents); out.close(); }//Ende der Methode /*SAX Document aus XML-Datei erzeugen */ public static Document buildDocumentFromXmlFile(String srcFile) throws ParserConfigurationException, FileNotFoundException, SAXException, IOException { Document mydomres; org.xml.sax.InputSource is; DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = dfactory.newDocumentBuilder(); FileInputStream in = new FileInputStream(srcFile); is=new org.xml.sax.InputSource(in); mydomres = builder.newDocument(); mydomres = builder.parse(is); return mydomres; } /*** * TODO: nach merge in master diese Methode löschen, und auf de.memtext.util.XMLUtils.parseXml(String) verweisen * @author Witt This function parses XML-containing string into documents while * preserving the namespaces and is primarily meant to be used withing * (jUnit) test cases * @param xmlString * @return * @throws ParserConfigurationException * @throws SAXException * @throws IOException */ public static Document parseXml(String xmlString) throws ParserConfigurationException, SAXException, IOException { DocumentBuilderFactory myFactory = DocumentBuilderFactory.newInstance(); myFactory.setNamespaceAware(true); DocumentBuilder myBuilder; myBuilder = myFactory.newDocumentBuilder(); Document myDocument = myBuilder.parse(new InputSource(new StringReader(xmlString))); return myDocument; } /** * @param src * @param tidInXmlFile * @return * @throws ParserConfigurationException * @throws SAXException * @throws IOException * @throws XPathExpressionException */ public static boolean isNodeValueInXml(String src, String searchPath, String expectedValue) throws ParserConfigurationException, SAXException, IOException, XPathExpressionException { boolean b=false; Document mydomres =de.superx.etl.EtlUtils.parseXml(src); XPathFactory factory = new net.sf.saxon.xpath.XPathFactoryImpl(); XPath xPath = factory.newXPath(); Node myNode=(Node) xPath.compile(searchPath).evaluate( mydomres, XPathConstants.NODE); if(myNode!=null) { String foundValue=de.memtext.util.XMLUtils.getTheValue(myNode); if(!(foundValue==null) && foundValue.trim().equals(expectedValue)) b=true; } return b; } public static String translateReturnCode(int returnCode) { String returnString="Fehlerhaft"; if(returnCode==0) returnString="Erfolg"; return returnString; } }