Search in sources :

Example 1 with Tag

use of org.osgi.service.indexer.impl.util.Tag in project bnd by bndtools.

the class RepoIndex method indexFragment.

public void indexFragment(Set<File> files, Writer out, Map<String, String> config) throws Exception {
    PrintWriter pw;
    if (out instanceof PrintWriter)
        pw = (PrintWriter) out;
    else
        pw = new PrintWriter(out);
    for (File file : files) {
        try {
            Tag resourceTag = generateResource(file, config);
            resourceTag.print(Indent.PRETTY, pw);
        } catch (Exception e) {
            log(LogService.LOG_WARNING, MessageFormat.format("Could not index {0}, skipped ({1}).", file, e), null);
        }
    }
}
Also used : Tag(org.osgi.service.indexer.impl.util.Tag) File(java.io.File) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) PrintWriter(java.io.PrintWriter)

Example 2 with Tag

use of org.osgi.service.indexer.impl.util.Tag in project bnd by bndtools.

the class RepoIndex method generateResource.

private Tag generateResource(File file, Map<String, String> config) throws Exception {
    JarResource resource = new JarResource(file);
    List<Capability> caps = new AddOnlyList<Capability>(new LinkedList<Capability>());
    List<Requirement> reqs = new AddOnlyList<Requirement>(new LinkedList<Requirement>());
    Tag resourceTag = new Tag(Schema.ELEM_RESOURCE);
    try {
        // Read config settings and save in thread local state
        if (config != null) {
            URL rootURL;
            String rootURLStr = config.get(ResourceIndexer.ROOT_URL);
            if (rootURLStr != null) {
                File rootDir = new File(rootURLStr);
                if (rootDir.isDirectory())
                    rootURL = rootDir.toURI().toURL();
                else
                    rootURL = new URL(rootURLStr);
            } else
                rootURL = new File(System.getProperty("user.dir")).toURI().toURL();
            String urlTemplate = config.get(ResourceIndexer.URL_TEMPLATE);
            bundleAnalyzer.setStateLocal(new GeneratorState(rootURL.toURI().normalize(), urlTemplate, resolvers));
        } else {
            bundleAnalyzer.setStateLocal(null);
        }
        // Iterate over the analyzers
        try {
            synchronized (analyzers) {
                for (Pair<ResourceAnalyzer, Filter> entry : analyzers) {
                    ResourceAnalyzer analyzer = entry.getFirst();
                    Filter filter = entry.getSecond();
                    if (filter == null || filter.match(resource.getProperties())) {
                        try {
                            analyzer.analyzeResource(resource, caps, reqs);
                        } catch (Exception e) {
                            log(LogService.LOG_ERROR, MessageFormat.format("Error calling analyzer \"{0}\" on resource {1}.", analyzer.getClass().getName(), resource.getLocation()), e);
                            StringWriter writer = new StringWriter();
                            Formatter comment = new Formatter(writer);
                            comment.format("Error calling analyzer \"%s\" on resource %s with message %s and stack: ", analyzer.getClass().getName(), resource.getLocation(), e);
                            comment.close();
                            e.printStackTrace(new PrintWriter(writer));
                            resourceTag.addComment(writer.toString());
                        }
                    }
                }
            }
        } finally {
            bundleAnalyzer.setStateLocal(null);
        }
    } finally {
        resource.close();
    }
    for (Capability cap : caps) {
        Tag capTag = new Tag(Schema.ELEM_CAPABILITY);
        capTag.addAttribute(Schema.ATTR_NAMESPACE, cap.getNamespace());
        appendAttributeAndDirectiveTags(capTag, cap.getAttributes(), cap.getDirectives());
        resourceTag.addContent(capTag);
    }
    for (Requirement req : reqs) {
        Tag reqTag = new Tag(Schema.ELEM_REQUIREMENT);
        reqTag.addAttribute(Schema.ATTR_NAMESPACE, req.getNamespace());
        appendAttributeAndDirectiveTags(reqTag, req.getAttributes(), req.getDirectives());
        resourceTag.addContent(reqTag);
    }
    return resourceTag;
}
Also used : ResourceAnalyzer(org.osgi.service.indexer.ResourceAnalyzer) Capability(org.osgi.service.indexer.Capability) Formatter(java.util.Formatter) URL(java.net.URL) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) Requirement(org.osgi.service.indexer.Requirement) AddOnlyList(org.osgi.service.indexer.impl.util.AddOnlyList) StringWriter(java.io.StringWriter) Filter(org.osgi.framework.Filter) FrameworkUtil.createFilter(org.osgi.framework.FrameworkUtil.createFilter) Tag(org.osgi.service.indexer.impl.util.Tag) File(java.io.File) PrintWriter(java.io.PrintWriter)

Example 3 with Tag

use of org.osgi.service.indexer.impl.util.Tag in project bnd by bndtools.

the class RepoIndex method appendAttributeAndDirectiveTags.

private static void appendAttributeAndDirectiveTags(Tag parentTag, Map<String, Object> attribs, Map<String, String> directives) {
    for (Entry<String, Object> attribEntry : attribs.entrySet()) {
        Tag attribTag = new Tag(Schema.ELEM_ATTRIBUTE);
        attribTag.addAttribute(Schema.ATTR_NAME, attribEntry.getKey());
        TypedValue value = TypedValue.valueOf(attribEntry.getValue());
        value.addTo(attribTag);
        parentTag.addContent(attribTag);
    }
    for (Entry<String, String> directiveEntry : directives.entrySet()) {
        Tag directiveTag = new Tag(Schema.ELEM_DIRECTIVE);
        directiveTag.addAttribute(Schema.ATTR_NAME, directiveEntry.getKey());
        directiveTag.addAttribute(Schema.ATTR_VALUE, directiveEntry.getValue());
        parentTag.addContent(directiveTag);
    }
}
Also used : Tag(org.osgi.service.indexer.impl.util.Tag) TypedValue(org.osgi.service.indexer.impl.types.TypedValue)

Example 4 with Tag

use of org.osgi.service.indexer.impl.util.Tag in project bnd by bndtools.

the class RepoIndex method index.

/*
	 * See ResourceIndexer interface
	 */
public void index(Set<File> files, OutputStream out, Map<String, String> config) throws Exception {
    if (config == null)
        config = new HashMap<String, String>(0);
    Set<File> filesToIndex = new TreeSet<File>();
    if (files != null && !files.isEmpty()) {
        resolveDirectories(files, filesToIndex);
    }
    String prettySetting = config.get(ResourceIndexer.PRETTY);
    String compressedSetting = config.get(ResourceIndexer.COMPRESSED);
    /**
		 * <pre>
		 *  pretty compressed out-pretty out-compressed null null
		 * Indent.NONE true* null false Indent.NONE false null true
		 * Indent.NONE true false null Indent.PRETTY false* false false
		 * Indent.NONE false false true Indent.NONE true true null
		 * Indent.PRETTY false* true false Indent.PRETTY false true true
		 * Indent.PRETTY true * = original behaviour, before compressed was
		 * introduced
		 * </pre>
		 */
    Indent indent = (prettySetting == null || (!Boolean.parseBoolean(prettySetting) && compressedSetting != null)) ? Indent.NONE : Indent.PRETTY;
    boolean compressed = (prettySetting == null && compressedSetting == null) || Boolean.parseBoolean(compressedSetting);
    try (PrintWriter pw = compressed ? new PrintWriter(new GZIPOutputStream(out, Deflater.BEST_COMPRESSION)) : new PrintWriter(new OutputStreamWriter(out, UTF_8))) {
        pw.print(Schema.XML_PROCESSING_INSTRUCTION);
        String stylesheet = config.get(STYLESHEET);
        if (stylesheet != null) {
            indent.print(pw);
            pw.printf(Schema.XML_STYLESHEET_INSTRUCTION, stylesheet, "text/xsl");
        }
        Tag repoTag = new Tag(Schema.ELEM_REPOSITORY);
        String repoName = config.get(REPOSITORY_NAME);
        if (repoName == null)
            repoName = REPOSITORYNAME_DEFAULT;
        repoTag.addAttribute(Schema.ATTR_NAME, repoName);
        String increment = config.get(REPOSITORY_INCREMENT_OVERRIDE);
        if (increment == null)
            increment = Long.toString(System.currentTimeMillis());
        repoTag.addAttribute(Schema.ATTR_INCREMENT, increment);
        repoTag.addAttribute(Schema.ATTR_XML_NAMESPACE, Schema.NAMESPACE);
        repoTag.printOpen(indent, pw, false);
        for (File file : filesToIndex) {
            try {
                Tag resourceTag = generateResource(file, config);
                resourceTag.print(indent.next(), pw);
            } catch (Exception e) {
                log(LogService.LOG_WARNING, MessageFormat.format("Could not index {0}, skipped ({1}).", file, e), null);
            }
        }
        repoTag.printClose(indent, pw);
    }
}
Also used : Indent(org.osgi.service.indexer.impl.util.Indent) HashMap(java.util.HashMap) GZIPOutputStream(java.util.zip.GZIPOutputStream) TreeSet(java.util.TreeSet) OutputStreamWriter(java.io.OutputStreamWriter) Tag(org.osgi.service.indexer.impl.util.Tag) File(java.io.File) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) PrintWriter(java.io.PrintWriter)

Aggregations

Tag (org.osgi.service.indexer.impl.util.Tag)4 File (java.io.File)3 PrintWriter (java.io.PrintWriter)3 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)3 OutputStreamWriter (java.io.OutputStreamWriter)1 StringWriter (java.io.StringWriter)1 URL (java.net.URL)1 Formatter (java.util.Formatter)1 HashMap (java.util.HashMap)1 TreeSet (java.util.TreeSet)1 GZIPOutputStream (java.util.zip.GZIPOutputStream)1 Filter (org.osgi.framework.Filter)1 FrameworkUtil.createFilter (org.osgi.framework.FrameworkUtil.createFilter)1 Capability (org.osgi.service.indexer.Capability)1 Requirement (org.osgi.service.indexer.Requirement)1 ResourceAnalyzer (org.osgi.service.indexer.ResourceAnalyzer)1 TypedValue (org.osgi.service.indexer.impl.types.TypedValue)1 AddOnlyList (org.osgi.service.indexer.impl.util.AddOnlyList)1 Indent (org.osgi.service.indexer.impl.util.Indent)1