| #* |
| Licensed to the Apache Software Foundation (ASF) under one |
| or more contributor license agreements. See the NOTICE file |
| distributed with this work for additional information |
| regarding copyright ownership. The ASF licenses this file |
| to you 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 |
| |
| http://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. |
| *# |
| #set ( $package = "org.apache.maven.hocon" ) |
| #set ( $className = "HoconReader" ) |
| # |
| #set ( $root = $model.getClass( $model.getRoot($version), $version ) ) |
| #set ( $rootXml = $Helper.xmlClassMetadata( $root ) ) |
| #set ( $rootTag = $rootXml.tagName ) |
| #set ( $rootUcapName = $Helper.capitalise( $root.name ) ) |
| #set ( $rootLcapName = $Helper.uncapitalise( $root.name ) ) |
| # |
| #MODELLO-VELOCITY#SAVE-OUTPUT-TO ${package.replace('.','/')}/${className}.java |
| // =================== DO NOT EDIT THIS FILE ==================== |
| // Generated by Modello Velocity from ${template} |
| // template, any modifications will be overwritten. |
| // ============================================================== |
| package ${package}; |
| |
| import java.io.IOException; |
| import java.io.InputStream; |
| import java.io.Reader; |
| import java.text.DateFormat; |
| import java.util.ArrayList; |
| import java.util.Date; |
| import java.util.HashMap; |
| import java.util.HashSet; |
| import java.util.LinkedHashMap; |
| import java.util.List; |
| import java.util.Map; |
| import java.util.Properties; |
| import java.util.Set; |
| import java.util.function.Function; |
| import java.util.stream.Collectors; |
| import org.apache.maven.api.annotations.Generated; |
| #foreach ( $class in $model.allClasses ) |
| import ${packageModelV4}.${class.name}; |
| #end |
| import com.typesafe.config.ConfigList; |
| import com.typesafe.config.ConfigObject; |
| import com.typesafe.config.ConfigValue; |
| import com.typesafe.config.ConfigValueType; |
| |
| @Generated |
| public class ${className} { |
| |
| #foreach ( $class in $model.allClasses ) |
| #if ( $class.name != "InputSource" && $class.name != "InputLocation" ) |
| #set ( $classUcapName = $Helper.capitalise( $class.name ) ) |
| #set ( $classLcapName = $Helper.uncapitalise( $class.name ) ) |
| #set ( $ancestors = $Helper.ancestors( $class ) ) |
| #set ( $allFields = [] ) |
| #foreach ( $cl in $ancestors ) |
| #set ( $dummy = $allFields.addAll( $cl.getFields($version) ) ) |
| #end |
| public ${classUcapName} parse${classUcapName}(ConfigValue value) { |
| if (value instanceof ConfigObject) { |
| ${classUcapName}.Builder ${classLcapName} = ${classUcapName}.newBuilder(true); |
| ((ConfigObject) value).forEach((k, v) -> { |
| switch (k) { |
| #foreach ( $field in $allFields ) |
| #if ( ! $Helper.xmlFieldMetadata( $field ).transient && $field.name != "root" ) |
| #set ( $fieldTagName = $Helper.xmlFieldMetadata( $field ).tagName ) |
| #if ( ! $fieldTagName ) |
| #set ( $fieldTagName = $field.name ) |
| #end |
| #if ( $Helper.isFlatItems( $field ) ) |
| #set ( $fieldTagName = $Helper.singular( $fieldTagName ) ) |
| #end |
| #set ( $fieldCapName = $Helper.capitalise( $field.name ) ) |
| case "${fieldTagName}": { |
| #if ( $field.type == "String" ) |
| ${classLcapName}.${field.name}(getStringValue(v)); |
| break; |
| #elseif ( $field.type == "boolean" || $field.type == "Boolean" ) |
| ${classLcapName}.${field.name}(getBooleanValue(v)); |
| break; |
| #elseif ( $field.type == "int" ) |
| ${classLcapName}.${field.name}(getIntegerValue(v)); |
| break; |
| #elseif ( $field.type == "DOM" ) |
| // ${classLcapName}.${field.name}(XmlNodeBuilder.build(parser, true)); |
| break; |
| #elseif ( $field.type == "java.util.List" && $field.to == "String" && $field.multiplicity == "*" ) |
| ${classLcapName}.${field.name}(getStringList(v)); |
| break; |
| #elseif ( $field.type == "java.util.Properties" && $field.to == "String" && $field.multiplicity == "*" ) |
| ${classLcapName}.${field.name}(getStringMap(v)); |
| break; |
| #elseif ( $field.to && $field.multiplicity == "1" ) |
| ${classLcapName}.${field.name}(parse${field.toClass.name}(v)); |
| break; |
| #elseif ( $field.to && $field.multiplicity == "*" && $Helper.isFlatItems( $field ) ) |
| ${field.name}.add(parse${field.toClass.name}(v)); |
| break; |
| #elseif ( $field.to && $field.multiplicity == "*" ) |
| ${classLcapName}.${field.name}(getList(v, this::parse${field.toClass.name})); |
| break; |
| #else |
| // TODO: type=${field.type} to=${field.to} multiplicity=${field.multiplicity} |
| break; |
| #end |
| } |
| #end |
| #end |
| default: { |
| checkUnknownElement(k, v); |
| break; |
| } |
| } |
| }); |
| #foreach ( $field in $allFields ) |
| #if ( $Helper.isFlatItems( $field ) ) |
| ${classLcapName}.${field.name}(${field.name}); |
| #end |
| #end |
| return ${classLcapName}.build(); |
| #if($class.name=="Dependency") |
| } else if (value != null && value.valueType() == ConfigValueType.STRING) { |
| Dependency.Builder dependency = Dependency.newBuilder(true); |
| String[] tokens = ((String) value.unwrapped()).split(":"); |
| if (tokens.length < 3 || tokens.length > 5) { |
| throw new RuntimeException("Invalid artifact, you must specify " |
| + "groupId:artifactId:version[:packaging[:classifier]] " + value); |
| } |
| dependency.groupId(tokens[0]); |
| dependency.artifactId(tokens[1]); |
| dependency.version(tokens[2]); |
| if (tokens.length >= 4) { |
| dependency.type(tokens[3]); |
| } |
| if (tokens.length == 5) { |
| dependency.classifier(tokens[4]); |
| } |
| return dependency.build(); |
| #end |
| } else if (value != null) { |
| throw new IllegalArgumentException("Invalid syntax: cannot parse: " + value); |
| } |
| return null; |
| } |
| |
| #end |
| #end |
| |
| protected String getStringValue(ConfigValue v) { |
| return v.unwrapped().toString(); |
| } |
| |
| protected List<String> getStringList(ConfigValue v) { |
| if (v instanceof ConfigList) { |
| return ((ConfigList) v).unwrapped().stream().map(Object::toString) |
| .collect(Collectors.toList()); |
| } |
| throw new IllegalArgumentException("Unable to convert to List<String>: '" + v + "'"); |
| } |
| |
| protected boolean getBooleanValue(ConfigValue v) { |
| return Boolean.parseBoolean(v.unwrapped().toString()); |
| } |
| |
| protected ConfigList getList(ConfigValue v) { |
| if (v instanceof ConfigList) { |
| return (ConfigList) v; |
| } |
| throw new IllegalArgumentException("Unable to convert to List<?>: '" + v + "'"); |
| } |
| |
| protected <T> List<T> getList(ConfigValue v, Function<ConfigValue, T> parser) { |
| return getList(v).stream().map(parser).collect(Collectors.toList()); |
| } |
| |
| protected Map<String, String> getStringMap(ConfigValue v) { |
| if (v instanceof ConfigObject) { |
| return ((ConfigObject) v).entrySet().stream().collect(Collectors.toMap( |
| e -> e.getKey(), |
| e -> getStringValue(e.getValue()) |
| )); |
| } |
| throw new IllegalArgumentException("Unable to convert to Map<String, String>: '" + v + "'"); |
| } |
| |
| protected void checkUnknownElement(String k, Object v) { |
| throw new IllegalArgumentException("Unrecognized element '" + k + "' with value '" + v + "'"); |
| } |
| |
| } |