use of com.devonfw.cobigen.impl.config.entity.io.TemplatesConfiguration in project cobigen by devonfw.
the class TemplatesConfigurationReader method readConfiguration.
/**
* Reads the templates configuration.
*/
private void readConfiguration() {
// workaround to make JAXB work in OSGi context by
// https://github.com/ControlSystemStudio/cs-studio/issues/2530#issuecomment-450991188
final ClassLoader orig = Thread.currentThread().getContextClassLoader();
if (JvmUtil.isRunningJava9OrLater()) {
Thread.currentThread().setContextClassLoader(JAXBContext.class.getClassLoader());
}
try (InputStream in = Files.newInputStream(this.configFilePath)) {
Unmarshaller unmarschaller = JAXBContext.newInstance(TemplatesConfiguration.class).createUnmarshaller();
// Unmarshal without schema checks for getting the version attribute of the root node.
// This is necessary to provide an automatic upgrade client later on
Object rootNode = unmarschaller.unmarshal(in);
if (rootNode instanceof TemplatesConfiguration) {
BigDecimal configVersion = ((TemplatesConfiguration) rootNode).getVersion();
if (configVersion == null) {
throw new InvalidConfigurationException(this.configFilePath.toUri().toString(), "The required 'version' attribute of node \"templatesConfiguration\" has not been set");
} else {
VersionValidator validator = new VersionValidator(Type.TEMPLATES_CONFIGURATION, MavenMetadata.VERSION);
validator.validate(configVersion.floatValue());
}
} else {
throw new InvalidConfigurationException(this.configFilePath.toUri().toString(), "Unknown Root Node. Use \"templatesConfiguration\" as root Node");
}
// If we reach this point, the configuration version and root node has been validated.
// Unmarshal with schema checks for checking the correctness and give the user more hints to
// correct his failures
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
TemplatesConfigurationVersion latestConfigurationVersion = TemplatesConfigurationVersion.getLatest();
try (InputStream schemaStream = getClass().getResourceAsStream("/schema/" + latestConfigurationVersion + "/templatesConfiguration.xsd");
InputStream configInputStream = Files.newInputStream(this.configFilePath)) {
Schema schema = schemaFactory.newSchema(new StreamSource(schemaStream));
unmarschaller.setSchema(schema);
rootNode = unmarschaller.unmarshal(configInputStream);
this.configNode = (TemplatesConfiguration) rootNode;
}
} catch (JAXBException e) {
// try getting SAXParseException for better error handling and user support
Throwable parseCause = ExceptionUtil.getCause(e, SAXParseException.class, UnmarshalException.class);
String message = "";
if (parseCause != null && parseCause.getMessage() != null) {
message = parseCause.getMessage();
}
throw new InvalidConfigurationException(this.configFilePath.toUri().toString(), "Could not parse configuration file:\n" + message, e);
} catch (SAXException e) {
// Should never occur. Programming error.
throw new IllegalStateException("Could not parse templates configuration schema. Please state this as a bug.");
} catch (NumberFormatException e) {
// So provide help
throw new InvalidConfigurationException(this.configFilePath.toUri().toString(), "Invalid version number defined. The version of the templates configuration should consist of 'major.minor' version.", e);
} catch (IOException e) {
throw new InvalidConfigurationException(this.configFilePath.toUri().toString(), "Could not read templates configuration file.", e);
} finally {
Thread.currentThread().setContextClassLoader(orig);
}
}
Aggregations