Search in sources :

Example 1 with Model

use of com.sun.tools.xjc.model.Model in project midpoint by Evolveum.

the class StepSchemaConstants method run.

@Override
public boolean run(Outline outline, Options opt, ErrorHandler errorHandler) throws Exception {
    Model model = outline.getModel();
    JDefinedClass schemaConstants = model.codeModel._class(CLASS_NAME);
    //creating namespaces
    List<FieldBox<String>> namespaces = new ArrayList<FieldBox<String>>();
    for (PrefixMapper prefix : PrefixMapper.values()) {
        namespaces.add(new FieldBox("NS_" + prefix.getNamespaceName(), prefix.getNamespace()));
    }
    Collections.sort(namespaces);
    for (FieldBox<String> field : namespaces) {
        JFieldVar var = createNSFieldDefinition(outline, schemaConstants, field.getFieldName(), field.getValue());
        getNamespaceFields().put(field.getValue(), var);
    }
    //creating qnames
    List<FieldBox<QName>> fields = new ArrayList<FieldBox<QName>>();
    Map<QName, CElementInfo> map = model.getElementMappings(null);
    Set<Map.Entry<QName, CElementInfo>> set = map.entrySet();
    for (Map.Entry<QName, CElementInfo> entry : set) {
        QName qname = entry.getKey();
        CElementInfo info = entry.getValue();
        String fieldName = ProcessorUtils.fieldPrefixedUnderscoredUpperCase(info.getSqueezedName(), qname);
        fields.add(new FieldBox(fieldName, qname));
    }
    //sort field by name and create qname definitions in class
    Collections.sort(fields);
    for (FieldBox<QName> field : fields) {
        JFieldVar var = namespaceFields.get(field.getValue().getNamespaceURI());
        if (var != null) {
            createQNameDefinition(outline, schemaConstants, field.getFieldName(), var, field.getValue());
        } else {
            ProcessorUtils.createPSFField(outline, schemaConstants, field.getFieldName(), field.getValue());
        }
    }
    return true;
}
Also used : PrefixMapper(com.evolveum.midpoint.schema.xjc.PrefixMapper) QName(javax.xml.namespace.QName) CElementInfo(com.sun.tools.xjc.model.CElementInfo) Model(com.sun.tools.xjc.model.Model)

Example 2 with Model

use of com.sun.tools.xjc.model.Model in project drools by kiegroup.

the class DroolsJaxbHelperProviderImpl method addXsdModel.

public static String[] addXsdModel(Resource resource, KnowledgeBuilderImpl kBuilder, Options xjcOpts, String systemId) throws IOException {
    InputSource source = new InputSource(new CachingRewindableReader(resource.getReader()));
    source.setSystemId(systemId.trim().startsWith(".") ? systemId : "." + systemId);
    xjcOpts.addGrammar(source);
    try {
        xjcOpts.parseArguments(new String[] { "-npa" });
    } catch (BadCommandLineException e) {
        throw new IllegalArgumentException("Unable to parse arguments", e);
    }
    ErrorReceiver errorReceiver = new JaxbErrorReceiver4Drools();
    Model model = ModelLoader.load(xjcOpts, new JCodeModel(), errorReceiver);
    model.generateCode(xjcOpts, errorReceiver);
    MapVfsCodeWriter codeWriter = new MapVfsCodeWriter();
    model.codeModel.build(xjcOpts.createCodeWriter(codeWriter));
    MemoryResourceReader src = new MemoryResourceReader();
    boolean useProjectClassLoader = kBuilder.getRootClassLoader() instanceof ProjectClassLoader;
    List<String> classNames = new ArrayList<String>();
    List<String> srcNames = new ArrayList<String>();
    for (Entry<String, byte[]> entry : codeWriter.getMap().entrySet()) {
        String name = entry.getKey();
        int dotPos = name.lastIndexOf('.');
        String pkgName = name.substring(0, dotPos);
        if (!name.endsWith("package-info.java")) {
            classNames.add(pkgName);
        }
        dotPos = pkgName.lastIndexOf('.');
        if (dotPos != -1) {
            pkgName = pkgName.substring(0, dotPos);
        }
        PackageRegistry pkgReg = kBuilder.getPackageRegistry(pkgName);
        if (pkgReg == null) {
            kBuilder.addPackage(new PackageDescr(pkgName));
            pkgReg = kBuilder.getPackageRegistry(pkgName);
        }
        if (useProjectClassLoader) {
            String srcName = convertToResource(entry.getKey());
            src.add(srcName, entry.getValue());
            srcNames.add(srcName);
        } else {
            JavaDialect dialect = (JavaDialect) pkgReg.getDialectCompiletimeRegistry().getDialect("java");
            dialect.addSrc(convertToResource(entry.getKey()), entry.getValue());
        }
    }
    if (useProjectClassLoader) {
        ProjectJavaCompiler compiler = new ProjectJavaCompiler(kBuilder.getBuilderConfiguration());
        List<KnowledgeBuilderResult> results = compiler.compileAll((ProjectClassLoader) kBuilder.getRootClassLoader(), srcNames, src);
        for (String className : classNames) {
            Class<?> clazz = null;
            try {
                clazz = Class.forName(className, true, kBuilder.getRootClassLoader());
            } catch (ClassNotFoundException e) {
                continue;
            }
            String pkgName = className.substring(0, className.lastIndexOf('.'));
            PackageRegistry pkgReg = kBuilder.getPackageRegistry(pkgName);
            pkgReg.getPackage().addTypeDeclaration(TypeDeclaration.createTypeDeclarationForBean(clazz));
        }
        kBuilder.updateResults(results);
    } else {
        kBuilder.compileAll();
        kBuilder.updateResults();
    }
    return classNames.toArray(new String[classNames.size()]);
}
Also used : InputSource(org.xml.sax.InputSource) ArrayList(java.util.ArrayList) ErrorReceiver(com.sun.tools.xjc.ErrorReceiver) MemoryResourceReader(org.drools.compiler.commons.jci.readers.MemoryResourceReader) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) ProjectClassLoader(org.drools.core.common.ProjectClassLoader) BadCommandLineException(com.sun.tools.xjc.BadCommandLineException) ProjectJavaCompiler(org.drools.compiler.compiler.ProjectJavaCompiler) JCodeModel(com.sun.codemodel.JCodeModel) JavaDialect(org.drools.compiler.rule.builder.dialect.java.JavaDialect) PackageRegistry(org.drools.compiler.compiler.PackageRegistry) Model(com.sun.tools.xjc.model.Model) JCodeModel(com.sun.codemodel.JCodeModel) KnowledgeBuilderResult(org.kie.internal.builder.KnowledgeBuilderResult)

Example 3 with Model

use of com.sun.tools.xjc.model.Model 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

Model (com.sun.tools.xjc.model.Model)3 JCodeModel (com.sun.codemodel.JCodeModel)2 ErrorReceiver (com.sun.tools.xjc.ErrorReceiver)2 ArrayList (java.util.ArrayList)2 PrefixMapper (com.evolveum.midpoint.schema.xjc.PrefixMapper)1 JDefinedClass (com.sun.codemodel.JDefinedClass)1 BadCommandLineException (com.sun.tools.xjc.BadCommandLineException)1 Options (com.sun.tools.xjc.Options)1 CElementInfo (com.sun.tools.xjc.model.CElementInfo)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 QName (javax.xml.namespace.QName)1 MemoryResourceReader (org.drools.compiler.commons.jci.readers.MemoryResourceReader)1 PackageRegistry (org.drools.compiler.compiler.PackageRegistry)1 ProjectJavaCompiler (org.drools.compiler.compiler.ProjectJavaCompiler)1 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)1 JavaDialect (org.drools.compiler.rule.builder.dialect.java.JavaDialect)1 ProjectClassLoader (org.drools.core.common.ProjectClassLoader)1