better regex and drag and drop

This commit is contained in:
nora 2020-11-19 22:00:51 +01:00
parent 501fa2f293
commit c24064c2c1
8 changed files with 110 additions and 11 deletions

8
.idea/artifacts/UMLClassParser_jar.xml generated Normal file
View file

@ -0,0 +1,8 @@
<component name="ArtifactManager">
<artifact type="jar" name="UMLClassParser:jar">
<output-path>$PROJECT_DIR$/out/artifacts/UMLClassParser_jar</output-path>
<root id="archive" name="UMLClassParser.jar">
<element id="module-output" name="UMLClassParser" />
</root>
</artifact>
</component>

3
src/META-INF/MANIFEST.MF Normal file
View file

@ -0,0 +1,3 @@
Manifest-Version: 1.0
Main-Class: UMLConverterMain

55
src/Regex.java Normal file
View file

@ -0,0 +1,55 @@
public class Regex {
/**
* Matches any method in the UML format, including a constructor
*
* <p>Examples:</p>
* <ul>
* <li>+ doStuff(foo: int, foo: int): int</li>
* <li>+ getFoo(): int</li>
* <li>- work()</li>
* </ul>
*
* <p> Groups:
* <ul>
* <li>1 The encapsulate modifier (+-~#)</li>
* <li>2 The name</li>
* <li>3 All Arguments</li>
* <li>4 The return type, "" if no return type is specified (void)</li>
* </ul>
*
*/
public static final String METHOD_FIND_REGEX = " *(?<capsule>[+\\-~#]) *(?<name>\\w+) *\\( *(?<args>(?: *\\w+ *: *\\w+ *,? *)*) *\\) *(?:: *(?<return>\\w+))?";
/**
* Matches any Field in the UML format, including a constructor
*
* <p>Examples:</p>
* <ul>
* <li>- age: int</li>
* <li>- name: String</li>
* <li># date: LocalDate</li>
* </ul>
*
* <p> Groups:
* <ul>
* <li>1 The encapsulate modifier (+-~#)</li>
* <li>2 The name</li>
* <li>3 The datatype</li>
* </ul>
*
*/
public static final String FIELD_FIND_REGEX = " *(?<capsule>[+\\-~#]) *(?<name>\\w+) *: *(?<type>\\w+)";
/**
* Matches a single arg in a method
*
* <p> Groups:
* <ul>
* <li>1 The name</li>
* <li>2 The datatype</li>
* </ul>
*/
public static final String ARG_SPLIT_REGEX = " ?(\\w+): (\\w+)";
}

View file

@ -21,9 +21,9 @@ public class UMLClass {
for (String line : linesBeheaded) { for (String line : linesBeheaded) {
if(line != null) { if(line != null) {
if (line.matches("([+\\-~#]) ?(.+)\\((.*: .*,?)?\\):? ?(.+)?")) { //MATCHES METHOD if (line.matches(Regex.METHOD_FIND_REGEX)) { //MATCHES METHOD
methods.add(new UMLMethod(line, name)); 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)); fields.add(new UMLField(line));
} }
} }

View file

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="UMLClassView"> <form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="UMLClassView">
<grid id="27dc6" binding="panel1" default-binding="true" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1"> <grid id="27dc6" binding="panel1" default-binding="true" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/> <margin top="3" left="3" bottom="3" right="3"/>
<constraints> <constraints>
<xy x="20" y="20" width="500" height="400"/> <xy x="20" y="20" width="500" height="400"/>
</constraints> </constraints>
@ -30,7 +30,7 @@
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/> <grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints> </constraints>
<properties> <properties>
<text value=".uxf File Path:"/> <text value=" .uxf File Path:"/>
</properties> </properties>
</component> </component>
<vspacer id="8c974"> <vspacer id="8c974">

View file

@ -1,8 +1,14 @@
import javax.swing.*; 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.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter; import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent; import java.awt.event.KeyEvent;
import java.io.File;
import java.util.List;
public class UMLClassView { public class UMLClassView {
private JTextArea inputArea; private JTextArea inputArea;
@ -33,6 +39,12 @@ public class UMLClassView {
UMLClass umlClass = new UMLClass(text, ""); UMLClass umlClass = new UMLClass(text, "");
outputArea.setText(umlClass.toString()); 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() { convertFileButton.addActionListener(new ActionListener() {
@ -44,6 +56,26 @@ public class UMLClassView {
manager.parseClasses(parser.getClassesText(), packagePath); manager.parseClasses(parser.getClassesText(), packagePath);
} }
}); });
pathField.setDropTarget(new DropTarget() {
@Override
public synchronized void drop(DropTargetDropEvent dtde) {
try {
dtde.acceptDrop(DnDConstants.ACTION_COPY);
List<File> droppedFiles = (List<File>)
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() { public JPanel getPanel1() {

View file

@ -19,7 +19,7 @@ public class UMLField {
*/ */
public UMLField(String line) { 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(";"); String[] formattedSplit = formatted.split(";");
this.encapsulation = switch (formattedSplit[0]) { this.encapsulation = switch (formattedSplit[0]) {

View file

@ -1,5 +1,4 @@
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
public class UMLMethod { public class UMLMethod {
@ -29,7 +28,7 @@ public class UMLMethod {
* name; * name;
* args in the UML format * 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(";"); String[] parts = formatted.split(";");
this.encapsulation = switch (parts[0]) { this.encapsulation = switch (parts[0]) {
case "+" -> "public "; case "+" -> "public ";
@ -53,13 +52,15 @@ public class UMLMethod {
String[] argsSplit = args.split(","); String[] argsSplit = args.split(",");
for (String s : argsSplit) { for (String s : argsSplit) {
String argFormatted = s.replaceAll(" ?(.*): (.*)", "$1;$2"); if(s.matches(Regex.ARG_SPLIT_REGEX)) {
String argFormatted = s.replaceAll(Regex.ARG_SPLIT_REGEX, "$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]);
} }
} }
} }
}
@Override @Override
public String toString() { public String toString() {