mirror of
https://github.com/Noratrieb/UMLetClassParser.git
synced 2026-01-14 16:45:05 +01:00
working
This commit is contained in:
parent
5b0937a8eb
commit
501fa2f293
6 changed files with 66 additions and 16 deletions
|
|
@ -3,11 +3,15 @@ import java.util.ArrayList;
|
||||||
public class UMLClass {
|
public class UMLClass {
|
||||||
|
|
||||||
private String name;
|
private String name;
|
||||||
|
private String packageString;
|
||||||
|
|
||||||
private ArrayList<UMLField> fields = new ArrayList<>();
|
private ArrayList<UMLField> fields = new ArrayList<>();
|
||||||
private ArrayList<UMLMethod> methods = 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[] lines = classDiagram.split("\n");
|
||||||
|
|
||||||
String[] linesBeheaded = new String[lines.length-1];
|
String[] linesBeheaded = new String[lines.length-1];
|
||||||
|
|
@ -17,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("([+\\-~#]) ?(.+)\\((.*: .*,?)?\\):? ?(.+)?")) { //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("([+\\-~#]) ?((?:[a-z]|[A-Z]|[0-1])+): (.*)")) { //MATCHES FIELD
|
||||||
fields.add(new UMLField(line));
|
fields.add(new UMLField(line));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -29,6 +33,9 @@ public class UMLClass {
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
StringBuilder s = new StringBuilder();
|
StringBuilder s = new StringBuilder();
|
||||||
|
if(!packageString.equals("")){
|
||||||
|
s.append("package ").append(packageString).append(";\n\n");
|
||||||
|
}
|
||||||
s.append("public class ").append(name).append(" {\n\n");
|
s.append("public class ").append(name).append(" {\n\n");
|
||||||
|
|
||||||
for (UMLField field : fields){
|
for (UMLField field : fields){
|
||||||
|
|
@ -45,4 +52,8 @@ public class UMLClass {
|
||||||
|
|
||||||
return s.toString();
|
return s.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,7 @@
|
||||||
<properties/>
|
<properties/>
|
||||||
<border type="none"/>
|
<border type="none"/>
|
||||||
<children>
|
<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"/>
|
<margin top="0" left="0" bottom="0" right="0"/>
|
||||||
<constraints>
|
<constraints>
|
||||||
<tabbedpane title="File Mode"/>
|
<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"/>
|
<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="Path:"/>
|
<text value=".uxf File Path:"/>
|
||||||
</properties>
|
</properties>
|
||||||
</component>
|
</component>
|
||||||
<vspacer id="8c974">
|
<vspacer id="8c974">
|
||||||
<constraints>
|
<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>
|
</constraints>
|
||||||
</vspacer>
|
</vspacer>
|
||||||
<component id="a9bfd" class="javax.swing.JTextField" binding="pathField">
|
<component id="a9bfd" class="javax.swing.JTextField" binding="pathField">
|
||||||
|
|
@ -48,12 +48,28 @@
|
||||||
</component>
|
</component>
|
||||||
<component id="fa74" class="javax.swing.JButton" binding="convertFileButton">
|
<component id="fa74" class="javax.swing.JButton" binding="convertFileButton">
|
||||||
<constraints>
|
<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>
|
</constraints>
|
||||||
<properties>
|
<properties>
|
||||||
<text value="convert"/>
|
<text value="convert"/>
|
||||||
</properties>
|
</properties>
|
||||||
</component>
|
</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>
|
</children>
|
||||||
</grid>
|
</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">
|
<grid id="425fa" layout-manager="GridLayoutManager" row-count="3" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,7 @@ public class UMLClassView {
|
||||||
private JTextArea outputArea;
|
private JTextArea outputArea;
|
||||||
private JTextField pathField;
|
private JTextField pathField;
|
||||||
private JButton convertFileButton;
|
private JButton convertFileButton;
|
||||||
|
private JTextField packagePathField;
|
||||||
|
|
||||||
private UMLManager manager;
|
private UMLManager manager;
|
||||||
|
|
||||||
|
|
@ -20,7 +21,7 @@ public class UMLClassView {
|
||||||
convertButton.addActionListener(e -> {
|
convertButton.addActionListener(e -> {
|
||||||
String text = inputArea.getText();
|
String text = inputArea.getText();
|
||||||
|
|
||||||
UMLClass umlClass = new UMLClass(text);
|
UMLClass umlClass = new UMLClass(text, "");
|
||||||
|
|
||||||
outputArea.setText(umlClass.toString());
|
outputArea.setText(umlClass.toString());
|
||||||
});
|
});
|
||||||
|
|
@ -29,7 +30,7 @@ public class UMLClassView {
|
||||||
@Override
|
@Override
|
||||||
public void keyPressed(KeyEvent e) {
|
public void keyPressed(KeyEvent e) {
|
||||||
String text = inputArea.getText();
|
String text = inputArea.getText();
|
||||||
UMLClass umlClass = new UMLClass(text);
|
UMLClass umlClass = new UMLClass(text, "");
|
||||||
outputArea.setText(umlClass.toString());
|
outputArea.setText(umlClass.toString());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
@ -38,8 +39,9 @@ public class UMLClassView {
|
||||||
@Override
|
@Override
|
||||||
public void actionPerformed(ActionEvent e) {
|
public void actionPerformed(ActionEvent e) {
|
||||||
String path = pathField.getText();
|
String path = pathField.getText();
|
||||||
|
String packagePath = packagePathField.getText();
|
||||||
XMLParser parser = new XMLParser(path);
|
XMLParser parser = new XMLParser(path);
|
||||||
manager.parseClasses(parser.getClassesText());
|
manager.parseClasses(parser.getClassesText(), packagePath);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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("([+\\-~#]) ?((?:[a-z]|[A-Z]|[0-1])+): (.*)", "$1;$3;$2");
|
||||||
String[] formattedSplit = formatted.split(";");
|
String[] formattedSplit = formatted.split(";");
|
||||||
|
|
||||||
this.encapsulation = switch (formattedSplit[0]) {
|
this.encapsulation = switch (formattedSplit[0]) {
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,7 @@
|
||||||
|
import java.io.BufferedWriter;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileWriter;
|
||||||
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
public class UMLManager {
|
public class UMLManager {
|
||||||
|
|
@ -9,13 +13,30 @@ public class UMLManager {
|
||||||
view.setManager(this);
|
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<>();
|
ArrayList<UMLClass> classes = new ArrayList<>();
|
||||||
|
|
||||||
for (String text : classesText) {
|
for (String text : classesText) {
|
||||||
classes.add(new UMLClass(text));
|
classes.add(new UMLClass(text, packageString));
|
||||||
}
|
}
|
||||||
|
|
||||||
classes.forEach(e -> System.out.println(e.toString()));
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -29,7 +29,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("([+\\-~#]) ?(.+)\\((.*: .*,?)?\\):? ?(.+)?", "$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 ";
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue