All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sun.tools.jxc.api.impl.j2s.JAXBModelImpl Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 1997, 2021 Oracle and/or its affiliates. All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Distribution License v. 1.0, which is available at
 * http://www.eclipse.org/org/documents/edl-v10.php.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

package com.sun.tools.jxc.api.impl.j2s;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import jakarta.xml.bind.SchemaOutputResolver;
import jakarta.xml.bind.annotation.XmlList;
import jakarta.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import javax.xml.namespace.QName;
import javax.xml.transform.Result;

import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.TypeMirror;
import com.sun.tools.xjc.api.ErrorListener;
import com.sun.tools.xjc.api.J2SJAXBModel;
import com.sun.tools.xjc.api.Reference;
import cn.lzgabel.jaxb.core.v2.model.annotation.AnnotationReader;
import cn.lzgabel.jaxb.core.v2.model.core.ArrayInfo;
import cn.lzgabel.jaxb.core.v2.model.core.ClassInfo;
import cn.lzgabel.jaxb.core.v2.model.core.Element;
import cn.lzgabel.jaxb.core.v2.model.core.ElementInfo;
import cn.lzgabel.jaxb.core.v2.model.core.EnumLeafInfo;
import cn.lzgabel.jaxb.core.v2.model.core.NonElement;
import cn.lzgabel.jaxb.core.v2.model.core.Ref;
import cn.lzgabel.jaxb.core.v2.model.core.TypeInfoSet;
import cn.lzgabel.jaxb.core.v2.model.nav.Navigator;
import cn.lzgabel.jaxb.runtime.v2.schemagen.XmlSchemaGenerator;
import com.sun.xml.txw2.output.ResultFactory;

/**
 * @author Kohsuke Kawaguchi ([email protected])
 */
final class JAXBModelImpl implements J2SJAXBModel {

    private final Map additionalElementDecls;

    private final List classList = new ArrayList();

    private final TypeInfoSet types;

    private final AnnotationReader reader;

    /**
     * Lazily created schema generator.
     */
    private XmlSchemaGenerator xsdgen;

    /**
     * Look up table from an externally visible {@link Reference} object
     * to our internal format.
     */
    private final Map> refMap = new HashMap>();

    public JAXBModelImpl(TypeInfoSet types,
                         AnnotationReader reader,
                         Collection rootClasses,
                         Map additionalElementDecls) {
        this.types = types;
        this.reader = reader;
        this.additionalElementDecls = additionalElementDecls;

        Navigator navigator = types.getNavigator();

        for (ClassInfo i : types.beans().values()) {
            classList.add(i.getName());
        }

        for (ArrayInfo a : types.arrays().values()) {
            String javaName = navigator.getTypeName(a.getType());
            classList.add(javaName);
        }

        for (EnumLeafInfo l : types.enums().values()) {
            QName tn = l.getTypeName();
            if(tn!=null) {
                String javaName = navigator.getTypeName(l.getType());
                classList.add(javaName);
            }
        }

        for (Reference ref : rootClasses)
            refMap.put(ref,getXmlType(ref));

        // check for collision between "additional" ones and the ones given to JAXB
        // and eliminate duplication
        Iterator> itr = additionalElementDecls.entrySet().iterator();
        while(itr.hasNext()) {
            Map.Entry entry = itr.next();
            if(entry.getValue()==null)      continue;

            NonElement xt = getXmlType(entry.getValue());

            assert xt!=null;
            refMap.put(entry.getValue(),xt);
            if(xt instanceof ClassInfo) {
                ClassInfo xct = (ClassInfo) xt;
                Element elem = xct.asElement();
                if(elem!=null && elem.getElementName().equals(entry.getKey())) {
                    itr.remove();
                    continue;
                }
            }
            ElementInfo ei = types.getElementInfo(null, entry.getKey());
            if(ei!=null && ei.getContentType()==xt)
                itr.remove();
        }
    }

    @Override
    public List getClassList() {
        return classList;
    }

    @Override
    public QName getXmlTypeName(Reference javaType) {
        NonElement ti = refMap.get(javaType);

        if(ti!=null)
            return ti.getTypeName();

        return null;
    }

    private NonElement getXmlType(Reference r) {
        if(r==null)
            throw new IllegalArgumentException();

        XmlJavaTypeAdapter xjta = r.annotations.getAnnotation(XmlJavaTypeAdapter.class);
        XmlList xl = r.annotations.getAnnotation(XmlList.class);

        Ref ref = new Ref(
            reader,types.getNavigator(),r.type,xjta,xl);

        return types.getTypeInfo(ref);
    }

    @Override
    public void generateSchema(SchemaOutputResolver outputResolver, ErrorListener errorListener) throws IOException {
        getSchemaGenerator().write(outputResolver,errorListener);
    }

    @Override
    public void generateEpisodeFile(Result output) {
        getSchemaGenerator().writeEpisodeFile(ResultFactory.createSerializer(output));
    }

    private synchronized XmlSchemaGenerator getSchemaGenerator() {
        if(xsdgen==null) {
            xsdgen = new XmlSchemaGenerator(types.getNavigator(), types);

            for (Map.Entry e : additionalElementDecls.entrySet()) {
                Reference value = e.getValue();
                if(value!=null) {
                    NonElement typeInfo = refMap.get(value);
                    if(typeInfo==null)
                        throw new IllegalArgumentException(e.getValue()+" was not specified to JavaCompiler.bind");
                    TypeMirror type = value.type;
                    xsdgen.add(e.getKey(), !(type != null && type.getKind().isPrimitive()), typeInfo);
                } else {
                    xsdgen.add(e.getKey(),false,null);
                }
            }
        }
        return xsdgen;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy