* @return object
*/
protected Object createInstance(String cname, UnmarshallingContext ctx) {
// set the class to be used for name converter instance
ValidationContext vctx = (ValidationContext)ctx.getUserContext();
if (cname == null) {
cname = "org.jibx.schema.codegen.extend.DefaultNameConverter";
}
try {
// make sure the class implements the required interface
Class clas = SchemaRootBase.class.getClassLoader().loadClass(cname);
if (NameConverter.class.isAssignableFrom(clas)) {
try {
// check for existing name converter instance to pass to constructor
SchemaRootBase outer = (SchemaRootBase)ctx.getStackTop();
NameConverter prior = outer.getNameConverter();
if (prior != null) {
// try to create instance of new converter passing old converter
try {
Constructor cons = clas.getConstructor(new Class[] { prior.getClass() });
try {
return cons.newInstance(new Object[] { prior });
} catch (IllegalArgumentException e) { /* ignore failure */
} catch (InvocationTargetException e) {
vctx.addWarning("Failed passing existing name converter to constructor for class " +
cname + ": " + e.getMessage(), new ProblemLocation(ctx));
}
} catch (SecurityException e) { /* ignore failure */
} catch (NoSuchMethodException e) { /* ignore failure */ }
}
// just create instance using no-argument constructor
return clas.newInstance();
} catch (InstantiationException e) {
vctx.addError("Error creating instance of class " + cname + ": " + e.getMessage(),
new ProblemLocation(ctx));
} catch (IllegalAccessException e) {
vctx.addError("Unable to access constructor for class " + cname + ": " + e.getMessage(),
new ProblemLocation(ctx));
}
} else {
vctx.addError("Class " + cname + " does not implement the required NameConverter interface",
new ProblemLocation(ctx));
}
} catch (ClassNotFoundException e) {
vctx.addError("Unable to find class " + cname + " in classpath", new ProblemLocation(ctx));
}
return null;
}