Untitled

mail@pastecode.io avatar
unknown
java
16 days ago
7.9 kB
6
Indexable
Never
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class CodeGenerator {
    public static void main(String[] args) {
        if (args.length == 0) {
            System.err.println("Please input mermaid file name");
        } else {
            // Get input
            String fileName = args[0];
            String mermaidCode = readMermaidCodeFromFile(fileName);
            List<String> classCodes = Parser.splitByClass(mermaidCode);
            for (String classCode : classCodes) {
                // Extract class name
                String className = extractClassName(classCode);
                if (className == null) {
                    System.err.println("Error: Class name not found in input");
                    continue;
                }
                // Write class code to file
                writeToFile(className + ".java", transformMermaidToJava(classCode));
            }
        }
    }

    private static String transformMermaidToJava(String classCode) {
        // Your transformation logic here
        StringBuilder javaCode = new StringBuilder();
        // Split the class diagram code by lines
        String[] lines = classCode.split("\n");
    
        // Loop through each line to process each class
        for (String line : lines) {
            // Trim the line
            line = line.trim();
            // If the line starts with "class", it defines a new class
            if (line.startsWith("class ")) {
                // Extract the class name
                String className = line.substring(6).trim();
                List<String> attributes = new ArrayList<>();
                List<String> methods = new ArrayList<>();
                // Loop through the remaining lines to process attributes and methods of the class
                for (String nextLine : lines) {
                    // Skip if it's not related to the current class
                    if (!nextLine.startsWith(className)) {
                        continue;
                    }
                    // Trim the line
                    nextLine = nextLine.trim();
                    // If the line contains attribute or method definitions
                    if (nextLine.contains(":")) {
                        // Split the line by colon to separate attribute/method name and visibility
                        String[] parts = nextLine.split(":");
                        // Trim attribute/method name and visibility
                        String declaration = parts[1].trim();
                        String visibility = declaration.substring(0, 1);
                        // If it's an attribute
                        if (visibility.equals("-")) {
                            // Add the attribute to the list
                            attributes.add(generatePrivateAttribute(declaration));
                        }
                        // If it's a method
                        else if (visibility.equals("+")) {
                            // Extract the return type and method name
                            String methodName = declaration.split("\\(")[0].trim();
                            // Add the method to the list with correct method signature
                            if (methodName.startsWith("get")) {
                                methods.add(generateGetterMethod(declaration));
                            } else if (methodName.startsWith("set")) {
                                methods.add(generateSetterMethod(declaration));
                            } else {
                                methods.add(generateMethod(declaration));
                            }
                        }
                    }
                }
                // Generate Java code for the current class
                javaCode.append("public class ").append(className).append(" {\n");
                // Generate attribute declarations
                for (String attribute : attributes) {
                    javaCode.append("    ").append(attribute).append("\n");
                }
                // Generate method declarations
                for (String method : methods) {
                    javaCode.append("    ").append(method).append("\n");
                }
                javaCode.append("}\n");
            }
        }
        return javaCode.toString();
    }
    

    private static String generatePrivateAttribute(String attribute) {
        return "private " + attribute.substring(1) + ";";
    }

    private static String generateGetterMethod(String declaration) {
        String attribute = declaration.substring(declaration.indexOf(":") + 1).trim();
        String attributeName = attribute.split(" ")[1];
        String returnType = attribute.split(" ")[0];
        return "public " + returnType + " get" + capitalize(attributeName) + "() {\n" +
                "        return " + attributeName + ";\n" +
                "    }";
    }

    private static String generateSetterMethod(String declaration) {
        String attribute = declaration.substring(declaration.indexOf(":") + 1).trim();
        String attributeName = attribute.split(" ")[1];
        String returnType = attribute.split(" ")[0];
        return "public void set" + capitalize(attributeName) + "(" + returnType + " " + attributeName + ") {\n" +
                "        this." + attributeName + " = " + attributeName + ";\n" +
                "    }";
    }
    private static String generateMethod(String declaration) {
        String[] parts = declaration.split(" ");
        String returnType = parts[1].trim();
        String methodName = parts[3].trim().split("\\(")[0].trim(); // Extract method name
        return "public " + returnType + " " + methodName + "() {\n" +
                "        return price;\n" +
                "    }";
    }
    
    
    

    private static String capitalize(String str) {
        return str.substring(0, 1).toUpperCase() + str.substring(1);
    }

    private static String readMermaidCodeFromFile(String fileName) {
        StringBuilder mermaidCode = new StringBuilder();

        try (FileReader fileReader = new FileReader(fileName)) {
            char[] buffer = new char[1024];
            int length;
            while ((length = fileReader.read(buffer)) > 0) {
                mermaidCode.append(buffer, 0, length);
            }
        } catch (IOException e) {
            System.err.println("Error reading file: " + e.getMessage());
        }

        return mermaidCode.toString();
    }

    private static String extractClassName(String classCode) {
        int startIndex = classCode.indexOf("class ");
        if (startIndex == -1) {
            return null;
        }
        startIndex += 6; // Skip "class " substring
        int endIndex = classCode.indexOf("\n", startIndex);
        if (endIndex == -1) {
            return null;
        }
        return classCode.substring(startIndex, endIndex).trim();
    }

    private static void writeToFile(String fileName, String classCode) {
        try (FileWriter fileWriter = new FileWriter(fileName)) {
            fileWriter.write(classCode);
        } catch (IOException e) {
            System.err.println("Error writing file: " + e.getMessage());
        }
    }
}

class Parser {
    public static List<String> splitByClass(String input) {
        List<String> classCodes = new ArrayList<>();
        String[] classes = input.split("class ");
        for (String cls : classes) {
            if (!cls.trim().isEmpty()) {
                cls = "class " + cls;
                classCodes.add(cls);
            }
        }
        return classCodes;
    }
}
Leave a Comment