javadoc and regex now Pattern

This commit is contained in:
nora 2020-11-21 16:14:32 +01:00
parent cb331331f1
commit 2daa47aed8
8 changed files with 76 additions and 27 deletions

View file

@ -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 {
* </ul>
*
*/
public static final String METHOD_FIND_REGEX = " *(?<capsule>[+\\-~#]) *(?<name>\\w+) *\\( *(?<args>(?: *\\w+ *: *\\w+ *,? *)*) *\\) *(?:: *(?<return>\\w+))?";
public static final Pattern METHOD_FIND_REGEX = Pattern.compile(" *(?<capsule>[+\\-~#]) *(?<name>\\w+) *\\( *(?<args>(?: *\\w+ *: *\\w+ *,? *)*) *\\) *(?:: *(?<return>\\w+))?");
/**
* Matches any Field in the UML format, including a constructor
*
*y
* <p>Examples:</p>
* <ul>
* <li>- age: int</li>
@ -39,7 +45,7 @@ public class Regex {
* </ul>
*
*/
public static final String FIELD_FIND_REGEX = " *(?<capsule>[+\\-~#]) *(?<name>\\w+) *: *(?<type>\\w+)";
public static final Pattern FIELD_FIND_REGEX = Pattern.compile(" *(?<capsule>[+\\-~#]) *(?<name>\\w+) *: *(?<type>\\w+)");
/**
* Matches a single arg in a method
@ -50,5 +56,5 @@ public class Regex {
* <li>2 The datatype</li>
* </ul>
*/
public static final String ARG_SPLIT_REGEX = " *(\\w+) *: *(\\w+)";
public static final Pattern ARG_SPLIT_REGEX = Pattern.compile(" *(\\w+) *: *(\\w+)");
}

View file

@ -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();

View file

@ -21,7 +21,9 @@ public class UMLClassView {
private UMLManager manager;
/**
* Creates the GUI for the application
*/
public UMLClassView() {
inputArea.addKeyListener(new KeyAdapter() {

View file

@ -1,5 +1,8 @@
import javax.swing.*;
/**
* Stars the UMLetClassParser
*/
public class UMLConverterMain {
public static void main(String[] args) {

View file

@ -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";

View file

@ -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<String> classesText, String packagePath) {
String packageString = packagePath.replaceAll(".*src\\\\(.*)", "$1");

View file

@ -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<String> argsNames = new ArrayList<>();
private final ArrayList<String> 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();

View file

@ -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<String> getClassesText(){
ArrayList<String> 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);
}
}
}