<commit_before>package bndtools.jareditor.internal;

import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.util.Collection;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.jar.Manifest;
import java.util.zip.ZipException;

import aQute.bnd.header.Attrs;
import aQute.bnd.header.Parameters;
import aQute.bnd.osgi.Analyzer;
import aQute.bnd.osgi.Constants;
import aQute.bnd.osgi.Descriptors.PackageRef;
import aQute.bnd.osgi.Domain;
import aQute.bnd.osgi.Jar;
import aQute.bnd.osgi.Processor;
import aQute.bnd.osgi.Resource;
import aQute.bnd.osgi.Verifier;
import aQute.lib.collections.SortedList;
import aQute.lib.io.IO;
import aQute.libg.generics.Create;
import bndtools.jareditor.internal.utils.CollectionUtil;

public class Printer extends Processor {

    final static int MANIFEST = 2;
    final static int LIST = 4;

    final static int IMPEXP = 16;
    final static int USES = 32;
    final static int USEDBY = 64;
    final static int COMPONENT = 128;
    final static int METATYPE = 256;
    final static int VERIFY = 1;

    PrintStream out = System.out;

    private static final String EOL = String.format("%n");

    public void setOut(PrintStream out) {
        this.out = out;
    }

    public void doPrint(String string, int options) throws Exception {
        int optionsi = options;
        File file = new File(string);
        if (!file.exists())
            error("File to print not found: " + string);
        else {
            if (optionsi == 0)
                optionsi = VERIFY | MANIFEST | IMPEXP | USES;
            doPrint(file, optionsi);
        }
    }

    private void doPrint(File file, int options) throws ZipException, IOException, Exception {
        Jar jar = new Jar(file.getName(), file);
        try {
            if ((options & VERIFY) != 0) {
                Verifier verifier = new Verifier(jar);
                verifier.setPedantic(isPedantic());
                verifier.verify();
                getInfo(verifier);
            }
            if ((options & MANIFEST) != 0) {
                Manifest manifest = jar.getManifest();
                if (manifest == null)
                    warning("JAR has no manifest " + file);
                else {
                    out.println("[MANIFEST " + jar.getName() + "]");
                    printManifest(manifest);
                }
                out.println();
            }
            if ((options & IMPEXP) != 0) {
                out.println("[IMPEXP]");
                Manifest m = jar.getManifest();
                Domain domain = Domain.domain(m);

                if (m != null) {
                    Parameters imports = domain.getImportPackage();
                    Parameters exports = domain.getExportPackage();
                    for (String p : exports.keySet()) {
                        if (imports.containsKey(p)) {
                            Attrs attrs = imports.get(p);
                            if (attrs.containsKey(VERSION_ATTRIBUTE)) {
                                exports.get(p).put("imported-as", attrs.get(VERSION_ATTRIBUTE));
                            }
                        }
                    }
                    print("Import-Package", new TreeMap<String,Attrs>(imports));
                    print("Export-Package", new TreeMap<String,Attrs>(exports));
                } else
                    warning("File has no manifest");
            }

            if ((options & (USES | USEDBY)) != 0) {
                out.println();
                Analyzer analyzer = new Analyzer();
                analyzer.setPedantic(isPedantic());
                analyzer.setJar(jar);
                analyzer.analyze();
                if ((options & USES) != 0) {
                    out.println("[USES]");
                    printMultiMap(analyzer.getUses());
                    out.println();
                }
                if ((options & USEDBY) != 0) {
                    out.println("[USEDBY]");
                    Map<PackageRef,Set<PackageRef>> usedBy = CollectionUtil.invertMapOfCollection(analyzer.getUses());
                    printMultiMap(usedBy);
                }
                out.println();
            }

            if ((options & COMPONENT) != 0) {
                printComponents(jar);
                out.println();
            }

            if ((options & METATYPE) != 0) {
                printMetatype(jar);
                out.println();
            }

            if ((options & LIST) != 0) {
                out.println("[LIST]");
                for (Map.Entry<String,Map<String,Resource>> entry : jar.getDirectories().entrySet()) {
                    String name = entry.getKey();
                    Map<String,Resource> contents = entry.getValue();
                    out.println(name);
                    if (contents != null) {
                        for (String element : contents.keySet()) {
                            int n = element.lastIndexOf('/');
                            if (n > 0)
                                element = element.substring(n + 1);
                            out.print("  ");
                            out.print(element);
                            String path = element;
                            if (name.length() != 0)
                                path = name + "/" + element;
                            Resource r = contents.get(path);
                            if (r != null) {
                                String extra = r.getExtra();
                                if (extra != null) {
                                    out.print(" extra='" + escapeUnicode(extra) + "'");
                                }
                            }
                            out.println();
                        }
                    } else {
                        out.println(name + " <no contents>");
                    }
                }
                out.println();
            }
        } finally {
            jar.close();
        }
    }

    /**
     * @param manifest
     */
    private void printManifest(Manifest manifest) {
        SortedSet<String> sorted = new TreeSet<String>();
        for (Object element : manifest.getMainAttributes().keySet()) {<commit_msg>jar editor: do not print pointless '<no contents>' texts<commit_after>package bndtools.jareditor.internal;

import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.util.Collection;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.jar.Manifest;
import java.util.zip.ZipException;

import aQute.bnd.header.Attrs;
import aQute.bnd.header.Parameters;
import aQute.bnd.osgi.Analyzer;
import aQute.bnd.osgi.Constants;
import aQute.bnd.osgi.Descriptors.PackageRef;
import aQute.bnd.osgi.Domain;
import aQute.bnd.osgi.Jar;
import aQute.bnd.osgi.Processor;
import aQute.bnd.osgi.Resource;
import aQute.bnd.osgi.Verifier;
import aQute.lib.collections.SortedList;
import aQute.lib.io.IO;
import aQute.libg.generics.Create;
import bndtools.jareditor.internal.utils.CollectionUtil;

public class Printer extends Processor {

    final static int MANIFEST = 2;
    final static int LIST = 4;

    final static int IMPEXP = 16;
    final static int USES = 32;
    final static int USEDBY = 64;
    final static int COMPONENT = 128;
    final static int METATYPE = 256;
    final static int VERIFY = 1;

    PrintStream out = System.out;

    private static final String EOL = String.format("%n");

    public void setOut(PrintStream out) {
        this.out = out;
    }

    public void doPrint(String string, int options) throws Exception {
        int optionsi = options;
        File file = new File(string);
        if (!file.exists())
            error("File to print not found: " + string);
        else {
            if (optionsi == 0)
                optionsi = VERIFY | MANIFEST | IMPEXP | USES;
            doPrint(file, optionsi);
        }
    }

    private void doPrint(File file, int options) throws ZipException, IOException, Exception {
        Jar jar = new Jar(file.getName(), file);
        try {
            if ((options & VERIFY) != 0) {
                Verifier verifier = new Verifier(jar);
                verifier.setPedantic(isPedantic());
                verifier.verify();
                getInfo(verifier);
            }
            if ((options & MANIFEST) != 0) {
                Manifest manifest = jar.getManifest();
                if (manifest == null)
                    warning("JAR has no manifest " + file);
                else {
                    out.println("[MANIFEST " + jar.getName() + "]");
                    printManifest(manifest);
                }
                out.println();
            }
            if ((options & IMPEXP) != 0) {
                out.println("[IMPEXP]");
                Manifest m = jar.getManifest();
                Domain domain = Domain.domain(m);

                if (m != null) {
                    Parameters imports = domain.getImportPackage();
                    Parameters exports = domain.getExportPackage();
                    for (String p : exports.keySet()) {
                        if (imports.containsKey(p)) {
                            Attrs attrs = imports.get(p);
                            if (attrs.containsKey(VERSION_ATTRIBUTE)) {
                                exports.get(p).put("imported-as", attrs.get(VERSION_ATTRIBUTE));
                            }
                        }
                    }
                    print("Import-Package", new TreeMap<String,Attrs>(imports));
                    print("Export-Package", new TreeMap<String,Attrs>(exports));
                } else
                    warning("File has no manifest");
            }

            if ((options & (USES | USEDBY)) != 0) {
                out.println();
                Analyzer analyzer = new Analyzer();
                analyzer.setPedantic(isPedantic());
                analyzer.setJar(jar);
                analyzer.analyze();
                if ((options & USES) != 0) {
                    out.println("[USES]");
                    printMultiMap(analyzer.getUses());
                    out.println();
                }
                if ((options & USEDBY) != 0) {
                    out.println("[USEDBY]");
                    Map<PackageRef,Set<PackageRef>> usedBy = CollectionUtil.invertMapOfCollection(analyzer.getUses());
                    printMultiMap(usedBy);
                }
                out.println();
            }

            if ((options & COMPONENT) != 0) {
                printComponents(jar);
                out.println();
            }

            if ((options & METATYPE) != 0) {
                printMetatype(jar);
                out.println();
            }

            if ((options & LIST) != 0) {
                out.println("[LIST]");
                for (Map.Entry<String,Map<String,Resource>> entry : jar.getDirectories().entrySet()) {
                    String name = entry.getKey();
                    Map<String,Resource> contents = entry.getValue();
                    out.println(name);
                    if (contents != null) {
                        for (String element : contents.keySet()) {
                            int n = element.lastIndexOf('/');
                            if (n > 0)
                                element = element.substring(n + 1);
                            out.print("  ");
                            out.print(element);
                            String path = element;
                            if (name.length() != 0)
                                path = name + "/" + element;
                            Resource r = contents.get(path);
                            if (r != null) {
                                String extra = r.getExtra();
                                if (extra != null) {
                                    out.print(" extra='" + escapeUnicode(extra) + "'");
                                }
                            }
                            out.println();
                        }
                    }
                }
                out.println();
            }
        } finally {
            jar.close();
        }
    }

    /**
     * @param manifest
     */
    private void printManifest(Manifest manifest) {
        SortedSet<String> sorted = new TreeSet<String>();
        for (Object element : manifest.getMainAttributes().keySet()) {
{'fraction_non_alphanumeric': 0.11109379869118105, 'fraction_numerical': 0.004362729822374571, 'mean_word_length': 1.3614790286975718, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4263311', 'n_tokens_mistral': 3512, 'n_tokens_neox': 3286, 'n_words': 734}