MetadataParser { public static MetadataRb parse(String content) throws MetadataParseException { MetadataRb metadata = new MetadataRb(); StringReader reader = new StringReader(content); Scanner scanner = new Scanner(reader); List<String> comments = new ArrayList<>(); while (scanner.hasNextLine()) { boolean found = false; String line = scanner.nextLine(); line = line.trim(); if (!line.isEmpty()) { if (line.startsWith("#")) { comments.add(line); found = true; } if (!found) { Matcher m1 = NAME.matcher(line); if (m1.matches()) { metadata.setName(m1.group(1)); found = true; } } if (!found) { Matcher m2 = DESC.matcher(line); if (m2.matches()) { metadata.setDescription(m2.group(1)); found = true; } } if (!found) { Matcher m3 = VERSION.matcher(line); if (m3.matches()) { metadata.setVersion(m3.group(1)); found = true; } } if (!found) { Matcher m4 = RECIPE.matcher(line); if (m4.matches()) { Recipe r = new Recipe(); r.setName(m4.group(1)); r.setDescription(m4.group(3)); r.parseComments(comments); metadata.getRecipes().add(r); found = true; comments.clear(); } } if (!found) { Matcher m5 = ATTR.matcher(line); if (m5.matches()) { Attribute attr = new Attribute(); attr.setName(m5.group(1)); while (line.matches(COMMA_CLOSING_LINE) && scanner.hasNext()) { boolean found2 = false; line = scanner.nextLine(); Matcher m6 = ATTR_DISP_NAME.matcher(line); if (m6.matches()) { attr.setDisplayName(m6.group(1)); found2 = true; } if (!found2) { Matcher m7 = ATTR_TYPE.matcher(line); if (m7.matches()) { attr.setType(m7.group(1)); found2 = true; } } if (!found2) { Matcher m8 = ATTR_DESC.matcher(line); if (m8.matches()) { attr.setDescription(m8.group(1)); found2 = true; } } if (!found2) { Matcher m92 = ATTR_DEFAULT_ARRAY.matcher(line); if (m92.matches()) { String sarr = m92.group(1); Matcher m921 = ATTR_DEFAULT_ARRAY_ITEMS.matcher(sarr); List<String> deflist = new ArrayList<>(); while (m921.find()) { String item = m921.group(1); deflist.add(item); } attr.setDefault(deflist); found2 = true; } } if (!found2) { Matcher m91 = ATTR_DEFAULT_SIMPLE.matcher(line); if (m91.matches()) { attr.setDefault(m91.group(1)); found2 = true; } } if (!found2) { Matcher m10 = ATTR_REQUIRED.matcher(line); if (m10.matches()) { attr.setRequired(m10.group(1)); found2 = true; } } if (!found2) { logger.debug(String.format("Urecognized line for attribute in the metadata.rb '%s'", line)); } } metadata.getAttributes().add(attr); found = true; } } if (!found) { logger.debug(String.format("Urecognized line in the metadata.rb '%s'", line)); } } else { comments.clear(); } } return metadata; }  static MetadataRb parse(String content); static Pattern NAME; static Pattern DESC; static Pattern VERSION; static Pattern RECIPE; static Pattern ATTR; static Pattern ATTR_DISP_NAME; static Pattern ATTR_DESC; static Pattern ATTR_TYPE; static Pattern ATTR_DEFAULT_SIMPLE; static Pattern ATTR_DEFAULT_ARRAY; static Pattern ATTR_DEFAULT_ARRAY_ITEMS; static Pattern ATTR_REQUIRED; static String COMMA_CLOSING_LINE; }
@Test public void testNdbRecognizedLines() throws IOException, MetadataParseException { String content = IoUtils.readContentFromClasspath("testgithub/testorg/testrepo/master/cookbooks/hopshadoop/ndb-chef/metadata.rb"); MetadataRb metadatarb = MetadataParser.parse(content); assertEquals("ndb", metadatarb.getName()); assertEquals("Installs/Configures NDB (MySQL Cluster)", metadatarb.getDescription()); assertEquals("1.0", metadatarb.getVersion()); List<Recipe> recipes = metadatarb.getRecipes(); assertEquals(11, recipes.size()); assertEquals("ndb::install", recipes.get(0).getName()); assertEquals("Installs MySQL Cluster binaries", recipes.get(0).getDescription()); assertEquals("ndb::purge", recipes.get(10).getName()); assertEquals("Removes all data and all binaries related to a MySQL Cluster installation", recipes.get(10).getDescription()); List<Attribute> attributes = metadatarb.getAttributes(); assertEquals(45, attributes.size()); assertEquals("ndb/ports", attributes.get(0).getName()); assertEquals("Dummy ports", attributes.get(0).getDescription()); assertEquals("array", attributes.get(0).getType()); assertEquals("required", attributes.get(0).getRequired()); assertEquals(Lists.newArrayList("123", "134", "145"), attributes.get(0).getDefault()); assertEquals("ndb/DataMemory", attributes.get(1).getName()); assertEquals("Data memory for each MySQL Cluster Data Node", attributes.get(1).getDescription()); assertEquals("string", attributes.get(1).getType()); assertEquals("required", attributes.get(1).getRequired()); assertEquals("80", attributes.get(1).getDefault()); assertEquals("kagent/enabled", attributes.get(44).getName()); assertEquals("Install kagent", attributes.get(44).getDescription()); assertEquals("string", attributes.get(44).getType()); assertEquals("optional", attributes.get(44).getRequired()); assertEquals("false", attributes.get(44).getDefault()); }
@Test public void testLinks() throws MetadataParseException, IOException { String content = IoUtils.readContentFromClasspath("se/kth/karamel/cookbook/metadata/metadata.rb"); MetadataRb metadatarb = MetadataParser.parse(content); List<Recipe> recipes = metadatarb.getRecipes(); assertEquals(recipes.size(), 2); Recipe r1 = recipes.get(0); Recipe r2 = recipes.get(1); assertEquals(r1.getName(), "hopsworks::install"); Set<String> l1 = r1.getLinks(); assertEquals(l1.size(), 0); assertEquals(r2.getName(), "hopsworks::default"); Set<String> l2 = r2.getLinks(); assertEquals(l2.size(), 2); }