001    package org.maltparser.core.helper;
002    
003    import java.io.File;
004    import java.io.IOException;
005    import java.net.URL;
006    import java.net.URLDecoder;
007    import java.util.jar.Attributes;
008    import java.util.jar.JarFile;
009    import java.util.jar.Manifest;
010    import java.util.regex.Pattern;
011    
012    import org.maltparser.core.exception.MaltChainedException;
013    import org.maltparser.core.options.OptionManager;
014    
015    /**
016     * 
017     * 
018     * @author Johan Hall
019     */
020    public class SystemInfo {
021            private static SystemInfo uniqueInstance = new SystemInfo();
022            private static String version;
023            private static String buildDate;
024            private static Attributes manifestAttributes;
025            private static File maltJarPath;
026    
027            private SystemInfo() {
028                    Pattern MALTJAR = Pattern.compile("^.*malt[^" + File.separator
029                                    + "]*\\.jar$");
030                    try {
031                            getManifestInfo();
032    
033                            String[] jarfiles = System.getProperty("java.class.path").split(
034                                            File.pathSeparator);
035                            for (int i = 0; i < jarfiles.length; i++) {
036    
037                                    if (MALTJAR.matcher(jarfiles[i]).matches()) {
038                                            maltJarPath = new File(new File(jarfiles[i])
039                                                            .getAbsolutePath());
040                                    }
041                            }
042                    } catch (MaltChainedException e) {
043                            if (SystemLogger.logger().isDebugEnabled()) {
044                                    SystemLogger.logger().debug("", e);
045                            } else {
046                                    SystemLogger.logger().error(e.getMessageChain());
047                            }
048                            System.exit(1);
049                    }
050            }
051    
052            /**
053             * Returns a reference to the single instance.
054             */
055            public static SystemInfo instance() {
056                    return uniqueInstance;
057            }
058    
059            /**
060             * Returns the application header
061             * 
062             * @return the application header
063             */
064            public static String header() {
065                    StringBuilder sb = new StringBuilder();
066                    sb
067                                    .append("-----------------------------------------------------------------------------\n"
068                                                    + "                          MaltParser "+ version + "                             \n"
069                                                    + "-----------------------------------------------------------------------------\n"
070                                                    + "         MALT (Models and Algorithms for Language Technology) Group          \n"
071                                                    + "             Vaxjo University and Uppsala University                         \n"
072                                                    + "                             Sweden                                          \n"
073                                                    + "-----------------------------------------------------------------------------\n");
074                    return sb.toString();
075            }
076    
077            /**
078             * Returns a short version of the help
079             * 
080             * @return a short version of the help
081             */
082            public static String shortHelp() {
083                    StringBuilder sb = new StringBuilder();
084                    sb.append("\n"
085                                    + "Usage: \n"
086                                    + "   java -jar malt.jar -f <path to option file> <options>\n"
087                                    + "   java -jar malt.jar -h for more help and options\n\n"
088                                    + OptionManager.instance().getOptionDescriptions()
089                                                    .toStringOptionGroup("system")
090                                    + "Documentation: docs/index.html\n");
091                    return sb.toString();
092            }
093    
094            /**
095             * Returns a set of attributes present in the jar manifest file
096             * 
097             * @return a set of attributes present in the jar manifest file
098             */
099            public static Attributes getManifestAttributes() {
100                    return manifestAttributes;
101            }
102    
103            /**
104             * Returns the version number as string
105             * 
106             * @return the version number as string
107             */
108            public static String getVersion() {
109                    return version;
110            }
111    
112            /**
113             * Returns the build date
114             * 
115             * @return the build date
116             */
117            public static String getBuildDate() {
118                    return buildDate;
119            }
120    
121            public static File getMaltJarPath() {
122                    return maltJarPath;
123            }
124    
125            /**
126             * Loads the manifest attributes from the manifest in the jar-file
127             * 
128             * @throws MaltChainedException
129             */
130            private void getManifestInfo() throws MaltChainedException {
131                    try {
132                            URL codeBase = SystemInfo.class.getProtectionDomain()
133                                            .getCodeSource().getLocation();
134                            if (codeBase != null && codeBase.getPath().endsWith(".jar")) {
135                                    JarFile jarfile = new JarFile(URLDecoder.decode(codeBase
136                                                    .getPath(), java.nio.charset.Charset.defaultCharset()
137                                                    .name()));
138                                    Manifest manifest = jarfile.getManifest();
139                                    Attributes manifestAttributes = manifest.getMainAttributes();
140                                    version = manifestAttributes.getValue("Implementation-Version");
141                                    buildDate = manifestAttributes.getValue("Build-Date");
142                            }
143                    } catch (IOException e) {
144                            version = "";
145                            buildDate = "Not available";
146                            e.printStackTrace();
147                    }
148            }
149    }