Skip to content

Commit a6327d4

Browse files
xml importation in beta...
bug in table.java line 520
1 parent 06e1a6a commit a6327d4

File tree

2 files changed

+113
-5
lines changed

2 files changed

+113
-5
lines changed

src/Main.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
import anon.database.*;
22
import anon.database.connect.Connection;
3-
import anon.database.exceptions.ColumnIndexOutOfBoundException;
4-
import anon.database.exceptions.DatabaseConnectionException;
5-
import anon.database.exceptions.NoTableFoundException;
6-
import anon.database.exceptions.TableCreationOutOfBoundException;
3+
import anon.database.exceptions.*;
74

85
import java.io.File;
96
import java.io.IOException;
107

118

129
public class Main {
13-
public static void main(String[] args) throws IOException, TableCreationOutOfBoundException, ColumnIndexOutOfBoundException, DatabaseConnectionException, NoTableFoundException {
10+
public static void main(String[] args) throws IOException, TableCreationOutOfBoundException, ColumnIndexOutOfBoundException, DatabaseConnectionException, NoTableFoundException, FileTypeNotSupportedException {
1411

1512
Database maindb = null;
1613
Table login_tb = null;
@@ -24,5 +21,6 @@ public static void main(String[] args) throws IOException, TableCreationOutOfBou
2421
}
2522

2623
System.out.println(login_tb.exportToXML(new File("C:/Users/Gaurav/Desktop/data.xml")));
24+
System.out.println(login_tb.importInANDB("C:/Users/Gaurav/Desktop/data.xml"));
2725
}
2826
}

src/anon/database/Table.java

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package anon.database;
22

3+
import anon.database.connect.Connection;
34
import anon.database.exceptions.ColumnIndexOutOfBoundException;
5+
import anon.database.exceptions.FileTypeNotSupportedException;
46
import anon.database.exceptions.TableCreationOutOfBoundException;
57
import java.io.*;
68
import java.util.ArrayList;
@@ -16,6 +18,14 @@ public class Table {
1618
private int noOfColumns;
1719
private String[] columnNames;
1820
private HashMap<String,Integer> colInfo = new HashMap<>();
21+
private ArrayList<String> columnArrayList=new ArrayList<>();
22+
private ArrayList<String> rowArrayList=new ArrayList<>();
23+
private BufferedReader importedFileReader;
24+
private BufferedWriter exportFileWriter;
25+
private Integer columnItemCounter=0;
26+
private Integer counterForANDB=0;
27+
private Integer rCounter=0;
28+
private Integer rowCounter=0;
1929

2030

2131
// Constructor
@@ -489,4 +499,104 @@ public String toString() {
489499
}
490500
return "\n\n"+super.toString();
491501
}
502+
503+
public boolean importInANDB(String pathname) throws FileTypeNotSupportedException, IOException {
504+
Boolean status=false;
505+
final String XML_FILE=".xml";
506+
final String JSON_FILE=".json";
507+
final String CSV_FILE=".csv";
508+
String lineReader=new String();
509+
if(pathname.contains(XML_FILE)){
510+
final String COLUMN_OPEN_TAG="<Column key=";
511+
final String COLUMN_CLOSE_TAG="</Column>";
512+
final String ROW_OPEN_TAG="<Row key";
513+
final String ROW_CLOSE_TAG="</Row>";
514+
final String CLOSE_BRACKET=">";
515+
final String OPEN_DATA_TAG="<data>";
516+
final String CLOSE_DATA_TAG="</data>";
517+
importedFileReader=new BufferedReader(new FileReader(new File(pathname)));
518+
while (importedFileReader.readLine()!=null){
519+
lineReader=importedFileReader.readLine();
520+
System.out.println("It works");
521+
if (lineReader.contains(COLUMN_OPEN_TAG)){
522+
columnArrayList.add(lineReader.substring(lineReader.indexOf(COLUMN_OPEN_TAG)+1,lineReader.indexOf(CLOSE_BRACKET)));
523+
columnItemCounter++;
524+
while (!lineReader.equals(COLUMN_CLOSE_TAG)){
525+
columnArrayList.add(lineReader.substring(lineReader.indexOf(OPEN_DATA_TAG)+1,lineReader.indexOf(CLOSE_DATA_TAG)));
526+
columnItemCounter++;
527+
}
528+
}
529+
else if (lineReader.contains(ROW_OPEN_TAG)){
530+
rowArrayList.add(lineReader.substring(lineReader.indexOf(ROW_OPEN_TAG)+1,lineReader.indexOf(CLOSE_BRACKET)));
531+
while (!lineReader.equals(ROW_CLOSE_TAG)){
532+
rowArrayList.add(lineReader.substring(lineReader.indexOf(OPEN_DATA_TAG)+1,lineReader.indexOf(CLOSE_DATA_TAG)));
533+
}
534+
}
535+
}
536+
importedFileReader.close();
537+
String dbLocation= Connection.dbLocation;
538+
status=forDataWriting(dbLocation);
539+
}
540+
else if (pathname.contains(JSON_FILE)){
541+
542+
}
543+
else if (pathname.contains(CSV_FILE)){
544+
545+
}
546+
else {
547+
throw new FileTypeNotSupportedException();
548+
}
549+
return status;
550+
}
551+
private boolean forDataWriting(String dbPath) throws IOException {
552+
boolean status=false;
553+
File andbFile=new File(dbPath+"export"+counterForANDB+".andb");
554+
counterForANDB++;
555+
status=columnWriter(andbFile);
556+
if (status){
557+
status=rowWriter(andbFile);
558+
if (status){
559+
columnArrayList.clear();
560+
rowArrayList.clear();
561+
}
562+
}
563+
564+
return status;
565+
}
566+
567+
private boolean rowWriter(File andbFile) throws IOException {
568+
boolean status=false;
569+
final String ROW_INDICATOR="ȸ";
570+
exportFileWriter=new BufferedWriter(new FileWriter(andbFile,true));
571+
exportFileWriter.write(ROW_INDICATOR);
572+
for (int i=rCounter;i<=rowArrayList.size();i++){
573+
rCounter++;
574+
rowCounter++;
575+
if (rowCounter<=columnItemCounter){
576+
exportFileWriter.write(i+ROW_INDICATOR);
577+
}
578+
else {
579+
exportFileWriter.newLine();
580+
exportFileWriter.close();
581+
rowCounter=0;
582+
rowWriter(andbFile);
583+
}
584+
}
585+
status=true;
586+
return status;
587+
}
588+
589+
private boolean columnWriter(File andbFile) throws IOException {
590+
boolean status=false;
591+
final String COLUMN_INDICATOR="¤";
592+
exportFileWriter=new BufferedWriter(new FileWriter(andbFile));
593+
exportFileWriter.write(COLUMN_INDICATOR);
594+
for (int i=0;i<=columnArrayList.size();i++){
595+
exportFileWriter.write(i+COLUMN_INDICATOR);
596+
}
597+
exportFileWriter.newLine();
598+
exportFileWriter.close();
599+
status=true;
600+
return status;
601+
}
492602
}

0 commit comments

Comments
 (0)