Skip to content

Commit

Permalink
Merge branch 'atom-roundtripping'
Browse files Browse the repository at this point in the history
  • Loading branch information
dhanji committed May 22, 2012
2 parents e396f24 + 5151390 commit f365996
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 63 deletions.
Expand Up @@ -16,13 +16,6 @@


package org.sonatype.maven.polyglot.atom; 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.Dependency;
import org.apache.maven.model.Model; import org.apache.maven.model.Model;
import org.apache.maven.model.Plugin; import org.apache.maven.model.Plugin;
Expand All @@ -34,6 +27,13 @@
import org.codehaus.plexus.util.xml.Xpp3Dom; import org.codehaus.plexus.util.xml.Xpp3Dom;
import org.sonatype.maven.polyglot.io.ModelWriterSupport; 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") @Component(role = ModelWriter.class, hint = "atom")
public class AtomModelWriter extends ModelWriterSupport { public class AtomModelWriter extends ModelWriterSupport {


Expand All @@ -51,11 +51,11 @@ public void write(final Writer output, final Map<String, Object> options, final
project(pw, model); project(pw, model);
id(pw, model); id(pw, model);
parent(pw, model); parent(pw, model);
packaging(pw, model);
properties(pw, model); properties(pw, model);
dependencyManagement(pw, model); dependencyManagement(pw, model);
dependencies(pw, model); dependencies(pw, model);
modules(pw, model); modules(pw, model);
pw.println();
pluginManagement(pw, model); pluginManagement(pw, model);
plugins(pw, model); plugins(pw, model);


Expand Down Expand Up @@ -83,7 +83,9 @@ private void project(PrintWriter pw, Model model) {
if (name == null) { if (name == null) {
name = model.getArtifactId(); 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) { private void id(PrintWriter pw, Model model) {
Expand All @@ -101,19 +103,17 @@ private void id(PrintWriter pw, Model model) {


private void parent(PrintWriter pw, Model model) { private void parent(PrintWriter pw, Model model) {
if (model.getParent() != null) { 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) { if (model.getParent().getRelativePath() != null) {
//pw.println(":" + model.getParent().getRelativePath()); //pw.println(":" + model.getParent().getRelativePath());
pw.println(":" + "../pom.atom"); // pw.println(":" + "../pom.atom");
} else {
pw.println();
} }
pw.println();
} }
} }


private void packaging(PrintWriter pw, Model model) { private void packaging(PrintWriter pw, Model model) {
pw.println(indent + "packaging: " + model.getPackaging()); pw.println(" as " + model.getPackaging());
pw.println();
} }


private void properties(PrintWriter pw, Model model) { private void properties(PrintWriter pw, Model model) {
Expand All @@ -134,7 +134,6 @@ private void properties(PrintWriter pw, Model model) {
} }
} }
pw.println(" ]"); pw.println(" ]");
pw.println();
} }
} }


Expand All @@ -145,15 +144,14 @@ private void modules(PrintWriter pw, Model model) {
for (int i = 0; i < modules.size(); i++) { for (int i = 0; i < modules.size(); i++) {
String module = modules.get(i); String module = modules.get(i);
if (i != 0) { if (i != 0) {
pw.print(" "); pw.print(indent + " ");
} }
pw.print(module); pw.print(module);
if (i + 1 != modules.size()) { if (i + 1 != modules.size()) {
pw.println(); pw.println();
} }
} }
pw.println(" ]"); pw.println(" ]");
pw.println();
} }
} }


Expand Down Expand Up @@ -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 // We are assuming the model is well-formed and that the parent is providing a version
// for this particular dependency. // for this particular dependency.
// //
pw.print(d.getGroupId() + ":" + d.getArtifactId()); pw.print(d.getGroupId() + ":" + d.getArtifactId());
} }
if (d.getClassifier() != null) { if (d.getClassifier() != null) {
pw.print("(" + d.getClassifier() + ")"); pw.print("(" + d.getClassifier() + ")");
} }
if (i + 1 != deps.size()) { if (i + 1 != deps.size()) {
pw.println(); pw.println();
} }
} }
pw.println(" ]"); pw.println(" ]");
pw.println();
} }
} }


Expand All @@ -211,40 +208,57 @@ private void plugins(PrintWriter pw, Model model) {
// need to write nested objects // need to write nested objects
private void plugins(PrintWriter pw, String elementName, List<Plugin> plugins) { private void plugins(PrintWriter pw, String elementName, List<Plugin> plugins) {
if (!plugins.isEmpty()) { if (!plugins.isEmpty()) {
pw.print(indent + elementName + ": [ ");
for (int i = 0; i < plugins.size(); i++) { for (int i = 0; i < plugins.size(); i++) {
Plugin plugin = plugins.get(i); Plugin plugin = plugins.get(i);
if (i != 0) { pw.println("plugin");
pw.print(" ");
} pw.print(indent + "id: " + plugin.getGroupId() + ":" + plugin.getArtifactId());
pw.print(plugin.getGroupId() + ":" + plugin.getArtifactId() + ":" + plugin.getVersion()); if (plugin.getVersion() != null)
pw.print(":" + plugin.getVersion());
if (plugin.getConfiguration() != null) { if (plugin.getConfiguration() != null) {
pw.println(); pw.println();
Xpp3Dom configuration = (Xpp3Dom) plugin.getConfiguration(); Xpp3Dom configuration = (Xpp3Dom) plugin.getConfiguration();
if (configuration.getChildCount() != 0) { printChildren(pw, configuration);
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(" ]");
}
} }
if (i + 1 != plugins.size()) { if (i + 1 != plugins.size()) {
pw.println(); pw.println();
} }
} }
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 ? '}' : ']');
}
}
Expand Up @@ -39,11 +39,13 @@ public AtomParser(ModelSource modelSource, List<Token> tokens) {
} }


private void parseException(String message, Throwable t) { 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) { 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() { public Project parse() {
Expand Down Expand Up @@ -336,7 +338,7 @@ private Plugin plugin() {
return null; return null;
} }


Id pluginId = id(); Id pluginId = id(true);
if (pluginId == null) { if (pluginId == null) {
log.severe("Plugin id declaration malformed"); log.severe("Plugin id declaration malformed");
return null; return null;
Expand Down Expand Up @@ -409,9 +411,13 @@ private Map<String, Object> configurationMap() {
match(Kind.EOL); match(Kind.EOL);
} else { } else {
// This is a multilevel thing, recurse! // 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(); 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"); parseException("Expected ']' after configuration properties");


// stash into parent map. // stash into parent map.
Expand Down Expand Up @@ -468,7 +474,7 @@ private List<String> modules() {
chewIndents(); chewIndents();


String module; String module;
while ((module = idFragment()).length() != 0) { while ((module = idFragment()) != null) {
chewEols(); chewEols();
chewIndents(); chewIndents();
modules.add(module); modules.add(module);
Expand Down Expand Up @@ -556,7 +562,7 @@ private Id id(boolean allowNullVersion) {
return null; return null;
} else { } else {
version = idFragment(); version = idFragment();
if (version == null) { if (version == null && !allowNullVersion) {
return null; return null;
} }
} }
Expand Down Expand Up @@ -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. // Nothing matched.
if (fragment.length() == 0) if (fragment.length() == 0)
return null; return null;
Expand Down
Expand Up @@ -44,6 +44,8 @@ public static enum Kind {
RPAREN, RPAREN,
LBRACKET, LBRACKET,
RBRACKET, RBRACKET,
LBRACE,
RBRACE,


// keywords // keywords
REQUIRE, REQUIRE,
Expand Down Expand Up @@ -93,8 +95,8 @@ public static enum Kind {
TOKEN_MAP.put(")", RPAREN); TOKEN_MAP.put(")", RPAREN);
TOKEN_MAP.put("[", LBRACKET); TOKEN_MAP.put("[", LBRACKET);
TOKEN_MAP.put("]", RBRACKET); TOKEN_MAP.put("]", RBRACKET);
TOKEN_MAP.put("{", LBRACKET); // Alias braces as brackets. TOKEN_MAP.put("{", LBRACE); // Alias braces as brackets.
TOKEN_MAP.put("}", RBRACKET); TOKEN_MAP.put("}", RBRACE);
TOKEN_MAP.put("\n", EOL); TOKEN_MAP.put("\n", EOL);


TOKEN_MAP.put("id", ID); TOKEN_MAP.put("id", ID);
Expand Down Expand Up @@ -159,4 +161,4 @@ public String toString() {
", kind=" + kind + ", kind=" + kind +
'}'; '}';
} }
} }
Expand Up @@ -24,7 +24,7 @@ public class AtomModelWithSBTest extends InjectedTestCase {
@Named("${basedir}/src/test/poms") @Named("${basedir}/src/test/poms")
private File poms; private File poms;


public void _testAtomModelWriter() throws Exception { public void testAtomModelWriter() throws Exception {
File pom = new File(poms, "sitebricks-parent-pom.xml"); File pom = new File(poms, "sitebricks-parent-pom.xml");
MavenXpp3Reader xmlModelReader = new MavenXpp3Reader(); MavenXpp3Reader xmlModelReader = new MavenXpp3Reader();
Model xmlModel = xmlModelReader.read(new FileInputStream(pom)); Model xmlModel = xmlModelReader.read(new FileInputStream(pom));
Expand Down Expand Up @@ -56,6 +56,9 @@ public void _testAtomModelWriter() throws Exception {
MavenXpp3Writer xmlWriter = new MavenXpp3Writer(); MavenXpp3Writer xmlWriter = new MavenXpp3Writer();
xmlWriter.write(w, xmlModel); xmlWriter.write(w, xmlModel);
System.out.println(w.toString()); System.out.println(w.toString());

assertEquals(xmlModel.getModules(), atomModel.getModules());
assertEquals(xmlModel.getRepositories().size(), atomModel.getRepositories().size());
} }


public void testAtomModelWriterWhereModelHasDependenciesWithNoVersions() throws Exception { public void testAtomModelWriterWhereModelHasDependenciesWithNoVersions() throws Exception {
Expand All @@ -82,9 +85,7 @@ public void testAtomModelWriterWhereModelHasDependenciesWithNoVersions() throws
// //
// Test for fidelity // Test for fidelity
// //
//assertNotNull(atomModel); assertNotNull(atomModel);


} }


void testMavenModelForCompleteness(Model model) { void testMavenModelForCompleteness(Model model) {
Expand Down Expand Up @@ -138,5 +139,4 @@ String gav(Dependency d) {
String gav(Plugin p) { String gav(Plugin p) {
return p.getGroupId() + ":" + p.getArtifactId() + ":" + p.getVersion(); return p.getGroupId() + ":" + p.getArtifactId() + ":" + p.getVersion();
} }

} }

0 comments on commit f365996

Please sign in to comment.