Skip to content

Commit

Permalink
TEDEFO-760 first cleanup pass, renamed some variables, changes in Jav…
Browse files Browse the repository at this point in the history
…aDoc, better tests, removed a file.
  • Loading branch information
rouschr committed Apr 28, 2022
1 parent 50a55bc commit bcb4e91
Show file tree
Hide file tree
Showing 14 changed files with 669 additions and 303 deletions.
180 changes: 89 additions & 91 deletions .mvn/wrapper/MavenWrapperDownloader.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
/*
* Copyright 2007-present the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
import java.net.*;
import java.io.*;
Expand All @@ -20,98 +18,98 @@

public class MavenWrapperDownloader {

private static final String WRAPPER_VERSION = "0.5.6";
/**
* Default URL to download the maven-wrapper.jar from, if no 'downloadUrl' is provided.
*/
private static final String DEFAULT_DOWNLOAD_URL = "https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/"
+ WRAPPER_VERSION + "/maven-wrapper-" + WRAPPER_VERSION + ".jar";
private static final String WRAPPER_VERSION = "0.5.6";
/**
* Default URL to download the maven-wrapper.jar from, if no 'downloadUrl' is provided.
*/
private static final String DEFAULT_DOWNLOAD_URL =
"https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/" + WRAPPER_VERSION
+ "/maven-wrapper-" + WRAPPER_VERSION + ".jar";

/**
* Path to the maven-wrapper.properties file, which might contain a downloadUrl property to
* use instead of the default one.
*/
private static final String MAVEN_WRAPPER_PROPERTIES_PATH =
".mvn/wrapper/maven-wrapper.properties";
/**
* Path to the maven-wrapper.properties file, which might contain a downloadUrl property to use
* instead of the default one.
*/
private static final String MAVEN_WRAPPER_PROPERTIES_PATH =
".mvn/wrapper/maven-wrapper.properties";

/**
* Path where the maven-wrapper.jar will be saved to.
*/
private static final String MAVEN_WRAPPER_JAR_PATH =
".mvn/wrapper/maven-wrapper.jar";
/**
* Path where the maven-wrapper.jar will be saved to.
*/
private static final String MAVEN_WRAPPER_JAR_PATH = ".mvn/wrapper/maven-wrapper.jar";

/**
* Name of the property which should be used to override the default download url for the wrapper.
*/
private static final String PROPERTY_NAME_WRAPPER_URL = "wrapperUrl";
/**
* Name of the property which should be used to override the default download url for the wrapper.
*/
private static final String PROPERTY_NAME_WRAPPER_URL = "wrapperUrl";

public static void main(String args[]) {
System.out.println("- Downloader started");
File baseDirectory = new File(args[0]);
System.out.println("- Using base directory: " + baseDirectory.getAbsolutePath());
public static void main(String args[]) {
System.out.println("- Downloader started");
File baseDirectory = new File(args[0]);
System.out.println("- Using base directory: " + baseDirectory.getAbsolutePath());

// If the maven-wrapper.properties exists, read it and check if it contains a custom
// wrapperUrl parameter.
File mavenWrapperPropertyFile = new File(baseDirectory, MAVEN_WRAPPER_PROPERTIES_PATH);
String url = DEFAULT_DOWNLOAD_URL;
if(mavenWrapperPropertyFile.exists()) {
FileInputStream mavenWrapperPropertyFileInputStream = null;
try {
mavenWrapperPropertyFileInputStream = new FileInputStream(mavenWrapperPropertyFile);
Properties mavenWrapperProperties = new Properties();
mavenWrapperProperties.load(mavenWrapperPropertyFileInputStream);
url = mavenWrapperProperties.getProperty(PROPERTY_NAME_WRAPPER_URL, url);
} catch (IOException e) {
System.out.println("- ERROR loading '" + MAVEN_WRAPPER_PROPERTIES_PATH + "'");
} finally {
try {
if(mavenWrapperPropertyFileInputStream != null) {
mavenWrapperPropertyFileInputStream.close();
}
} catch (IOException e) {
// Ignore ...
}
}
}
System.out.println("- Downloading from: " + url);

File outputFile = new File(baseDirectory.getAbsolutePath(), MAVEN_WRAPPER_JAR_PATH);
if(!outputFile.getParentFile().exists()) {
if(!outputFile.getParentFile().mkdirs()) {
System.out.println(
"- ERROR creating output directory '" + outputFile.getParentFile().getAbsolutePath() + "'");
}
}
System.out.println("- Downloading to: " + outputFile.getAbsolutePath());
// If the maven-wrapper.properties exists, read it and check if it contains a custom
// wrapperUrl parameter.
File mavenWrapperPropertyFile = new File(baseDirectory, MAVEN_WRAPPER_PROPERTIES_PATH);
String url = DEFAULT_DOWNLOAD_URL;
if (mavenWrapperPropertyFile.exists()) {
FileInputStream mavenWrapperPropertyFileInputStream = null;
try {
mavenWrapperPropertyFileInputStream = new FileInputStream(mavenWrapperPropertyFile);
Properties mavenWrapperProperties = new Properties();
mavenWrapperProperties.load(mavenWrapperPropertyFileInputStream);
url = mavenWrapperProperties.getProperty(PROPERTY_NAME_WRAPPER_URL, url);
} catch (IOException e) {
System.out.println("- ERROR loading '" + MAVEN_WRAPPER_PROPERTIES_PATH + "'");
} finally {
try {
downloadFileFromURL(url, outputFile);
System.out.println("Done");
System.exit(0);
} catch (Throwable e) {
System.out.println("- Error downloading");
e.printStackTrace();
System.exit(1);
if (mavenWrapperPropertyFileInputStream != null) {
mavenWrapperPropertyFileInputStream.close();
}
} catch (IOException e) {
// Ignore ...
}
}
}
System.out.println("- Downloading from: " + url);

File outputFile = new File(baseDirectory.getAbsolutePath(), MAVEN_WRAPPER_JAR_PATH);
if (!outputFile.getParentFile().exists()) {
if (!outputFile.getParentFile().mkdirs()) {
System.out.println("- ERROR creating output directory '"
+ outputFile.getParentFile().getAbsolutePath() + "'");
}
}
System.out.println("- Downloading to: " + outputFile.getAbsolutePath());
try {
downloadFileFromURL(url, outputFile);
System.out.println("Done");
System.exit(0);
} catch (Throwable e) {
System.out.println("- Error downloading");
e.printStackTrace();
System.exit(1);
}
}

private static void downloadFileFromURL(String urlString, File destination) throws Exception {
if (System.getenv("MVNW_USERNAME") != null && System.getenv("MVNW_PASSWORD") != null) {
String username = System.getenv("MVNW_USERNAME");
char[] password = System.getenv("MVNW_PASSWORD").toCharArray();
Authenticator.setDefault(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
private static void downloadFileFromURL(String urlString, File destination) throws Exception {
if (System.getenv("MVNW_USERNAME") != null && System.getenv("MVNW_PASSWORD") != null) {
String username = System.getenv("MVNW_USERNAME");
char[] password = System.getenv("MVNW_PASSWORD").toCharArray();
Authenticator.setDefault(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
URL website = new URL(urlString);
ReadableByteChannel rbc;
rbc = Channels.newChannel(website.openStream());
FileOutputStream fos = new FileOutputStream(destination);
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
fos.close();
rbc.close();
});
}
URL website = new URL(urlString);
ReadableByteChannel rbc;
rbc = Channels.newChannel(website.openStream());
FileOutputStream fos = new FileOutputStream(destination);
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
fos.close();
rbc.close();
}

}
23 changes: 12 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,31 @@
As input it takes an SDK view template EFX file and a notice XML file.
The output is HTML showing labels, values and so on.


## Building

### Required technologies

* Java 11, higher may work for example Java 15 worked
* Maven 3.8.2, lower or higher may work
* Java 11, higher may work for example Java 15 works
* Maven 3.8.2 as a reference but lower or higher may work

### Required projects

### Required other projects
You will need https://citnet.tech.ec.europa.eu/CITnet/stash/projects/TEDEFO/repos/eforms-expression-language/browse (git clone, see README of that project)
After the setup, run `mvn clean install` in this project from the same folder as the `pom.xml`

1. https://citnet.tech.ec.europa.eu/CITnet/stash/projects/TEDEFO/repos/eforms-sdk-lib/browse (git clone and `mvn clean install`)
2. https://citnet.tech.ec.europa.eu/CITnet/stash/projects/TEDEFO/repos/eforms-expression-language/browse (git clone and `mvn clean install`)
3. After the setup of dependencies, run `mvn clean install` in this project from the same folder as the README.md

## Command line

Usage: `<xml file to view> <two letter language code> [<view id to use>]`
Usage: `<two letter language code> <xml file to view (without .xml)> [<view id to use>]`

Example: I want to generate HTML for the file `X02_registration.xml` with `english` (en) labels.
### Example

Example running it using Maven:
I want to generate an HTML to view the file `X02_registration.xml` in `en` (english). Running it using Maven:

```
mvn compile exec:java -Dspring.datasource.username=${EFORMS_DATABASE_USERNAME} -Dspring.datasource.password=${EFORMS_DATABASE_PASSWORD} -Dexec.mainClass="eu.europa.ted.eforms.viewer.Application" -Dexec.args="en X02_registration"
```

Read the logs in the console for details about the location of the generated XSL, HTML, ...
For technical details see Application.java for the entry point, also see unit tests
After running it read the logs in the console for details about the location of the generated XSL, HTML, ...
For technical details see `Application.java` for the entry point, also see unit tests for usage.
46 changes: 46 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,20 @@
<version>11.3</version>
</dependency>

<!-- For genericode .gc files (SDK codelist related) -->
<dependency>
<groupId>com.helger</groupId>
<artifactId>ph-genericode</artifactId>
<version>6.2.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.jsoup/jsoup -->
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.14.3</version>
</dependency>

</dependencies>

<build>
Expand Down Expand Up @@ -91,6 +99,44 @@
</plugin>

</plugins>

<pluginManagement>
<plugins>

<plugin>
<!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-checkstyle-plugin -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.1.2</version>
<dependencies>
<dependency>
<groupId>com.puppycrawl.tools</groupId>
<artifactId>checkstyle</artifactId>
<!-- NOTE: versions before 8 something got confused by lambdas, luckily 9 solves this. -->
<version>9.0</version>
</dependency>
</dependencies>
<configuration>
<configLocation>/src/main/resources/checkstyle-step2.xml
</configLocation>
<encoding>UTF-8</encoding>
<consoleOutput>true</consoleOutput>
<failsOnError>false</failsOnError>
<linkXRef>false</linkXRef>
</configuration>
<executions>
<execution>
<id>validate</id>
<phase>validate</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>

</plugins>
</pluginManagement>
</build>


Expand Down
9 changes: 6 additions & 3 deletions src/main/java/eu/europa/ted/eforms/viewer/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
import org.slf4j.LoggerFactory;
import org.xml.sax.SAXException;

/**
* Entry point.
*/
public class Application {

private static final Logger logger = LoggerFactory.getLogger(Application.class);
Expand All @@ -25,7 +28,7 @@ public static void main(final String[] args)

logger.info("eForms Notice Viewer");
logger.info(
"Usage: <xml file to view (without .xml)> <two letter language code> [<view id to use>]");
"Usage: <two letter language code> <xml file to view (without .xml)> [<view id to use>]");
logger.info("Example: en X02_registrations");
logger.info("args={}", Arrays.toString(args));

Expand All @@ -39,10 +42,10 @@ public static void main(final String[] args)
"Language: expecting two letter code like 'en', 'fr', ..., but found '%s'", language));
}

final String noticeXml = args[1];
final String noticeXmlName = args[1];
final Optional<String> viewIdOpt = args.length > 2 ? Optional.of(args[2]) : Optional.empty();

final Path htmlPath = NoticeViewer.generateHtml(language, noticeXml, viewIdOpt);
final Path htmlPath = NoticeViewer.generateHtml(language, noticeXmlName, viewIdOpt);
logger.info("Created HTML file: {}", htmlPath);
System.exit(0);
}
Expand Down
19 changes: 5 additions & 14 deletions src/main/java/eu/europa/ted/eforms/viewer/CustomUriResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,12 @@
import eu.europa.ted.eforms.viewer.helpers.SafeDocumentBuilder;

public final class CustomUriResolver implements URIResolver {

/**
* Currently this allows to load the labels.
*/
@Override
public Source resolve(final String href, final String base) {
// TODO lookup in SDK ?
// IDEALLY WE PASS THE LANGUAGE TO THE XSD AS A PARAMETER.

// final String newHref;
// if ("labels.xml".equals(href)) {
// newHref = "eforms-sdk/translations/en.xml";
// } else {
// newHref = href;
// }

try (InputStream is = ResourceLoader.getResourceAsStream("eforms-sdk/translations/" + href)) {
if (is == null) {
throw new RuntimeException(
Expand All @@ -33,15 +27,12 @@ public Source resolve(final String href, final String base) {
// DOM parser based.
final DocumentBuilder builder = SafeDocumentBuilder.buildSafeDocumentBuilderAllowDoctype();
final Document document = builder.parse(is);

return new DOMSource(document);

} catch (SAXException | IOException | ParserConfigurationException e) {
throw new RuntimeException(e.toString(), e);
}

// An alternative is to use a StreamSource, this uses less memory.
// This can fail when it tries to load a dtd (proxy ...).
// This can fail when it tries to load a dtd (proxy issues ...).
// return new StreamSource(is);
}

Expand Down
Loading

0 comments on commit bcb4e91

Please sign in to comment.