Example usage for java.util.jar Attributes get

List of usage examples for java.util.jar Attributes get

Introduction

In this page you can find the example usage for java.util.jar Attributes get.

Prototype

public Object get(Object name) 

Source Link

Document

Returns the value of the specified attribute name, or null if the attribute name was not found.

Usage

From source file:org.hecl.jarhack.JarHack.java

/**
 * The <code>substHecl</code> method takes the filenames of two
 * .jar's - one as input, the second as output, in addition to the
 * name of the application.  Where it counts, the old name (Hecl,
 * usually) is overridden with the new name, and the new .jar file
 * is written to the specified outfile.  Via the iconname argument
 * it is also possible to specify a new icon file to use.
 *
 * @param infile a <code>FileInputStream</code> value
 * @param outfile a <code>String</code> value
 * @param newname a <code>String</code> value
 * @param iconname a <code>String</code> value
 * @exception IOException if an error occurs
 *///from  ww  w  . j  a  v a  2 s. c  o m
public static void substHecl(InputStream infile, String outfile, String newname, String iconname,
        String scriptfile) throws IOException {

    JarInputStream jif = new JarInputStream(infile);
    Manifest mf = jif.getManifest();
    Attributes attrs = mf.getMainAttributes();

    Set keys = attrs.keySet();
    Iterator it = keys.iterator();
    while (it.hasNext()) {
        Object key = it.next();
        Object value = attrs.get(key);
        String keyname = key.toString();

        /* These are the three cases that interest us in
         * particular, where we need to make changes. */
        if (keyname.equals("MIDlet-Name")) {
            attrs.putValue(keyname, newname);
        } else if (keyname.equals("MIDlet-1")) {
            String valuestr = value.toString();
            /* FIXME - the stringsplit method is used for older
             * versions of GCJ.  Once newer versions are common,
             * it can go away.  Or not - it works just fine. */
            String properties[] = stringsplit(valuestr, ", ");
            attrs.putValue(keyname, newname + ", " + properties[1] + ", " + properties[2]);
        } else if (keyname.equals("MicroEdition-Configuration")) {
            cldcversion = value.toString();
        } else if (keyname.equals("MicroEdition-Profile")) {
            midpversion = value.toString();
        } else if (keyname.equals("MIDlet-Jar-URL")) {
            attrs.put(key, newname + ".jar");
        }
    }

    JarOutputStream jof = new JarOutputStream(new FileOutputStream(outfile), mf);

    byte[] buf = new byte[4096];

    /* Go through the various entries. */
    JarEntry entry;
    int read;
    while ((entry = jif.getNextJarEntry()) != null) {

        /* Don't copy the manifest file. */
        if ("META-INF/MANIFEST.MF".equals(entry.getName()))
            continue;

        /* Insert our own icon */
        if (iconname != null && "Hecl.png".equals(entry.getName())) {
            jof.putNextEntry(new JarEntry("Hecl.png"));
            FileInputStream inf = new FileInputStream(iconname);
            while ((read = inf.read(buf)) != -1) {
                jof.write(buf, 0, read);
            }
            inf.close();
        }
        /* Insert our own copy of the script file. */
        else if ("script.hcl".equals(entry.getName())) {
            jof.putNextEntry(new JarEntry("script.hcl"));
            FileInputStream inf = new FileInputStream(scriptfile);
            while ((read = inf.read(buf)) != -1) {
                jof.write(buf, 0, read);
            }
            inf.close();
        } else {
            /* Otherwise, just copy the entry. */
            jof.putNextEntry(entry);
            while ((read = jif.read(buf)) != -1) {
                jof.write(buf, 0, read);
            }
        }

        jof.closeEntry();
    }

    jof.flush();
    jof.close();
    jif.close();
}

From source file:org.wso2.carbon.wsdl2code.POMGenerator.java

public static String getVersion(String artifact) throws Exception {

    java.io.File file = new java.io.File(artifact);
    java.util.jar.JarFile jar = new java.util.jar.JarFile(file);
    java.util.jar.Manifest manifest = jar.getManifest();

    String versionNumber = "";
    java.util.jar.Attributes attributes = manifest.getMainAttributes();
    if (attributes != null) {
        java.util.Iterator it = attributes.keySet().iterator();
        while (it.hasNext()) {
            java.util.jar.Attributes.Name key = (java.util.jar.Attributes.Name) it.next();
            String keyword = key.toString();
            if (keyword.equals("Implementation-Version") || keyword.equals("Bundle-Version")) {
                versionNumber = (String) attributes.get(key);
                break;
            }//from   w  w  w .j  a va 2s .  c o m
        }
    }
    jar.close();

    if (versionNumber != null && !versionNumber.equals("")) {
        return versionNumber;
    }

    //if manifest does not contain version number it had to be extracted from the file name
    String fileName = file.getName().substring(0, file.getName().lastIndexOf("."));
    if (fileName.contains(".")) {
        String majorVersion = fileName.substring(0, fileName.indexOf("."));
        String minorVersion = fileName.substring(fileName.indexOf("."));
        int delimiter = majorVersion.lastIndexOf("-");
        if (majorVersion.indexOf("_") > delimiter)
            delimiter = majorVersion.indexOf("_");
        majorVersion = majorVersion.substring(delimiter + 1, fileName.indexOf("."));
        versionNumber = majorVersion + minorVersion;
    }

    return versionNumber;

}