Coverage Report - org.argouml.util.Tools
 
Classes in this File Line Coverage Branch Coverage Complexity
Tools
83%
70/84
75%
15/20
5
 
 1  
 /* $Id: Tools.java 17887 2010-01-12 21:17:18Z linus $
 2  
  *****************************************************************************
 3  
  * Copyright (c) 2009 Contributors - see below
 4  
  * All rights reserved. This program and the accompanying materials
 5  
  * are made available under the terms of the Eclipse Public License v1.0
 6  
  * which accompanies this distribution, and is available at
 7  
  * http://www.eclipse.org/legal/epl-v10.html
 8  
  *
 9  
  * Contributors:
 10  
  *    tfmorris
 11  
  *****************************************************************************
 12  
  *
 13  
  * Some portions of this file was previously release using the BSD License:
 14  
  */
 15  
 
 16  
 // Copyright (c) 1996-2006 The Regents of the University of California. All
 17  
 // Rights Reserved. Permission to use, copy, modify, and distribute this
 18  
 // software and its documentation without fee, and without a written
 19  
 // agreement is hereby granted, provided that the above copyright notice
 20  
 // and this paragraph appear in all copies.  This software program and
 21  
 // documentation are copyrighted by The Regents of the University of
 22  
 // California. The software program and documentation are supplied "AS
 23  
 // IS", without any accompanying services from The Regents. The Regents
 24  
 // does not warrant that the operation of the program will be
 25  
 // uninterrupted or error-free. The end-user understands that the program
 26  
 // was developed for research purposes and is advised not to rely
 27  
 // exclusively on the program for any reason.  IN NO EVENT SHALL THE
 28  
 // UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
 29  
 // SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,
 30  
 // ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
 31  
 // THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
 32  
 // SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY
 33  
 // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 34  
 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
 35  
 // PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
 36  
 // CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT,
 37  
 // UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 38  
 
 39  
 package org.argouml.util;
 40  
 
 41  
 import java.io.BufferedReader;
 42  
 import java.io.File;
 43  
 import java.io.IOException;
 44  
 import java.io.StringReader;
 45  
 import java.util.Locale;
 46  
 
 47  
 import javax.xml.parsers.SAXParserFactory;
 48  
 
 49  
 import org.apache.log4j.Logger;
 50  
 import org.argouml.i18n.Translator;
 51  
 
 52  
 /**
 53  
  * Some tools thrown together...
 54  
  *
 55  
  */
 56  0
 public class Tools {
 57  
 
 58  
     /**
 59  
      * Logger.
 60  
      */
 61  900
     private static final Logger LOG = Logger.getLogger(Tools.class);
 62  
 
 63  900
     private static final String[] PACKAGELIST =
 64  
         new String[]{
 65  
             "org.argouml.application", 
 66  
             // TODO: The following is MDR specific.  We need something generic
 67  
             // to all Model subsystems - tfm 20070716
 68  
             "org.netbeans.mdr",
 69  
             "org.tigris.gef.base", 
 70  
             "org.xml.sax",
 71  
             "java.lang", 
 72  
             "org.apache.log4j",
 73  
         };
 74  
 
 75  
     private static void getComponentVersionInfo(StringBuffer sb, String pn) {
 76  6643
         sb.append(Translator.localize("label.package")).append(": ");
 77  6643
         sb.append(pn);
 78  6643
         sb.append('\n');
 79  6643
         Package pkg = Package.getPackage(pn);
 80  6643
         if (pkg == null) {
 81  900
             sb.append(Translator.localize("label.no-version"));
 82  
         } else {
 83  5743
             String in = pkg.getImplementationTitle();
 84  5743
             if (in != null) {
 85  3796
                 sb.append(Translator.localize("label.component"));
 86  3796
                 sb.append(": ");
 87  3796
                 sb.append(in);
 88  
             }
 89  5743
             in = pkg.getImplementationVendor();
 90  5743
             if (in != null) {
 91  3796
                 sb.append(Translator.localize("label.by"));
 92  3796
                 sb.append(": ");
 93  3796
                 sb.append(in);
 94  
             }
 95  5743
             in = pkg.getImplementationVersion();
 96  5743
             if (in != null) {
 97  3796
                 sb.append(", ");
 98  3796
                 sb.append(Translator.localize("label.version"));
 99  3796
                 sb.append(" ");
 100  3796
                 sb.append(in);
 101  3796
                 sb.append('\n');
 102  
             }
 103  
         }
 104  6643
         sb.append('\n');
 105  6643
     }
 106  
 
 107  
     /**
 108  
      * @return a String containing the version information
 109  
      */
 110  
     public static String getVersionInfo() {
 111  
         try {
 112  
 
 113  
             // class preloading, so packages are there...
 114  949
             Class cls = org.tigris.gef.base.Editor.class;
 115  949
             cls = org.xml.sax.AttributeList.class;
 116  949
             cls = org.apache.log4j.Logger.class;
 117  
             
 118  
             // TODO: The following is MDR specific.  We need something generic
 119  
             // to all Model subsystems - tfm 20070716
 120  
             try {
 121  949
                 cls = Class.forName("org.netbeans.api.mdr.MDRManager");
 122  0
             } catch (ClassNotFoundException e) {
 123  
                 // ignore
 124  949
             }
 125  
 
 126  949
             StringBuffer sb = new StringBuffer();
 127  
 
 128  949
             String saxFactory =
 129  
                 System.getProperty("javax.xml.parsers.SAXParserFactory");
 130  949
             if (saxFactory != null) {
 131  0
                 Object[] msgArgs = {
 132  
                     saxFactory,
 133  
                 };
 134  0
                 sb.append(Translator.messageFormat("label.sax-factory1",
 135  
                                                    msgArgs));
 136  
             }
 137  
 
 138  949
             Object saxObject = null;
 139  
             try {
 140  949
                 saxObject = SAXParserFactory.newInstance();
 141  949
                 Object[] msgArgs = {
 142  
                     saxObject.getClass().getName(),
 143  
                 };
 144  949
                 sb.append(Translator.messageFormat("label.sax-factory2",
 145  
                                                    msgArgs));
 146  949
                 sb.append("\n");
 147  0
             } catch (Exception ex) {
 148  0
                 sb.append(Translator.localize("label.error-sax-factory"));
 149  949
             }
 150  
 
 151  6643
             for (int i = 0; i < PACKAGELIST.length; i++) {
 152  5694
                 getComponentVersionInfo(sb, PACKAGELIST[i]);
 153  
             }
 154  
 
 155  949
             if (saxObject != null) {
 156  
                 // ...getPackage() can return null's, so we have to
 157  
                 // cater for this:
 158  949
                 Package pckg = saxObject.getClass().getPackage();
 159  949
                 if (pckg != null) {
 160  949
                     getComponentVersionInfo(sb, pckg.getName());
 161  
                 }
 162  
             }
 163  
 
 164  
 
 165  
 
 166  949
             sb.append("\n");
 167  949
             sb.append(Translator.localize("label.os"));
 168  949
             sb.append(System.getProperty("os.name", "unknown"));
 169  949
             sb.append('\n');
 170  949
             sb.append(Translator.localize("label.os-version"));
 171  949
             sb.append(System.getProperty("os.version", "unknown"));
 172  949
             sb.append('\n');
 173  949
             sb.append(Translator.localize("label.language"));
 174  949
             sb.append(Locale.getDefault().getLanguage());
 175  949
             sb.append('\n');
 176  949
             sb.append(Translator.localize("label.country"));
 177  949
             sb.append(Locale.getDefault().getCountry());
 178  949
             sb.append('\n');
 179  949
             sb.append('\n');
 180  
 
 181  
 
 182  949
             return sb.toString();
 183  
 
 184  0
         } catch (Exception e) {
 185  0
             return e.toString();
 186  
         }
 187  
 
 188  
     }
 189  
 
 190  
     /**
 191  
      * Print out some version info for debugging.
 192  
      */
 193  
     public static void logVersionInfo() {
 194  900
         BufferedReader r =
 195  
             new BufferedReader(new StringReader(getVersionInfo()));
 196  
 
 197  
         try {
 198  
             while (true) {
 199  26100
                 String s = r.readLine();
 200  26100
                 if (s == null) {
 201  900
                     break;
 202  
                 }
 203  25200
                 LOG.info(s);
 204  25200
             }
 205  900
         } catch (IOException ioe) { }
 206  900
     }
 207  
 
 208  
     /**
 209  
      * Gets the file extension of a file.
 210  
      *
 211  
      * @param file the File to examine
 212  
      * @return extension including the dot, or null
 213  
      */
 214  
     public static String getFileExtension(File file) {
 215  0
         String ext = null;
 216  0
         String s = file.getName();
 217  0
         int i = s.lastIndexOf('.');
 218  
 
 219  0
         if (i > 0) {
 220  0
             ext = s.substring(i).toLowerCase();
 221  
         }
 222  0
         return ext;
 223  
     }
 224  
 }
 225