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