use of org.bndtools.templating.ResourceMap in project bndtools by bndtools.
the class CapabilityBasedTemplate method getMetadata.
@Override
public ObjectClassDefinition getMetadata(IProgressMonitor monitor) throws Exception {
String resourceId = ResourceUtils.getIdentityCapability(capability.getResource()).osgi_identity();
final CompositeOCD compositeOcd = new CompositeOCD(name, description, null);
if (metaTypePath != null) {
try (JarFile bundleJarFile = new JarFile(fetchBundle())) {
JarEntry metaTypeEntry = bundleJarFile.getJarEntry(metaTypePath);
try (InputStream entryInput = bundleJarFile.getInputStream(metaTypeEntry)) {
MetaData metaData = new MetaDataReader().parse(entryInput);
@SuppressWarnings("rawtypes") Map ocdMap = metaData.getObjectClassDefinitions();
if (ocdMap != null) {
if (ocdMap.size() == 1) {
@SuppressWarnings("unchecked") Entry<String, OCD> entry = (Entry<String, OCD>) ocdMap.entrySet().iterator().next();
// There is exactly one OCD, but if the capability specified the 'ocd' property then it must match.
if (ocdRef == null || ocdRef.equals(entry.getKey())) {
compositeOcd.addDelegate(new FelixOCDAdapter(entry.getValue()));
} else {
log(IStatus.WARNING, String.format("MetaType entry '%s' from resource '%s' did not contain an Object Class Definition with id '%s'", metaTypePath, resourceId, ocdRef), null);
}
} else {
// There are multiple OCDs in the MetaType record, so the capability must have specified the 'ocd' property.
if (ocdRef != null) {
OCD felixOcd = (OCD) ocdMap.get(ocdRef);
if (felixOcd != null) {
compositeOcd.addDelegate(new FelixOCDAdapter(felixOcd));
} else {
log(IStatus.WARNING, String.format("MetaType entry '%s' from resource '%s' did not contain an Object Class Definition with id '%s'", metaTypePath, resourceId, ocdRef), null);
}
} else {
log(IStatus.WARNING, String.format("MetaType entry '%s' from resource '%s' contains multiple Object Class Definitions, and no 'ocd' property was specified.", metaTypePath, resourceId), null);
}
}
}
}
}
}
// Add attribute definitions for any parameter names found in the templates and not already
// loaded from the Metatype XML.
ObjectClassDefinitionImpl ocdImpl = new ObjectClassDefinitionImpl(name, description, null);
ResourceMap inputs = getInputSources();
Map<String, String> params = engine.getTemplateParameters(inputs, monitor);
for (Entry<String, String> entry : params.entrySet()) {
AttributeDefinitionImpl ad = new AttributeDefinitionImpl(entry.getKey(), entry.getKey(), 0, AttributeDefinition.STRING);
if (entry.getValue() != null)
ad.setDefaultValue(new String[] { entry.getValue() });
ocdImpl.addAttribute(ad, true);
}
compositeOcd.addDelegate(ocdImpl);
return compositeOcd;
}
use of org.bndtools.templating.ResourceMap in project bndtools by bndtools.
the class MustacheTemplateEngineTest method testGetDefaults.
@Test
public void testGetDefaults() throws Exception {
MustacheTemplateEngine engine = new MustacheTemplateEngine();
ResourceMap input = new ResourceMap();
input.put("_defaults.properties", new StringResource("fish=carp\nbattleship=potemkin"));
input.put("readme.txt", new StringResource("Blah {{fish}} blah {{battleship}} blah {{antidisestablishmentarianism}}"));
Map<String, String> params = engine.getTemplateParameters(input, new NullProgressMonitor());
assertEquals("carp", params.get("fish"));
assertEquals("potemkin", params.get("battleship"));
assertNull(params.get("antidisestablishmentarianism"));
}
use of org.bndtools.templating.ResourceMap in project bndtools by bndtools.
the class MustacheTemplateEngineTest method testIgnore.
@Test
public void testIgnore() throws Exception {
MustacheTemplateEngine engine = new MustacheTemplateEngine();
ResourceMap input = new ResourceMap();
input.put("_template.properties", new StringResource("ignore=*/donotcopy.*"));
input.put("{{srcDir}}/", new FolderResource());
input.put("{{srcDir}}/{{packageDir}}/", new FolderResource());
input.put("{{srcDir}}/{{packageDir}}/package-info.java", new StringResource("package {{packageName}};"));
input.put("{{srcDir}}/{{packageDir}}/donotcopy.txt", new StringResource(""));
Map<String, List<Object>> params = new HashMap<>();
params.put("srcDir", Collections.<Object>singletonList("src"));
params.put("packageDir", Collections.<Object>singletonList("org/example/foo"));
params.put("packageName", Collections.<Object>singletonList("org.example.foo"));
ResourceMap output = engine.generateOutputs(input, params, new NullProgressMonitor());
assertEquals(3, output.size());
assertEquals("package org.example.foo;", IO.collect(output.get("src/org/example/foo/package-info.java").getContent()));
assertNull(output.get("src/org/example/foo/donotcopy.txt"));
}
use of org.bndtools.templating.ResourceMap in project bndtools by bndtools.
the class MustacheTemplateEngineTest method testBasic.
@Test
public void testBasic() throws Exception {
MustacheTemplateEngine engine = new MustacheTemplateEngine();
ResourceMap input = new ResourceMap();
input.put("{{srcDir}}/", new FolderResource());
input.put("{{srcDir}}/{{packageDir}}/", new FolderResource());
input.put("{{srcDir}}/{{packageDir}}/package-info.java", new StringResource("package {{packageName}};"));
Map<String, List<Object>> params = new HashMap<>();
params.put("srcDir", Collections.<Object>singletonList("src"));
params.put("packageDir", Collections.<Object>singletonList("org/example/foo"));
params.put("packageName", Collections.<Object>singletonList("org.example.foo"));
ResourceMap output = engine.generateOutputs(input, params, new NullProgressMonitor());
assertEquals(3, output.size());
assertEquals("package org.example.foo;", IO.collect(output.get("src/org/example/foo/package-info.java").getContent()));
}
use of org.bndtools.templating.ResourceMap in project bndtools by bndtools.
the class MustacheTemplateEngineTest method testAlternativeDelimiters2.
@Test
public void testAlternativeDelimiters2() throws Exception {
MustacheTemplateEngine engine = new MustacheTemplateEngine();
ResourceMap input = new ResourceMap();
input.put("_template.properties", new StringResource("leftDelim=_\nrightDelim=_"));
input.put("readme.txt", new StringResource("Unprocessed: {{packageName}}. Processed: _packageName_"));
Map<String, List<Object>> params = new HashMap<>();
params.put("packageName", Collections.<Object>singletonList("org.example.foo"));
ResourceMap output = engine.generateOutputs(input, params, new NullProgressMonitor());
assertEquals(1, output.size());
assertEquals("Unprocessed: {{packageName}}. Processed: org.example.foo", IO.collect(output.get("readme.txt").getContent()));
}
Aggregations