View Javadoc

1   package org.codehaus.mojo.pde.descriptor;
2   
3   /*
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.io.BufferedReader;
20  import java.io.File;
21  import java.io.FileReader;
22  import java.util.ArrayList;
23  
24  /**
25   * MANIFEST.MF java object
26   */
27  public class ManifestBean
28  {
29  
30      /**
31       * Manifest id.
32       */
33      private String id;
34  
35      /**
36       * Manifest version.
37       */
38      private String version;
39  
40      /**
41       * Create ManifestBean
42       * 
43       * @param dir pde root directory for locating META-INF/MANIFEST.MF
44       */
45      public ManifestBean( File dir )
46      {
47          this.load( dir );
48      }
49  
50      /**
51       * Load the META-INF/MANIFEST.MF.
52       * 
53       * @param dir pde root directory for locating META-INF/MANIFEST.MF
54       */
55      private void load( File dir )
56      {
57          BufferedReader reader;
58          try
59          {
60              reader = new BufferedReader( new FileReader( new File( dir, "META-INF/MANIFEST.MF" ) ) );
61          }
62          catch ( Exception e )
63          {
64              throw new RuntimeException( e );
65          }
66  
67          // load the entired file into a array of lines
68          ArrayList lines = new ArrayList();
69          try
70          {
71              String line;
72              do
73              {
74                  line = reader.readLine();
75                  if ( line != null )
76                  {
77                      lines.add( line );
78                  }
79              }
80              while ( line != null );
81          }
82          catch ( Exception e )
83          {
84              throw new RuntimeException( e );
85          }
86  
87          this.id = this.getValue( "Bundle-SymbolicName:", lines );
88  
89          this.version = this.getValue( "Bundle-Version:", lines );
90      }
91  
92      /**
93       * Read key,value out of the META-INF/MANIFEST.MF
94       * 
95       * @param key the key to look for
96       * @param lines the META-INF/MANIFEST.MF as an array of strings
97       * @return null if the key is not found, otherwise the trimmed value from the
98       *         META-INF/MANIFEST.MF
99       */
100     private String getValue( String key, ArrayList lines )
101     {
102         String retValue = null;
103 
104         for ( int i = 0; i < lines.size(); ++i )
105         {
106             String line = lines.get( i ).toString();
107 
108             if ( line.startsWith( key ) )
109             {
110                 retValue = line.substring( key.length() );
111 
112                 if ( retValue.indexOf( ";" ) != -1 )
113                 {
114                     retValue = retValue.substring( 0, retValue.indexOf( ";" ) );
115                 }
116 
117             }
118         }
119 
120         if ( retValue != null )
121         {
122             retValue = retValue.trim();
123         }
124 
125         return retValue;
126     }
127 
128     /**
129      * @return the id
130      */
131     public String getId()
132     {
133         return id;
134     }
135 
136     /**
137      * @param theId the id to set
138      */
139     public void setId( String theId )
140     {
141         this.id = theId;
142     }
143 
144     /**
145      * @return the version
146      */
147     public String getVersion()
148     {
149         return version;
150     }
151 
152     /**
153      * @param theVersion the version to set
154      */
155     public void setVersion( String theVersion )
156     {
157         this.version = theVersion;
158     }
159 
160 }