
com.predic8.membrane.annot.generator.Parsers Maven / Gradle / Ivy
/* Copyright 2009, 2021 predic8 GmbH, www.predic8.com
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
package com.predic8.membrane.annot.generator;
import com.predic8.membrane.annot.*;
import com.predic8.membrane.annot.model.*;
import javax.annotation.processing.*;
import javax.lang.model.element.*;
import javax.tools.*;
import java.io.*;
import java.util.*;
public class Parsers {
private final ProcessingEnvironment processingEnv;
public Parsers(ProcessingEnvironment processingEnv) {
this.processingEnv = processingEnv;
}
public void writeParserDefinitior(Model m) throws IOException {
for (MainInfo main : m.getMains()) {
List sources = new ArrayList<>(main.getInterceptorElements());
sources.add(main.getElement());
try {
FileObject o = processingEnv.getFiler().createSourceFile(
main.getAnnotation().outputPackage() + ".NamespaceHandlerAutoGenerated",
sources.toArray(new Element[0]));
try (BufferedWriter bw = new BufferedWriter(o.openWriter())) {
String copyright = """
/* Copyright 2012,2013 predic8 GmbH, www.predic8.com
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
""";
bw.write(copyright +
"\r\n" +
"package " + main.getAnnotation().outputPackage() + ";\r\n" +
"\r\n" +
"/**\r\n" +
" * Automatically generated by " + Parsers.class.getName() + ".\r\n" +
" */\r\n" +
"public class NamespaceHandlerAutoGenerated {\r\n" +
"\r\n" +
" public static void registerBeanDefinitionParsers(NamespaceHandler nh) {\r\n");
for (ElementInfo i : main.getIis()) {
if (i.getAnnotation().topLevel()) {
bw.write(" nh.registerGlobalBeanDefinitionParser(\"" + i.getAnnotation().name() + "\", new " + i.getParserClassSimpleName() + "());\r\n");
} else {
for (ChildElementDeclarationInfo cedi : i.getUsedBy()) {
for (ChildElementInfo cei : cedi.getUsedBy()) {
TypeElement element = cei.getEi().getElement();
String clazz = AnnotUtils.getRuntimeClassName(element);
bw.write(" nh.registerLocalBeanDefinitionParser(\"" + clazz + "\", \"" + i.getAnnotation().name() + "\", new " + i.getParserClassSimpleName() + "());\r\n");
}
}
}
}
bw.write(
"""
}\r
}\r
""");
}
} catch (FilerException e) {
if (e.getMessage().contains("Source file already created"))
return;
throw e;
}
}
}
public void writeParsers(Model m) throws IOException {
for (MainInfo main : m.getMains()) {
for (ElementInfo ii : main.getIis()) {
List sources = new ArrayList<>();
sources.add(main.getElement());
sources.add(ii.getElement());
String interceptorClassName = ii.getElement().getQualifiedName().toString();
try {
FileObject o = processingEnv.getFiler().createSourceFile(main.getAnnotation().outputPackage() + "." + ii.getParserClassSimpleName(),
sources.toArray(new Element[0]));
try (BufferedWriter bw = new BufferedWriter(o.openWriter())) {
String copyright = """
/* Copyright 2012 predic8 GmbH, www.predic8.com
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at\r
http://www.apache.org/licenses/LICENSE-2.0\r
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
""";
bw.write(copyright +
"\r\n" +
"package " + main.getAnnotation().outputPackage() + ";\r\n" +
"\r\n" +
"import org.w3c.dom.Element;\r\n" +
"import org.springframework.beans.factory.xml.ParserContext;\r\n" +
"import org.springframework.beans.factory.support.BeanDefinitionBuilder;\r\n");
if (!main.getAnnotation().outputPackage().equals("com.predic8.membrane.core.config.spring"))
bw.write("import com.predic8.membrane.core.config.spring.*;\r\n");
bw.write(
"\r\n" +
"/**\r\n" +
" * Automatically generated by " + Parsers.class.getName() + ".\r\n" +
" */\r\n" +
"public class " + ii.getParserClassSimpleName() + " extends AbstractParser {\r\n" +
"\r\n" +
" protected Class> getBeanClass(org.w3c.dom.Element element) {\r\n" +
" return " + interceptorClassName + ".class;\r\n" +
" }\r\n");
bw.write("""
@Override\r
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {\r
""");
if (ii.isHasIdField())
bw.write(" setPropertyIfSet(\"id\", element, builder);\r\n");
bw.write(
" setIdIfNeeded(element, parserContext, \"" + ii.getAnnotation().name() + "\");\r\n");
for (AttributeInfo ai : ii.getAis()) {
if (ai.getXMLName().equals("id"))
continue;
if (ai.isBeanReference(processingEnv.getTypeUtils())) {
if (!ai.isRequired())
bw.write(" if (element.hasAttribute(\"" + ai.getXMLName() + "\"))\r\n");
bw.write(" builder.addPropertyReference(\"" + ai.getSpringName() + "\", element.getAttribute(\"" + ai.getXMLName() + "\"));\r\n");
} else {
bw.write(" setProperty" + (ai.isRequired() ? "" : "IfSet") + "(\"" + ai.getXMLName() + "\", \"" + ai.getSpringName() + "\", element, builder" + (ai.isEnum(processingEnv.getTypeUtils()) ? ", true" : "") + ");\r\n");
}
if (ai.getXMLName().equals("name"))
bw.write(" element.removeAttribute(\"name\");\r\n");
}
if (ii.getOai() != null) {
bw.write(" setProperties(\"" + ii.getOai().getSpringName() + "\", element, builder);\r\n");
}
for (ChildElementInfo cei : ii.getCeis())
if (cei.isList())
bw.write(" builder.addPropertyValue(\"" + cei.getPropertyName() + "\", new java.util.ArrayList
© 2015 - 2025 Weber Informatics LLC | Privacy Policy