EMMA Coverage Report (generated Wed Dec 16 17:19:59 CET 2009)
[all classes][org.sourceforge.xradar.resolver]

COVERAGE SUMMARY FOR SOURCE FILE [XRadarResourceLoader.java]

nameclass, %method, %block, %line, %
XRadarResourceLoader.java100% (1/1)57%  (4/7)39%  (96/244)40%  (17/43)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class XRadarResourceLoader100% (1/1)57%  (4/7)39%  (96/244)40%  (17/43)
connect (): void 0%   (0/1)0%   (0/1)0%   (0/1)
getInputStreamFromFileSystem (String): InputStream 0%   (0/1)0%   (0/70)0%   (0/12)
windowsHack (String): String 0%   (0/1)0%   (0/55)0%   (0/9)
getInputStream (): InputStream 100% (1/1)69%  (38/55)75%  (6/8)
XRadarResourceLoader (URL): void 100% (1/1)70%  (7/10)75%  (3/4)
isRunningOnWindowsOS (): boolean 100% (1/1)92%  (23/25)86%  (6/7)
<static initializer> 100% (1/1)100% (28/28)100% (2/2)

1/**
2 * BSD-style license; for more info see http://xradar.sourceforge.net/license.html
3 */
4package org.sourceforge.xradar.resolver;
5 
6import java.io.File;
7import java.io.FileInputStream;
8import java.io.IOException;
9import java.io.InputStream;
10import java.net.URL;
11import java.net.URLConnection;
12import java.util.logging.Level;
13import java.util.logging.Logger;
14import java.util.regex.Pattern;
15 
16/**
17 * XRadarResourceLoader handles the so called 'xradar' protocol
18 * Basicly, it look for the file in the classpath.
19 *
20 * @author Romain PELISSE <belaran@gmail.com>
21 *
22 */
23public class XRadarResourceLoader extends URLConnection
24{
25        private static final String[] windows = { "Windows 98", "Windows ME", "Windows 2000", "Windows XP", "Vista, Windows 2003" };
26        private static final Logger logger = Logger.getLogger(XRadarResourceLoader.class.getSimpleName());
27        private boolean isRunningOnWindows;
28 
29        protected XRadarResourceLoader(URL url)
30        {
31                super(url);
32                //FIXME: not really sure this OS checking code is still usefull
33                if ( isRunningOnWindowsOS() )
34                {
35                        isRunningOnWindows = true; 
36                }
37        }
38 
39        private boolean isRunningOnWindowsOS()
40        {
41                boolean status = false;
42                int idWindows;
43                String os = System.getProperty("os.name");
44                if (os != null )
45                        for ( idWindows = 0; idWindows < windows.length; idWindows++ )
46                                if ( os.startsWith(windows[idWindows]) )
47                                        status = true;
48                return status;
49        }
50 
51        @Override
52        public void connect() throws IOException
53        {
54                // Not implemented as xradar:// is a "deconnected protocol"
55        }
56 
57        /**
58         * Returns input stream
59         *
60         * @return An inputstream to file or null if the file is not found
61         */
62        @Override
63        public InputStream getInputStream() throws IOException
64        {
65                logger.log(Level.FINEST,"Resolving URL:" + this.getURL());
66                String file = this.getURL().getFile();
67                logger.log(Level.FINEST, "Loading file " + file + " from classpath.");
68                InputStream stream = this.getClass().getResourceAsStream(file);
69                if ( stream == null )
70                {
71                        logger.log(Level.FINEST, "File " + file + " not loaded. Trying file system...");
72                        // If this failed, we look on the file system...
73                        stream = this.getInputStreamFromFileSystem(file);
74                }
75                return stream;
76        }
77 
78        /**
79         * If we can't file the file from the classpath, we use this method to retrieve it
80         * from the filesystem.
81         *
82         * @param file
83         * @return the inputstream or null.
84         * @throws IOException
85         */
86        protected InputStream getInputStreamFromFileSystem(String file) throws IOException
87        {
88                InputStream stream = null;
89                // Hack for windows: testing if canonicalPath return a properly "rooted" path
90                if ( isRunningOnWindows )
91                        file = windowsHack(file);
92                File fileDescriptor = new File(file);
93                if ( ! fileDescriptor.exists() )
94                        logger.log(Level.FINER, "File " + file + " does not exist.");
95                else
96                {
97                        if ( ! fileDescriptor.canRead() )
98                                logger.log(Level.FINEST, "File " + file + "can't be read.");
99                        else
100                                stream = new FileInputStream(fileDescriptor);
101                }
102                if ( stream != null )
103                        logger.log(Level.FINEST,"File " + file + "was found on file system.");
104                return stream;
105        }
106 
107        public String windowsHack(String file) throws IOException
108        {
109                logger.finest("windows adaptation need for filepath:" + file);
110                file = new File(file).getCanonicalPath();
111                if ( file.contains("\\") && file.contains("/") )
112                {
113                        // We replace only the '\\' by '/'
114                        file = file.replace('\\', '/');
115                        // If this is an absolute path, we remove the drive
116                        Pattern pattern = Pattern.compile("^[A-Za-z]:.*$");
117                        if ( pattern.matcher(file).matches() )
118                        {
119                                file = file.substring(2,file.length());
120                        }
121                }
122                logger.finest("Filepath modified to" + file);
123                return file;
124        }
125}

[all classes][org.sourceforge.xradar.resolver]
EMMA 2.0.5312 (C) Vladimir Roubtsov