Search in sources :

Example 26 with Tag

use of aQute.lib.tag.Tag in project bnd by bndtools.

the class BaselineCommands method _schema.

/**
	 * Create a schema of a set of jars outling the packages and their versions.
	 * This will create a list of packages with multiple versions, link to their
	 * specifications, and the deltas between versions.
	 * 
	 * <pre>
	 *  bnd package schema
	 * <file.jar>*
	 * </pre>
	 * 
	 * @param opts
	 * @throws Exception
	 */
public void _schema(schemaOptions opts) throws Exception {
    MultiMap<String, PSpec> map = new MultiMap<String, PSpec>();
    Tag top = new Tag("jschema");
    int n = 1000;
    for (String spec : opts._arguments()) {
        File f = bnd.getFile(spec);
        if (!f.isFile()) {
            bnd.messages.NoSuchFile_(f);
        } else {
            // For each specification jar we found
            logger.debug("spec {}", f);
            // spec
            Jar jar = new Jar(f);
            Manifest m = jar.getManifest();
            Attributes main = m.getMainAttributes();
            Tag specTag = new Tag(top, "specification");
            specTag.addAttribute("jar", spec);
            specTag.addAttribute("name", main.getValue("Specification-Name"));
            specTag.addAttribute("title", main.getValue("Specification-Title"));
            specTag.addAttribute("jsr", main.getValue("Specification-JSR"));
            specTag.addAttribute("url", main.getValue("Specification-URL"));
            specTag.addAttribute("version", main.getValue("Specification-Version"));
            specTag.addAttribute("vendor", main.getValue("Specification-Vendor"));
            specTag.addAttribute("id", n);
            specTag.addContent(main.getValue(Constants.BUNDLE_DESCRIPTION));
            Parameters exports = OSGiHeader.parseHeader(m.getMainAttributes().getValue(Constants.EXPORT_PACKAGE));
            // Create a map with versions. Ensure import ranges overwrite
            // the
            // exported versions
            Parameters versions = new Parameters();
            versions.putAll(exports);
            versions.putAll(OSGiHeader.parseHeader(m.getMainAttributes().getValue(Constants.IMPORT_PACKAGE)));
            Analyzer analyzer = new Analyzer();
            analyzer.setJar(jar);
            analyzer.analyze();
            Tree tree = differ.tree(analyzer);
            for (Entry<String, Attrs> entry : exports.entrySet()) {
                // For each exported package in the specification JAR
                Attrs attrs = entry.getValue();
                String packageName = entry.getKey();
                PackageRef packageRef = analyzer.getPackageRef(packageName);
                String version = attrs.get(Constants.VERSION_ATTRIBUTE);
                PSpec pspec = new PSpec();
                pspec.packageName = packageName;
                pspec.version = new Version(version);
                pspec.id = n;
                pspec.attrs = attrs;
                pspec.tree = tree;
                Collection<PackageRef> uses = analyzer.getUses().get(packageRef);
                if (uses != null) {
                    for (PackageRef x : uses) {
                        if (x.isJava())
                            continue;
                        String imp = x.getFQN();
                        if (imp.equals(packageName))
                            continue;
                        String v = null;
                        if (versions.containsKey(imp))
                            v = versions.get(imp).get(Constants.VERSION_ATTRIBUTE);
                        pspec.uses.put(imp, v);
                    }
                }
                map.add(packageName, pspec);
            }
            jar.close();
            n++;
        }
    }
    // We now gather all the information about all packages in the map.
    // Next phase is generating the XML. Sorting the packages is
    // important because XSLT is brain dead.
    SortedList<String> names = new SortedList<String>(map.keySet());
    Tag packagesTag = new Tag(top, "packages");
    Tag baselineTag = new Tag(top, "baseline");
    for (String pname : names) {
        // For each distinct package name
        SortedList<PSpec> specs = new SortedList<PSpec>(map.get(pname));
        PSpec older = null;
        Parameters olderExport = null;
        for (PSpec newer : specs) {
            // For each package in the total set
            Tag pack = new Tag(packagesTag, "package");
            pack.addAttribute("name", newer.packageName);
            pack.addAttribute("version", newer.version);
            pack.addAttribute("spec", newer.id);
            Parameters newerExport = new Parameters();
            newerExport.put(pname, newer.attrs);
            if (older != null) {
                String compareId = newer.packageName + "-" + newer.id + "-" + older.id;
                pack.addAttribute("delta", compareId);
                logger.debug(" newer={} older={}", newerExport, olderExport);
                Set<Info> infos = baseline.baseline(newer.tree, newerExport, older.tree, olderExport, new Instructions(pname));
                for (Info info : infos) {
                    Tag tag = getTag(info);
                    tag.addAttribute("id", compareId);
                    tag.addAttribute("newerSpec", newer.id);
                    tag.addAttribute("olderSpec", older.id);
                    baselineTag.addContent(tag);
                }
                older.tree = null;
                older.attrs = null;
                older = newer;
            }
            for (Entry<String, String> uses : newer.uses.entrySet()) {
                Tag reference = new Tag(pack, "import");
                reference.addAttribute("name", uses.getKey());
                reference.addAttribute("version", uses.getValue());
            }
            older = newer;
            olderExport = newerExport;
        }
    }
    String o = opts.output("schema.xml");
    File of = bnd.getFile(o);
    File pof = of.getParentFile();
    IO.mkdirs(pof);
    try (PrintWriter pw = IO.writer(of, UTF_8)) {
        pw.print("<?xml version='1.0' encoding='UTF-8'?>\n");
        top.print(0, pw);
    }
    if (opts.xsl() != null) {
        URL home = bnd.getBase().toURI().toURL();
        URL xslt = new URL(home, opts.xsl());
        String path = of.getAbsolutePath();
        if (path.endsWith(".xml"))
            path = path.substring(0, path.length() - 4);
        path = path + ".html";
        File html = new File(path);
        logger.debug("xslt {} {} {} {}", xslt, of, html, html.exists());
        try (OutputStream out = IO.outputStream(html);
            InputStream in = xslt.openStream()) {
            Transformer transformer = transformerFactory.newTransformer(new StreamSource(in));
            transformer.transform(new StreamSource(of), new StreamResult(out));
        }
    }
}
Also used : Transformer(javax.xml.transform.Transformer) OutputStream(java.io.OutputStream) Attributes(java.util.jar.Attributes) Attrs(aQute.bnd.header.Attrs) SortedList(aQute.lib.collections.SortedList) Analyzer(aQute.bnd.osgi.Analyzer) URL(java.net.URL) MultiMap(aQute.lib.collections.MultiMap) Version(aQute.bnd.version.Version) Tree(aQute.bnd.service.diff.Tree) PackageRef(aQute.bnd.osgi.Descriptors.PackageRef) PrintWriter(java.io.PrintWriter) Parameters(aQute.bnd.header.Parameters) StreamResult(javax.xml.transform.stream.StreamResult) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) Instructions(aQute.bnd.osgi.Instructions) Manifest(java.util.jar.Manifest) Info(aQute.bnd.differ.Baseline.Info) BundleInfo(aQute.bnd.differ.Baseline.BundleInfo) Jar(aQute.bnd.osgi.Jar) Tag(aQute.lib.tag.Tag) File(java.io.File)

Example 27 with Tag

use of aQute.lib.tag.Tag in project bnd by bndtools.

the class ComponentDef method getTag.

/**
	 * Returns a tag describing the component element.
	 * 
	 * @return a component element
	 */
Tag getTag() {
    String xmlns = this.xmlns;
    if (xmlns == null && !version.equals(AnnotationReader.V1_0))
        xmlns = NAMESPACE_STEM + "/v" + version;
    Tag component = new Tag(xmlns == null ? "component" : "scr:component");
    Namespaces namespaces = null;
    if (xmlns != null) {
        namespaces = new Namespaces();
        namespaces.registerNamespace("scr", xmlns);
        addNamespaces(namespaces, xmlns);
        for (ReferenceDef ref : references.values()) ref.addNamespaces(namespaces, xmlns);
        namespaces.addNamespaces(component);
    }
    component.addAttribute("name", name);
    if (configurationPolicy != null)
        component.addAttribute("configuration-policy", configurationPolicy.toString().toLowerCase());
    if (enabled != null)
        component.addAttribute("enabled", enabled);
    if (immediate != null)
        component.addAttribute("immediate", immediate);
    if (factory != null)
        component.addAttribute("factory", factory);
    if (activate != null && !version.equals(AnnotationReader.V1_0))
        component.addAttribute("activate", activate);
    if (deactivate != null && !version.equals(AnnotationReader.V1_0))
        component.addAttribute("deactivate", deactivate);
    if (modified != null)
        component.addAttribute("modified", modified);
    if (configurationPid != null) {
        StringBuilder b = new StringBuilder();
        String space = "";
        for (String pid : configurationPid) {
            if ("$".equals(pid))
                pid = name;
            b.append(space).append(pid);
            space = " ";
        }
        component.addAttribute("configuration-pid", b.toString());
    }
    addAttributes(component, namespaces);
    Tag impl = new Tag(component, "implementation");
    impl.addAttribute("class", implementation.getFQN());
    if (service != null && service.length != 0) {
        Tag s = new Tag(component, "service");
        if (scope != null) {
            // TODO check for DEFAULT???
            if (AnnotationReader.V1_3.compareTo(version) > 0) {
                if (scope == ServiceScope.PROTOTYPE) {
                    throw new IllegalStateException("verification failed, pre 1.3 component with scope PROTOTYPE");
                }
                s.addAttribute("servicefactory", scope == ServiceScope.BUNDLE);
            } else {
                s.addAttribute("scope", scope.toString().toLowerCase());
            }
        }
        for (TypeRef ss : service) {
            Tag provide = new Tag(s, "provide");
            provide.addAttribute("interface", ss.getFQN());
        }
    }
    for (ReferenceDef ref : references.values()) {
        Tag refTag = ref.getTag(namespaces);
        component.addContent(refTag);
    }
    for (Tag tag : propertyTags) component.addContent(tag);
    for (String entry : properties) {
        Tag properties = new Tag(component, "properties");
        properties.addAttribute("entry", entry);
    }
    return component;
}
Also used : Namespaces(aQute.bnd.xmlattribute.Namespaces) TypeRef(aQute.bnd.osgi.Descriptors.TypeRef) Tag(aQute.lib.tag.Tag)

Example 28 with Tag

use of aQute.lib.tag.Tag in project bnd by bndtools.

the class ComponentDef method prepare.

/**
	 * Called to prepare. If will look for any errors or inconsistencies in the
	 * setup.
	 * 
	 * @param analyzer the analyzer to report errors and create references
	 * @throws Exception
	 */
void prepare(Analyzer analyzer) throws Exception {
    prepareVersion(analyzer);
    if (implementation == null) {
        analyzer.error("No Implementation defined for component %s", name);
        return;
    }
    analyzer.referTo(implementation);
    if (name == null)
        name = implementation.getFQN();
    if (service != null && service.length > 0) {
        for (TypeRef interfaceName : service) analyzer.referTo(interfaceName);
    } else if (scope != null && scope != ServiceScope.BUNDLE)
        analyzer.warning("The servicefactory:=true directive is set but no service is provided, ignoring it");
    for (Map.Entry<String, List<String>> kvs : property.entrySet()) {
        Tag property = new Tag("property");
        String name = kvs.getKey();
        String type = propertyType.get(name);
        property.addAttribute("name", name);
        if (type != null) {
            property.addAttribute("type", type);
        }
        if (kvs.getValue().size() == 1) {
            String value = kvs.getValue().get(0);
            value = check(type, value, analyzer);
            property.addAttribute("value", value);
        } else {
            StringBuilder sb = new StringBuilder();
            String del = "";
            for (String v : kvs.getValue()) {
                if (v == MARKER) {
                    continue;
                }
                sb.append(del);
                v = check(type, v, analyzer);
                sb.append(v);
                del = "\n";
            }
            property.addContent(sb.toString());
        }
        propertyTags.add(property);
    }
}
Also used : TypeRef(aQute.bnd.osgi.Descriptors.TypeRef) ArrayList(java.util.ArrayList) List(java.util.List) Tag(aQute.lib.tag.Tag) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap) MultiMap(aQute.lib.collections.MultiMap) Map(java.util.Map)

Aggregations

Tag (aQute.lib.tag.Tag)28 PrintWriter (java.io.PrintWriter)8 File (java.io.File)5 Attrs (aQute.bnd.header.Attrs)3 Parameters (aQute.bnd.header.Parameters)3 TypeRef (aQute.bnd.osgi.Descriptors.TypeRef)3 Namespaces (aQute.bnd.xmlattribute.Namespaces)3 List (java.util.List)3 Map (java.util.Map)3 Matcher (java.util.regex.Matcher)3 MethodDef (aQute.bnd.osgi.Clazz.MethodDef)2 Instructions (aQute.bnd.osgi.Instructions)2 Diff (aQute.bnd.service.diff.Diff)2 Tree (aQute.bnd.service.diff.Tree)2 MultiMap (aQute.lib.collections.MultiMap)2 IOException (java.io.IOException)2 OutputStreamWriter (java.io.OutputStreamWriter)2 Date (java.util.Date)2 Meta (aQute.bnd.annotation.metatype.Meta)1 Project (aQute.bnd.build.Project)1