mirror of
https://github.com/Noratrieb/UMLetClassParser.git
synced 2026-01-14 16:45:05 +01:00
javadoc and regex now Pattern
This commit is contained in:
parent
cb331331f1
commit
2daa47aed8
8 changed files with 76 additions and 27 deletions
|
|
@ -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
|
* Matches any method in the UML format, including a constructor
|
||||||
|
|
@ -19,11 +25,11 @@ public class Regex {
|
||||||
* </ul>
|
* </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
|
* Matches any Field in the UML format, including a constructor
|
||||||
*
|
*y
|
||||||
* <p>Examples:</p>
|
* <p>Examples:</p>
|
||||||
* <ul>
|
* <ul>
|
||||||
* <li>- age: int</li>
|
* <li>- age: int</li>
|
||||||
|
|
@ -39,7 +45,7 @@ public class Regex {
|
||||||
* </ul>
|
* </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
|
* Matches a single arg in a method
|
||||||
|
|
@ -50,5 +56,5 @@ public class Regex {
|
||||||
* <li>2 The datatype</li>
|
* <li>2 The datatype</li>
|
||||||
* </ul>
|
* </ul>
|
||||||
*/
|
*/
|
||||||
public static final String ARG_SPLIT_REGEX = " *(\\w+) *: *(\\w+)";
|
public static final Pattern ARG_SPLIT_REGEX = Pattern.compile(" *(\\w+) *: *(\\w+)");
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,6 @@
|
||||||
|
import javax.print.DocFlavor;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
public class UMLClass {
|
public class UMLClass {
|
||||||
|
|
||||||
|
|
@ -11,6 +13,13 @@ public class UMLClass {
|
||||||
|
|
||||||
private UMLManager manager;
|
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) {
|
public UMLClass(String classDiagram, String packageString, UMLManager manager) {
|
||||||
|
|
||||||
this.manager = manager;
|
this.manager = manager;
|
||||||
|
|
@ -26,15 +35,19 @@ public class UMLClass {
|
||||||
|
|
||||||
for (String line : linesBeheaded) {
|
for (String line : linesBeheaded) {
|
||||||
if (line != null) {
|
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));
|
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));
|
fields.add(new UMLField(line));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the class as compilable Java code
|
||||||
|
* @return The class as compilable Java code
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
StringBuilder s = new StringBuilder();
|
StringBuilder s = new StringBuilder();
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,9 @@ public class UMLClassView {
|
||||||
|
|
||||||
private UMLManager manager;
|
private UMLManager manager;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates the GUI for the application
|
||||||
|
*/
|
||||||
public UMLClassView() {
|
public UMLClassView() {
|
||||||
|
|
||||||
inputArea.addKeyListener(new KeyAdapter() {
|
inputArea.addKeyListener(new KeyAdapter() {
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,8 @@
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stars the UMLetClassParser
|
||||||
|
*/
|
||||||
public class UMLConverterMain {
|
public class UMLConverterMain {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
|
||||||
|
|
@ -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 {
|
public class UMLField {
|
||||||
|
|
||||||
private final String dataType;
|
private final String dataType;
|
||||||
|
|
@ -6,12 +9,6 @@ public class UMLField {
|
||||||
|
|
||||||
private boolean valid;
|
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
|
* New Field from UML line
|
||||||
*
|
*
|
||||||
|
|
@ -19,7 +16,7 @@ public class UMLField {
|
||||||
*/
|
*/
|
||||||
public UMLField(String line) {
|
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(";");
|
String[] formattedSplit = formatted.split(";");
|
||||||
|
|
||||||
this.encapsulation = switch (formattedSplit[0]) {
|
this.encapsulation = switch (formattedSplit[0]) {
|
||||||
|
|
@ -34,7 +31,10 @@ public class UMLField {
|
||||||
this.dataType = formattedSplit[1];
|
this.dataType = formattedSplit[1];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the field as the Java code representation
|
||||||
|
* @return The field as the Java code representation
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return " " + encapsulation + dataType + " " + name + ";\n";
|
return " " + encapsulation + dataType + " " + name + ";\n";
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,9 @@ import java.io.FileWriter;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Manages everything about the parsing
|
||||||
|
*/
|
||||||
public class UMLManager {
|
public class UMLManager {
|
||||||
|
|
||||||
private UMLClassView view;
|
private UMLClassView view;
|
||||||
|
|
@ -14,6 +17,11 @@ public class UMLManager {
|
||||||
view.setManager(this);
|
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) {
|
public void parseClasses(ArrayList<String> classesText, String packagePath) {
|
||||||
|
|
||||||
String packageString = packagePath.replaceAll(".*src\\\\(.*)", "$1");
|
String packageString = packagePath.replaceAll(".*src\\\\(.*)", "$1");
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,8 @@
|
||||||
import java.util.ArrayList;
|
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 {
|
public class UMLMethod {
|
||||||
|
|
||||||
private final String returnType;
|
private final String returnType;
|
||||||
|
|
@ -9,11 +12,17 @@ public class UMLMethod {
|
||||||
private final ArrayList<String> argsNames = new ArrayList<>();
|
private final ArrayList<String> argsNames = new ArrayList<>();
|
||||||
private final ArrayList<String> argsTypes = 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) {
|
public UMLMethod(String line, String className) {
|
||||||
|
|
||||||
//First, format it nicely
|
//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(";");
|
String[] parts = formatted.split(";");
|
||||||
this.encapsulation = switch (parts[0]) {
|
this.encapsulation = switch (parts[0]) {
|
||||||
case "+" -> "public ";
|
case "+" -> "public ";
|
||||||
|
|
@ -26,19 +35,19 @@ public class UMLMethod {
|
||||||
this.name = parts[2];
|
this.name = parts[2];
|
||||||
|
|
||||||
|
|
||||||
if(parts[1].equals("") && !className.equals(name)){
|
if (parts[1].equals("") && !className.equals(name)) {
|
||||||
this.returnType = "void ";
|
this.returnType = "void ";
|
||||||
} else {
|
} else {
|
||||||
this.returnType = parts[1] + " ";
|
this.returnType = parts[1] + " ";
|
||||||
}
|
}
|
||||||
|
|
||||||
if(parts.length == 4) {
|
if (parts.length == 4) {
|
||||||
String args = parts[3];
|
String args = parts[3];
|
||||||
String[] argsSplit = args.split(",");
|
String[] argsSplit = args.split(",");
|
||||||
|
|
||||||
for (String s : argsSplit) {
|
for (String s : argsSplit) {
|
||||||
if(s.matches(Regex.ARG_SPLIT_REGEX)) {
|
if (s.matches(Regex.ARG_SPLIT_REGEX.pattern())) {
|
||||||
String argFormatted = s.replaceAll(Regex.ARG_SPLIT_REGEX, "$1;$2");
|
String argFormatted = s.replaceAll(Regex.ARG_SPLIT_REGEX.pattern(), "$1;$2");
|
||||||
String[] formattedSplit = argFormatted.split(";");
|
String[] formattedSplit = argFormatted.split(";");
|
||||||
argsNames.add(formattedSplit[0]);
|
argsNames.add(formattedSplit[0]);
|
||||||
argsTypes.add(formattedSplit[1]);
|
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
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
StringBuilder returnString = new StringBuilder();
|
StringBuilder returnString = new StringBuilder();
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,9 @@ import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
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 {
|
public class XMLParser {
|
||||||
|
|
||||||
private Document doc;
|
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(){
|
public ArrayList<String> getClassesText(){
|
||||||
|
|
||||||
ArrayList<String> classes = new ArrayList<>();
|
ArrayList<String> classes = new ArrayList<>();
|
||||||
|
|
||||||
if(valid) {
|
if(valid) {
|
||||||
|
|
||||||
NodeList nList = doc.getDocumentElement().getElementsByTagName("element");
|
NodeList nList = doc.getDocumentElement().getElementsByTagName("element");
|
||||||
|
|
||||||
for (int i = 0; i < nList.getLength(); i++) {
|
for (int i = 0; i < nList.getLength(); i++) { Node node = nList.item(i);
|
||||||
Node node = nList.item(i);
|
|
||||||
|
|
||||||
if (node.getNodeType() == Node.ELEMENT_NODE) {
|
if (node.getNodeType() == Node.ELEMENT_NODE) {
|
||||||
|
|
||||||
Element element = (Element) node;
|
Element element = (Element) node;
|
||||||
|
|
||||||
if (element.getElementsByTagName("id").item(0).getTextContent().equals("UMLClass")) {
|
if (element.getElementsByTagName("id").item(0).getTextContent().equals("UMLClass")) {
|
||||||
|
|
||||||
String classBody = element.getElementsByTagName("panel_attributes").item(0).getTextContent();
|
String classBody = element.getElementsByTagName("panel_attributes").item(0).getTextContent();
|
||||||
classes.add(classBody);
|
classes.add(classBody);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue