1 /*
2 * Copyright (C) The Spice Group. All rights reserved.
3 *
4 * This software is published under the terms of the Spice
5 * Software License version 1.1, a copy of which has been included
6 * with this distribution in the LICENSE.txt file.
7 */
8 package org.codehaus.spice.configkit;
9
10 import java.io.InputStream;
11 import java.net.URL;
12 import org.xml.sax.InputSource;
13
14 /***
15 * Utility class to get ConfigValidator objects for components. See {@link
16 * #getComponentConfigValidator} for a detailed explanation about how
17 * ConfigValidator objects are loaded.
18 *
19 * @author Peter Donald
20 * @version $Revision: 1.1 $ $Date: 2003/12/03 03:19:28 $
21 */
22 public class ComponentConfigUtil
23 {
24 /*** Postfix added to classname of component to look for schema. */
25 private static final String DEFAULT_LOCATION_POSTFIX = "-schema.xml";
26
27 /***
28 * Return the ConfigValidator for specified component. The component is
29 * specified by classname and classloader. The ConfigValidator is loaded
30 * from specified location and has specified type. If the type is null then
31 * ConfigKit will attempt to guess the type based on schema location. The
32 * location parameter can be relative to the component (ie FooSchema.xml for
33 * class com.biz.Bar will load resource "/com/biz/FooSchema.xml" from
34 * classloader), absolute resource location in classloader (must start with
35 * "/") or null. If the location is null then it will assume resource name
36 * is the same name as class with the postfix "-schema.xml" added to
37 * classname. If no such resource is located in the ClassLoader then null is
38 * returned.
39 *
40 * @param classname the classname of component
41 * @param classLoader the classloader component loaded from
42 * @param location the location of schema
43 * @param type the type of schema
44 * @return the ConfigValidator
45 * @throws java.lang.Exception if error creating validator
46 */
47 public static ConfigValidator getComponentConfigValidator(
48 final String classname,
49 final ClassLoader classLoader,
50 final String location,
51 final String type )
52 throws Exception
53 {
54 if( null == classname )
55 {
56 throw new NullPointerException( "classname" );
57 }
58 if( null == classLoader )
59 {
60 throw new NullPointerException( "classLoader" );
61 }
62 final String actualLocation = calculateLocation( classname, location );
63 final InputSource inputSource =
64 getSchemaInputSource( actualLocation.substring( 1 ), classLoader );
65 if( null != inputSource )
66 {
67 return ConfigValidatorFactory.create( type, inputSource );
68 }
69 else
70 {
71 return null;
72 }
73 }
74
75 /***
76 * Determine the location of configuration schema for class. If the
77 * specified location is not null then that will be returned otherwise the
78 * location is the resource name of the with {@link #DEFAULT_LOCATION_POSTFIX}
79 * appended rather than ".class". ie If the classname was "com.biz.Foo" then
80 * the schema location would be at "/com/biz/Foo-schema.xml".
81 *
82 * @param classname the name of the class
83 * @param location the specified location of schema
84 * @return the actual location of schema
85 */
86 static String calculateLocation( final String classname,
87 final String location )
88 {
89 if( null == location )
90 {
91 return "/" +
92 classname.replace( '.', '/' ) +
93 DEFAULT_LOCATION_POSTFIX;
94 }
95 else if( location.startsWith( "/" ) )
96 {
97 return location;
98 }
99 else
100 {
101 final int index = classname.lastIndexOf( '.' );
102 String packageName;
103 if( -1 != index )
104 {
105 packageName = classname.substring( 0, index + 1 );
106 }
107 else
108 {
109 packageName = "";
110 }
111 return "/" + packageName.replace( '.', '/' ) + location;
112 }
113 }
114
115 /***
116 * Get the input source for schema specified for component.
117 *
118 * @param resource the resource location of schema
119 * @param classLoader the ClassLoader to load schema from
120 * @return the InputSource for schema
121 */
122 static InputSource getSchemaInputSource( final String resource,
123 final ClassLoader classLoader )
124 {
125 final InputStream inputStream = classLoader.getResourceAsStream(
126 resource );
127 if( null == inputStream )
128 {
129 return null;
130 }
131
132 final InputSource inputSource = new InputSource( inputStream );
133 final URL url = classLoader.getResource( resource );
134 if( null != url )
135 {
136 inputSource.setSystemId( url.toExternalForm() );
137 }
138 else
139 {
140 inputSource.setSystemId( resource );
141 }
142 return inputSource;
143 }
144 }
This page was automatically generated by Maven