Permalink
Browse files

Configuration is parsing OK for atom grammar (1-level deep)

  • Loading branch information...
1 parent eba8dfd commit dbed90a137b451080905620a5c4d475dea1fc257 @dhanji dhanji committed Apr 19, 2012
@@ -11,8 +11,10 @@
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
+import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
+import java.util.Map.Entry;
import java.util.logging.Logger;
/**
@@ -59,8 +61,9 @@ public Project parse() {
/**
* Parsing rule for a single project build definition.
- * <p/>
- * project := 'project' STRING AT URL EOL
+ *
+ * project := 'project' STRING (AT URL)? ('as' PACKAGING)? EOL
+ * (idFragment COLON list EOL)+
*/
private Project project(Repositories repositories) {
if (match(Token.Kind.PROJECT) == null) {
@@ -140,14 +143,13 @@ private Project project(Repositories repositories) {
chewIndents();
List<String> modules = modules();
+ ScmElement scm = scm();
+
// modules
chewEols();
chewIndents();
List<Plugin> plugins = plugins();
- chewEols();
- ScmElement scm = scm();
-
return new Project(projectId,
parent,
packaging,
@@ -301,46 +303,120 @@ else if ("url".equals(label))
}
/**
- * Dependencies of a project. The real meat of it.
+ * Additional plugins and their configuration.
*/
private List<Plugin> plugins() {
- indent();
- if (match(Token.Kind.PLUGINS, Token.Kind.COLON, Token.Kind.LBRACKET) == null) {
- return null; // no deps.
- }
List<Plugin> plugins = new ArrayList<Plugin>();
chewEols();
- chewIndents();
+ Plugin plugin;
+ while ((plugin = plugin()) != null) {
+ plugins.add(plugin);
- // Slurp up the dep ids.
- Id id;
- while ((id = id()) != null) {
chewEols();
- chewIndents();
- List<Property> properties = properties(Token.Kind.PROPS);
- Plugin plugin = new Plugin();
- plugin.setGroupId(id.getGroup());
- plugin.setArtifactId(id.getArtifact());
- plugin.setVersion(id.getVersion());
- if (properties != null) {
- Xpp3Dom pluginConfiguration = new Xpp3Dom("configuration");
- for (Property p : properties) {
- Xpp3Dom child = new Xpp3Dom(p.getKey());
- child.setValue(p.getValue());
- pluginConfiguration.addChild(child);
- }
- plugin.setConfiguration(pluginConfiguration);
+ }
+
+ return plugins;
+ }
+
+ private Plugin plugin() {
+ if (match(Kind.PLUGIN) == null)
+ return null;
+
+ if (match(Kind.EOL) == null) {
+ parseException("Expected newline after 'plugin' keyword");
+ }
+
+ Plugin plugin = new Plugin();
+ List<Token> propKey;
+
+ chewIndents();
+ if (match(Kind.ID, Kind.COLON) == null) {
+ log.severe("Plugin declaration is missing an 'id' tag");
+ return null;
+ }
+
+ Id pluginId = id();
+ if (pluginId == null) {
+ log.severe("Plugin id declaration malformed");
+ return null;
+ }
+
+ if (match(Kind.EOL) == null) {
+ log.severe("Expected newline after plugin id declaration");
+ return null;
+ }
+
+ // Dont forget to set the id properties.
+ plugin.setGroupId(pluginId.getGroup());
+ plugin.setArtifactId(pluginId.getArtifact());
+ plugin.setVersion(pluginId.getVersion());
+
+ Map<String, Object> config;
+ if ((config = configurationMap()) == null)
+ return plugin;
+
+ // Transform the parsed config map into maven's XPP3 Dom thing.
+ Xpp3Dom xConfig = new Xpp3Dom("configuration");
+ for (Entry<String, Object> entry : config.entrySet()) {
+ if (entry.getValue() instanceof String) {
+ Xpp3Dom node = new Xpp3Dom(entry.getKey());
+ node.setValue(entry.getValue().toString());
+
+ xConfig.addChild(node);
}
- plugins.add(plugin);
}
- if (match(Token.Kind.RBRACKET) == null) {
- // ERROR!
- parseException("Expected ]");
+ plugin.setConfiguration(xConfig);
+
+ return plugin;
+ }
+
+ private Map<String, Object> configurationMap() {
+ Map<String, Object> config = new LinkedHashMap<String, Object>();
+ List<Token> propKey;
+
+ chewIndents();
+ while ((propKey = match(Kind.IDENT, Kind.COLON)) != null) {
+ // Match the rest of the line as either an atom or as another set of properties.
+ String atom = idFragment();
+
+ if (atom == null || StringUtils.isEmpty(atom)) {
+ List<Token> tokens = match(Kind.STRING);
+ if (tokens != null)
+ atom = tokens.get(0).value;
+ }
+
+ if (atom != null) {
+ atom = atom.trim();
+ // Strip quotes.
+ if (atom.startsWith("\"") && atom.endsWith("\""))
+ atom = atom.substring(1, atom.length() - 1);
+
+ config.put(propKey.get(0).value, atom);
+ if (match(Kind.EOL) == null)
+ log.severe("Expected end of line in plugin config");
+ } else {
+ // This is a multilevel thing, recurse!
+ if (match(Kind.LBRACKET) != null) {
+ Map<String, Object> childProps = configurationMap();
+ if (match(Kind.RBRACKET) == null)
+ parseException("Expected ']' after configuration properties");
+
+ // stash into parent map.
+ config.put(propKey.get(0).value, childProps);
+
+ } else {
+ // ignore. Later we can force-parse this as a string.
+ log.warning("Unknown element type in plugin declaration");
+ return null;
+ }
+ }
+
+ chewIndents();
}
- return plugins;
+ return config;
}
private List<Property> properties(Token.Kind kind) {
@@ -535,18 +611,26 @@ private String relativePath() {
private String idFragment() {
StringBuilder fragment = new StringBuilder();
- List<Token> idFragment;
- while ((idFragment = match(Token.Kind.IDENT)) != null) {
- fragment.append(idFragment.get(0).value);
- if (match(Token.Kind.PROJECT) != null) {
- fragment.append("project");
- }
+ Token idFragment;
+ while ((idFragment = anyOf(
+ Token.Kind.IDENT,
+ Kind.PLUGIN,
+ Kind.PROJECT,
+ Kind.DEPS,
+ Kind.SCM,
+ Kind.SRCS,
+ Kind.MODULES,
+ Kind.ID,
+ Kind.PACKAGING,
+ Kind.PARENT,
+ Kind.OVERRIDES,
+ Kind.REPOSITORIES,
+ Kind.PROPS
+ )) != null) {
+ fragment.append(idFragment.value);
if (match(Token.Kind.DOT) != null) {
fragment.append('.');
}
- if (match(Token.Kind.PLUGINS) != null) {
- fragment.append("plugins");
- }
if (match(Token.Kind.DASH) != null) {
fragment.append('-');
}
@@ -54,6 +54,22 @@ public String getPackaging() {
return packaging;
}
+ public List<Plugin> getPlugins() {
+ return plugins;
+ }
+
+ public List<String> getModules() {
+ return modules;
+ }
+
+ public List<Id> getOverrides() {
+ return overrides;
+ }
+
+ public List<Property> getProperties() {
+ return properties;
+ }
+
public Repositories getRepositories() {
return repositories;
}
@@ -19,10 +19,7 @@ public Token(String value, Kind kind) {
public static enum Kind {
IDENT,
- TYPE_IDENT,
- INTEGER,
STRING,
- REGEX,
DOT,
COLON,
@@ -38,7 +35,6 @@ public Token(String value, Kind kind) {
HASHROCKET,
// Comparison operators
- EQUALS,
LEFT_WAVE,
GREATER,
LEQ,
@@ -61,16 +57,9 @@ public Token(String value, Kind kind) {
OR,
AND,
NOT,
- FOR,
- IN,
- IF,
DEPS,
ID,
PARENT,
- UNLESS,
- WHERE,
-
- WHEN,
// specials
EOL,
@@ -80,7 +69,7 @@ public Token(String value, Kind kind) {
OVERRIDES,
PROPS,
MODULES,
- PLUGINS;
+ PLUGIN;
//DOLLAR,
//PROJECT_DOT_VERSION;
@@ -104,15 +93,12 @@ public Token(String value, Kind kind) {
TOKEN_MAP.put("(", LPAREN);
TOKEN_MAP.put(")", RPAREN);
- //TOKEN_MAP.put("$", DOLLAR);
- //TOKEN_MAP.put("{", LBRACE);
- //TOKEN_MAP.put("}", RBRACE);
TOKEN_MAP.put("[", LBRACKET);
TOKEN_MAP.put("]", RBRACKET);
TOKEN_MAP.put("\n", EOL);
TOKEN_MAP.put("id", ID);
- TOKEN_MAP.put("inherit", PARENT);
+ TOKEN_MAP.put("inherits", PARENT);
TOKEN_MAP.put("as", PACKAGING);
TOKEN_MAP.put("properties", PROPS);
TOKEN_MAP.put("deps", DEPS);
@@ -122,17 +108,12 @@ public Token(String value, Kind kind) {
TOKEN_MAP.put("srcs", SRCS);
TOKEN_MAP.put("scm", SCM);
TOKEN_MAP.put("modules", MODULES);
- TOKEN_MAP.put("plugins", PLUGINS );
-
- TOKEN_MAP.put("require", REQUIRE);
+ TOKEN_MAP.put("plugin", PLUGIN);
TOKEN_MAP.put("||", OR);
- TOKEN_MAP.put("or", OR);
TOKEN_MAP.put("&&", AND);
- TOKEN_MAP.put("and", AND);
TOKEN_MAP.put("!", NOT);
- TOKEN_MAP.put("not", NOT);
}
/**
@@ -1,11 +1,14 @@
package org.sonatype.maven.polyglot.atom.parsing;
import junit.framework.TestCase;
+import org.apache.maven.model.Plugin;
import org.apache.maven.model.building.ModelSource;
import org.apache.maven.model.building.StringModelSource;
import org.codehaus.plexus.util.IOUtil;
+import org.codehaus.plexus.util.xml.Xpp3Dom;
import java.net.MalformedURLException;
+import java.util.List;
/**
* @author dhanji@gmail.com (Dhanji R. Prasanna)
@@ -45,6 +48,7 @@ public final void testProjectParsing() {
assertEquals("\"Google Guice\"", project.getDescription());
assertEquals("\"http://code.google.com/p/google-guice\"", project.getUrl());
+ assertEquals("jar", project.getPackaging());
assertEquals("com.google.inject", project.getProjectId().getGroup());
assertEquals("guice", project.getProjectId().getArtifact());
@@ -69,9 +73,30 @@ public final void testProjectParsing() {
assertEquals("\"con:git:git@github.com:mikebrock/mvel.git\"", project.getScm().getConnection());
assertEquals("\"dev:git:git@github.com:mikebrock/mvel.git\"",
project.getScm().getDeveloperConnection());
+
+
+ List<Plugin> plugins = project.getPlugins();
+ assertNotNull(plugins);
+ assertEquals(2, plugins.size());
+
+ Plugin compiler = plugins.get(0);
+ Plugin surefire = plugins.get(1);
+
+ assertEquals("org.apache.maven.plugins:maven-compiler-plugin:2.0.1", compiler.getId());
+ assertEquals("org.apache.maven.plugins:maven-surefire-plugin:2.0.1", surefire.getId());
+
+ Xpp3Dom config = (Xpp3Dom) compiler.getConfiguration();
+ assertTrue(containsChild(config, "source", "1.5"));
+ assertTrue(containsChild(config, "target", "1.5"));
}
- public final void testScmParsing() {
+ private static boolean containsChild(Xpp3Dom node, String key, String value) {
+ for (Xpp3Dom child : node.getChildren()) {
+ if (key.equals(child.getName()) && value.equals(child.getValue()))
+ return true;
+ }
+ System.out.println("No such node - " + key + ": " + value);
+ return false;
}
}
Oops, something went wrong.

0 comments on commit dbed90a

Please sign in to comment.