Permalink
Browse files

Merge branch 'atom-roundtripping'

  • Loading branch information...
dhanji committed May 22, 2012
2 parents e396f24 + 5151390 commit f365996162e0190338386ef5175ff3a94ae04839
@@ -16,13 +16,6 @@
package org.sonatype.maven.polyglot.atom;
-import java.io.IOException;
-import java.io.PrintWriter;
-import java.io.Writer;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Map;
-
import org.apache.maven.model.Dependency;
import org.apache.maven.model.Model;
import org.apache.maven.model.Plugin;
@@ -34,6 +27,13 @@
import org.codehaus.plexus.util.xml.Xpp3Dom;
import org.sonatype.maven.polyglot.io.ModelWriterSupport;
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.io.Writer;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
@Component(role = ModelWriter.class, hint = "atom")
public class AtomModelWriter extends ModelWriterSupport {
@@ -51,11 +51,11 @@ public void write(final Writer output, final Map<String, Object> options, final
project(pw, model);
id(pw, model);
parent(pw, model);
- packaging(pw, model);
properties(pw, model);
dependencyManagement(pw, model);
dependencies(pw, model);
modules(pw, model);
+ pw.println();
pluginManagement(pw, model);
plugins(pw, model);
@@ -83,7 +83,9 @@ private void project(PrintWriter pw, Model model) {
if (name == null) {
name = model.getArtifactId();
}
- pw.println("project \"" + name + "\" @ \"" + model.getUrl() + "\"");
+ String url = model.getUrl() == null ? "" : model.getUrl();
+ pw.print("project \"" + name + "\" @ \"" + url + "\"");
+ packaging(pw, model);
}
private void id(PrintWriter pw, Model model) {
@@ -101,19 +103,17 @@ private void id(PrintWriter pw, Model model) {
private void parent(PrintWriter pw, Model model) {
if (model.getParent() != null) {
- pw.print(indent + "inherit: " + model.getParent().getGroupId() + ":" + model.getParent().getArtifactId() + ":" + model.getParent().getVersion());
+ pw.print(indent + "inherits: " + model.getParent().getGroupId() + ":" + model.getParent().getArtifactId() + ":" + model.getParent().getVersion());
if (model.getParent().getRelativePath() != null) {
//pw.println(":" + model.getParent().getRelativePath());
- pw.println(":" + "../pom.atom");
- } else {
- pw.println();
+// pw.println(":" + "../pom.atom");
}
+ pw.println();
}
}
private void packaging(PrintWriter pw, Model model) {
- pw.println(indent + "packaging: " + model.getPackaging());
- pw.println();
+ pw.println(" as " + model.getPackaging());
}
private void properties(PrintWriter pw, Model model) {
@@ -134,7 +134,6 @@ private void properties(PrintWriter pw, Model model) {
}
}
pw.println(" ]");
- pw.println();
}
}
@@ -145,15 +144,14 @@ private void modules(PrintWriter pw, Model model) {
for (int i = 0; i < modules.size(); i++) {
String module = modules.get(i);
if (i != 0) {
- pw.print(" ");
+ pw.print(indent + " ");
}
pw.print(module);
if (i + 1 != modules.size()) {
pw.println();
}
}
pw.println(" ]");
- pw.println();
}
}
@@ -182,17 +180,16 @@ private void deps(PrintWriter pw, String elementName, List<Dependency> deps) {
// We are assuming the model is well-formed and that the parent is providing a version
// for this particular dependency.
//
- pw.print(d.getGroupId() + ":" + d.getArtifactId());
+ pw.print(d.getGroupId() + ":" + d.getArtifactId());
}
if (d.getClassifier() != null) {
pw.print("(" + d.getClassifier() + ")");
- }
+ }
if (i + 1 != deps.size()) {
pw.println();
}
}
pw.println(" ]");
- pw.println();
}
}
@@ -211,40 +208,57 @@ private void plugins(PrintWriter pw, Model model) {
// need to write nested objects
private void plugins(PrintWriter pw, String elementName, List<Plugin> plugins) {
if (!plugins.isEmpty()) {
- pw.print(indent + elementName + ": [ ");
for (int i = 0; i < plugins.size(); i++) {
Plugin plugin = plugins.get(i);
- if (i != 0) {
- pw.print(" ");
- }
- pw.print(plugin.getGroupId() + ":" + plugin.getArtifactId() + ":" + plugin.getVersion());
+ pw.println("plugin");
+
+ pw.print(indent + "id: " + plugin.getGroupId() + ":" + plugin.getArtifactId());
+ if (plugin.getVersion() != null)
+ pw.print(":" + plugin.getVersion());
if (plugin.getConfiguration() != null) {
pw.println();
Xpp3Dom configuration = (Xpp3Dom) plugin.getConfiguration();
- if (configuration.getChildCount() != 0) {
- pw.print(" properties:[ ");
- int count = configuration.getChildCount();
- for (int j = 0; j < count; j++) {
- Xpp3Dom c = configuration.getChild(j);
- if (c.getValue() != null) {
- if (j != 0) {
- pw.print(" ");
- }
- pw.print(c.getName() + ": " + c.getValue());
- if (j + 1 != count) {
- pw.println();
- }
- }
- }
- pw.print(" ]");
- }
+ printChildren(pw, configuration);
}
if (i + 1 != plugins.size()) {
pw.println();
}
}
- pw.println(" ]");
pw.println();
}
}
-}
+
+ private boolean flipBrackets = false;
+
+ private void printChildren(PrintWriter pw, Xpp3Dom configuration) {
+ if (configuration.getChildCount() > 0) {
+ int count = configuration.getChildCount();
+ for (int j = 0; j < count; j++) {
+ Xpp3Dom c = configuration.getChild(j);
+ if (c.getValue() != null) {
+ pw.print(indent + c.getName() + ": " + c.getValue());
+ if (j + 1 != count) {
+ pw.println();
+ }
+ } else {
+ pw.println(indent + c.getName() + ": " + lbraceket());
+ String oldIndent = indent;
+ indent += " ";
+ flipBrackets = !flipBrackets;
+ printChildren(pw, c);
+ flipBrackets = !flipBrackets;
+ indent = oldIndent;
+ pw.print("\n" + indent + rbraceket());
+ }
+ }
+ }
+ }
+
+ private char lbraceket() {
+ return (flipBrackets ? '{' : '[');
+ }
+
+ private char rbraceket() {
+ return (flipBrackets ? '}' : ']');
+ }
+}
@@ -39,11 +39,13 @@ public AtomParser(ModelSource modelSource, List<Token> tokens) {
}
private void parseException(String message, Throwable t) {
- throw new RuntimeException("Error parsing " + modelSource.getLocation() + ": " + message, t);
+ String location = modelSource != null ? modelSource.getLocation() : "";
+ throw new RuntimeException("Error parsing " + location + ": " + message, t);
}
private void parseException(String message) {
- throw new RuntimeException("Error parsing " + modelSource.getLocation() + ": " + message);
+ String location = modelSource != null ? modelSource.getLocation() : "";
+ throw new RuntimeException("Error parsing " + location + ": " + message);
}
public Project parse() {
@@ -336,7 +338,7 @@ private Plugin plugin() {
return null;
}
- Id pluginId = id();
+ Id pluginId = id(true);
if (pluginId == null) {
log.severe("Plugin id declaration malformed");
return null;
@@ -409,9 +411,13 @@ private Xpp3Dom toXpp3DomTree(String name, Map<String, Object> config) {
match(Kind.EOL);
} else {
// This is a multilevel thing, recurse!
- if (match(Kind.LBRACKET) != null) {
+ if (anyOf(Kind.LBRACKET, Kind.LBRACE) != null) {
+ chewEols();
+ chewIndents();
Map<String, Object> childProps = configurationMap();
- if (match(Kind.RBRACKET) == null)
+ chewEols();
+ chewIndents();
+ if (match(Kind.RBRACKET) == null && match(Kind.RBRACE) == null)
parseException("Expected ']' after configuration properties");
// stash into parent map.
@@ -468,7 +474,7 @@ private Xpp3Dom toXpp3DomTree(String name, Map<String, Object> config) {
chewIndents();
String module;
- while ((module = idFragment()).length() != 0) {
+ while ((module = idFragment()) != null) {
chewEols();
chewIndents();
modules.add(module);
@@ -556,7 +562,7 @@ private Id id(boolean allowNullVersion) {
return null;
} else {
version = idFragment();
- if (version == null) {
+ if (version == null && !allowNullVersion) {
return null;
}
}
@@ -647,6 +653,23 @@ private String idFragment() {
}
}
+ // Try parsing as property expression.
+ if (fragment.length() == 1 && fragment.charAt(0) == '$') {
+ List<Token> startOfExpr = match(Kind.LBRACE);
+ if (startOfExpr == null)
+ return null;
+
+ fragment.append("{");
+ String prop = idFragment();
+ if (prop == null)
+ parseException("Expected a property expression after ${");
+
+ if (match(Kind.RBRACE) == null)
+ parseException("Expected '}' after property expression");
+
+ fragment.append(prop).append("}");
+ }
+
// Nothing matched.
if (fragment.length() == 0)
return null;
@@ -44,6 +44,8 @@ public Token(String value, Kind kind) {
RPAREN,
LBRACKET,
RBRACKET,
+ LBRACE,
+ RBRACE,
// keywords
REQUIRE,
@@ -93,8 +95,8 @@ public Token(String value, Kind kind) {
TOKEN_MAP.put(")", RPAREN);
TOKEN_MAP.put("[", LBRACKET);
TOKEN_MAP.put("]", RBRACKET);
- TOKEN_MAP.put("{", LBRACKET); // Alias braces as brackets.
- TOKEN_MAP.put("}", RBRACKET);
+ TOKEN_MAP.put("{", LBRACE); // Alias braces as brackets.
+ TOKEN_MAP.put("}", RBRACE);
TOKEN_MAP.put("\n", EOL);
TOKEN_MAP.put("id", ID);
@@ -159,4 +161,4 @@ public String toString() {
", kind=" + kind +
'}';
}
-}
+}
@@ -24,7 +24,7 @@
@Named("${basedir}/src/test/poms")
private File poms;
- public void _testAtomModelWriter() throws Exception {
+ public void testAtomModelWriter() throws Exception {
File pom = new File(poms, "sitebricks-parent-pom.xml");
MavenXpp3Reader xmlModelReader = new MavenXpp3Reader();
Model xmlModel = xmlModelReader.read(new FileInputStream(pom));
@@ -56,6 +56,9 @@ public void _testAtomModelWriter() throws Exception {
MavenXpp3Writer xmlWriter = new MavenXpp3Writer();
xmlWriter.write(w, xmlModel);
System.out.println(w.toString());
+
+ assertEquals(xmlModel.getModules(), atomModel.getModules());
+ assertEquals(xmlModel.getRepositories().size(), atomModel.getRepositories().size());
}
public void testAtomModelWriterWhereModelHasDependenciesWithNoVersions() throws Exception {
@@ -82,9 +85,7 @@ public void testAtomModelWriterWhereModelHasDependenciesWithNoVersions() throws
//
// Test for fidelity
//
- //assertNotNull(atomModel);
-
-
+ assertNotNull(atomModel);
}
void testMavenModelForCompleteness(Model model) {
@@ -138,5 +139,4 @@ String gav(Dependency d) {
String gav(Plugin p) {
return p.getGroupId() + ":" + p.getArtifactId() + ":" + p.getVersion();
}
-
}
Oops, something went wrong.

0 comments on commit f365996

Please sign in to comment.