From c24064c2c15394a902f6a47ddc7ed40203cc1b0e Mon Sep 17 00:00:00 2001 From: Nilstrieb Date: Thu, 19 Nov 2020 22:00:51 +0100 Subject: [PATCH] better regex and drag and drop --- .idea/artifacts/UMLClassParser_jar.xml | 8 ++++ src/META-INF/MANIFEST.MF | 3 ++ src/Regex.java | 55 ++++++++++++++++++++++++++ src/UMLClass.java | 4 +- src/UMLClassView.form | 4 +- src/UMLClassView.java | 32 +++++++++++++++ src/UMLField.java | 2 +- src/UMLMethod.java | 13 +++--- 8 files changed, 110 insertions(+), 11 deletions(-) create mode 100644 .idea/artifacts/UMLClassParser_jar.xml create mode 100644 src/META-INF/MANIFEST.MF create mode 100644 src/Regex.java diff --git a/.idea/artifacts/UMLClassParser_jar.xml b/.idea/artifacts/UMLClassParser_jar.xml new file mode 100644 index 0000000..fe3e856 --- /dev/null +++ b/.idea/artifacts/UMLClassParser_jar.xml @@ -0,0 +1,8 @@ + + + $PROJECT_DIR$/out/artifacts/UMLClassParser_jar + + + + + \ No newline at end of file diff --git a/src/META-INF/MANIFEST.MF b/src/META-INF/MANIFEST.MF new file mode 100644 index 0000000..5428727 --- /dev/null +++ b/src/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: UMLConverterMain + diff --git a/src/Regex.java b/src/Regex.java new file mode 100644 index 0000000..99b45ec --- /dev/null +++ b/src/Regex.java @@ -0,0 +1,55 @@ +public class Regex { + + /** + * Matches any method in the UML format, including a constructor + * + *

Examples:

+ * + * + *

Groups: + *

+ * + */ + public static final String METHOD_FIND_REGEX = " *(?[+\\-~#]) *(?\\w+) *\\( *(?(?: *\\w+ *: *\\w+ *,? *)*) *\\) *(?:: *(?\\w+))?"; + + /** + * Matches any Field in the UML format, including a constructor + * + *

Examples:

+ *
    + *
  • - age: int
  • + *
  • - name: String
  • + *
  • # date: LocalDate
  • + *
+ * + *

Groups: + *

    + *
  • 1 The encapsulate modifier (+-~#)
  • + *
  • 2 The name
  • + *
  • 3 The datatype
  • + *
+ * + */ + public static final String FIELD_FIND_REGEX = " *(?[+\\-~#]) *(?\\w+) *: *(?\\w+)"; + + /** + * Matches a single arg in a method + * + *

Groups: + *

    + *
  • 1 The name
  • + *
  • 2 The datatype
  • + *
+ */ + public static final String ARG_SPLIT_REGEX = " ?(\\w+): (\\w+)"; + +} diff --git a/src/UMLClass.java b/src/UMLClass.java index b5075f8..cacc554 100644 --- a/src/UMLClass.java +++ b/src/UMLClass.java @@ -21,9 +21,9 @@ public class UMLClass { for (String line : linesBeheaded) { if(line != null) { - if (line.matches("([+\\-~#]) ?(.+)\\((.*: .*,?)?\\):? ?(.+)?")) { //MATCHES METHOD + if (line.matches(Regex.METHOD_FIND_REGEX)) { //MATCHES METHOD methods.add(new UMLMethod(line, name)); - } else if (line.matches("([+\\-~#]) ?((?:[a-z]|[A-Z]|[0-1])+): (.*)")) { //MATCHES FIELD + } else if (line.matches(Regex.FIELD_FIND_REGEX)) { //MATCHES FIELD fields.add(new UMLField(line)); } } diff --git a/src/UMLClassView.form b/src/UMLClassView.form index b62c513..c6a73e1 100644 --- a/src/UMLClassView.form +++ b/src/UMLClassView.form @@ -1,7 +1,7 @@
- + @@ -30,7 +30,7 @@ - + diff --git a/src/UMLClassView.java b/src/UMLClassView.java index 47b3f1d..57141c4 100644 --- a/src/UMLClassView.java +++ b/src/UMLClassView.java @@ -1,8 +1,14 @@ import javax.swing.*; +import java.awt.datatransfer.DataFlavor; +import java.awt.dnd.DnDConstants; +import java.awt.dnd.DropTarget; +import java.awt.dnd.DropTargetDropEvent; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; +import java.io.File; +import java.util.List; public class UMLClassView { private JTextArea inputArea; @@ -33,6 +39,12 @@ public class UMLClassView { UMLClass umlClass = new UMLClass(text, ""); outputArea.setText(umlClass.toString()); } + @Override + public void keyReleased(KeyEvent e) { + String text = inputArea.getText(); + UMLClass umlClass = new UMLClass(text, ""); + outputArea.setText(umlClass.toString()); + } }); convertFileButton.addActionListener(new ActionListener() { @@ -44,6 +56,26 @@ public class UMLClassView { manager.parseClasses(parser.getClassesText(), packagePath); } }); + + pathField.setDropTarget(new DropTarget() { + @Override + public synchronized void drop(DropTargetDropEvent dtde) { + try { + dtde.acceptDrop(DnDConstants.ACTION_COPY); + List droppedFiles = (List) + dtde.getTransferable().getTransferData(DataFlavor.javaFileListFlavor); + for (File file : droppedFiles) { + String path = file.getAbsolutePath(); + pathField.setText(path); + String packagePath = packagePathField.getText(); + XMLParser parser = new XMLParser(path); + manager.parseClasses(parser.getClassesText(), packagePath); + } + } catch (Exception ex) { + ex.printStackTrace(); + } + } + }); } public JPanel getPanel1() { diff --git a/src/UMLField.java b/src/UMLField.java index 87e335e..34e4da0 100644 --- a/src/UMLField.java +++ b/src/UMLField.java @@ -19,7 +19,7 @@ public class UMLField { */ public UMLField(String line) { - String formatted = line.replaceAll("([+\\-~#]) ?((?:[a-z]|[A-Z]|[0-1])+): (.*)", "$1;$3;$2"); + String formatted = line.replaceAll(Regex.FIELD_FIND_REGEX, "$1;$3;$2"); String[] formattedSplit = formatted.split(";"); this.encapsulation = switch (formattedSplit[0]) { diff --git a/src/UMLMethod.java b/src/UMLMethod.java index 897b28d..1a535ce 100644 --- a/src/UMLMethod.java +++ b/src/UMLMethod.java @@ -1,5 +1,4 @@ import java.util.ArrayList; -import java.util.Arrays; public class UMLMethod { @@ -29,7 +28,7 @@ public class UMLMethod { * name; * args in the UML format */ - String formatted = line.replaceAll("([+\\-~#]) ?(.+)\\((.*: .*,?)?\\):? ?(.+)?", "$1;$4;$2;$3"); + String formatted = line.replaceAll(Regex.METHOD_FIND_REGEX, "$1;$4;$2;$3"); String[] parts = formatted.split(";"); this.encapsulation = switch (parts[0]) { case "+" -> "public "; @@ -53,10 +52,12 @@ public class UMLMethod { String[] argsSplit = args.split(","); for (String s : argsSplit) { - String argFormatted = s.replaceAll(" ?(.*): (.*)", "$1;$2"); - String[] formattedSplit = argFormatted.split(";"); - argsNames.add(formattedSplit[0]); - argsTypes.add(formattedSplit[1]); + if(s.matches(Regex.ARG_SPLIT_REGEX)) { + String argFormatted = s.replaceAll(Regex.ARG_SPLIT_REGEX, "$1;$2"); + String[] formattedSplit = argFormatted.split(";"); + argsNames.add(formattedSplit[0]); + argsTypes.add(formattedSplit[1]); + } } } }