Search in sources :

Example 6 with Model

use of com.predic8.membrane.annot.model.Model in project service-proxy by membrane.

the class JsonSchemaGenerator method collectDefinitions.

private void collectDefinitions(Model m, MainInfo main, ElementInfo i, Schema schema) {
    Map<String, ElementInfo> all = new HashMap<>();
    Stack<ElementInfo> stack = new Stack<>();
    stack.push(i);
    while (!stack.isEmpty()) {
        ElementInfo current = stack.pop();
        current.getCeis().stream().flatMap(cei -> main.getChildElementDeclarations().get(cei.getTypeDeclaration()).getElementInfo().stream()).filter(ei -> !all.containsKey(ei.getXSDTypeName(m))).forEach(ei -> {
            all.put(ei.getXSDTypeName(m), ei);
            stack.push(ei);
        });
    }
    if (all.isEmpty())
        return;
    for (Map.Entry<String, ElementInfo> entry : all.entrySet()) {
        SchemaObject so = new SchemaObject(entry.getKey());
        so.addAttribute("type", "object");
        so.addAttribute("additionalProperties", entry.getValue().getOai() != null);
        collectAttributes(entry.getValue(), so);
        collectTextContent(entry.getValue(), so);
        collectChildElements(m, main, entry.getValue(), so);
        schema.addDefinition(so);
    }
}
Also used : FileObject(javax.tools.FileObject) SchemaObject(com.predic8.membrane.annot.generator.kubernetes.model.SchemaObject) java.util(java.util) BufferedWriter(java.io.BufferedWriter) ProcessingException(com.predic8.membrane.annot.ProcessingException) com.predic8.membrane.annot.model(com.predic8.membrane.annot.model) ProcessingEnvironment(javax.annotation.processing.ProcessingEnvironment) Writer(java.io.Writer) IOException(java.io.IOException) ISchema(com.predic8.membrane.annot.generator.kubernetes.model.ISchema) Schema(com.predic8.membrane.annot.generator.kubernetes.model.Schema) SchemaObject(com.predic8.membrane.annot.generator.kubernetes.model.SchemaObject)

Example 7 with Model

use of com.predic8.membrane.annot.model.Model in project service-proxy by membrane.

the class LoginDialog2 method showPage.

private void showPage(Exchange exc, int page, Object... params) throws Exception {
    String target = StringUtils.defaultString(URLParamUtil.getParams(uriFactory, exc).get("target"));
    exc.getDestinations().set(0, "/index.html");
    wsi.handleRequest(exc);
    Engine engine = new Engine();
    engine.setErrorHandler(new ErrorHandler() {

        @Override
        public void error(String arg0, Token arg1, Map<String, Object> arg2) throws ParseException {
            log.error(arg0);
        }

        @Override
        public void error(String arg0, Token arg1) throws ParseException {
            log.error(arg0);
        }
    });
    Map pages = ImmutableMap.builder().put(0, "login").put(1, "token").put(2, "consent").build();
    Map<String, Object> model = new HashMap<String, Object>();
    model.put("action", StringEscapeUtils.escapeXml(path) + pages.get(page));
    model.put("target", StringEscapeUtils.escapeXml(target));
    model.put(pages.get(page).toString(), true);
    for (int i = 0; i < params.length; i += 2) model.put((String) params[i], params[i + 1]);
    exc.getResponse().setBodyContent(engine.transform(exc.getResponse().getBodyAsStringDecoded(), model).getBytes(Constants.UTF_8_CHARSET));
}
Also used : ErrorHandler(com.floreysoft.jmte.ErrorHandler) Token(com.floreysoft.jmte.token.Token) ParseException(com.floreysoft.jmte.message.ParseException) ImmutableMap(com.google.common.collect.ImmutableMap) ResolverMap(com.predic8.membrane.core.resolver.ResolverMap) Engine(com.floreysoft.jmte.Engine)

Example 8 with Model

use of com.predic8.membrane.annot.model.Model in project service-proxy by membrane.

the class SpringConfigurationXSDGeneratingAnnotationProcessor method process.

public void process(Model m) throws IOException {
    new Schemas(processingEnv).writeXSD(m);
    new KubernetesBootstrapper(processingEnv).boot(m);
    new Parsers(processingEnv).writeParsers(m);
    new Parsers(processingEnv).writeParserDefinitior(m);
    new HelpReference(processingEnv).writeHelp(m);
    new NamespaceInfo(processingEnv).writeInfo(m);
    if (processingEnv.getElementUtils().getTypeElement("org.apache.aries.blueprint.ParserContext") != null) {
        new BlueprintParsers(processingEnv).writeParserDefinitior(m);
        new BlueprintParsers(processingEnv).writeParsers(m);
    }
}
Also used : KubernetesBootstrapper(com.predic8.membrane.annot.generator.kubernetes.KubernetesBootstrapper)

Example 9 with Model

use of com.predic8.membrane.annot.model.Model in project service-proxy by membrane.

the class SpringConfigurationXSDGeneratingAnnotationProcessor method process.

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    // * in the last round, "roundEnv.processingOver()" is true
    try {
        String status = "process() a=" + annotations.size() + " r=" + roundEnv.getRootElements().size() + " h=" + hashCode() + (roundEnv.processingOver() ? " processing-over" : " ");
        log(status);
        read();
        if (roundEnv.processingOver())
            write();
        if (annotations.size() > 0) {
            // a class with one of our annotation needs to be compiled
            status = "working with " + getCachedElementsAnnotatedWith(roundEnv, MCMain.class).size() + " and " + getCachedElementsAnnotatedWith(roundEnv, MCElement.class).size();
            log(status);
            Model m = new Model();
            Set<? extends Element> mcmains = getCachedElementsAnnotatedWith(roundEnv, MCMain.class);
            if (mcmains.isEmpty()) {
                processingEnv.getMessager().printMessage(Kind.WARNING, "@MCMain was nowhere found.");
                return true;
            }
            for (Element element : mcmains) {
                MainInfo main = new MainInfo();
                main.setElement((TypeElement) element);
                main.setAnnotation(element.getAnnotation(MCMain.class));
                m.getMains().add(main);
            }
            for (Element e : getCachedElementsAnnotatedWith(roundEnv, MCElement.class)) {
                ElementInfo ii = new ElementInfo();
                ii.setElement((TypeElement) e);
                ii.setAnnotation(e.getAnnotation(MCElement.class));
                MainInfo main = ii.getMain(m);
                main.getIis().add(ii);
                main.getElements().put(ii.getElement(), ii);
                if (main.getGlobals().containsKey(ii.getAnnotation().name()))
                    throw new ProcessingException("Duplicate global @MCElement name.", main.getGlobals().get(ii.getAnnotation().name()).getElement(), ii.getElement());
                if (main.getIds().containsKey(ii.getId()))
                    throw new ProcessingException("Duplicate element id \"" + ii.getId() + "\". Please assign one using @MCElement(id=\"...\").", e, main.getIds().get(ii.getId()).getElement());
                main.getIds().put(ii.getId(), ii);
                scan(m, main, ii);
                if (ii.getTci() != null && !ii.getAnnotation().mixed())
                    throw new ProcessingException("@MCTextContent requires @MCElement(..., mixed=true) on the class.", ii.getElement());
                if (ii.getTci() == null && ii.getAnnotation().mixed())
                    throw new ProcessingException("@MCElement(..., mixed=true) requires @MCTextContent on a property.", ii.getElement());
            }
            for (MainInfo main : m.getMains()) {
                for (Map.Entry<TypeElement, ChildElementDeclarationInfo> f : main.getChildElementDeclarations().entrySet()) {
                    ChildElementDeclarationInfo cedi = f.getValue();
                    ElementInfo ei = main.getElements().get(f.getKey());
                    if (ei != null)
                        cedi.getElementInfo().add(ei);
                    else {
                        for (Map.Entry<TypeElement, ElementInfo> e : main.getElements().entrySet()) if (processingEnv.getTypeUtils().isAssignable(e.getKey().asType(), f.getKey().asType()))
                            cedi.getElementInfo().add(e.getValue());
                    }
                    for (ElementInfo ei2 : cedi.getElementInfo()) ei2.addUsedBy(f.getValue());
                    if (cedi.getElementInfo().isEmpty() && cedi.isRaiseErrorWhenNoSpecimen()) {
                        processingEnv.getMessager().printMessage(Kind.ERROR, "@MCChildElement references " + f.getKey().getQualifiedName() + ", but there is no @MCElement among it and its subclasses.", f.getKey());
                        return true;
                    }
                }
            }
            if (mcmains.isEmpty()) {
                processingEnv.getMessager().printMessage(Kind.ERROR, "@MCMain but no @MCElement found.", mcmains.iterator().next());
                return true;
            }
            process(m);
        }
        return true;
    } catch (IOException e) {
        throw new RuntimeException(e);
    } catch (ProcessingException e1) {
        for (int i = 0; i < e1.getElements().length; i++) processingEnv.getMessager().printMessage(Kind.ERROR, i == 0 ? e1.getMessage() : "also here", e1.getElements()[i]);
        return true;
    }
}
Also used : ElementInfo(com.predic8.membrane.annot.model.ElementInfo) ChildElementInfo(com.predic8.membrane.annot.model.ChildElementInfo) IOException(java.io.IOException) MainInfo(com.predic8.membrane.annot.model.MainInfo) Model(com.predic8.membrane.annot.model.Model) ChildElementDeclarationInfo(com.predic8.membrane.annot.model.ChildElementDeclarationInfo)

Example 10 with Model

use of com.predic8.membrane.annot.model.Model in project service-proxy by membrane.

the class SpringConfigurationXSDGeneratingAnnotationProcessor method scan.

private void scan(Model m, MainInfo main, ElementInfo ii, TypeElement te) {
    TypeMirror superclass = te.getSuperclass();
    if (superclass instanceof DeclaredType)
        scan(m, main, ii, (TypeElement) ((DeclaredType) superclass).asElement());
    for (Element e2 : te.getEnclosedElements()) {
        MCAttribute a = e2.getAnnotation(MCAttribute.class);
        if (a != null) {
            AttributeInfo ai = new AttributeInfo();
            ai.setAnnotation(a);
            ai.setE((ExecutableElement) e2);
            ai.setRequired(isRequired(e2));
            ii.getAis().add(ai);
            ii.setHasIdField(ii.isHasIdField() || ai.getXMLName().equals("id"));
        }
        MCOtherAttributes d = e2.getAnnotation(MCOtherAttributes.class);
        if (d != null) {
            OtherAttributesInfo oai = new OtherAttributesInfo();
            oai.setOtherAttributesSetter((ExecutableElement) e2);
            ii.setOai(oai);
        }
        MCChildElement b = e2.getAnnotation(MCChildElement.class);
        if (b != null) {
            ChildElementInfo cei = new ChildElementInfo();
            cei.setEi(ii);
            cei.setAnnotation(b);
            cei.setE((ExecutableElement) e2);
            List<? extends VariableElement> parameters = cei.getE().getParameters();
            if (parameters.size() == 0)
                throw new ProcessingException("Setter must have exactly one parameter.", e2);
            TypeMirror setterArgType = parameters.get(0).asType();
            if (!(setterArgType instanceof DeclaredType))
                throw new ProcessingException("Setter argument must be of an @MCElement-annotated type.", parameters.get(0));
            cei.setTypeDeclaration((TypeElement) ((DeclaredType) setterArgType).asElement());
            cei.setPropertyName(AnnotUtils.dejavaify(e2.getSimpleName().toString().substring(3)));
            cei.setRequired(isRequired(e2));
            ii.getCeis().add(cei);
            // unwrap "java.util.List<?>" and "java.util.Collection<?>"
            if (cei.getTypeDeclaration().getQualifiedName().toString().startsWith("java.util.List") || cei.getTypeDeclaration().getQualifiedName().toString().startsWith("java.util.Collection")) {
                cei.setTypeDeclaration((TypeElement) ((DeclaredType) ((DeclaredType) setterArgType).getTypeArguments().get(0)).asElement());
                cei.setList(true);
            }
            ChildElementDeclarationInfo cedi;
            if (!main.getChildElementDeclarations().containsKey(cei.getTypeDeclaration())) {
                cedi = new ChildElementDeclarationInfo();
                cedi.setTarget(cei.getTypeDeclaration());
                cedi.setRaiseErrorWhenNoSpecimen(!cei.getAnnotation().allowForeign());
                main.getChildElementDeclarations().put(cei.getTypeDeclaration(), cedi);
            } else {
                cedi = main.getChildElementDeclarations().get(cei.getTypeDeclaration());
                cedi.setRaiseErrorWhenNoSpecimen(cedi.isRaiseErrorWhenNoSpecimen() || !cei.getAnnotation().allowForeign());
            }
            cedi.addUsedBy(cei);
        }
        MCTextContent c = e2.getAnnotation(MCTextContent.class);
        if (c != null) {
            TextContentInfo tci = new TextContentInfo();
            tci.setPropertyName(AnnotUtils.dejavaify(e2.getSimpleName().toString().substring(3)));
            ii.setTci(tci);
        }
    }
    HashSet<Integer> childOrders = new HashSet<Integer>();
    for (ChildElementInfo cei : ii.getCeis()) {
        if (!childOrders.add(cei.getAnnotation().order()))
            throw new ProcessingException("@MCChildElement(order=...) must be unique.", cei.getE());
    }
    Collections.sort(ii.getCeis());
}
Also used : OtherAttributesInfo(com.predic8.membrane.annot.model.OtherAttributesInfo) TextContentInfo(com.predic8.membrane.annot.model.TextContentInfo) ChildElementInfo(com.predic8.membrane.annot.model.ChildElementInfo) AttributeInfo(com.predic8.membrane.annot.model.AttributeInfo) TypeMirror(javax.lang.model.type.TypeMirror) ChildElementDeclarationInfo(com.predic8.membrane.annot.model.ChildElementDeclarationInfo) DeclaredType(javax.lang.model.type.DeclaredType)

Aggregations

ChildElementInfo (com.predic8.membrane.annot.model.ChildElementInfo)11 ElementInfo (com.predic8.membrane.annot.model.ElementInfo)9 MainInfo (com.predic8.membrane.annot.model.MainInfo)9 FileObject (javax.tools.FileObject)7 BufferedWriter (java.io.BufferedWriter)6 ArrayList (java.util.ArrayList)6 AttributeInfo (com.predic8.membrane.annot.model.AttributeInfo)5 ChildElementDeclarationInfo (com.predic8.membrane.annot.model.ChildElementDeclarationInfo)5 FilerException (javax.annotation.processing.FilerException)5 Element (javax.lang.model.element.Element)5 TypeElement (javax.lang.model.element.TypeElement)5 ISchema (com.predic8.membrane.annot.generator.kubernetes.model.ISchema)3 IOException (java.io.IOException)3 ProcessingException (com.predic8.membrane.annot.ProcessingException)2 Schema (com.predic8.membrane.annot.generator.kubernetes.model.Schema)2 SchemaObject (com.predic8.membrane.annot.generator.kubernetes.model.SchemaObject)2 OtherAttributesInfo (com.predic8.membrane.annot.model.OtherAttributesInfo)2 Engine (com.floreysoft.jmte.Engine)1 ErrorHandler (com.floreysoft.jmte.ErrorHandler)1 ParseException (com.floreysoft.jmte.message.ParseException)1