View Javadoc

1   package net.sourceforge.argval.manifestinfo.impl;
2   
3   import java.io.File;
4   import java.util.Collections;
5   import java.util.HashMap;
6   import java.util.Map;
7   import java.util.Set;
8   import java.util.jar.Attributes;
9   import java.util.jar.Manifest;
10  import java.util.jar.Attributes.Name;
11  import java.util.regex.Matcher;
12  import java.util.regex.Pattern;
13  
14  import net.sourceforge.argval.manifestinfo.ManifestInfo;
15  import net.sourceforge.argval.manifestinfo.ManifestInfoVisitor;
16  import net.sourceforge.argval.packageinfo.AbstractPackageInfo;
17  import net.sourceforge.argval.packageinfo.PackageInfo;
18  
19  public class ManifestInfoAsPackageInfo extends AbstractPackageInfo 
20          implements ManifestInfo {
21      private static final String JAR_FILE_AS_IMPL_TITLE = "title";
22      private static final String JAR_FILE_AS_IMPL_VERSION = "version";
23      
24      private File jarPath;
25      private Manifest manifest;
26      
27      
28      public ManifestInfoAsPackageInfo(File jarPath, Manifest manifest) {
29          super();
30          this.jarPath = jarPath;
31          this.manifest = manifest;
32      }
33  
34      public File getJarPath() {
35          return jarPath;
36      }
37  
38  //    public JarFile getJarFile() {
39  //        return new JarFile(jarPath);
40  //    }
41  
42      public Manifest getManifest() {
43          return manifest;
44      }
45      
46      public PackageInfo getPackageInfo() {
47          return this;
48      }
49  
50      public void addPackageName(String name) {
51          // Not supported
52      }
53  
54      public String getSpecificationTitle() {
55          return getMainAttribute(Attributes.Name.SPECIFICATION_TITLE);
56      }
57  
58      public String getSpecificationVendor() {
59          return getMainAttribute(Attributes.Name.IMPLEMENTATION_VENDOR);
60      }
61  
62      public String getSpecificationVersion() {
63          return getMainAttribute(Attributes.Name.SPECIFICATION_VERSION);
64      }
65  
66      public String getImplementationTitle() {
67          String title = getMainAttribute(Attributes.Name.IMPLEMENTATION_TITLE);
68          if (title == null)
69          {
70              title = extractTitleAndVersion(jarPath.getName()).get(JAR_FILE_AS_IMPL_TITLE);
71          }
72          if (title == null)
73          {
74              title = jarPath.getName();
75          }
76          return title;
77      }
78  
79      public String getImplementationVendor() {
80          return getMainAttribute(Attributes.Name.IMPLEMENTATION_VENDOR);
81      }
82  
83      public String getImplementationVersion() {
84          String version = getMainAttribute(Attributes.Name.IMPLEMENTATION_VERSION);
85          if (version == null)
86          {
87              version = extractTitleAndVersion(jarPath.getName()).get(JAR_FILE_AS_IMPL_VERSION);
88          }
89          if (version == null)
90          {
91              version = PackageInfo.UNKNOWN_VERSION;
92          }
93          return version;
94      }
95  
96      public Set<String> getNameSet() {
97          return Collections.emptySet();
98      }
99  
100     
101     public void accept(ManifestInfoVisitor visitor) {
102         visitor.visit(this);
103     }
104 
105 
106     public String getMainAttribute(Name attributeName) {
107         if (manifest == null)
108         {
109             return null;
110         }
111         return manifest.getMainAttributes().getValue(attributeName);
112     }
113 
114     /**
115      * Extract the title and version of a package from the filename.
116      * 
117      * @param jarName 
118      * @return
119      */
120     private Map<String, String> extractTitleAndVersion(String jarName) {
121         Pattern pattern = Pattern.compile("(.*)-([0-9]\\.[0-9].*)\\.jar$");
122         Matcher matcher = pattern.matcher(jarName);
123         Map<String, String> resultMap = new HashMap<String, String>();  
124         if (matcher.matches() && matcher.groupCount() == 2)
125         {
126             resultMap.put(JAR_FILE_AS_IMPL_TITLE, matcher.group(1));
127             resultMap.put(JAR_FILE_AS_IMPL_VERSION, matcher.group(2));
128         }
129         return resultMap;
130     }
131     
132 }