1
1
package anon .database ;
2
2
3
+ import anon .database .connect .Connection ;
3
4
import anon .database .exceptions .ColumnIndexOutOfBoundException ;
5
+ import anon .database .exceptions .FileTypeNotSupportedException ;
4
6
import anon .database .exceptions .TableCreationOutOfBoundException ;
5
7
import java .io .*;
6
8
import java .util .ArrayList ;
@@ -16,6 +18,14 @@ public class Table {
16
18
private int noOfColumns ;
17
19
private String [] columnNames ;
18
20
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 ;
19
29
20
30
21
31
// Constructor
@@ -489,4 +499,104 @@ public String toString() {
489
499
}
490
500
return "\n \n " +super .toString ();
491
501
}
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
+ }
492
602
}
0 commit comments