List of usage examples for java.util.jar JarFile JarFile
public JarFile(File file) throws IOException
From source file:de.interactive_instruments.ShapeChange.Target.FeatureCatalogue.FeatureCatalogue.java
public void xsltWrite(File transformationSource, String xsltfileName, File transformationTarget) { try {/*from w ww. j a v a 2 s . c o m*/ // ============================== // 1. perform additional checks // ============================== URI xsltMainFileUri = null; if (xsltPath.toLowerCase().startsWith("http")) { URL url = new URL(xsltPath + "/" + xsltfileName); xsltMainFileUri = url.toURI(); } else { File xsl = new File(xsltPath + "/" + xsltfileName); if (xsl.exists()) { xsltMainFileUri = xsl.toURI(); } else { result.addError(this, 18, xsl.getAbsolutePath()); return; } } // ============================== // 2. perform the transformation // ============================== // determine if we need to run with a specific JRE if (pathToJavaExe == null) { // continue using current runtime environment XsltWriter writer = new XsltWriter(xslTransformerFactory, hrefMappings, transformationParameters, result); writer.xsltWrite(transformationSource, xsltMainFileUri, transformationTarget); } else { // execute with JRE from configuration List<String> cmds = new ArrayList<String>(); cmds.add(pathToJavaExe); if (javaOptions != null) { cmds.add(javaOptions); } cmds.add("-cp"); List<String> cpEntries = new ArrayList<String>(); // determine if execution from jar or from class file URL writerResource = XsltWriter.class.getResource("XsltWriter.class"); String writerResourceAsString = writerResource.toString(); if (writerResourceAsString.startsWith("jar:")) { // execution from jar // get path to main ShapeChange jar file String jarPath = writerResourceAsString.substring(4, writerResourceAsString.indexOf("!")); URI jarUri = new URI(jarPath); // add path to man jar file to class path entries File jarF = new File(jarUri); cpEntries.add(jarF.getPath()); /* * Get parent directory in which ShapeChange JAR file * exists, because class path entries in manifest are * defined relative to it. */ File jarDir = jarF.getParentFile(); // get manifest and the classpath entries defined by it Manifest mf = new JarFile(jarF).getManifest(); String classPath = mf.getMainAttributes().getValue("Class-Path"); if (classPath != null) { for (String dependency : classPath.split(" ")) { // add path to dependency to class path entries File dependencyF = new File(jarDir, dependency); cpEntries.add(dependencyF.getPath()); } } } else { // execution with class files // get classpath entries from system class loader ClassLoader cl = ClassLoader.getSystemClassLoader(); URL[] urls = ((URLClassLoader) cl).getURLs(); for (URL url : urls) { File dependencyF = new File(url.getPath()); cpEntries.add(dependencyF.getPath()); } } String cpValue = StringUtils.join(cpEntries, System.getProperty("path.separator")); cmds.add("\"" + cpValue + "\""); /* add fully qualified name of XsltWriter class to command */ cmds.add(XsltWriter.class.getName()); // add parameter for hrefMappings (if defined) if (!hrefMappings.isEmpty()) { List<NameValuePair> hrefMappingsList = new ArrayList<NameValuePair>(); for (Entry<String, URI> hrefM : hrefMappings.entrySet()) { hrefMappingsList.add(new BasicNameValuePair(hrefM.getKey(), hrefM.getValue().toString())); } String hrefMappingsString = URLEncodedUtils.format(hrefMappingsList, XsltWriter.ENCODING_CHARSET); /* * NOTE: surrounding href mapping string with double quotes * to avoid issues with using '=' inside the string when * passed as parameter in invocation of java executable. */ cmds.add(XsltWriter.PARAM_hrefMappings); cmds.add("\"" + hrefMappingsString + "\""); } if (!transformationParameters.isEmpty()) { List<NameValuePair> transformationParametersList = new ArrayList<NameValuePair>(); for (Entry<String, String> transParam : transformationParameters.entrySet()) { transformationParametersList .add(new BasicNameValuePair(transParam.getKey(), transParam.getValue())); } String transformationParametersString = URLEncodedUtils.format(transformationParametersList, XsltWriter.ENCODING_CHARSET); /* * NOTE: surrounding transformation parameter string with * double quotes to avoid issues with using '=' inside the * string when passed as parameter in invocation of java * executable. */ cmds.add(XsltWriter.PARAM_transformationParameters); cmds.add("\"" + transformationParametersString + "\""); } if (xslTransformerFactory != null) { cmds.add(XsltWriter.PARAM_xslTransformerFactory); cmds.add(xslTransformerFactory); } String transformationSourcePath = transformationSource.getPath(); String xsltMainFileUriString = xsltMainFileUri.toString(); String transformationTargetPath = transformationTarget.getPath(); cmds.add(XsltWriter.PARAM_transformationSourcePath); cmds.add("\"" + transformationSourcePath + "\""); cmds.add(XsltWriter.PARAM_transformationTargetPath); cmds.add("\"" + transformationTargetPath + "\""); cmds.add(XsltWriter.PARAM_xsltMainFileUri); cmds.add("\"" + xsltMainFileUriString + "\""); result.addInfo(this, 26, StringUtils.join(cmds, " ")); ProcessBuilder pb = new ProcessBuilder(cmds); try { Process proc = pb.start(); StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream()); StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream()); errorGobbler.start(); outputGobbler.start(); errorGobbler.join(); outputGobbler.join(); int exitVal = proc.waitFor(); if (outputGobbler.hasResult()) { result.addInfo(this, 25, outputGobbler.getResult()); } if (exitVal != 0) { // log error if (errorGobbler.hasResult()) { result.addError(this, 23, errorGobbler.getResult(), "" + exitVal); } else { result.addError(this, 24, "" + exitVal); } } } catch (InterruptedException e) { result.addFatalError(this, 22); throw new ShapeChangeAbortException(); } } // ============== // 2. log result // ============== if (OutputFormat.toLowerCase().contains("docx")) { // nothing to do here, the writeDOCX method adds the proper // result } else if (OutputFormat.toLowerCase().contains("framehtml")) { String outputDir = outputDirectory + "/" + outputFilename; result.addResult(getTargetID(), outputDir, "index.html", null); } else { result.addResult(getTargetID(), outputDirectory, transformationTarget.getName(), null); } } catch (Exception e) { String m = e.getMessage(); if (m != null) { result.addError(m); } e.printStackTrace(System.err); } }