From 778c621ddb07d55e7fe136594f42193741b2d01f Mon Sep 17 00:00:00 2001 From: imad Date: Tue, 26 Apr 2022 01:22:31 +0200 Subject: [PATCH 1/3] Support JSON format for parameter filter files #MRESOURCES-284 --- pom.xml | 10 + .../AbstractMavenFilteringRequest.java | 21 ++ .../maven/shared/filtering/BaseFilter.java | 13 +- .../filtering/MavenResourcesExecution.java | 51 ++++- .../shared/filtering/PropertiesJson.java | 185 ++++++++++++++++++ .../maven/shared/filtering/PropertyUtils.java | 44 ++++- .../filtering/DefaultMavenFileFilterTest.java | 16 +- .../DefaultMavenResourcesFilteringTest.java | 36 ++-- .../shared/filtering/PropertyUtilsTest.java | 77 +++++++- 9 files changed, 416 insertions(+), 37 deletions(-) create mode 100644 src/main/java/org/apache/maven/shared/filtering/PropertiesJson.java diff --git a/pom.xml b/pom.xml index 12434295..c0dff56a 100644 --- a/pom.xml +++ b/pom.xml @@ -63,6 +63,11 @@ Graham Leggett + + @ImadBL + Imad BELMOUJAHID + belmoujahid.i@gmail.com + @@ -95,6 +100,11 @@ maven-shared-utils 3.3.4 + + com.fasterxml.jackson.core + jackson-databind + 2.2.2 + org.codehaus.plexus plexus-utils diff --git a/src/main/java/org/apache/maven/shared/filtering/AbstractMavenFilteringRequest.java b/src/main/java/org/apache/maven/shared/filtering/AbstractMavenFilteringRequest.java index 0b10ab81..5afacebb 100644 --- a/src/main/java/org/apache/maven/shared/filtering/AbstractMavenFilteringRequest.java +++ b/src/main/java/org/apache/maven/shared/filtering/AbstractMavenFilteringRequest.java @@ -28,6 +28,7 @@ import org.apache.maven.project.MavenProject; /** + * @author Imad BELMOUJAHID @ImadBL + */ class BaseFilter extends AbstractLogEnabled implements DefaultFilterInfo @@ -120,7 +123,7 @@ public List getDefaultFilterWrappers( final AbstractMav File basedir = request.getMavenProject() != null ? request.getMavenProject().getBasedir() : new File( "." ); - loadProperties( filterProperties, basedir, request.getFileFilters(), baseProps ); + loadProperties( filterProperties, basedir, request.getFileFilters(), baseProps, request.getRootNode() ); if ( filterProperties.size() < 1 ) { filterProperties.putAll( baseProps ); @@ -138,7 +141,7 @@ public List getDefaultFilterWrappers( final AbstractMav buildFilters.removeAll( request.getFileFilters() ); } - loadProperties( filterProperties, basedir, buildFilters, baseProps ); + loadProperties( filterProperties, basedir, buildFilters, baseProps, request.getRootNode() ); } // Project properties @@ -182,10 +185,11 @@ public List getDefaultFilterWrappers( final AbstractMav } /** + * UseRootThisNode this parameter is null (used only for JSON files) for more information ### MRESOURCES-284 ### * default visibility only for testing reason ! */ void loadProperties( Properties filterProperties, File basedir, List propertiesFilePaths, - Properties baseProps ) + Properties baseProps, String rootNode ) throws MavenFilteringException { if ( propertiesFilePaths != null ) @@ -203,7 +207,8 @@ void loadProperties( Properties filterProperties, File basedir, List pro try { File propFile = FileUtils.resolveFile( basedir, filterFile ); - Properties properties = PropertyUtils.loadPropertyFile( propFile, workProperties, getLogger() ); + Properties properties = PropertyUtils.loadPropertyFile( propFile, workProperties, getLogger(), + rootNode ); filterProperties.putAll( properties ); workProperties.putAll( properties ); } diff --git a/src/main/java/org/apache/maven/shared/filtering/MavenResourcesExecution.java b/src/main/java/org/apache/maven/shared/filtering/MavenResourcesExecution.java index 449e405b..287db33a 100644 --- a/src/main/java/org/apache/maven/shared/filtering/MavenResourcesExecution.java +++ b/src/main/java/org/apache/maven/shared/filtering/MavenResourcesExecution.java @@ -34,7 +34,7 @@ /** * A bean to configure a resources filtering execution. - * + * @author Imad BELMOUJAHID @ImadBL + */ +public class PropertiesJson +{ + /** + * properties + */ + private final Properties properties; + + /** + * default constructor + */ + public PropertiesJson () + { + properties = new Properties (); + } + + /** + * + * @return properties + */ + public Properties getProperties () + { + return properties; + } + + /** + * + * @param fr + * @param useThisRoot + * @throws IOException + */ + public void load ( File fr, String useThisRoot ) + throws IOException + { + try ( FileReader reader = new FileReader ( fr ) ) + { + for ( Map.Entry e + : getPropertiesFromString( IOUtils.toString( reader ), useThisRoot ).entrySet( ) ) + { + + this.properties.put ( e.getKey (), e.getValue () ); + } + } + } + + /** + * + * @param json + * @param useThisRoot + * @return + * @throws JsonProcessingException + */ + private HashMap getPropertiesFromString( String json, String useThisRoot ) + throws IOException + { + int i = 0; + String str = "", r = ""; + HashMap hashMap = new HashMap<>(); + ObjectMapper mapper = new ObjectMapper(); + List keys = new ArrayList<>(); + JsonNode jsonNode = mapper.readTree( json ); + getPropertiesFromJsonNode( hashMap, r, i, str, jsonNode, keys, useThisRoot ); + return hashMap; + } + + /** + * + * @param result + * @param r + * @param i + * @param str + * @param jsonNode + * @param keys + * @param useThisRoot + */ + private void getPropertiesFromJsonNode ( HashMap result, String r, int i, String str, + JsonNode jsonNode, List keys, String useThisRoot ) + { + if ( jsonNode.isObject() ) + { + Iterator fieldNames = jsonNode.fieldNames(); + while ( fieldNames.hasNext() ) + { + String fieldName = fieldNames.next(); + keys.add ( fieldName ); + if ( jsonNode.get( fieldName ).isObject() ) + { + i++; + if ( i != 1 ) + { + if ( str.isEmpty() ) + { + str = fieldName; + } + else + { + str = str.concat ( "." ).concat( fieldName ); + } + } + else + { + r = fieldName; + } + } + getPropertiesFromJsonNode ( result, r, i, str, jsonNode.get( fieldName ), keys, useThisRoot ); + } + //str = ""; + //i = 0; + } + else if ( jsonNode.isArray () ) + { + ArrayNode arrayField = ( ArrayNode ) jsonNode; + for ( JsonNode node : arrayField ) + { + getPropertiesFromJsonNode ( result, r, i, str, node, keys, useThisRoot ) ; + } + } + else + { + String key2; + if ( str.isEmpty () ) + { + key2 = keys.get ( keys.size () - 1 ); + } + else + { + key2 = str.concat ( "." ).concat ( keys.get ( keys.size() - 1 ) ); + } + String s = r + "." + key2; + + if ( null != useThisRoot && !useThisRoot.isEmpty() ) + { + if ( s.startsWith( useThisRoot + "." ) ) + { + result.put ( s.replaceFirst( useThisRoot + ".", "" ), jsonNode.textValue( ) ); + } + } + else + { + result.put ( s, jsonNode.textValue() ); + } + + } + } +} \ No newline at end of file diff --git a/src/main/java/org/apache/maven/shared/filtering/PropertyUtils.java b/src/main/java/org/apache/maven/shared/filtering/PropertyUtils.java index b37f7de4..00e4c86e 100644 --- a/src/main/java/org/apache/maven/shared/filtering/PropertyUtils.java +++ b/src/main/java/org/apache/maven/shared/filtering/PropertyUtils.java @@ -32,6 +32,7 @@ import org.codehaus.plexus.logging.Logger; /** + * @author Kenney Westerhof * @author William Ferguson */ @@ -54,13 +55,14 @@ private PropertyUtils() * * @param propFile The property file to load. * @param baseProps Properties containing the initial values to substitute into the properties file. + * @param rootNode I have added this parameter for new feature with JSON files ### MRESOURCES-284 ### * @return Properties object containing the properties in the file with their values fully resolved. * @throws IOException if profile does not exist, or cannot be read. */ - public static Properties loadPropertyFile( File propFile, Properties baseProps ) + public static Properties loadPropertyFile( File propFile, Properties baseProps, String rootNode ) throws IOException { - return loadPropertyFile( propFile, baseProps, null ); + return loadPropertyFile( propFile, baseProps, null, rootNode ); } /** @@ -73,12 +75,14 @@ public static Properties loadPropertyFile( File propFile, Properties baseProps ) * @param propFile The property file to load. * @param baseProps Properties containing the initial values to substitute into the properties file. * @param logger Logger instance + * @param rootNode I have added this parameter for new feature with JSON files ### MRESOURCES-284 ### * @return Properties object containing the properties in the file with their values fully resolved. * @throws IOException if profile does not exist, or cannot be read. * * @since 3.1.2 */ - public static Properties loadPropertyFile( File propFile, Properties baseProps, Logger logger ) + public static Properties loadPropertyFile( File propFile, Properties baseProps, Logger logger, + String rootNode ) throws IOException { if ( !propFile.exists() ) @@ -90,7 +94,17 @@ public static Properties loadPropertyFile( File propFile, Properties baseProps, try ( InputStream inStream = Files.newInputStream( propFile.toPath() ) ) { - fileProps.load( inStream ); + // I added this control to support JSON files + if ( propFile.getName().endsWith( ".json" ) ) + { + PropertiesJson propertiesJson = new PropertiesJson(); + propertiesJson.load( propFile, rootNode ); + fileProps.putAll( propertiesJson.getProperties() ); + } + else + { + fileProps.load( inStream ); + } } final Properties combinedProps = new Properties(); @@ -122,13 +136,15 @@ public static Properties loadPropertyFile( File propFile, Properties baseProps, * @param propfile The property file to load * @param fail whether to throw an exception when the file cannot be loaded or to return null * @param useSystemProps whether to incorporate System.getProperties settings into the returned Properties object. + * @param rootNode I have added this parameter for new feature with JSON files ### MRESOURCES-284 ### * @return the loaded and fully resolved Properties object * @throws IOException if profile does not exist, or cannot be read. */ - public static Properties loadPropertyFile( File propfile, boolean fail, boolean useSystemProps ) + public static Properties loadPropertyFile( File propfile, boolean fail, boolean useSystemProps, + String rootNode ) throws IOException { - return loadPropertyFile( propfile, fail, useSystemProps, null ); + return loadPropertyFile( propfile, fail, useSystemProps, null, rootNode ); } /** @@ -138,12 +154,14 @@ public static Properties loadPropertyFile( File propfile, boolean fail, boolean * @param fail whether to throw an exception when the file cannot be loaded or to return null * @param useSystemProps whether to incorporate System.getProperties settings into the returned Properties object. * @param logger Logger instance + * @param rootNode I have added this parameter for new feature with JSON files ### MRESOURCES-284 ### * @return the loaded and fully resolved Properties object * @throws IOException if profile does not exist, or cannot be read. * * @since 3.1.2 */ - public static Properties loadPropertyFile( File propfile, boolean fail, boolean useSystemProps, Logger logger ) + public static Properties loadPropertyFile( File propfile, boolean fail, boolean useSystemProps, Logger logger, + String rootNode ) throws IOException { @@ -157,7 +175,17 @@ public static Properties loadPropertyFile( File propfile, boolean fail, boolean final Properties resolvedProps = new Properties(); try { - resolvedProps.putAll( loadPropertyFile( propfile, baseProps, logger ) ); + // I added this control to support JSON files + if ( propfile.getName().endsWith( ".json" ) ) + { + PropertiesJson propertiesJson = new PropertiesJson(); + propertiesJson.load( propfile, rootNode ); + resolvedProps.putAll( propertiesJson.getProperties() ); + } + else + { + resolvedProps.putAll( loadPropertyFile( propfile, baseProps, logger, null ) ); + } } catch ( FileNotFoundException e ) { diff --git a/src/test/java/org/apache/maven/shared/filtering/DefaultMavenFileFilterTest.java b/src/test/java/org/apache/maven/shared/filtering/DefaultMavenFileFilterTest.java index 249aa790..60230267 100644 --- a/src/test/java/org/apache/maven/shared/filtering/DefaultMavenFileFilterTest.java +++ b/src/test/java/org/apache/maven/shared/filtering/DefaultMavenFileFilterTest.java @@ -37,7 +37,7 @@ /** * @author Olivier Lamy - * + * @author Date: Tue, 26 Apr 2022 08:03:37 +0200 Subject: [PATCH 2/3] fix for read node of json file --- .../shared/filtering/PropertiesJson.java | 43 ++++++------------- 1 file changed, 13 insertions(+), 30 deletions(-) diff --git a/src/main/java/org/apache/maven/shared/filtering/PropertiesJson.java b/src/main/java/org/apache/maven/shared/filtering/PropertiesJson.java index 08631fc9..5aa4c998 100644 --- a/src/main/java/org/apache/maven/shared/filtering/PropertiesJson.java +++ b/src/main/java/org/apache/maven/shared/filtering/PropertiesJson.java @@ -93,27 +93,24 @@ public void load ( File fr, String useThisRoot ) private HashMap getPropertiesFromString( String json, String useThisRoot ) throws IOException { - int i = 0; - String str = "", r = ""; + String str = ""; HashMap hashMap = new HashMap<>(); ObjectMapper mapper = new ObjectMapper(); List keys = new ArrayList<>(); JsonNode jsonNode = mapper.readTree( json ); - getPropertiesFromJsonNode( hashMap, r, i, str, jsonNode, keys, useThisRoot ); + getPropertiesFromJsonNode( hashMap, str, jsonNode, keys, useThisRoot ); return hashMap; } /** * * @param result - * @param r - * @param i * @param str * @param jsonNode * @param keys * @param useThisRoot */ - private void getPropertiesFromJsonNode ( HashMap result, String r, int i, String str, + private void getPropertiesFromJsonNode ( HashMap result, String str, JsonNode jsonNode, List keys, String useThisRoot ) { if ( jsonNode.isObject() ) @@ -125,9 +122,6 @@ private void getPropertiesFromJsonNode ( HashMap result, String keys.add ( fieldName ); if ( jsonNode.get( fieldName ).isObject() ) { - i++; - if ( i != 1 ) - { if ( str.isEmpty() ) { str = fieldName; @@ -136,50 +130,39 @@ private void getPropertiesFromJsonNode ( HashMap result, String { str = str.concat ( "." ).concat( fieldName ); } - } - else - { - r = fieldName; - } + getPropertiesFromJsonNode ( result, str, jsonNode.get( fieldName ), keys, useThisRoot ); + str = ""; + } + else + { + getPropertiesFromJsonNode ( result, str, jsonNode.get( fieldName ), keys, useThisRoot ); } - getPropertiesFromJsonNode ( result, r, i, str, jsonNode.get( fieldName ), keys, useThisRoot ); } - //str = ""; - //i = 0; } else if ( jsonNode.isArray () ) { ArrayNode arrayField = ( ArrayNode ) jsonNode; for ( JsonNode node : arrayField ) { - getPropertiesFromJsonNode ( result, r, i, str, node, keys, useThisRoot ) ; + getPropertiesFromJsonNode ( result, str, node, keys, useThisRoot ) ; } } else { - String key2; - if ( str.isEmpty () ) - { - key2 = keys.get ( keys.size () - 1 ); - } - else - { - key2 = str.concat ( "." ).concat ( keys.get ( keys.size() - 1 ) ); - } - String s = r + "." + key2; + String s = str.concat ( "." ).concat ( keys.get ( keys.size() - 1 ) ); if ( null != useThisRoot && !useThisRoot.isEmpty() ) { if ( s.startsWith( useThisRoot + "." ) ) { - result.put ( s.replaceFirst( useThisRoot + ".", "" ), jsonNode.textValue( ) ); + result.put ( s.replaceFirst( "^[a-zA-Z1-9]*.", "" ), jsonNode.textValue( ) ); } } else { result.put ( s, jsonNode.textValue() ); } - + str = ""; } } } \ No newline at end of file From bbb1dfcd7a8cd51741cf402c35100047ffe3f5b4 Mon Sep 17 00:00:00 2001 From: imad Date: Wed, 27 Apr 2022 13:48:16 +0200 Subject: [PATCH 3/3] fix read sup node of json file --- .../shared/filtering/PropertiesJson.java | 48 +++++++++++-------- 1 file changed, 28 insertions(+), 20 deletions(-) diff --git a/src/main/java/org/apache/maven/shared/filtering/PropertiesJson.java b/src/main/java/org/apache/maven/shared/filtering/PropertiesJson.java index 5aa4c998..9e68495c 100644 --- a/src/main/java/org/apache/maven/shared/filtering/PropertiesJson.java +++ b/src/main/java/org/apache/maven/shared/filtering/PropertiesJson.java @@ -74,7 +74,7 @@ public void load ( File fr, String useThisRoot ) { try ( FileReader reader = new FileReader ( fr ) ) { - for ( Map.Entry e + for ( Map.Entry e : getPropertiesFromString( IOUtils.toString( reader ), useThisRoot ).entrySet( ) ) { @@ -90,11 +90,11 @@ public void load ( File fr, String useThisRoot ) * @return * @throws JsonProcessingException */ - private HashMap getPropertiesFromString( String json, String useThisRoot ) + private HashMap getPropertiesFromString( String json, String useThisRoot ) throws IOException { String str = ""; - HashMap hashMap = new HashMap<>(); + HashMap hashMap = new HashMap<>(); ObjectMapper mapper = new ObjectMapper(); List keys = new ArrayList<>(); JsonNode jsonNode = mapper.readTree( json ); @@ -110,7 +110,7 @@ private HashMap getPropertiesFromString( String json, String use * @param keys * @param useThisRoot */ - private void getPropertiesFromJsonNode ( HashMap result, String str, + private void getPropertiesFromJsonNode ( HashMap result, String str, JsonNode jsonNode, List keys, String useThisRoot ) { if ( jsonNode.isObject() ) @@ -122,20 +122,29 @@ private void getPropertiesFromJsonNode ( HashMap result, String keys.add ( fieldName ); if ( jsonNode.get( fieldName ).isObject() ) { - if ( str.isEmpty() ) - { - str = fieldName; - } - else - { - str = str.concat ( "." ).concat( fieldName ); - } - getPropertiesFromJsonNode ( result, str, jsonNode.get( fieldName ), keys, useThisRoot ); - str = ""; + String r; + if ( str != null && str.isEmpty () ) + { + r = str.concat( fieldName ); + } + else + { + r = str.concat( "." ).concat( fieldName ); + } + getPropertiesFromJsonNode ( result, r, jsonNode.get( fieldName ), keys, useThisRoot ); } else { - getPropertiesFromJsonNode ( result, str, jsonNode.get( fieldName ), keys, useThisRoot ); + String r; + if ( str != null && str.isEmpty() ) + { + r = str.concat( fieldName ); + } + else + { + r = str.concat( "." ).concat( fieldName ); + } + getPropertiesFromJsonNode ( result, r, jsonNode.get( fieldName ), keys, useThisRoot ); } } } @@ -149,20 +158,19 @@ else if ( jsonNode.isArray () ) } else { - String s = str.concat ( "." ).concat ( keys.get ( keys.size() - 1 ) ); if ( null != useThisRoot && !useThisRoot.isEmpty() ) { - if ( s.startsWith( useThisRoot + "." ) ) + if ( str.startsWith( useThisRoot + "." ) ) { - result.put ( s.replaceFirst( "^[a-zA-Z1-9]*.", "" ), jsonNode.textValue( ) ); + result.put ( str.replaceFirst( "^[a-zA-Z1-9]*.", "" ), jsonNode.textValue( ) ); } } else { - result.put ( s, jsonNode.textValue() ); + result.put ( str, jsonNode.textValue() ); } - str = ""; + } } } \ No newline at end of file