Skip to content

Commit

Permalink
Fix all tests and make parser more lenient to packaging
Browse files Browse the repository at this point in the history
  • Loading branch information
dhanji committed Apr 19, 2012
1 parent 8e78ff2 commit eba8dfd
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 99 deletions.
@@ -1,5 +1,12 @@
package org.sonatype.maven.polyglot.atom.parsing;

import org.apache.maven.model.Parent;
import org.apache.maven.model.Plugin;
import org.apache.maven.model.building.ModelSource;
import org.codehaus.plexus.util.StringUtils;
import org.codehaus.plexus.util.xml.Xpp3Dom;
import org.sonatype.maven.polyglot.atom.parsing.Token.Kind;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
Expand All @@ -8,15 +15,9 @@
import java.util.Map;
import java.util.logging.Logger;

import org.apache.maven.model.Parent;
import org.apache.maven.model.Plugin;
import org.apache.maven.model.building.ModelSource;
import org.codehaus.plexus.util.StringUtils;
import org.codehaus.plexus.util.xml.Xpp3Dom;

/**
* Parses the atom token stream into an internal model, which can be emitted as a Maven model.
*
*
* @author dhanji@gmail.com (Dhanji R. Prasanna)
*/
public class AtomParser {
Expand Down Expand Up @@ -52,14 +53,13 @@ public Project parse() {
}

chewEols();
Project project = project(repositories);

return project;
return project(repositories);
}

/**
* Parsing rule for a single project build definition.
*
* <p/>
* project := 'project' STRING AT URL EOL
*/
private Project project(Repositories repositories) {
Expand All @@ -80,6 +80,12 @@ private Project project(Repositories repositories) {
projectUrl = signature.get(1).value;
}

List<Token> packagingTokens = match(Kind.PACKAGING, Kind.IDENT);
String packaging = null;
if (null != packagingTokens) {
packaging = packagingTokens.get(1).value;
}

if (match(Token.Kind.EOL) == null) {
log.severe("Expected end of line after project declaration");
return null;
Expand All @@ -103,34 +109,12 @@ private Project project(Repositories repositories) {
// parent
chewEols();
chewIndents();
if (match(Token.Kind.PARENT) == null) {
log.severe("Expected 'inherit' after id declaration");
return null;
}

// Now expect a colon.
if (match(Token.Kind.COLON) == null) {
log.severe("Expected ':' after 'inherit'");
return null;
}

Parent parent = parent();

// packaging
chewEols();
chewIndents();
if (match(Token.Kind.PACKAGING) == null) {
log.severe("Expected 'packaging' after inherit declaration");
return null;
}

if (match(Token.Kind.COLON) == null) {
log.severe("Expected ':' after 'packaging'");
return null;
}

// packaging
String packaging = idFragment();

// srcs
chewEols();
Expand Down Expand Up @@ -164,7 +148,19 @@ private Project project(Repositories repositories) {
chewEols();
ScmElement scm = scm();

return new Project(projectId, parent, packaging, properties, repositories, projectDescription, projectUrl, overrides, deps, modules, plugins, dirs, scm);
return new Project(projectId,
parent,
packaging,
properties,
repositories,
projectDescription,
projectUrl,
overrides,
deps,
modules,
plugins,
dirs,
scm);
}

private ScmElement scm() {
Expand Down Expand Up @@ -211,7 +207,6 @@ else if ("url".equals(label))

/**
* Custom directory structure for maven builds.
*
*/
private Map<String, String> srcs() {
indent();
Expand Down Expand Up @@ -444,8 +439,8 @@ private Property property() {

/**
* Id of a project definition.
*
* id := IDENT (DOT IDENT)* COLON IDENT COLON IDENT EOL
* <p/>
* id := IDENT (DOT IDENT)* COLON IDENT (COLON IDENT)? EOL
*/
private Id id() {
return id(false);
Expand All @@ -469,7 +464,7 @@ private Id id(boolean allowNullVersion) {
}

// Now expect a colon.
String version = null;
String version;
if (match(Token.Kind.COLON) == null && !allowNullVersion) {
return null;
} else {
Expand All @@ -483,27 +478,18 @@ private Id id(boolean allowNullVersion) {
}

private Parent parent() {

String groupId = idFragment();
if (groupId == null) {
if (match(Kind.PARENT) == null)
return null;
}

if (match(Token.Kind.COLON) == null) {
if (match(Kind.COLON) == null) {
log.severe("Expected ':' after 'inherits'");
return null;
}

String artifactId = idFragment();
if (artifactId == null) {
return null;
}

if (match(Token.Kind.COLON) == null) {
return null;
}
Id parentId = id(true);

String version = idFragment();
if (version == null) {
if (parentId == null) {
log.severe("Expected complete artifact identifier in 'parent' clause");
return null;
}

Expand All @@ -516,9 +502,9 @@ private Parent parent() {
}

Parent parent = new Parent();
parent.setGroupId(groupId);
parent.setArtifactId(artifactId);
parent.setVersion(version);
parent.setGroupId(parentId.getGroup());
parent.setArtifactId(parentId.getArtifact());
parent.setVersion(parentId.getVersion());
parent.setRelativePath(relativePath);

return parent;
Expand Down Expand Up @@ -569,38 +555,9 @@ private String idFragment() {
return fragment.toString();
}

// private String variable() {

// if (match(Token.Kind.PROJECT_DOT_VERSION) != null) {
// return "${project.version}";
// }
//
// StringBuilder fragment = new StringBuilder();
// List<Token> idFragment;
// while ((idFragment = match(Token.Kind.DOLLAR, Token.Kind.LBRACE)) != null) {
// fragment.append(idFragment.get(0).value);
// while ((idFragment = match(Token.Kind.IDENT)) != null) {
// fragment.append(idFragment.get(0).value);
// if (match(Token.Kind.DOT) != null) {
// fragment.append('.');
// } else if (match(Token.Kind.DASH) != null) {
// fragment.append('-');
// }
// }
// }
//
// if(match(Token.Kind.RBRACE) != null) {
// fragment.append('}');
// } else {
// return null;
// }
//
// return fragment.toString();
// }

/**
* Optional repositories declaration at the top of the file.
*
* <p/>
* repositories := 'repositories' LEFT_WAVE STRING (COMMA STRING)*
*/
private Repositories repositories() {
Expand All @@ -616,6 +573,7 @@ private Repositories repositories() {
}

// Validate first URL...
//noinspection ConstantConditions
String url = repositories.get(0).value; // Strip ""
repoUrls.add(validateUrl(url));

Expand Down
Expand Up @@ -74,14 +74,14 @@ public static enum Kind {

// specials
EOL,
INDENT,
DASH,
PACKAGING,
OVERRIDES,
INDENT,
DASH,
PACKAGING,
OVERRIDES,
PROPS,
MODULES,
MODULES,
PLUGINS;
//DOLLAR,
//DOLLAR,
//PROJECT_DOT_VERSION;

private static final Map<String, Kind> TOKEN_MAP = new HashMap<String, Kind>();
Expand All @@ -104,7 +104,7 @@ public static enum Kind {

TOKEN_MAP.put("(", LPAREN);
TOKEN_MAP.put(")", RPAREN);
//TOKEN_MAP.put("$", DOLLAR);
//TOKEN_MAP.put("$", DOLLAR);
//TOKEN_MAP.put("{", LBRACE);
//TOKEN_MAP.put("}", RBRACE);
TOKEN_MAP.put("[", LBRACKET);
Expand All @@ -113,16 +113,16 @@ public static enum Kind {

TOKEN_MAP.put("id", ID);
TOKEN_MAP.put("inherit", PARENT);
TOKEN_MAP.put("packaging", PACKAGING);
TOKEN_MAP.put("as", PACKAGING);
TOKEN_MAP.put("properties", PROPS);
TOKEN_MAP.put("deps", DEPS);
TOKEN_MAP.put("overrides", OVERRIDES);
TOKEN_MAP.put("repositories", REPOSITORIES);
TOKEN_MAP.put("project", PROJECT);
TOKEN_MAP.put("srcs", SRCS);
TOKEN_MAP.put("scm", SCM);
TOKEN_MAP.put("modules", MODULES);
TOKEN_MAP.put("plugins", PLUGINS );
TOKEN_MAP.put("scm", SCM);
TOKEN_MAP.put("modules", MODULES);
TOKEN_MAP.put("plugins", PLUGINS );

TOKEN_MAP.put("require", REQUIRE);

Expand Down
@@ -1,6 +1,8 @@
package org.sonatype.maven.polyglot.atom.parsing;

import junit.framework.TestCase;
import org.apache.maven.model.building.ModelSource;
import org.apache.maven.model.building.StringModelSource;
import org.codehaus.plexus.util.IOUtil;

import java.net.MalformedURLException;
Expand All @@ -13,16 +15,18 @@ public class AtomParserTest extends TestCase {
" http://maven.org/central, http://repo1.maven.org/maven2";

private String pom;
private ModelSource modelSource;

@Override
protected void setUp() throws Exception {
this.pom = IOUtil.toString(AtomParserTest.class.getResourceAsStream("example_pom.atom"));
this.modelSource = new StringModelSource(pom);
}

public final void testRepositoryLineMalformedUrls() {
Exception thrown = null;
try {
new AtomParser(new Tokenizer("repositories << \"...\"\n").tokenize()).parse();
new AtomParser(modelSource, new Tokenizer("repositories << \"...\"\n").tokenize()).parse();
fail("Expected exception for Malformed URL");
} catch (RuntimeException e) {
thrown = e;
Expand All @@ -31,13 +35,13 @@ public final void testRepositoryLineMalformedUrls() {
}

public final void testRepositoryLineParsing() {
Project element = new AtomParser(new Tokenizer(pom).tokenize()).parse();
Project element = new AtomParser(modelSource, new Tokenizer(pom).tokenize()).parse();

assertEquals(String.format("[%s]", REPO_URLS), element.getRepositories().toString());
}

public final void testProjectParsing() {
Project project = new AtomParser(new Tokenizer(pom).tokenize()).parse();
Project project = new AtomParser(modelSource, new Tokenizer(pom).tokenize()).parse();

assertEquals("\"Google Guice\"", project.getDescription());
assertEquals("\"http://code.google.com/p/google-guice\"", project.getUrl());
Expand Down

0 comments on commit eba8dfd

Please sign in to comment.