From: Dennis S. <dso...@us...> - 2010-07-07 02:33:30
|
Update of /cvsroot/jibx/core/build/src/org/jibx/schema/codegen/custom In directory sfp-cvsdas-3.v30.ch3.sourceforge.com:/tmp/cvs-serv3984/build/src/org/jibx/schema/codegen/custom Modified Files: SchemasetCustom.java Log Message: Move loadCustomizations() code here from command line handling, for easier use by other tools. Index: SchemasetCustom.java =================================================================== RCS file: /cvsroot/jibx/core/build/src/org/jibx/schema/codegen/custom/SchemasetCustom.java,v retrieving revision 1.11 retrieving revision 1.12 diff -C2 -d -r1.11 -r1.12 *** SchemasetCustom.java 22 Sep 2009 11:32:22 -0000 1.11 --- SchemasetCustom.java 7 Jul 2010 02:33:19 -0000 1.12 *************** *** 22,31 **** --- 22,38 ---- package org.jibx.schema.codegen.custom; + import java.io.FileInputStream; + import java.io.IOException; + import java.util.ArrayList; import java.util.HashMap; import java.util.Map; import org.apache.log4j.Logger; + import org.jibx.runtime.BindingDirectory; + import org.jibx.runtime.IBindingFactory; import org.jibx.runtime.IUnmarshallingContext; + import org.jibx.runtime.JiBXException; import org.jibx.schema.elements.SchemaElement; + import org.jibx.schema.validation.ProblemHandler; import org.jibx.schema.validation.ValidationContext; import org.jibx.schema.validation.ValidationProblem; *************** *** 255,257 **** --- 262,306 ---- return valid; } + + /** + * Load a customizations file. The specified file must exist and have as root element either a <schema-set> or + * <schema> element. + * + * @param path customization file path (<code>null</code> if none) + * @param handler validation problem handler + * @return unmarshalled customizations, or <code>null</code> if errors + * @throws JiBXException + * @throws IOException + */ + public static SchemasetCustom loadCustomizations(String path, ProblemHandler handler) + throws JiBXException, IOException { + + // load customizations and check for errors + SchemasetCustom custom = null; + ValidationContext vctx = new ValidationContext(); + if (path == null) { + custom = new SchemasetCustom((SchemasetCustom)null); + } else { + + // unmarshal either a <schema-set> or <schema> element + IBindingFactory fact = BindingDirectory.getFactory(SchemasetCustom.class); + IUnmarshallingContext ictx = fact.createUnmarshallingContext(); + FileInputStream is = new FileInputStream(path); + ictx.setDocument(is, null); + ictx.setUserContext(vctx); + Object obj = ictx.unmarshalElement(); + if (obj instanceof SchemasetCustom) { + custom = (SchemasetCustom)obj; + } else if (obj instanceof SchemaCustom) { + custom = new SchemasetCustom((SchemaCustom)obj); + } else { + throw new IOException("Customization document root element must be 'schema-set' or 'schema'"); + } + + } + if (vctx.reportProblems(handler)) { + return null; + } + return custom; + } } \ No newline at end of file |