Skip to content

Commit

Permalink
devonfw#1696: applied code reformat
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-vcapgemini committed Aug 10, 2023
1 parent de2371c commit bdaae62
Showing 1 changed file with 28 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,35 +1,39 @@
package com.devonfw.cobigen.impl.config.reader;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.nio.file.Files;
import java.nio.file.Path;

import javax.xml.XMLConstants;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;

import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

import com.devonfw.cobigen.api.exception.InvalidConfigurationException;
import com.devonfw.cobigen.api.util.ExceptionUtil;
import com.devonfw.cobigen.api.util.JvmUtil;
import com.devonfw.cobigen.impl.config.constant.ConfigurationVersionEnum;
import com.devonfw.cobigen.impl.config.constant.MavenMetadata;
import com.devonfw.cobigen.impl.config.versioning.VersionValidator;

import jakarta.xml.bind.JAXBContext;
import jakarta.xml.bind.JAXBException;
import jakarta.xml.bind.UnmarshalException;
import jakarta.xml.bind.Unmarshaller;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

import javax.xml.XMLConstants;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.nio.file.Files;
import java.nio.file.Path;

public abstract class JaxbDeserializer {

/**
* Reads the templates configuration.
*/
<R, E extends ConfigurationVersionEnum> R deserialize(Path file, Class<R> deserializeTo, Class<E> versionEnum, String rootNodeName) {
<R, E extends ConfigurationVersionEnum> R deserialize(Path file, Class<R> deserializeTo, Class<E> versionEnum,
String rootNodeName) {

// workaround to make JAXB work in OSGi context by
// https://github.com/ControlSystemStudio/cs-studio/issues/2530#issuecomment-450991188
Expand All @@ -48,14 +52,15 @@ <R, E extends ConfigurationVersionEnum> R deserialize(Path file, Class<R> deseri
BigDecimal configVersion = (BigDecimal) rootNode.getClass().getDeclaredMethod("getVersion").invoke(rootNode);
if (configVersion == null) {
throw new InvalidConfigurationException(file.toUri().toString(),
"The required 'version' attribute of node \""+ rootNodeName + "\" has not been set");
"The required 'version' attribute of node \"" + rootNodeName + "\" has not been set");
} else {
VersionValidator validator = new VersionValidator(VersionValidator.Type.TEMPLATES_CONFIGURATION, MavenMetadata.VERSION);
VersionValidator validator = new VersionValidator(VersionValidator.Type.TEMPLATES_CONFIGURATION,
MavenMetadata.VERSION);
validator.validate(configVersion.floatValue());
}
} else {
throw new InvalidConfigurationException(file.toUri().toString(),
"Unknown Root Node. Use \""+ rootNodeName + "\" as root Node");
"Unknown Root Node. Use \"" + rootNodeName + "\" as root Node");
}

// If we reach this point, the configuration version and root node has been validated.
Expand All @@ -65,9 +70,9 @@ <R, E extends ConfigurationVersionEnum> R deserialize(Path file, Class<R> deseri

E latestConfigurationVersion = versionEnum.cast(versionEnum.getDeclaredMethod("getLatest").invoke(null));
try (
InputStream schemaStream = getClass()
.getResourceAsStream("/schema/" + latestConfigurationVersion + "/"+rootNodeName+".xsd");
InputStream configInputStream = Files.newInputStream(file)) {
InputStream schemaStream = getClass()
.getResourceAsStream("/schema/" + latestConfigurationVersion + "/" + rootNodeName + ".xsd");
InputStream configInputStream = Files.newInputStream(file)) {

Schema schema = schemaFactory.newSchema(new StreamSource(schemaStream));
unmarschaller.setSchema(schema);
Expand All @@ -82,16 +87,16 @@ <R, E extends ConfigurationVersionEnum> R deserialize(Path file, Class<R> deseri
message = parseCause.getMessage();
}
throw new InvalidConfigurationException(file.toUri().toString(),
"Could not parse configuration file:\n" + message, e);
"Could not parse configuration file:\n" + message, e);
} catch (SAXException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
// Should never occur. Programming error.
throw new IllegalStateException("Could not parse configuration schema. Please state this as a bug.");
} catch (NumberFormatException e) {
// The version number is currently the only xml value which will be parsed to a number data type
// So provide help
throw new InvalidConfigurationException(file.toUri().toString(),
"Invalid version number defined. The version of the configuration should consist of 'major.minor' version.",
e);
"Invalid version number defined. The version of the configuration should consist of 'major.minor' version.",
e);
} catch (IOException e) {
throw new InvalidConfigurationException(file.toUri().toString(), "Could not read configuration file.", e);
} finally {
Expand Down

0 comments on commit bdaae62

Please sign in to comment.