1 | package org.greenstone.gsdl3.gs3build.doctypes;
|
---|
2 |
|
---|
3 | import java.util.List;
|
---|
4 | import java.util.ArrayList;
|
---|
5 |
|
---|
6 | import java.io.PrintWriter;
|
---|
7 | import java.io.FileWriter;
|
---|
8 | import java.io.File;
|
---|
9 | import java.io.IOException;
|
---|
10 |
|
---|
11 | import org.greenstone.gsdl3.gs3build.util.GS3SQLConnection;
|
---|
12 |
|
---|
13 | public class DocumentList
|
---|
14 | {
|
---|
15 | DocumentInterface [] list;
|
---|
16 | int size;
|
---|
17 | int used;
|
---|
18 | DocumentIDFactoryInterface idFactory;
|
---|
19 |
|
---|
20 | public DocumentList(DocumentIDFactoryInterface idFactory)
|
---|
21 | { this.idFactory = idFactory;
|
---|
22 | this.list = new DocumentInterface[10];
|
---|
23 | this.used = 0;
|
---|
24 | this.size = 10;
|
---|
25 | }
|
---|
26 |
|
---|
27 | public void addDocument(DocumentInterface document)
|
---|
28 | { if (this.used == this.size) {
|
---|
29 | this.ensureSize(this.size * 2);
|
---|
30 | }
|
---|
31 | this.list[this.used] = document;
|
---|
32 | DocumentID id = this.idFactory.getNewDocumentID(document);
|
---|
33 | document.setID(id);
|
---|
34 | this.used ++;
|
---|
35 | }
|
---|
36 |
|
---|
37 | public DocumentInterface getDocument(int index)
|
---|
38 | { if (index < 0 || index >= this.used)
|
---|
39 | { return null;
|
---|
40 | }
|
---|
41 | return this.list[index];
|
---|
42 | }
|
---|
43 |
|
---|
44 | public DocumentID getDocumentID(int index)
|
---|
45 | { if (index < 0 || index >= this.used)
|
---|
46 | { return null;
|
---|
47 | }
|
---|
48 | return this.list[index].getID();
|
---|
49 | }
|
---|
50 |
|
---|
51 | public void ensureSize(int size)
|
---|
52 | { DocumentInterface [] newList = new DocumentInterface[size];
|
---|
53 | System.arraycopy(this.list, 0, newList, 0, this.size);
|
---|
54 | this.list = newList;
|
---|
55 | this.size = size;
|
---|
56 | }
|
---|
57 |
|
---|
58 | public void writeDocuments(File directory)
|
---|
59 | { for (int i = 0; i < this.used; i ++)
|
---|
60 | { try
|
---|
61 | {
|
---|
62 | File localFile = new File(directory, "Doc"+Integer.toString(i)+".xml");
|
---|
63 | FileWriter fileWriter = new FileWriter(localFile);
|
---|
64 | PrintWriter writer = new PrintWriter(fileWriter);
|
---|
65 | this.list[i].getMETSWriter().writeDocument(this.list[i], writer);
|
---|
66 | writer.close();
|
---|
67 | fileWriter.close();
|
---|
68 | }
|
---|
69 | catch (IOException io)
|
---|
70 | {
|
---|
71 | }
|
---|
72 | }
|
---|
73 | }
|
---|
74 |
|
---|
75 | public void writeSQLDocuments(GS3SQLConnection connection)
|
---|
76 | { for (int i = 0; i < this.used; i ++)
|
---|
77 | { this.list[i].getSQLWriter().writeDocument(this.list[i], connection);
|
---|
78 | }
|
---|
79 | }
|
---|
80 |
|
---|
81 | public int size()
|
---|
82 | { return this.used;
|
---|
83 | }
|
---|
84 | }
|
---|