1 package org.apache.maven.project.interpolation; 2 3 18 19 import org.apache.maven.model.Model; 20 import org.apache.maven.model.io.xpp3.MavenXpp3Reader; 21 import org.apache.maven.model.io.xpp3.MavenXpp3Writer; 22 import org.codehaus.plexus.logging.AbstractLogEnabled; 23 import org.codehaus.plexus.logging.Logger; 24 import org.codehaus.plexus.util.StringUtils; 25 import org.codehaus.plexus.util.cli.CommandLineUtils; 26 import org.codehaus.plexus.util.introspection.ReflectionValueExtractor; 27 import org.codehaus.plexus.util.xml.pull.XmlPullParserException; 28 29 import java.io.IOException ; 30 import java.io.StringReader ; 31 import java.io.StringWriter ; 32 import java.util.Map ; 33 import java.util.Properties ; 34 import java.util.regex.Matcher ; 35 import java.util.regex.Pattern ; 36 37 44 public class RegexBasedModelInterpolator 45 extends AbstractLogEnabled 46 implements ModelInterpolator 47 { 48 private static final Pattern EXPRESSION_PATTERN = Pattern.compile( "\\$\\{(pom\\.|project\\.|env\\.)?([^}]+)\\}" ); 49 50 private Properties envars; 51 52 public RegexBasedModelInterpolator( Properties envars ) 53 { 54 this.envars = envars; 55 } 56 57 public RegexBasedModelInterpolator() 58 throws IOException 59 { 60 envars = CommandLineUtils.getSystemEnvVars(); 61 } 62 63 public Model interpolate( Model model, Map context ) 64 throws ModelInterpolationException 65 { 66 return interpolate( model, context, true ); 67 } 68 69 79 public Model interpolate( Model model, Map context, boolean strict ) 80 throws ModelInterpolationException 81 { 82 StringWriter sWriter = new StringWriter (); 83 84 MavenXpp3Writer writer = new MavenXpp3Writer(); 85 try 86 { 87 writer.write( sWriter, model ); 88 } 89 catch ( IOException e ) 90 { 91 throw new ModelInterpolationException( "Cannot serialize project model for interpolation.", e ); 92 } 93 94 String serializedModel = sWriter.toString(); 95 serializedModel = interpolateInternal( serializedModel, model, context ); 96 97 StringReader sReader = new StringReader ( serializedModel ); 98 99 MavenXpp3Reader modelReader = new MavenXpp3Reader(); 100 try 101 { 102 model = modelReader.read( sReader ); 103 } 104 catch ( IOException e ) 105 { 106 throw new ModelInterpolationException( 107 "Cannot read project model from interpolating filter of serialized version.", e ); 108 } 109 catch ( XmlPullParserException e ) 110 { 111 throw new ModelInterpolationException( 112 "Cannot read project model from interpolating filter of serialized version.", e ); 113 } 114 115 return model; 116 } 117 118 private String interpolateInternal( String src, Model model, Map context ) 119 throws ModelInterpolationException 120 { 121 String result = src; 122 Matcher matcher = EXPRESSION_PATTERN.matcher( result ); 123 while ( matcher.find() ) 124 { 125 String wholeExpr = matcher.group( 0 ); 126 String realExpr = matcher.group( 2 ); 127 128 Object value = context.get( realExpr ); 129 130 if ( value == null ) 131 { 132 if ( context.containsKey( realExpr ) ) 134 { 135 continue; 137 } 138 139 value = model.getProperties().getProperty( realExpr ); 140 } 141 142 if ( value == null ) 143 { 144 try 145 { 146 value = ReflectionValueExtractor.evaluate( realExpr, model, false ); 150 } 151 catch ( Exception e ) 152 { 153 Logger logger = getLogger(); 154 if ( logger != null ) 155 { 156 logger.debug( "POM interpolation cannot proceed with expression: " + wholeExpr + ". Skipping...", e ); 157 } 158 } 159 } 160 161 if ( value == null ) 162 { 163 value = envars.getProperty( realExpr ); 164 } 165 166 if ( String.valueOf( value ).indexOf( wholeExpr ) > -1 ) 168 { 169 throw new ModelInterpolationException( wholeExpr, model.getId() + " references itself." ); 170 } 171 172 if ( value != null ) 173 { 174 result = StringUtils.replace( result, wholeExpr, String.valueOf( value ) ); 175 matcher.reset( result ); 179 } 180 190 } 191 192 return result; 193 } 194 195 } | Popular Tags |