Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge branch 'atom-roundtripping'

  • Loading branch information...
commit f365996162e0190338386ef5175ff3a94ae04839 2 parents e396f24 + 5151390
Dhanji R. Prasanna authored May 22, 2012
104  tesla-polyglot/tesla-polyglot-atom/src/main/java/org/sonatype/maven/polyglot/atom/AtomModelWriter.java
@@ -16,13 +16,6 @@
16 16
 
17 17
 package org.sonatype.maven.polyglot.atom;
18 18
 
19  
-import java.io.IOException;
20  
-import java.io.PrintWriter;
21  
-import java.io.Writer;
22  
-import java.util.ArrayList;
23  
-import java.util.List;
24  
-import java.util.Map;
25  
-
26 19
 import org.apache.maven.model.Dependency;
27 20
 import org.apache.maven.model.Model;
28 21
 import org.apache.maven.model.Plugin;
@@ -34,6 +27,13 @@
34 27
 import org.codehaus.plexus.util.xml.Xpp3Dom;
35 28
 import org.sonatype.maven.polyglot.io.ModelWriterSupport;
36 29
 
  30
+import java.io.IOException;
  31
+import java.io.PrintWriter;
  32
+import java.io.Writer;
  33
+import java.util.ArrayList;
  34
+import java.util.List;
  35
+import java.util.Map;
  36
+
37 37
 @Component(role = ModelWriter.class, hint = "atom")
38 38
 public class AtomModelWriter extends ModelWriterSupport {
39 39
 
@@ -51,11 +51,11 @@ public void write(final Writer output, final Map<String, Object> options, final
51 51
     project(pw, model);
52 52
     id(pw, model);
53 53
     parent(pw, model);
54  
-    packaging(pw, model);
55 54
     properties(pw, model);
56 55
     dependencyManagement(pw, model);
57 56
     dependencies(pw, model);
58 57
     modules(pw, model);
  58
+    pw.println();
59 59
     pluginManagement(pw, model);
60 60
     plugins(pw, model);
61 61
 
@@ -83,7 +83,9 @@ private void project(PrintWriter pw, Model model) {
83 83
     if (name == null) {
84 84
       name = model.getArtifactId();
85 85
     }
86  
-    pw.println("project \"" + name + "\" @ \"" + model.getUrl() + "\"");
  86
+    String url = model.getUrl() == null ? "" : model.getUrl();
  87
+    pw.print("project \"" + name + "\" @ \"" + url + "\"");
  88
+    packaging(pw, model);
87 89
   }
88 90
 
89 91
   private void id(PrintWriter pw, Model model) {
@@ -101,19 +103,17 @@ private void id(PrintWriter pw, Model model) {
101 103
 
102 104
   private void parent(PrintWriter pw, Model model) {
103 105
     if (model.getParent() != null) {
104  
-      pw.print(indent + "inherit: " + model.getParent().getGroupId() + ":" + model.getParent().getArtifactId() + ":" + model.getParent().getVersion());
  106
+      pw.print(indent + "inherits: " + model.getParent().getGroupId() + ":" + model.getParent().getArtifactId() + ":" + model.getParent().getVersion());
105 107
       if (model.getParent().getRelativePath() != null) {
106 108
         //pw.println(":" + model.getParent().getRelativePath());
107  
-        pw.println(":" + "../pom.atom");        
108  
-      } else {
109  
-        pw.println();
  109
+//        pw.println(":" + "../pom.atom");
110 110
       }
  111
+      pw.println();
111 112
     }
112 113
   }
113 114
 
114 115
   private void packaging(PrintWriter pw, Model model) {
115  
-    pw.println(indent + "packaging: " + model.getPackaging());
116  
-    pw.println();
  116
+    pw.println(" as " + model.getPackaging());
117 117
   }
118 118
 
119 119
   private void properties(PrintWriter pw, Model model) {
@@ -134,7 +134,6 @@ private void properties(PrintWriter pw, Model model) {
134 134
         }
135 135
       }
136 136
       pw.println(" ]");
137  
-      pw.println();
138 137
     }
139 138
   }
140 139
 
@@ -145,7 +144,7 @@ private void modules(PrintWriter pw, Model model) {
145 144
       for (int i = 0; i < modules.size(); i++) {
146 145
         String module = modules.get(i);
147 146
         if (i != 0) {
148  
-          pw.print("             ");
  147
+          pw.print(indent + "           ");
149 148
         }
150 149
         pw.print(module);
151 150
         if (i + 1 != modules.size()) {
@@ -153,7 +152,6 @@ private void modules(PrintWriter pw, Model model) {
153 152
         }
154 153
       }
155 154
       pw.println(" ]");
156  
-      pw.println();
157 155
     }
158 156
   }
159 157
 
@@ -182,17 +180,16 @@ private void deps(PrintWriter pw, String elementName, List<Dependency> deps) {
182 180
           // We are assuming the model is well-formed and that the parent is providing a version
183 181
           // for this particular dependency.
184 182
           //
185  
-          pw.print(d.getGroupId() + ":" + d.getArtifactId());          
  183
+          pw.print(d.getGroupId() + ":" + d.getArtifactId());
186 184
         }
187 185
         if (d.getClassifier() != null) {
188 186
           pw.print("(" + d.getClassifier() + ")");
189  
-        }        
  187
+        }
190 188
         if (i + 1 != deps.size()) {
191 189
           pw.println();
192 190
         }
193 191
       }
194 192
       pw.println(" ]");
195  
-      pw.println();
196 193
     }
197 194
   }
198 195
 
@@ -211,40 +208,57 @@ private void plugins(PrintWriter pw, Model model) {
211 208
   // need to write nested objects
212 209
   private void plugins(PrintWriter pw, String elementName, List<Plugin> plugins) {
213 210
     if (!plugins.isEmpty()) {
214  
-      pw.print(indent + elementName + ": [ ");
215 211
       for (int i = 0; i < plugins.size(); i++) {
216 212
         Plugin plugin = plugins.get(i);
217  
-        if (i != 0) {
218  
-          pw.print("             ");
219  
-        }
220  
-        pw.print(plugin.getGroupId() + ":" + plugin.getArtifactId() + ":" + plugin.getVersion());
  213
+        pw.println("plugin");
  214
+
  215
+        pw.print(indent + "id: " + plugin.getGroupId() + ":" + plugin.getArtifactId());
  216
+        if (plugin.getVersion() != null)
  217
+          pw.print(":" + plugin.getVersion());
221 218
         if (plugin.getConfiguration() != null) {
222 219
           pw.println();
223 220
           Xpp3Dom configuration = (Xpp3Dom) plugin.getConfiguration();
224  
-          if (configuration.getChildCount() != 0) {
225  
-            pw.print("               properties:[ ");
226  
-            int count = configuration.getChildCount();
227  
-            for (int j = 0; j < count; j++) {
228  
-              Xpp3Dom c = configuration.getChild(j);
229  
-              if (c.getValue() != null) {
230  
-                if (j != 0) {
231  
-                  pw.print("                            ");
232  
-                }
233  
-                pw.print(c.getName() + ": " + c.getValue());
234  
-                if (j + 1 != count) {
235  
-                  pw.println();
236  
-                }
237  
-              }
238  
-            }
239  
-            pw.print(" ]");
240  
-          }
  221
+          printChildren(pw, configuration);
241 222
         }
242 223
         if (i + 1 != plugins.size()) {
243 224
           pw.println();
244 225
         }
245 226
       }
246  
-      pw.println(" ]");
247 227
       pw.println();
248 228
     }
249 229
   }
250  
-}
  230
+
  231
+  private boolean flipBrackets = false;
  232
+
  233
+  private void printChildren(PrintWriter pw, Xpp3Dom configuration) {
  234
+    if (configuration.getChildCount() > 0) {
  235
+      int count = configuration.getChildCount();
  236
+      for (int j = 0; j < count; j++) {
  237
+        Xpp3Dom c = configuration.getChild(j);
  238
+        if (c.getValue() != null) {
  239
+          pw.print(indent + c.getName() + ": " + c.getValue());
  240
+          if (j + 1 != count) {
  241
+            pw.println();
  242
+          }
  243
+        } else {
  244
+          pw.println(indent + c.getName() + ": " + lbraceket());
  245
+          String oldIndent = indent;
  246
+          indent += "  ";
  247
+          flipBrackets = !flipBrackets;
  248
+          printChildren(pw, c);
  249
+          flipBrackets = !flipBrackets;
  250
+          indent = oldIndent;
  251
+          pw.print("\n" + indent + rbraceket());
  252
+        }
  253
+      }
  254
+    }
  255
+  }
  256
+
  257
+  private char lbraceket() {
  258
+    return (flipBrackets ? '{' : '[');
  259
+  }
  260
+
  261
+  private char rbraceket() {
  262
+    return (flipBrackets ? '}' : ']');
  263
+  }
  264
+}
37  tesla-polyglot/tesla-polyglot-atom/src/main/java/org/sonatype/maven/polyglot/atom/parsing/AtomParser.java
@@ -39,11 +39,13 @@ public AtomParser(ModelSource modelSource, List<Token> tokens) {
39 39
   }
40 40
 
41 41
   private void parseException(String message, Throwable t) {
42  
-    throw new RuntimeException("Error parsing " + modelSource.getLocation() + ": " + message, t);
  42
+    String location = modelSource != null ? modelSource.getLocation() : "";
  43
+    throw new RuntimeException("Error parsing " + location + ": " + message, t);
43 44
   }
44 45
 
45 46
   private void parseException(String message) {
46  
-    throw new RuntimeException("Error parsing " + modelSource.getLocation() + ": " + message);
  47
+    String location = modelSource != null ? modelSource.getLocation() : "";
  48
+    throw new RuntimeException("Error parsing " + location + ": " + message);
47 49
   }
48 50
 
49 51
   public Project parse() {
@@ -336,7 +338,7 @@ private Plugin plugin() {
336 338
       return null;
337 339
     }
338 340
 
339  
-    Id pluginId = id();
  341
+    Id pluginId = id(true);
340 342
     if (pluginId == null) {
341 343
       log.severe("Plugin id declaration malformed");
342 344
       return null;
@@ -409,9 +411,13 @@ private Xpp3Dom toXpp3DomTree(String name, Map<String, Object> config) {
409 411
         match(Kind.EOL);
410 412
       } else {
411 413
         // This is a multilevel thing, recurse!
412  
-        if (match(Kind.LBRACKET) != null) {
  414
+        if (anyOf(Kind.LBRACKET, Kind.LBRACE) != null) {
  415
+          chewEols();
  416
+          chewIndents();
413 417
           Map<String, Object> childProps = configurationMap();
414  
-          if (match(Kind.RBRACKET) == null)
  418
+          chewEols();
  419
+          chewIndents();
  420
+          if (match(Kind.RBRACKET) == null && match(Kind.RBRACE) == null)
415 421
             parseException("Expected ']' after configuration properties");
416 422
 
417 423
           // stash into parent map.
@@ -468,7 +474,7 @@ private Xpp3Dom toXpp3DomTree(String name, Map<String, Object> config) {
468 474
     chewIndents();
469 475
 
470 476
     String module;
471  
-    while ((module = idFragment()).length() != 0) {
  477
+    while ((module = idFragment()) != null) {
472 478
       chewEols();
473 479
       chewIndents();
474 480
       modules.add(module);
@@ -556,7 +562,7 @@ private Id id(boolean allowNullVersion) {
556 562
       return null;
557 563
     } else {
558 564
       version = idFragment();
559  
-      if (version == null) {
  565
+      if (version == null && !allowNullVersion) {
560 566
         return null;
561 567
       }
562 568
     }
@@ -647,6 +653,23 @@ private String idFragment() {
647 653
       }
648 654
     }
649 655
 
  656
+    // Try parsing as property expression.
  657
+    if (fragment.length() == 1 && fragment.charAt(0) == '$') {
  658
+      List<Token> startOfExpr = match(Kind.LBRACE);
  659
+      if (startOfExpr == null)
  660
+        return null;
  661
+
  662
+      fragment.append("{");
  663
+      String prop = idFragment();
  664
+      if (prop == null)
  665
+        parseException("Expected a property expression after ${");
  666
+
  667
+      if (match(Kind.RBRACE) == null)
  668
+        parseException("Expected '}' after property expression");
  669
+
  670
+      fragment.append(prop).append("}");
  671
+    }
  672
+
650 673
     // Nothing matched.
651 674
     if (fragment.length() == 0)
652 675
       return null;
8  tesla-polyglot/tesla-polyglot-atom/src/main/java/org/sonatype/maven/polyglot/atom/parsing/Token.java
@@ -44,6 +44,8 @@ public Token(String value, Kind kind) {
44 44
     RPAREN,
45 45
     LBRACKET,
46 46
     RBRACKET,
  47
+    LBRACE,
  48
+    RBRACE,
47 49
 
48 50
     // keywords
49 51
     REQUIRE,
@@ -93,8 +95,8 @@ public Token(String value, Kind kind) {
93 95
       TOKEN_MAP.put(")", RPAREN);
94 96
       TOKEN_MAP.put("[", LBRACKET);
95 97
       TOKEN_MAP.put("]", RBRACKET);
96  
-      TOKEN_MAP.put("{", LBRACKET);   // Alias braces as brackets.
97  
-      TOKEN_MAP.put("}", RBRACKET);
  98
+      TOKEN_MAP.put("{", LBRACE);   // Alias braces as brackets.
  99
+      TOKEN_MAP.put("}", RBRACE);
98 100
       TOKEN_MAP.put("\n", EOL);
99 101
 
100 102
       TOKEN_MAP.put("id", ID);
@@ -159,4 +161,4 @@ public String toString() {
159 161
         ", kind=" + kind +
160 162
         '}';
161 163
   }
162  
-}
  164
+}
10  tesla-polyglot/tesla-polyglot-atom/src/test/java/org/sonatype/maven/polyglot/atom/AtomModelWithSBTest.java
@@ -24,7 +24,7 @@
24 24
   @Named("${basedir}/src/test/poms")
25 25
   private File poms;
26 26
 
27  
-  public void _testAtomModelWriter() throws Exception {
  27
+  public void testAtomModelWriter() throws Exception {
28 28
     File pom = new File(poms, "sitebricks-parent-pom.xml");
29 29
     MavenXpp3Reader xmlModelReader = new MavenXpp3Reader();
30 30
     Model xmlModel = xmlModelReader.read(new FileInputStream(pom));
@@ -56,6 +56,9 @@ public void _testAtomModelWriter() throws Exception {
56 56
     MavenXpp3Writer xmlWriter = new MavenXpp3Writer();
57 57
     xmlWriter.write(w, xmlModel);
58 58
     System.out.println(w.toString());
  59
+
  60
+    assertEquals(xmlModel.getModules(), atomModel.getModules());
  61
+    assertEquals(xmlModel.getRepositories().size(), atomModel.getRepositories().size());
59 62
   }
60 63
 
61 64
   public void testAtomModelWriterWhereModelHasDependenciesWithNoVersions() throws Exception {
@@ -82,9 +85,7 @@ public void testAtomModelWriterWhereModelHasDependenciesWithNoVersions() throws
82 85
     //
83 86
     // Test for fidelity
84 87
     //
85  
-    //assertNotNull(atomModel);
86  
-
87  
-
  88
+    assertNotNull(atomModel);
88 89
   }
89 90
 
90 91
   void testMavenModelForCompleteness(Model model) {
@@ -138,5 +139,4 @@ String gav(Dependency d) {
138 139
   String gav(Plugin p) {
139 140
     return p.getGroupId() + ":" + p.getArtifactId() + ":" + p.getVersion();
140 141
   }
141  
-
142 142
 }
0  ...e/maven/polyglot/atom/parsing/AtomParserTest.java → ...e/maven/polyglot/atom/parsing/AtomParserTest.java
File renamed without changes
35  tesla-polyglot/tesla-polyglot-atom/src/test/poms/sitebricks-pom.xml
... ...
@@ -1,4 +1,6 @@
1  
-<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  1
+<project xmlns="http://maven.apache.org/POM/4.0.0"
  2
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
2 4
   <modelVersion>4.0.0</modelVersion>
3 5
   <parent>
4 6
     <groupId>com.google.sitebricks</groupId>
@@ -7,6 +9,7 @@
7 9
   </parent>
8 10
   <artifactId>sitebricks</artifactId>
9 11
   <name>Sitebricks :: Core</name>
  12
+  <url>http://sitebricks.org</url>
10 13
 
11 14
   <dependencies>
12 15
     <dependency>
@@ -21,8 +24,8 @@
21 24
       <artifactId>sitebricks-converter</artifactId>
22 25
     </dependency>
23 26
     <dependency>
24  
-     <groupId>com.google.sitebricks</groupId>
25  
-     <artifactId>sitebricks-client</artifactId>
  27
+      <groupId>com.google.sitebricks</groupId>
  28
+      <artifactId>sitebricks-client</artifactId>
26 29
     </dependency>
27 30
     <dependency>
28 31
       <groupId>org.mvel</groupId>
@@ -137,4 +140,30 @@
137 140
     </repository>
138 141
   </distributionManagement>
139 142
 
  143
+  <build>
  144
+    <plugins>
  145
+      <plugin>
  146
+        <artifactId>maven-assembly-plugin</artifactId>
  147
+        <executions>
  148
+          <execution>
  149
+            <id>source-release-assembly</id>
  150
+            <configuration>
  151
+              <!-- we have a dedicated distribution module -->
  152
+              <skipAssembly>true</skipAssembly>
  153
+            </configuration>
  154
+          </execution>
  155
+        </executions>
  156
+        <configuration>
  157
+          <someCompoundKey>
  158
+            <someKey>someValue</someKey>
  159
+            <someKey2>someValue</someKey2>
  160
+            <someCompoundKeyInner>
  161
+              <someKey>someValue</someKey>
  162
+            </someCompoundKeyInner>
  163
+          </someCompoundKey>
  164
+        </configuration>
  165
+      </plugin>
  166
+    </plugins>
  167
+  </build>
  168
+
140 169
 </project>

0 notes on commit f365996

Please sign in to comment.
Something went wrong with that request. Please try again.