Search in sources :

Example 26 with FileObject

use of javax.tools.FileObject in project mapstruct by mapstruct.

the class ModelWriter method writeModel.

public void writeModel(FileObject sourceFile, Writable model) {
    try {
        BufferedWriter writer = new BufferedWriter(new IndentationCorrectingWriter(sourceFile.openWriter()));
        Map<Class<?>, Object> values = new HashMap<Class<?>, Object>();
        values.put(Configuration.class, CONFIGURATION);
        model.write(new DefaultModelElementWriterContext(values), writer);
        writer.flush();
        writer.close();
    } catch (RuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : HashMap(java.util.HashMap) FileObject(javax.tools.FileObject) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter)

Example 27 with FileObject

use of javax.tools.FileObject in project service-proxy by membrane.

the class SpringConfigurationXSDGeneratingAnnotationProcessor method write.

private void write() {
    try {
        FileObject o = processingEnv.getFiler().createResource(StandardLocation.CLASS_OUTPUT, "", "META-INF/membrane.cache");
        BufferedWriter bw = new BufferedWriter(o.openWriter());
        try {
            bw.write("1\n");
            for (Map.Entry<Class<? extends Annotation>, HashSet<Element>> e : cache.entrySet()) {
                bw.write(e.getKey().getName());
                bw.write("\n");
                for (Element f : e.getValue()) {
                    bw.write(" ");
                    bw.write(((TypeElement) f).getQualifiedName().toString());
                    bw.write("\n");
                }
            }
        } finally {
            bw.close();
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : FileObject(javax.tools.FileObject) IOException(java.io.IOException) Annotation(java.lang.annotation.Annotation) BufferedWriter(java.io.BufferedWriter)

Example 28 with FileObject

use of javax.tools.FileObject in project service-proxy by membrane.

the class BlueprintParsers method writeParsers.

public void writeParsers(Model m) throws IOException {
    for (MainInfo main : m.getMains()) {
        for (ElementInfo ii : main.getIis()) {
            List<Element> sources = new ArrayList<Element>();
            sources.add(main.getElement());
            sources.add(ii.getElement());
            String interceptorClassName = ii.getElement().getQualifiedName().toString();
            try {
                FileObject o = processingEnv.getFiler().createSourceFile(main.getAnnotation().outputPackage() + ".blueprint" + "." + ii.getParserClassSimpleName(), sources.toArray(new Element[0]));
                BufferedWriter bw = new BufferedWriter(o.openWriter());
                try {
                    bw.write("/* Copyright 2014 predic8 GmbH, www.predic8.com\r\n" + "\r\n" + "   Licensed under the Apache License, Version 2.0 (the \"License\");\r\n" + "   you may not use this file except in compliance with the License.\r\n" + "   You may obtain a copy of the License at\r\n" + "\r\n" + "   http://www.apache.org/licenses/LICENSE-2.0\r\n" + "\r\n" + "   Unless required by applicable law or agreed to in writing, software\r\n" + "   distributed under the License is distributed on an \"AS IS\" BASIS,\r\n" + "   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r\n" + "   See the License for the specific language governing permissions and\r\n" + "   limitations under the License. */\r\n" + "\r\n" + "package " + main.getAnnotation().outputPackage() + ".blueprint;\r\n" + "\r\n" + "import com.predic8.membrane.annot.parser.BlueprintElementParser;\r\n" + "import com.predic8.membrane.annot.parser.BlueprintParser;\r\n" + "import org.apache.aries.blueprint.ParserContext;\r\n" + "import org.osgi.service.blueprint.reflect.Metadata;\r\n" + "import org.apache.aries.blueprint.mutable.MutableBeanMetadata;\r\n" + "import org.osgi.service.blueprint.reflect.BeanMetadata;\r\n" + "import org.w3c.dom.Element;\r\n" + "");
                    bw.write("\r\n" + "/**\r\n" + "  * Automatically generated by " + BlueprintParsers.class.getName() + ".\r\n" + "  */\r\n" + "public class " + ii.getParserClassSimpleName() + " extends BlueprintElementParser {\r\n" + "	public Metadata parse(BlueprintParser globalParser, Element element, ParserContext context) {\r\n" + "		MutableBeanMetadata mcm = context.createMetadata(MutableBeanMetadata.class);\r\n" + "		mcm.setId(context.generateId());\r\n" + "		mcm.setScope(BeanMetadata.SCOPE_SINGLETON);\r\n" + "		mcm.setRuntimeClass(" + interceptorClassName + ".class);\r\n" + "		applySpringInterfacePatches(context, " + interceptorClassName + ".class, mcm);\r\n" + "\r\n");
                    if (ii.isHasIdField())
                        bw.write("		setPropertyIfSet(context, \"id\", element, mcm);\r\n");
                    bw.write("		setIdIfNeeded(element, context, \"" + 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("		setPropertyReference(context, \"" + ai.getSpringName() + "\", element.getAttribute(\"" + ai.getXMLName() + "\"), mcm);\r\n");
                        } else {
                            bw.write("		setProperty" + (ai.isRequired() ? "" : "IfSet") + "(context, \"" + ai.getXMLName() + "\", \"" + ai.getSpringName() + "\", element, mcm" + (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(context, \"" + ii.getOai().getSpringName() + "\", element, mcm);\r\n");
                    }
                    if (ii.getTci() != null)
                        bw.write("		setProperty(context, mcm, \"" + ii.getTci().getPropertyName() + "\", element.getTextContent());\r\n");
                    else
                        bw.write("		parseChildren(element, context, mcm, globalParser);\r\n");
                    for (ChildElementInfo cei : ii.getCeis()) if (cei.isList() && cei.isRequired()) {
                        bw.write("		if (!isPropertySet(mcm, \"" + cei.getPropertyName() + "\"))\r\n");
                        bw.write("			throw new RuntimeException(\"Property '" + cei.getPropertyName() + "' is required, but none was defined (empty list).\");\r\n");
                    }
                    bw.write("" + "		return mcm;\r\n");
                    bw.write("	}\r\n");
                    bw.write("	@Override\r\n" + "	protected void handleChildObject(Element ele, ParserContext context, MutableBeanMetadata mcm, Class<?> clazz, Object child) {\r\n");
                    for (ChildElementInfo cei : ii.getCeis()) {
                        bw.write("		if (" + cei.getTypeDeclaration().getQualifiedName() + ".class.isAssignableFrom(clazz)) {\r\n");
                        if (cei.isList())
                            bw.write("			appendToListProperty(context, mcm, \"" + cei.getPropertyName() + "\"" + ", child);\r\n");
                        else
                            bw.write("			setProperty(context, mcm, \"" + cei.getPropertyName() + "\"" + ", child);\r\n");
                        bw.write("		} else \r\n");
                    }
                    bw.write("		{\r\n" + "			throw new RuntimeException(\"Unknown child class \\\"\" + clazz + \"\\\".\");\r\n" + "		}\r\n");
                    bw.write("	}\r\n");
                    bw.write("}\r\n" + "");
                } finally {
                    bw.close();
                }
            } catch (FilerException e) {
                if (e.getMessage().contains("Source file already created"))
                    return;
                throw e;
            }
        }
    }
}
Also used : AttributeInfo(com.predic8.membrane.annot.model.AttributeInfo) MainInfo(com.predic8.membrane.annot.model.MainInfo) ElementInfo(com.predic8.membrane.annot.model.ElementInfo) ChildElementInfo(com.predic8.membrane.annot.model.ChildElementInfo) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) ArrayList(java.util.ArrayList) FileObject(javax.tools.FileObject) FilerException(javax.annotation.processing.FilerException) ChildElementInfo(com.predic8.membrane.annot.model.ChildElementInfo) BufferedWriter(java.io.BufferedWriter)

Example 29 with FileObject

use of javax.tools.FileObject in project service-proxy by membrane.

the class NamespaceInfo method writeInfo.

public void writeInfo(Model m) {
    try {
        FileObject o = processingEnv.getFiler().createResource(StandardLocation.CLASS_OUTPUT, "", "META-INF/membrane.namespaces");
        OutputStream oo = o.openOutputStream();
        BufferedOutputStream bos = new BufferedOutputStream(oo);
        try {
            Properties p = new Properties();
            int i = 1;
            for (MainInfo mi : m.getMains()) {
                String key = "schema" + (i++);
                p.put(key + "-targetNamespace", mi.getAnnotation().targetNamespace());
                p.put(key + "-outputPackage", mi.getAnnotation().outputPackage());
                p.put(key + "-outputName", mi.getAnnotation().outputName());
            }
            p.store(bos, "Autogenerated by " + getClass().getName());
        } finally {
            bos.close();
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : MainInfo(com.predic8.membrane.annot.model.MainInfo) OutputStream(java.io.OutputStream) BufferedOutputStream(java.io.BufferedOutputStream) FileObject(javax.tools.FileObject) IOException(java.io.IOException) Properties(java.util.Properties) BufferedOutputStream(java.io.BufferedOutputStream)

Example 30 with FileObject

use of javax.tools.FileObject in project service-proxy by membrane.

the class Parsers method writeParsers.

public void writeParsers(Model m) throws IOException {
    for (MainInfo main : m.getMains()) {
        for (ElementInfo ii : main.getIis()) {
            List<Element> sources = new ArrayList<Element>();
            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]));
                BufferedWriter bw = new BufferedWriter(o.openWriter());
                try {
                    bw.write("/* Copyright 2012 predic8 GmbH, www.predic8.com\r\n" + "\r\n" + "   Licensed under the Apache License, Version 2.0 (the \"License\");\r\n" + "   you may not use this file except in compliance with the License.\r\n" + "   You may obtain a copy of the License at\r\n" + "\r\n" + "   http://www.apache.org/licenses/LICENSE-2.0\r\n" + "\r\n" + "   Unless required by applicable law or agreed to in writing, software\r\n" + "   distributed under the License is distributed on an \"AS IS\" BASIS,\r\n" + "   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r\n" + "   See the License for the specific language governing permissions and\r\n" + "   limitations under the License. */\r\n" + "\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\n" + "	protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {\r\n");
                    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<Object>());\r\n");
                    if (ii.getTci() != null)
                        bw.write("		builder.addPropertyValue(\"" + ii.getTci().getPropertyName() + "\", element.getTextContent());\r\n");
                    else
                        bw.write("		parseChildren(element, parserContext, builder);\r\n");
                    for (ChildElementInfo cei : ii.getCeis()) if (cei.isList() && cei.isRequired()) {
                        bw.write("		if (builder.getBeanDefinition().getPropertyValues().getPropertyValue(\"" + cei.getPropertyName() + "[0]\") == null)\r\n");
                        bw.write("			throw new RuntimeException(\"Property '" + cei.getPropertyName() + "' is required, but none was defined (empty list).\");\r\n");
                    }
                    bw.write("	}\r\n" + "");
                    bw.write("@Override\r\n" + "protected void handleChildObject(Element ele, ParserContext parserContext, BeanDefinitionBuilder builder, Class<?> clazz, Object child) {\r\n");
                    for (ChildElementInfo cei : ii.getCeis()) {
                        bw.write("	if (" + cei.getTypeDeclaration().getQualifiedName() + ".class.isAssignableFrom(clazz)) {\r\n" + "		setProperty(builder, \"" + cei.getPropertyName() + "\"" + (cei.isList() ? "+\"[\"+ incrementCounter(builder, \"" + cei.getPropertyName() + "\") + \"]\" " : "") + ", child);\r\n" + "	} else \r\n");
                    }
                    bw.write("	{\r\n" + "		throw new RuntimeException(\"Unknown child class \\\"\" + clazz + \"\\\".\");\r\n" + "	}\r\n");
                    bw.write("}\r\n");
                    bw.write("}\r\n" + "");
                } finally {
                    bw.close();
                }
            } catch (FilerException e) {
                if (e.getMessage().contains("Source file already created"))
                    return;
                throw e;
            }
        }
    }
}
Also used : AttributeInfo(com.predic8.membrane.annot.model.AttributeInfo) MainInfo(com.predic8.membrane.annot.model.MainInfo) ElementInfo(com.predic8.membrane.annot.model.ElementInfo) ChildElementInfo(com.predic8.membrane.annot.model.ChildElementInfo) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) ArrayList(java.util.ArrayList) FileObject(javax.tools.FileObject) FilerException(javax.annotation.processing.FilerException) ChildElementInfo(com.predic8.membrane.annot.model.ChildElementInfo) BufferedWriter(java.io.BufferedWriter)

Aggregations

FileObject (javax.tools.FileObject)91 IOException (java.io.IOException)57 TypeElement (javax.lang.model.element.TypeElement)19 File (java.io.File)18 PrintWriter (java.io.PrintWriter)16 Element (javax.lang.model.element.Element)14 Writer (java.io.Writer)13 Filer (javax.annotation.processing.Filer)13 BufferedWriter (java.io.BufferedWriter)12 ArrayList (java.util.ArrayList)12 OutputStream (java.io.OutputStream)11 JavaFileObject (javax.tools.JavaFileObject)11 OutputStreamWriter (java.io.OutputStreamWriter)10 Properties (java.util.Properties)10 InputStream (java.io.InputStream)8 URI (java.net.URI)8 FilerException (javax.annotation.processing.FilerException)7 MainInfo (com.predic8.membrane.annot.model.MainInfo)6 BufferedReader (java.io.BufferedReader)6 FileWriter (java.io.FileWriter)6