From 2daa47aed81e0884c897cd040d015a92d3149b9e Mon Sep 17 00:00:00 2001 From: Nilstrieb Date: Sat, 21 Nov 2020 16:14:32 +0100 Subject: [PATCH] javadoc and regex now Pattern --- src/Regex.java | 16 +++++++++++----- src/UMLClass.java | 17 +++++++++++++++-- src/UMLClassView.java | 4 +++- src/UMLConverterMain.java | 3 +++ src/UMLField.java | 16 ++++++++-------- src/UMLManager.java | 8 ++++++++ src/UMLMethod.java | 25 ++++++++++++++++++++----- src/XMLParser.java | 14 ++++++++------ 8 files changed, 76 insertions(+), 27 deletions(-) diff --git a/src/Regex.java b/src/Regex.java index 5fa96a1..3f72964 100644 --- a/src/Regex.java +++ b/src/Regex.java @@ -1,4 +1,10 @@ -public class Regex { +import java.util.regex.Pattern; + +/** + * Contains all Regex + */ +public abstract class Regex { + /** * Matches any method in the UML format, including a constructor @@ -19,11 +25,11 @@ public class Regex { * * */ - public static final String METHOD_FIND_REGEX = " *(?[+\\-~#]) *(?\\w+) *\\( *(?(?: *\\w+ *: *\\w+ *,? *)*) *\\) *(?:: *(?\\w+))?"; + public static final Pattern METHOD_FIND_REGEX = Pattern.compile(" *(?[+\\-~#]) *(?\\w+) *\\( *(?(?: *\\w+ *: *\\w+ *,? *)*) *\\) *(?:: *(?\\w+))?"); /** * Matches any Field in the UML format, including a constructor - * + *y *

Examples:

*
    *
  • - age: int
  • @@ -39,7 +45,7 @@ public class Regex { *
* */ - public static final String FIELD_FIND_REGEX = " *(?[+\\-~#]) *(?\\w+) *: *(?\\w+)"; + public static final Pattern FIELD_FIND_REGEX = Pattern.compile(" *(?[+\\-~#]) *(?\\w+) *: *(?\\w+)"); /** * Matches a single arg in a method @@ -50,5 +56,5 @@ public class Regex { *
  • 2 The datatype
  • * */ - public static final String ARG_SPLIT_REGEX = " *(\\w+) *: *(\\w+)"; + public static final Pattern ARG_SPLIT_REGEX = Pattern.compile(" *(\\w+) *: *(\\w+)"); } diff --git a/src/UMLClass.java b/src/UMLClass.java index ad101aa..11534b7 100644 --- a/src/UMLClass.java +++ b/src/UMLClass.java @@ -1,4 +1,6 @@ +import javax.print.DocFlavor; import java.util.ArrayList; +import java.util.regex.Pattern; public class UMLClass { @@ -11,6 +13,13 @@ public class UMLClass { private UMLManager manager; + /** + * Contains all information about a Java class loaded from a UMLet XML file + * Use the {@link #toString()} method to get the class as compilable, valid Java Code + * @param classDiagram + * @param packageString + * @param manager + */ public UMLClass(String classDiagram, String packageString, UMLManager manager) { this.manager = manager; @@ -26,15 +35,19 @@ public class UMLClass { for (String line : linesBeheaded) { if (line != null) { - if (line.matches(Regex.METHOD_FIND_REGEX)) { //MATCHES METHOD + if (line.matches(Regex.METHOD_FIND_REGEX.pattern())) { //MATCHES METHOD methods.add(new UMLMethod(line, name)); - } else if (line.matches(Regex.FIELD_FIND_REGEX)) { //MATCHES FIELD + } else if (line.matches(Regex.FIELD_FIND_REGEX.pattern())) { //MATCHES FIELD fields.add(new UMLField(line)); } } } } + /** + * Returns the class as compilable Java code + * @return The class as compilable Java code + */ @Override public String toString() { StringBuilder s = new StringBuilder(); diff --git a/src/UMLClassView.java b/src/UMLClassView.java index 3f73834..f8a73c7 100644 --- a/src/UMLClassView.java +++ b/src/UMLClassView.java @@ -21,7 +21,9 @@ public class UMLClassView { private UMLManager manager; - + /** + * Creates the GUI for the application + */ public UMLClassView() { inputArea.addKeyListener(new KeyAdapter() { diff --git a/src/UMLConverterMain.java b/src/UMLConverterMain.java index c50da39..4f3f9df 100644 --- a/src/UMLConverterMain.java +++ b/src/UMLConverterMain.java @@ -1,5 +1,8 @@ import javax.swing.*; +/** + * Stars the UMLetClassParser + */ public class UMLConverterMain { public static void main(String[] args) { diff --git a/src/UMLField.java b/src/UMLField.java index e00b460..03e0405 100644 --- a/src/UMLField.java +++ b/src/UMLField.java @@ -1,3 +1,6 @@ +/** + * Stores all information about a field in a class and converts it into Java code using the {@link #toString()} method + */ public class UMLField { private final String dataType; @@ -6,12 +9,6 @@ public class UMLField { private boolean valid; - public UMLField(String dataType, String name, String encapsulation) { - this.dataType = dataType; - this.name = name; - this.encapsulation = encapsulation; - } - /** * New Field from UML line * @@ -19,7 +16,7 @@ public class UMLField { */ public UMLField(String line) { - String formatted = line.replaceAll(Regex.FIELD_FIND_REGEX, "$1;$3;$2"); + String formatted = line.replaceAll(Regex.FIELD_FIND_REGEX.pattern(), "$1;$3;$2"); String[] formattedSplit = formatted.split(";"); this.encapsulation = switch (formattedSplit[0]) { @@ -34,7 +31,10 @@ public class UMLField { this.dataType = formattedSplit[1]; } - + /** + * Returns the field as the Java code representation + * @return The field as the Java code representation + */ @Override public String toString() { return " " + encapsulation + dataType + " " + name + ";\n"; diff --git a/src/UMLManager.java b/src/UMLManager.java index 63f9897..0b07c25 100644 --- a/src/UMLManager.java +++ b/src/UMLManager.java @@ -3,6 +3,9 @@ import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; +/** + * Manages everything about the parsing + */ public class UMLManager { private UMLClassView view; @@ -14,6 +17,11 @@ public class UMLManager { view.setManager(this); } + /** + * Converts the class text into compilable Java files at the package destination containing the code + * @param classesText An ArrayList of Type String containing all classes in UML text represantation + * @param packagePath The path to the package where the classes should be stored to + */ public void parseClasses(ArrayList classesText, String packagePath) { String packageString = packagePath.replaceAll(".*src\\\\(.*)", "$1"); diff --git a/src/UMLMethod.java b/src/UMLMethod.java index 53df229..ccf9c15 100644 --- a/src/UMLMethod.java +++ b/src/UMLMethod.java @@ -1,5 +1,8 @@ import java.util.ArrayList; +/** + * Stores all information about a method in a class and converts it into Java code using the {@link #toString()} method + */ public class UMLMethod { private final String returnType; @@ -9,11 +12,17 @@ public class UMLMethod { private final ArrayList argsNames = new ArrayList<>(); private final ArrayList argsTypes = new ArrayList<>(); + /** + * Create a new method/constructor from the UML representation of that method + * + * @param line The line in the UML diagram + * @param className The name of the class + */ public UMLMethod(String line, String className) { //First, format it nicely - String formatted = line.replaceAll(Regex.METHOD_FIND_REGEX, "$1;$4;$2;$3"); + String formatted = line.replaceAll(Regex.METHOD_FIND_REGEX.pattern(), "$1;$4;$2;$3"); String[] parts = formatted.split(";"); this.encapsulation = switch (parts[0]) { case "+" -> "public "; @@ -26,19 +35,19 @@ public class UMLMethod { this.name = parts[2]; - if(parts[1].equals("") && !className.equals(name)){ + if (parts[1].equals("") && !className.equals(name)) { this.returnType = "void "; } else { this.returnType = parts[1] + " "; } - if(parts.length == 4) { + if (parts.length == 4) { String args = parts[3]; String[] argsSplit = args.split(","); for (String s : argsSplit) { - if(s.matches(Regex.ARG_SPLIT_REGEX)) { - String argFormatted = s.replaceAll(Regex.ARG_SPLIT_REGEX, "$1;$2"); + if (s.matches(Regex.ARG_SPLIT_REGEX.pattern())) { + String argFormatted = s.replaceAll(Regex.ARG_SPLIT_REGEX.pattern(), "$1;$2"); String[] formattedSplit = argFormatted.split(";"); argsNames.add(formattedSplit[0]); argsTypes.add(formattedSplit[1]); @@ -47,6 +56,12 @@ public class UMLMethod { } } + + /** + * Returns the method as the Java code representation + * + * @return The method as the Java code representation + */ @Override public String toString() { StringBuilder returnString = new StringBuilder(); diff --git a/src/XMLParser.java b/src/XMLParser.java index ae2fbdd..f7a1dd5 100644 --- a/src/XMLParser.java +++ b/src/XMLParser.java @@ -11,6 +11,9 @@ import java.io.File; import java.io.IOException; import java.util.ArrayList; +/** + * Opens a UMLet XML file and parses its content to an ArrayList of Type String containing all UML class texts + */ public class XMLParser { private Document doc; @@ -27,26 +30,25 @@ public class XMLParser { } } + /** + * Get all classes from a UMLet XML file + * @return an ArrayList of Type String containing all UML classes in their text form + */ public ArrayList getClassesText(){ ArrayList classes = new ArrayList<>(); - if(valid) { - NodeList nList = doc.getDocumentElement().getElementsByTagName("element"); - for (int i = 0; i < nList.getLength(); i++) { - Node node = nList.item(i); + for (int i = 0; i < nList.getLength(); i++) { Node node = nList.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { Element element = (Element) node; if (element.getElementsByTagName("id").item(0).getTextContent().equals("UMLClass")) { - String classBody = element.getElementsByTagName("panel_attributes").item(0).getTextContent(); classes.add(classBody); - } } }