This commit is contained in:
nora 2020-11-19 20:05:35 +01:00
parent 5b0937a8eb
commit 501fa2f293
6 changed files with 66 additions and 16 deletions

View file

@ -3,11 +3,15 @@ import java.util.ArrayList;
public class UMLClass {
private String name;
private String packageString;
private ArrayList<UMLField> fields = new ArrayList<>();
private ArrayList<UMLMethod> methods = new ArrayList<>();
public UMLClass(String classDiagram) {
public UMLClass(String classDiagram, String packageString) {
this.packageString = packageString;
String[] lines = classDiagram.split("\n");
String[] linesBeheaded = new String[lines.length-1];
@ -17,9 +21,9 @@ public class UMLClass {
for (String line : linesBeheaded) {
if(line != null) {
if (line.matches("([+\\-~#]) (.+)\\((.*: .*,?)?\\):? ?(.+)?")) { //MATCHES METHOD
if (line.matches("([+\\-~#]) ?(.+)\\((.*: .*,?)?\\):? ?(.+)?")) { //MATCHES METHOD
methods.add(new UMLMethod(line, name));
} else if (line.matches("([+\\-~#]) ((?:[a-z]|[A-Z]|[0-1])+): (.*)")) { //MATCHES FIELD
} else if (line.matches("([+\\-~#]) ?((?:[a-z]|[A-Z]|[0-1])+): (.*)")) { //MATCHES FIELD
fields.add(new UMLField(line));
}
}
@ -29,6 +33,9 @@ public class UMLClass {
@Override
public String toString() {
StringBuilder s = new StringBuilder();
if(!packageString.equals("")){
s.append("package ").append(packageString).append(";\n\n");
}
s.append("public class ").append(name).append(" {\n\n");
for (UMLField field : fields){
@ -45,4 +52,8 @@ public class UMLClass {
return s.toString();
}
public String getName() {
return name;
}
}

View file

@ -17,7 +17,7 @@
<properties/>
<border type="none"/>
<children>
<grid id="fc27a" layout-manager="GridLayoutManager" row-count="2" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="fc27a" layout-manager="GridLayoutManager" row-count="3" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<tabbedpane title="File Mode"/>
@ -30,12 +30,12 @@
<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>
<properties>
<text value="Path:"/>
<text value=".uxf File Path:"/>
</properties>
</component>
<vspacer id="8c974">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<component id="a9bfd" class="javax.swing.JTextField" binding="pathField">
@ -48,12 +48,28 @@
</component>
<component id="fa74" class="javax.swing.JButton" binding="convertFileButton">
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
<grid row="2" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="convert"/>
</properties>
</component>
<component id="17a65" class="javax.swing.JLabel">
<constraints>
<grid row="1" 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>
<properties>
<text value="Package Destination Path"/>
</properties>
</component>
<component id="96709" class="javax.swing.JTextField" binding="packagePathField">
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties/>
</component>
</children>
</grid>
<grid id="425fa" layout-manager="GridLayoutManager" row-count="3" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">

View file

@ -11,6 +11,7 @@ public class UMLClassView {
private JTextArea outputArea;
private JTextField pathField;
private JButton convertFileButton;
private JTextField packagePathField;
private UMLManager manager;
@ -20,7 +21,7 @@ public class UMLClassView {
convertButton.addActionListener(e -> {
String text = inputArea.getText();
UMLClass umlClass = new UMLClass(text);
UMLClass umlClass = new UMLClass(text, "");
outputArea.setText(umlClass.toString());
});
@ -29,7 +30,7 @@ public class UMLClassView {
@Override
public void keyPressed(KeyEvent e) {
String text = inputArea.getText();
UMLClass umlClass = new UMLClass(text);
UMLClass umlClass = new UMLClass(text, "");
outputArea.setText(umlClass.toString());
}
});
@ -38,8 +39,9 @@ public class UMLClassView {
@Override
public void actionPerformed(ActionEvent e) {
String path = pathField.getText();
String packagePath = packagePathField.getText();
XMLParser parser = new XMLParser(path);
manager.parseClasses(parser.getClassesText());
manager.parseClasses(parser.getClassesText(), packagePath);
}
});
}

View file

@ -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("([+\\-~#]) ?((?:[a-z]|[A-Z]|[0-1])+): (.*)", "$1;$3;$2");
String[] formattedSplit = formatted.split(";");
this.encapsulation = switch (formattedSplit[0]) {

View file

@ -1,3 +1,7 @@
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
public class UMLManager {
@ -9,13 +13,30 @@ public class UMLManager {
view.setManager(this);
}
public void parseClasses(ArrayList<String> classesText){
public void parseClasses(ArrayList<String> classesText, String packagePath) {
String packageString = packagePath.replaceAll(".*src\\\\(.*)", "$1");
packageString = packageString.replaceAll("\\\\", ".");
ArrayList<UMLClass> classes = new ArrayList<>();
for (String text : classesText) {
classes.add(new UMLClass(text));
classes.add(new UMLClass(text, packageString));
}
classes.forEach(e -> System.out.println(e.toString()));
for (UMLClass c : classes) {
try {
String path = packagePath + "/" + c.getName() + ".java";
FileWriter fw = new FileWriter(path);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(c.toString());
bw.close();
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

View file

@ -29,7 +29,7 @@ public class UMLMethod {
* name;
* args in the UML format
*/
String formatted = line.replaceAll("([+\\-~#]) (.+)\\((.*: .*,?)?\\):? ?(.+)?", "$1;$4;$2;$3");
String formatted = line.replaceAll("([+\\-~#]) ?(.+)\\((.*: .*,?)?\\):? ?(.+)?", "$1;$4;$2;$3");
String[] parts = formatted.split(";");
this.encapsulation = switch (parts[0]) {
case "+" -> "public ";