Search in sources :

Example 16 with ResourceMap

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;
}
Also used : CompositeOCD(org.bndtools.templating.util.CompositeOCD) JarInputStream(java.util.jar.JarInputStream) InputStream(java.io.InputStream) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) ObjectClassDefinitionImpl(org.bndtools.templating.util.ObjectClassDefinitionImpl) ResourceMap(org.bndtools.templating.ResourceMap) OCD(org.apache.felix.metatype.OCD) CompositeOCD(org.bndtools.templating.util.CompositeOCD) JarEntry(java.util.jar.JarEntry) Entry(java.util.Map.Entry) MetaData(org.apache.felix.metatype.MetaData) AttributeDefinitionImpl(org.bndtools.templating.util.AttributeDefinitionImpl) Map(java.util.Map) ResourceMap(org.bndtools.templating.ResourceMap) MetaDataReader(org.apache.felix.metatype.MetaDataReader)

Example 17 with ResourceMap

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"));
}
Also used : ResourceMap(org.bndtools.templating.ResourceMap) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) StringResource(org.bndtools.templating.StringResource) Test(org.junit.Test)

Example 18 with ResourceMap

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"));
}
Also used : ResourceMap(org.bndtools.templating.ResourceMap) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) StringResource(org.bndtools.templating.StringResource) HashMap(java.util.HashMap) FolderResource(org.bndtools.templating.FolderResource) List(java.util.List) Test(org.junit.Test)

Example 19 with ResourceMap

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()));
}
Also used : ResourceMap(org.bndtools.templating.ResourceMap) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) StringResource(org.bndtools.templating.StringResource) HashMap(java.util.HashMap) FolderResource(org.bndtools.templating.FolderResource) List(java.util.List) Test(org.junit.Test)

Example 20 with ResourceMap

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()));
}
Also used : ResourceMap(org.bndtools.templating.ResourceMap) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) StringResource(org.bndtools.templating.StringResource) HashMap(java.util.HashMap) List(java.util.List) Test(org.junit.Test)

Aggregations

ResourceMap (org.bndtools.templating.ResourceMap)22 List (java.util.List)13 StringResource (org.bndtools.templating.StringResource)13 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)13 HashMap (java.util.HashMap)12 Resource (org.bndtools.templating.Resource)10 Test (org.junit.Test)9 Template (org.bndtools.templating.Template)6 Entry (java.util.Map.Entry)5 FolderResource (org.bndtools.templating.FolderResource)5 ProgressMonitorReporter (org.bndtools.utils.progress.ProgressMonitorReporter)4 File (java.io.File)3 InputStream (java.io.InputStream)3 DigestInputStream (java.security.DigestInputStream)3 StringReader (java.io.StringReader)2 Properties (java.util.Properties)2 JarEntry (java.util.jar.JarEntry)2 JarFile (java.util.jar.JarFile)2 JarInputStream (java.util.jar.JarInputStream)2 BuiltInTemplate (org.bndtools.core.ui.wizards.shared.BuiltInTemplate)2