Coverage report

| %line | %branch
--- | --- | ---
tsukuba_bunko.peko.resource.LocationConfigurator |
0% |
0% |

1 |  |
     /*
2 |  |
      * "Peko" Visual Novel System
3 |  |
      *
4 |  |
      * All Rights Reserved.
5 |  |
      * Copyright (c) 1999-2003 Tsukuba Bunko.
6 |  |
      *
7 |  |
      * $Id: LocationConfigurator.java,v 1.1 2005/07/11 12:49:18 ppoi Exp $
8 |  |
      */
9 |  |
     package tsukuba_bunko.peko.resource;
10 |  |
11 |  |
     import	java.io.File;
12 |  |
13 |  |
     import	java.net.MalformedURLException;
14 |  |
     import	java.net.URL;
15 |  |
16 |  |
     import	tsukuba_bunko.resource.Resources;
17 |  |
18 |  |
     import	tsukuba_bunko.peko.InitializationError;
19 |  |
     import	tsukuba_bunko.peko.Logger;
20 |  |
21 |  |
     import	tsukuba_bunko.util.ResourceLocator;
22 |  |
23 |  |
24 |  |
     /**
25 |  |
      * ディレクトリロケーション設定を構成する機能を提供します。
26 |  |
      * @author	$Author: ppoi $
27 |  |
      * @version	$Revision: 1.1 $
28 |  |
      */
29 |  |
     public class LocationConfigurator	{
30 |  |
31 |  |
     	/**
32 |  |
     	 * <code>LocationConfigurator</code> のインスタンスを作成します。
33 |  |
     	 */
34 |  |
     	public LocationConfigurator()
35 |  |
     	{
36 | 0 |
     		super();
37 | 0 |
     	}
38 |  |
39 |  |
40 |  |
     	/**
41 |  |
     	 * インストールディレクトリを取得します。
42 |  |
     	 * @return	インストールディレクトリのファイルシステム上の抽象パス
43 |  |
     	 */
44 |  |
     	protected File detectInstallDirectory()
45 |  |
     	{
46 | 0 |
     		File	baseLocation = null;
47 |  |
     		try	{
48 | 0 |
     			ResourceLocator	locator = ResourceLocator.newInstance();
49 | 0 |
     			baseLocation = locator.findLocation( "tsukuba_bunko/peko/PekoSystem.class" );
50 |  |
51 | 0 |
     			if( baseLocation.isFile() )	{
52 | 0 |
     				baseLocation = baseLocation.getParentFile().getParentFile();
53 | 0 |
     			}
54 |  |
     			else	{
55 | 0 |
     				baseLocation = baseLocation.getParentFile();
56 |  |
     			}
57 |  |
     		}
58 | 0 |
     		catch( Exception e )	{
59 | 0 |
     			Logger.fatal( MessageIDs.RES0006F, e );
60 | 0 |
     			throw new InitializationError( "fail to detect install directory.", e );
61 | 0 |
     		}
62 | 0 |
     		Logger.debug( "install dir: " + baseLocation.getAbsolutePath() );
63 |  |
64 | 0 |
     		return baseLocation;
65 |  |
     	}
66 |  |
67 |  |
     	/**
68 |  |
     	 * ディレクトリ URL を構成します。指定されたディレクトリがファイルだった場合、JAR アーカイブとして処理します。
69 |  |
     	 * @param	directory	データ格納ディレクトリ名
70 |  |
     	 * @param	installDir	インストールディレクトリ
71 |  |
     	 * @return	構成された URL
72 |  |
     	 */
73 |  |
     	protected URL configureDirectoryURL( String directory, File installDir )
74 |  |
     	{
75 | 0 |
     		File	dataDirectory = new File( installDir, directory );
76 | 0 |
     		Logger.debug( "dir: " + dataDirectory );
77 | 0 |
     		if( dataDirectory.isDirectory() )	{
78 |  |
     			try	{
79 | 0 |
     				return dataDirectory.toURL();
80 |  |
     			}
81 | 0 |
     			catch( MalformedURLException mue )	{
82 | 0 |
     				InitializationError	ie = new InitializationError( "invalid data directory.", mue );
83 | 0 |
     				Logger.fatal( MessageIDs.RES0008F, new Object[]{directory}, ie );
84 | 0 |
     				throw ie;
85 |  |
     			}
86 |  |
     		}
87 | 0 |
     		else if( dataDirectory.isFile() )	{
88 |  |
     			try	{
89 | 0 |
     				return new URL( "jar:" + dataDirectory.toURL().toString() + "!/" );
90 |  |
     			}
91 | 0 |
     			catch( MalformedURLException mue )	{
92 | 0 |
     				InitializationError	ie = new InitializationError( "invalid data directory.", mue );
93 | 0 |
     				Logger.fatal( MessageIDs.RES0008F, new Object[]{directory}, ie );
94 | 0 |
     				throw ie;
95 |  |
     			}
96 |  |
     		}
97 |  |
     		else	{
98 | 0 |
     			InitializationError	ie = new InitializationError( "invalid data directory." );
99 | 0 |
     			Logger.fatal( MessageIDs.RES0008F, new Object[]{directory}, ie );
100 | 0 |
     			throw ie;
101 |  |
     		}
102 |  |
     	}
103 |  |
104 |  |
     	protected File ensureSaveDirectory( String directory, File installDir )
105 |  |
     	{
106 | 0 |
     		File	saveDir = new File( installDir, directory );
107 | 0 |
     		if( saveDir.exists() )	{
108 | 0 |
     			if( saveDir.isDirectory() )	{
109 | 0 |
     				return saveDir;
110 |  |
     			}
111 |  |
     			else	{
112 | 0 |
     				InitializationError	ie = new InitializationError( "already exist file." );
113 | 0 |
     				Logger.fatal( MessageIDs.RES0012F, ie );
114 | 0 |
     				throw ie;
115 |  |
     			}
116 |  |
     		}
117 |  |
     		else	{
118 | 0 |
     			if( !saveDir.mkdir() )	{
119 | 0 |
     				InitializationError ie = new InitializationError( "fail to create dir." );
120 | 0 |
     				Logger.fatal( MessageIDs.RES0013F, ie );
121 | 0 |
     				throw ie;
122 |  |
     			}
123 | 0 |
     			return saveDir;
124 |  |
     		}
125 |  |
     	}
126 |  |
127 |  |
128 |  |
     	/**
129 |  |
     	 * システム構成リソースの内容から構成された <code>LocationResources</code> を生成します。
130 |  |
     	 * @param	systemResources	システム構成リソース
131 |  |
     	 * @return	構成済みの <code>LocationResources</code> インスタンス
132 |  |
     	 */
133 |  |
     	public LocationResources configure( Resources systemResources )
134 |  |
     	{
135 | 0 |
     		LocationResources	locations = new LocationResources();
136 |  |
137 | 0 |
     		File	installDir = detectInstallDirectory();
138 |  |
139 | 0 |
     		String[]	keys = { "scenes", "images", "sounds", "misc" };
140 | 0 |
     		URL	url = null;
141 | 0 |
     		String	value = null;
142 | 0 |
     		for( int i = 0; i < keys.length; ++i )	{
143 | 0 |
     			value = (String)systemResources.getResource( "peko.system.location." + keys[i] );
144 | 0 |
     			if( value == null )	{
145 | 0 |
     				InitializationError	ie = new InitializationError( "directory is not specified." );
146 | 0 |
     				Logger.fatal( MessageIDs.RES0011F, new Object[]{keys[i]}, ie );
147 | 0 |
     				throw ie;
148 |  |
     			}
149 |  |
     			else	{
150 | 0 |
     				url = configureDirectoryURL( value, installDir );
151 | 0 |
     				Logger.debug( url.toString() );
152 | 0 |
     				locations.setResource( "peko.system.location." + keys[i], url );
153 |  |
     			}
154 |  |
     		}
155 |  |
156 | 0 |
     		String	saveDirectory = (String)systemResources.getResource( "peko.system.location.save" );
157 | 0 |
     		if( saveDirectory == null )	{
158 | 0 |
     			saveDirectory = "save";
159 |  |
     		}
160 | 0 |
     		locations.setResource( LocationResources.DIR_SAVE, ensureSaveDirectory(saveDirectory, installDir) );
161 |  |
162 | 0 |
     		locations.setResource( LocationResources.DIR_INSTALL, installDir );
163 |  |
164 | 0 |
     		return locations;
165 |  |
     	}
166 |  |
     }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.