Search in sources :

Example 1 with ErrorReceiverFilter

use of com.sun.tools.xjc.util.ErrorReceiverFilter in project raml-for-jax-rs by mulesoft-labs.

the class JAXBHelper method generateClassesFromXmlSchemas.

private static List<JDefinedClass> generateClassesFromXmlSchemas(String pack, JCodeModel codeModel, File schemaFile) throws GenerationException {
    try {
        ArrayList<JDefinedClass> classList = new ArrayList<JDefinedClass>();
        ArrayList<String> argList = new ArrayList<>();
        argList.add("-mark-generated");
        argList.add("-p");
        argList.add(pack);
        argList.add(schemaFile.getAbsolutePath());
        String[] args = argList.toArray(new String[argList.size()]);
        final Options opt = new Options();
        opt.setSchemaLanguage(Language.XMLSCHEMA);
        opt.parseArguments(args);
        ErrorReceiver receiver = new ErrorReceiverFilter() {

            @Override
            public void info(SAXParseException exception) {
                if (opt.verbose)
                    super.info(exception);
            }

            @Override
            public void warning(SAXParseException exception) {
                if (!opt.quiet)
                    super.warning(exception);
            }
        };
        Model model = ModelLoader.load(opt, codeModel, receiver);
        Outline outline = model.generateCode(opt, receiver);
        for (ClassOutline co : outline.getClasses()) {
            JDefinedClass cl = co.implClass;
            if (cl.outer() == null) {
                classList.add(cl);
            }
        }
        return classList;
    } catch (Exception e) {
        throw new GenerationException(e);
    }
}
Also used : Options(com.sun.tools.xjc.Options) JDefinedClass(com.sun.codemodel.JDefinedClass) ArrayList(java.util.ArrayList) ClassOutline(com.sun.tools.xjc.outline.ClassOutline) Outline(com.sun.tools.xjc.outline.Outline) ErrorReceiver(com.sun.tools.xjc.ErrorReceiver) IOException(java.io.IOException) GenerationException(org.raml.jaxrs.generator.GenerationException) SAXParseException(org.xml.sax.SAXParseException) ClassOutline(com.sun.tools.xjc.outline.ClassOutline) ErrorReceiverFilter(com.sun.tools.xjc.util.ErrorReceiverFilter) SAXParseException(org.xml.sax.SAXParseException) Model(com.sun.tools.xjc.model.Model) JCodeModel(com.sun.codemodel.JCodeModel) GenerationException(org.raml.jaxrs.generator.GenerationException)

Aggregations

JCodeModel (com.sun.codemodel.JCodeModel)1 JDefinedClass (com.sun.codemodel.JDefinedClass)1 ErrorReceiver (com.sun.tools.xjc.ErrorReceiver)1 Options (com.sun.tools.xjc.Options)1 Model (com.sun.tools.xjc.model.Model)1 ClassOutline (com.sun.tools.xjc.outline.ClassOutline)1 Outline (com.sun.tools.xjc.outline.Outline)1 ErrorReceiverFilter (com.sun.tools.xjc.util.ErrorReceiverFilter)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 GenerationException (org.raml.jaxrs.generator.GenerationException)1 SAXParseException (org.xml.sax.SAXParseException)1