View Javadoc

1   /*
2    * All Rights Reserved.
3    * Copyright (C) 1999-2005 Tsukuba Bunko.
4    *
5    * Licensed under the BSD License ("the License"); you may not use
6    * this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    *       http://www.tsukuba-bunko.org/licenses/LICENSE.txt
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   *
17   * $Id: Resources.java,v 1.2 2005/07/24 20:55:57 ppoi Exp $
18   */
19  package tsukuba_bunko.resource;
20  
21  import	java.io.IOException;
22  
23  import	java.net.URL;
24  
25  import	java.util.Map;
26  
27  import	javax.xml.parsers.ParserConfigurationException;
28  
29  import	org.xml.sax.SAXException;
30  
31  
32  /***
33   * リソースとリソースIDの関連を管理します。
34   * @author	$Author: ppoi $
35   * @version	$Revision: 1.2 $
36   */
37  public class Resources	{
38  
39  	/***
40  	 * リソース
41  	 */
42  	private Map	_resources = null;
43  
44  
45  	/***
46  	 * <code>Resources</code> のインスタンスを作成します。
47  	 */
48  	public Resources()
49  	{
50  		_resources = new java.util.HashMap( 89 );
51  	}
52  
53  
54  	/***
55  	 * リソースを取得します。
56  	 * @param	resourceName	リソース名
57  	 * @return	リソース
58  	 */
59  	public Object getResource( String resourceName )
60  	{
61  		return _resources.get( resourceName );
62  	}
63  
64  	/***
65  	 * リソースを設定します。
66  	 * @param	resourceName	リソース名
67  	 * @param	resource	リソース
68  	 */
69  	public final void setResource( String resourceName, Object resource )
70  	{
71  		_resources.put( resourceName, resource );
72  	}
73  
74  	/***
75  	 * この <code>Resources</code> が管理する全リソースIDを取得します。
76  	 * @return	リソースIDの配列
77  	 */
78  	public String[] getResourceIDs()
79  	{
80  		return (String[])_resources.keySet().toArray( new String[_resources.size()] );
81  	}
82  
83  	/***
84  	 * リソースファイルからリソースをロードし、<code>Resources</code> のインスタンスを作成します.
85  	 * @param	resourceURL	リソースファイルの URL
86  	 * @return	生成された <code>Resources</code>
87  	 * @throws	IllegalResourceException	リソースファイルが不正な場合
88  	 * @throws	ParserConfigurationException	JAXP が正常に構成されていない場合
89  	 * @throws	SAXException	JAXP が正常に構成されていない場合
90  	 * @throws	IOException	I/O エラーが発生した場合
91  	 */
92  	public static Resources newInstance( URL resourceURL )
93  		throws IllegalResourceException, ParserConfigurationException, SAXException, IOException
94  	{
95  		Resources	resources = new Resources();
96  		ResourceLoader	loader = new ResourceLoader();
97  		loader.loadResource( resourceURL, resources );
98  		return resources;
99  	}
100 }