001/*
002 *  Copyright (c) 2003-2010 The Regents of the University of California.
003 *  All rights reserved.
004 *  Permission is hereby granted, without written agreement and without
005 *  license or royalty fees, to use, copy, modify, and distribute this
006 *  software and its documentation for any purpose, provided that the above
007 *  copyright notice and the following two paragraphs appear in all copies
008 *  of this software.
009 *  IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
010 *  FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
011 *  ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
012 *  THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
013 *  SUCH DAMAGE.
014 *  THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
015 *  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
016 *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
017 *  PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
018 *  CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
019 *  ENHANCEMENTS, OR MODIFICATIONS.
020 *  PT_COPYRIGHT_VERSION_2
021 *  COPYRIGHTENDKEY
022 */
023package org.kepler.kar;
024
025import java.io.IOException;
026import java.io.InputStream;
027import java.util.Map;
028import java.util.jar.Attributes;
029import java.util.jar.Manifest;
030
031/**
032 * KAR manifest file. This extends java.util.jar.Manifest with utility methods.
033 */
034public class KARManifest extends Manifest {
035        
036        private final static String MANIFEST_VERSION = "1.4.2";
037        
038        /**
039         * Constructor.
040         */
041        public KARManifest() {
042                super();
043                init();
044        }
045
046        /**
047         * Constructor. Create the manifest from the inputstream
048         * 
049         * @param is
050         */
051        public KARManifest(InputStream is) throws IOException {
052                super(is);
053                init();
054        }
055
056        /**
057         * Constructor. Create the manifest as a copy of the passed manifest
058         * 
059         * @param man
060         */
061        public KARManifest(Manifest man) {
062                super(man);
063                init();
064        }
065
066        /**
067         * initialize the attributes
068         */
069        private void init() {
070                Attributes mainAtts = getMainAttributes();
071                if (mainAtts == null) {
072                        mainAtts = new Attributes();
073                }
074                mainAtts.put(Attributes.Name.MANIFEST_VERSION, MANIFEST_VERSION);
075                // mainAtts.put(new Attributes.Name("KAR-Version"), "1.0");
076                mainAtts.put(KARFile.KAR_VERSION, KARFile.CURRENT_VERSION);
077        }
078
079        /**
080         * add a main manifest attribute
081         * 
082         * @param name
083         * @param value
084         */
085        public void addMainAttribute(String name, String value) {
086                Attributes atts = getMainAttributes();
087                atts.putValue(name, value);
088        }
089
090        /**
091         * get a main manifest attribute
092         * 
093         * @param name
094         */
095        public String getMainAttribute(String name) {
096                Attributes atts = getMainAttributes();
097                return atts.getValue(name);
098        }
099
100        /**
101         * add an entry specific attribute
102         * 
103         * @param entry
104         *            the JarEntry that you are adding an attribute for
105         * @param name
106         *            the name of the attribute
107         * @param value
108         *            the value of the attribute
109         */
110        public void addEntryAttribute(KAREntry entry, String name, String value) {
111                addEntryAttribute(entry.getName(), name, value);
112        }
113
114        /**
115         * add an entry specific attribute
116         * 
117         * @param entryName
118         *            the name of the JarEntry that you are adding an attribute for
119         * @param name
120         *            the name of the attribute
121         * @param value
122         *            the value of the attribute
123         */
124        public void addEntryAttribute(String entryName, String name, String value) {
125                Map entries = getEntries();
126                if (entries.get(entryName) == null) {
127                        entries.put(entryName, new Attributes());
128                }
129
130                Attributes atts = getAttributes(entryName);
131                if (atts == null) {
132                        atts = new Attributes();
133                }
134
135                atts.put(new Attributes.Name(name), value);
136        }
137
138        /**
139         * get a JarEntry specific attribute
140         * 
141         * @param entry
142         *            the entry you're getting the attribute for
143         * @param name
144         *            the name of the attribute
145         */
146        public String getEntryAttribute(KAREntry entry, String name) {
147                Attributes atts = getAttributes(entry.getName());
148                return atts.getValue(name);
149        }
150
151        /**
152         * get a JarEntry specific attribute
153         * 
154         * @param entryName
155         *            the name of the JarEntry you're getting the attribute for
156         * @param name
157         *            the name of the attribute
158         */
159        public String getEntryAttribute(String entryName, String name) {
160                Attributes atts = getAttributes(entryName);
161                return atts.getValue(name);
162        }
163}