<commit_before>package io.fabianterhorst.fastlayout;

import org.apache.commons.io.IOUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import java.io.StringReader;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.PackageElement;
import javax.lang.model.element.TypeElement;
import javax.tools.Diagnostic;
import javax.tools.JavaFileObject;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.Version;
import io.fabianterhorst.fastlayout.annotations.Layout;

@SupportedAnnotationTypes("io.fabianterhorst.fastlayout.annotations.Layout")
public class LayoutProcessor extends AbstractProcessor {

    private static final ArrayList<String> nativeSupportedAttributes = new ArrayList<String>() {{
        add("layout_height");
        add("layout_width");
        add("id");
        add("paddingLeft");
        add("paddingTop");
        add("paddingRight");
        add("paddingBottom");
        add("layout_marginLeft");
        add("layout_marginTop");
        add("layout_marginRight");
        add("layout_marginBottom");
        add("layout_weight");
    }};

    private static final String SUFFIX_PREF_WRAPPER = "Layout";

    private Configuration mFreemarkerConfiguration;

    private final List<LayoutEntity> mChilds = new ArrayList<>();

    @Override
    public SourceVersion getSupportedSourceVersion() {
        return SourceVersion.latestSupported();
    }

    private Configuration getFreemarkerConfiguration() {
        if (mFreemarkerConfiguration == null) {
            mFreemarkerConfiguration = new Configuration(new Version(2, 3, 22));
            mFreemarkerConfiguration.setClassForTemplateLoading(getClass(), "");
        }
        return mFreemarkerConfiguration;
    }

    @Override
    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
        String debug = "Log" + System.currentTimeMillis() + ": ";
        String packageName = null;

        List<LayoutObject> layouts = new ArrayList<>();

        for (TypeElement te : annotations) {
            for (javax.lang.model.element.Element element : roundEnv.getElementsAnnotatedWith(te)) {
                TypeElement classElement = (TypeElement) element;
                PackageElement packageElement = (PackageElement) classElement.getEnclosingElement();
                String layout = element.getAnnotation(Layout.class).value();
                LayoutEntity rootLayout = new LayoutEntity();

                packageName = packageElement.getQualifiedName().toString();

                try {
                    DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
                    InputSource is = new InputSource();
                    is.setCharacterStream(new StringReader(layout));
                    Document document = documentBuilder.parse(is);
                    Element rootLayoutElement = document.getDocumentElement();
                    /*rootLayout.setId(normalizeLayoutId(rootLayoutElement.getAttribute("android:id")));
                    rootLayout.setName(rootLayoutElement.getTagName());
                    LayoutParam layoutParam = new LayoutParam();
                    layoutParam.setName(getLayoutParamsForViewGroup(rootLayout.getName()));
                    layoutParam.setWidth(rootLayoutElement.getAttribute("android:layout_width").toUpperCase());
                    layoutParam.setHeight(rootLayoutElement.getAttribute("android:layout_height").toUpperCase());
                    rootLayout.setLayoutParams(layoutParam);*/
                    rootLayout = createLayoutFromChild(rootLayoutElement);
                    if (rootLayoutElement.hasChildNodes()) {
                        createChildList(rootLayoutElement);
                        rootLayout.addChildren(mChilds);
                    }
                } catch (Exception ignore) {
                    debug += " exception:" + ignore.getMessage();
                }

                JavaFileObject javaFileObject;
                try {
                    String layoutName = classElement.getQualifiedName() + SUFFIX_PREF_WRAPPER;
                    layouts.add(new LayoutObject(layoutName));
                    Map<String, Object> args = new HashMap<>();
                    //Layout Wrapper
                    javaFileObject = processingEnv.getFiler().createSourceFile(layoutName);
                    Template template = getFreemarkerConfiguration().getTemplate("layoutwrapper.ftl");
                    args.put("package", packageElement.getQualifiedName());
                    args.put("keyWrapperClassName", classElement.getSimpleName() + SUFFIX_PREF_WRAPPER);
                    args.put("rootLayout", rootLayout);
                    args.put("log", debug);
                    Writer writer = javaFileObject.openWriter();
                    template.process(args, writer);
                    IOUtils.closeQuietly(writer);

                } catch (Exception e) {
                    processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR,
                            "En error occurred while generating Prefs code " + e.getClass() + e.getMessage(), element);
                    e.printStackTrace();
                    // Problem detected: halt
                    return true;
                }
            }
        }

        if (layouts.size() > 0) {
            JavaFileObject javaFileObject;
            try {
                Map<String, Object> args = new HashMap<>();
                //Layout Cache Wrapper
                javaFileObject = processingEnv.getFiler().createSourceFile("LayoutCache");
                Template template = getFreemarkerConfiguration().getTemplate("layoutcachewrapper.ftl");
                args.put("package", packageName);
                args.put("layouts", layouts);
                Writer writer = javaFileObject.openWriter();
                template.process(args, writer);
                IOUtils.closeQuietly(writer);

            } catch (Exception e) {
                processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR,
                        "En error occurred while generating Prefs code " + e.getClass() + e.getMessage());
                e.printStackTrace();
                // Problem detected: halt
                return true;
            }
        }

        /*try {
            JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
            StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null);

            Iterable<? extends File> locations = fm.getLocation(StandardLocation.SOURCE_PATH);
            for(File file : locations){
                System.out.print(file.getAbsolutePath());
            }
            //FileInputStream fileInputStream = new FileInputStream("");
        }catch(Exception ignore){
        }*/

        return true;
    }<commit_msg>fix LayoutCache file path<commit_after>package io.fabianterhorst.fastlayout;

import org.apache.commons.io.IOUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import java.io.StringReader;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.PackageElement;
import javax.lang.model.element.TypeElement;
import javax.tools.Diagnostic;
import javax.tools.JavaFileObject;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.Version;
import io.fabianterhorst.fastlayout.annotations.Layout;

@SupportedAnnotationTypes("io.fabianterhorst.fastlayout.annotations.Layout")
public class LayoutProcessor extends AbstractProcessor {

    private static final ArrayList<String> nativeSupportedAttributes = new ArrayList<String>() {{
        add("layout_height");
        add("layout_width");
        add("id");
        add("paddingLeft");
        add("paddingTop");
        add("paddingRight");
        add("paddingBottom");
        add("layout_marginLeft");
        add("layout_marginTop");
        add("layout_marginRight");
        add("layout_marginBottom");
        add("layout_weight");
    }};

    private static final String SUFFIX_PREF_WRAPPER = "Layout";

    private Configuration mFreemarkerConfiguration;

    private final List<LayoutEntity> mChilds = new ArrayList<>();

    @Override
    public SourceVersion getSupportedSourceVersion() {
        return SourceVersion.latestSupported();
    }

    private Configuration getFreemarkerConfiguration() {
        if (mFreemarkerConfiguration == null) {
            mFreemarkerConfiguration = new Configuration(new Version(2, 3, 22));
            mFreemarkerConfiguration.setClassForTemplateLoading(getClass(), "");
        }
        return mFreemarkerConfiguration;
    }

    @Override
    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
        String debug = "Log" + System.currentTimeMillis() + ": ";
        String packageName = null;

        List<LayoutObject> layouts = new ArrayList<>();

        for (TypeElement te : annotations) {
            for (javax.lang.model.element.Element element : roundEnv.getElementsAnnotatedWith(te)) {
                TypeElement classElement = (TypeElement) element;
                PackageElement packageElement = (PackageElement) classElement.getEnclosingElement();
                String layout = element.getAnnotation(Layout.class).value();
                LayoutEntity rootLayout = new LayoutEntity();

                packageName = packageElement.getQualifiedName().toString();

                try {
                    DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
                    InputSource is = new InputSource();
                    is.setCharacterStream(new StringReader(layout));
                    Document document = documentBuilder.parse(is);
                    Element rootLayoutElement = document.getDocumentElement();
                    /*rootLayout.setId(normalizeLayoutId(rootLayoutElement.getAttribute("android:id")));
                    rootLayout.setName(rootLayoutElement.getTagName());
                    LayoutParam layoutParam = new LayoutParam();
                    layoutParam.setName(getLayoutParamsForViewGroup(rootLayout.getName()));
                    layoutParam.setWidth(rootLayoutElement.getAttribute("android:layout_width").toUpperCase());
                    layoutParam.setHeight(rootLayoutElement.getAttribute("android:layout_height").toUpperCase());
                    rootLayout.setLayoutParams(layoutParam);*/
                    rootLayout = createLayoutFromChild(rootLayoutElement);
                    if (rootLayoutElement.hasChildNodes()) {
                        createChildList(rootLayoutElement);
                        rootLayout.addChildren(mChilds);
                    }
                } catch (Exception ignore) {
                    debug += " exception:" + ignore.getMessage();
                }

                JavaFileObject javaFileObject;
                try {
                    String layoutName = classElement.getQualifiedName() + SUFFIX_PREF_WRAPPER;
                    layouts.add(new LayoutObject(layoutName));
                    Map<String, Object> args = new HashMap<>();
                    //Layout Wrapper
                    javaFileObject = processingEnv.getFiler().createSourceFile(layoutName);
                    Template template = getFreemarkerConfiguration().getTemplate("layoutwrapper.ftl");
                    args.put("package", packageElement.getQualifiedName());
                    args.put("keyWrapperClassName", classElement.getSimpleName() + SUFFIX_PREF_WRAPPER);
                    args.put("rootLayout", rootLayout);
                    args.put("log", debug);
                    Writer writer = javaFileObject.openWriter();
                    template.process(args, writer);
                    IOUtils.closeQuietly(writer);

                } catch (Exception e) {
                    processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR,
                            "En error occurred while generating Prefs code " + e.getClass() + e.getMessage(), element);
                    e.printStackTrace();
                    // Problem detected: halt
                    return true;
                }
            }
        }

        if (layouts.size() > 0) {
            JavaFileObject javaFileObject;
            try {
                Map<String, Object> args = new HashMap<>();
                //Layout Cache Wrapper
                javaFileObject = processingEnv.getFiler().createSourceFile(packageName + ".LayoutCache");
                Template template = getFreemarkerConfiguration().getTemplate("layoutcachewrapper.ftl");
                args.put("package", packageName);
                args.put("layouts", layouts);
                Writer writer = javaFileObject.openWriter();
                template.process(args, writer);
                IOUtils.closeQuietly(writer);

            } catch (Exception e) {
                processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR,
                        "En error occurred while generating Prefs code " + e.getClass() + e.getMessage());
                e.printStackTrace();
                // Problem detected: halt
                return true;
            }
        }

        /*try {
            JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
            StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null);

            Iterable<? extends File> locations = fm.getLocation(StandardLocation.SOURCE_PATH);
            for(File file : locations){
                System.out.print(file.getAbsolutePath());
            }
            //FileInputStream fileInputStream = new FileInputStream("");
        }catch(Exception ignore){
        }*/

        return true;
    }
{'fraction_non_alphanumeric': 0.09786750451233371, 'fraction_numerical': 0.0013369877665619359, 'mean_word_length': 2.3475050346833743, 'pattern_counts': {'":': 2, '<': 27, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4827609', 'n_tokens_mistral': 3639, 'n_tokens_neox': 3473, 'n_words': 668}