-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
86 lines (75 loc) · 2.38 KB
/
Main.java
File metadata and controls
86 lines (75 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.io.IOException;
import java.util.Scanner;
import file_reorder.*;
public class Main {
public static void main(String[] args) {
// System.out.println("File name: ");
// Scanner sc = new Scanner(System.in);
// String filename = sc.nextLine();
String filename = args[0];
try {
// Checking if file has the correct format
Checker.checkName(filename);
File file = new File(filename);
// Checking if file has content
Scanner reader = new Scanner(file);
Checker.checkContent(file.length());
// Creating an arraylist for starting and ending lines and for containers
ArrayList<String> fixed = new ArrayList<>();
ArrayList<Container> containers = new ArrayList<>();
int before = 0;
while (reader.hasNextLine()) {
String tmp = reader.nextLine();
// If the next lines include a container
if (tmp.contains("CONTAINER")) {
String id = tmp;
String shortn = reader.nextLine();
String longn = reader.nextLine();
if (Container.getEnd() == null)
Container.setEnd(reader.nextLine());
else
reader.nextLine();
containers.add(new Container(id, shortn, longn));
} else {
// if it's a starting/ending line
fixed.add(tmp);
if (containers.size() == 0) {
before++;
}
}
}
Collections.sort(containers);
//Gather all the data into one string called fileContent
String fileContent = "";
for (int i = 0; i < before; i++) {
fileContent += fixed.get(i) + '\n';
}
for (int i = 0; i < containers.size(); i++) {
fileContent += containers.get(i).getID() + '\n';
fileContent += containers.get(i).getShortName() + '\n';
fileContent += containers.get(i).getLongName() + '\n';
fileContent += Container.getEnd() + '\n';
}
for (int i = before; i < fixed.size(); i++) {
fileContent += fixed.get(i);
if (i < fixed.size() - 1)
fileContent += "\n";
}
//New file name
String modFileName = filename.substring(0, filename.lastIndexOf(".arxml")) + "_mod.arxml";
FileWriter writer = new FileWriter(modFileName);
writer.write(fileContent);
writer.close();
} catch (EmptyAutosarFileException e) {
System.out.println(e);
} catch (NotVaildAutosarFileException e) {
System.out.println(e);
} catch (IOException e) {
System.out.println(e);
}
}
}