001    /*
002     * Copyright 2004-2006 Geert Bevin <gbevin[remove] at uwyn dot com>
003     * Distributed under the terms of either:
004     * - the common development and distribution license (CDDL), v1.0; or
005     * - the GNU Lesser General Public License, v2.1 or later
006     * $Id: JHighlightVersion.java 3108 2006-03-13 18:03:00Z gbevin $
007     */
008    package com.uwyn.jhighlight;
009    
010    import java.io.ByteArrayOutputStream;
011    import java.io.IOException;
012    import java.io.InputStream;
013    import java.net.URL;
014    import java.net.URLConnection;
015    
016    /**
017     * Provides acces to the version number of this JHighlight release.
018     * 
019     * @author Geert Bevin (gbevin[remove] at uwyn dot com)
020     * @version $Revision: 3108 $
021     * @since 1.0
022     */
023    public class JHighlightVersion
024    {
025            private String  mVersion = null;
026            
027            JHighlightVersion()
028            {
029                    URL version_url = getClass().getClassLoader().getResource("JHIGHLIGHT_VERSION");
030                    if (version_url != null)
031                    {
032                            try
033                            {
034                                    URLConnection connection = version_url.openConnection();
035                                    connection.setUseCaches(false);
036                                    InputStream inputStream = connection.getInputStream();
037                                    
038                                    byte[]                  buffer = new byte[64];
039                                    int                     return_value = -1;
040                                    ByteArrayOutputStream   output_stream = new ByteArrayOutputStream(buffer.length);
041                                    
042                                    try
043                                    {
044                                            return_value = inputStream.read(buffer);
045                                            
046                                            while (-1 != return_value)
047                                            {
048                                                    output_stream.write(buffer, 0, return_value);
049                                                    return_value = inputStream.read(buffer);
050                                            }
051                                    }
052                                    finally
053                                    {
054                                            output_stream.close();
055                                            inputStream.close();
056                                    }
057                                    
058                                    mVersion = output_stream.toString("UTF-8");
059                            }
060                            catch (IOException e)
061                            {
062                                    mVersion = null;
063                            }
064                    }
065                    
066                    if (mVersion != null)
067                    {
068                            mVersion = mVersion.trim();
069                    }
070                    if (null == mVersion)
071                    {
072                            mVersion = "(unknown version)";
073                    }
074            }
075            
076            private String getVersionString()
077            {
078                    return mVersion;
079            }
080            
081            /**
082             * Returns the version number of this JHighlight release.
083             * 
084             * @return the version number
085             * @since 1.0
086             */
087            public static String getVersion()
088            {
089                    return JHighlightVersionSingleton.INSTANCE.getVersionString();
090            }
091    }