View Javadoc

1   package org.apache.maven.jar;
2   
3   /* ====================================================================
4    *   Licensed to the Apache Software Foundation (ASF) under one or more
5    *   contributor license agreements.  See the NOTICE file distributed with
6    *   this work for additional information regarding copyright ownership.
7    *   The ASF licenses this file to You under the Apache License, Version 2.0
8    *   (the "License"); you may not use this file except in compliance with
9    *   the License.  You may obtain a copy of the License at
10   *
11   *       http://www.apache.org/licenses/LICENSE-2.0
12   *
13   *   Unless required by applicable law or agreed to in writing, software
14   *   distributed under the License is distributed on an "AS IS" BASIS,
15   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   *   See the License for the specific language governing permissions and
17   *   limitations under the License.
18   * ====================================================================
19   */
20  
21  
22  import java.io.File;
23  import java.io.FileInputStream;
24  import java.io.IOException;
25  import java.io.InputStream;
26  
27  import java.util.Iterator;
28  import java.util.Map;
29  import java.util.jar.Attributes;
30  import java.util.jar.JarFile;
31  import java.util.jar.Manifest;
32  
33  
34  /**
35   * Utility class for maven-jar-plugin. Currently only used for
36   * testing the manifest created by Maven's jar:jar goal.
37   *
38   * @author <a href="mailto:ltheussl@apache.org">Lukas Theussl</a>
39   * @version $Id: JarUtils.java 531612 2007-04-23 21:28:38Z ltheussl $
40   */
41  public class JarUtils
42  {
43  
44      /** The manifest. */
45      private Manifest manifest;
46  
47      /** The Map of manifest entries. */
48      private Map manifestEntries;
49  
50      /** The main Attributes of the manifest. */
51      private Attributes mainAttributes;
52  
53      /** The section Attributes of the manifest. */
54      private Attributes sectionAttributes;
55  
56  
57      /**
58       * Extracts the manifest from the given jar file.
59       * The manifest entries can then be retrieved via
60       * getManifestEntries() and getMainAttributes().
61       *
62       * @param theJarFile The jar file to extract the manifest from.
63       * @throws IOException If there is an error opening the jar file.
64       */
65      public void extractManifestFromJar( File theJarFile ) throws IOException
66      {
67          JarFile jarfile = new JarFile( theJarFile );
68          this.manifest = jarfile.getManifest();
69          extractManifestEntries();
70      }
71  
72      /**
73       * Extracts the manifest from a given file.
74       * The manifest entries can then be retrieved via
75       * getManifestEntries() and getMainAttributes().
76       *
77       * @param manifestFile The manifest file.
78       * @throws IOException If there is an error opening the file.
79       */
80      public void extractManifestFromFile( File manifestFile ) throws IOException
81      {
82          InputStream fis = new FileInputStream( manifestFile );
83          this.manifest = new Manifest( fis );
84          extractManifestEntries();
85      }
86  
87      /**
88       * Sets the manifest.
89       *
90       * @param mf The new manifest.
91       */
92      public void setManifest( Manifest mf )
93      {
94        this.manifest = mf;
95      }
96  
97      /**
98       * Gets the manifest.
99       *
100      * @return The manifest.
101      */
102     public Manifest getManifest()
103     {
104       return manifest;
105     }
106 
107     /**
108      * Returns a Map of manifest entries.
109      *
110      * @return Map of manifest entries.
111      */
112     public Map getManifestEntries()
113     {
114         return manifestEntries;
115     }
116 
117     /**
118      * Returns the main attributes of the manifest as Attributes.
119      *
120      * @return The main attributes.
121      */
122     public Attributes getMainAttributes()
123     {
124         return mainAttributes;
125     }
126 
127     /**
128      * Checks if the current manifest contains a section with the given key.
129      *
130      * @param key The section name.
131      * @return true if the manifest contains a section with the given key.
132      */
133     public boolean containsSection( String key )
134     {
135         return manifestEntries.containsKey( key );
136     }
137 
138     /**
139      * Returns the value of the main attribute key.
140      *
141      * @param key The attribute name.
142      * @return The attribute value, null if the attribute is not found.
143      */
144     public String getMainAttribute( String key )
145     {
146         return mainAttributes.getValue( key );
147     }
148 
149     /**
150      * Checks if the current manifest contains a main attribute
151      * with the given key.
152      *
153      * @param key The main attribute name.
154      * @return true if the manifest contains a main attribute
155      * with the given key.
156      */
157     public boolean containsMainAttribute( String key )
158     {
159         boolean contains = false;
160         if ( getMainAttribute( key ) != null )
161         {
162             contains = true;
163         }
164         return contains;
165     }
166 
167     /**
168      * Returns the value of the section attribute key.
169      *
170      * @param key The attribute name.
171      * @return The attribute value, null if the attribute is not found.
172      */
173     public String getSectionAttribute( String key )
174     {
175         return sectionAttributes.getValue( key );
176     }
177 
178     /**
179      * Checks if the current manifest contains a section attribute
180      * with the given key.
181      *
182      * @param key The section attribute name.
183      * @return true if the manifest contains a  section attribute
184      * with the given key.
185      */
186     public boolean containsSectionAttribute( String key )
187     {
188         boolean contains = false;
189         if ( getSectionAttribute( key ) != null )
190         {
191             contains = true;
192         }
193         return contains;
194     }
195 
196     /**
197      * Extracts the manifest entries, main attributes and section attributes.
198      */
199     private void extractManifestEntries()
200     {
201         this.manifestEntries = manifest.getEntries();
202         this.mainAttributes = manifest.getMainAttributes();
203 
204         for (Iterator it = manifestEntries.keySet().iterator(); it.hasNext(); )
205         {
206             String entryName = (String) it.next();
207             this.sectionAttributes =
208                 (Attributes) manifestEntries.get( entryName );
209         }
210     }
211 
212 }