KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > examples > ExampleUtils


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.examples;
16
17 import java.net.URL JavaDoc;
18 import java.util.Locale JavaDoc;
19
20 import org.apache.hivemind.ClassResolver;
21 import org.apache.hivemind.Registry;
22 import org.apache.hivemind.Resource;
23 import org.apache.hivemind.impl.DefaultClassResolver;
24 import org.apache.hivemind.impl.RegistryBuilder;
25 import org.apache.hivemind.impl.XmlModuleReader;
26 import org.apache.hivemind.util.URLResource;
27
28 /**
29  * Utilities needed by the examples.
30  *
31  * @author Howard Lewis Ship
32  */

33 public final class ExampleUtils
34 {
35
36     /** Prevent instantiation. */
37     private ExampleUtils()
38     {
39     }
40
41     /**
42      * Convenience method for invoking {@link #buildClasspathRegistry(String[])}
43      * with only a single file.
44      */

45     public static Registry buildClasspathRegistry(String JavaDoc file)
46         throws Exception JavaDoc
47     {
48         return buildClasspathRegistry(new String JavaDoc[] { file });
49     }
50
51     /**
52      * Builds a registry for files in the classpath.
53      */

54     public static Registry buildClasspathRegistry(String JavaDoc[] files)
55         throws Exception JavaDoc
56     {
57         ClassResolver resolver = new DefaultClassResolver();
58         
59         RegistryBuilder builder = new RegistryBuilder();
60         builder.autoDetectModules();
61
62         for (int i = 0; i < files.length; i++)
63         {
64             Resource resource = getResource(files[i]);
65
66             XmlModuleReader reader = new XmlModuleReader(builder.getRegistryDefinition(),
67                     resolver, builder.getErrorHandler());
68             reader.readModule(resource);
69         }
70
71         return builder.constructRegistry(Locale.getDefault());
72     }
73
74
75     /**
76      * Returns the given file as a {@link Resource} from the classpath.
77      * Typically, this is to find files in the same folder as the invoking
78      * class.
79      */

80     protected static Resource getResource(String JavaDoc file)
81     {
82         URL JavaDoc url = ExampleUtils.class.getResource(file);
83
84         if (url == null) throw new NullPointerException JavaDoc("No resource named '" + file + "'.");
85
86         return new URLResource(url);
87     }
88
89 }
90
Popular Tags