1 | package org.greenstone.gsdl3.gs3build.doctypes;
|
---|
2 |
|
---|
3 | import java.util.ArrayList;
|
---|
4 | import java.net.URL;
|
---|
5 | import org.w3c.dom.Element;
|
---|
6 |
|
---|
7 | import org.greenstone.gsdl3.gs3build.metadata.*;
|
---|
8 | import org.greenstone.gsdl3.gs3build.util.HTTPTools;
|
---|
9 | /**
|
---|
10 | A simple base class for Recognisers
|
---|
11 | A new recogniser should set preferred_mime_type, filename_extensions, and document_type in the constructor.
|
---|
12 | The DocumentFactory class needs to be modified to map the documentType to
|
---|
13 | the corresponding document
|
---|
14 | And thats all for simple recognisers.
|
---|
15 | More complicated recognisers can just implement from scratch RecogniserInterface
|
---|
16 | */
|
---|
17 | abstract public class AbstractRecogniser implements RecogniserInterface
|
---|
18 | {
|
---|
19 |
|
---|
20 | DocumentList list_repository = null;
|
---|
21 | String preferred_mime_type = "SET THIS IN THE CONCRETE CLASS";
|
---|
22 | ArrayList filename_extensions = null;
|
---|
23 | String document_type = "SET THIS IN THE CONCRETE CLASS";
|
---|
24 |
|
---|
25 | /** The constructor should set the variables
|
---|
26 | * preferredMimeType, filename_extensions and documentType
|
---|
27 | */
|
---|
28 | public AbstractRecogniser() {
|
---|
29 |
|
---|
30 | }
|
---|
31 |
|
---|
32 | /** configure by default does nothing */
|
---|
33 | public boolean configure(Element config_elem){
|
---|
34 | return true;
|
---|
35 | }
|
---|
36 | public void setListRepository(DocumentList docList) {
|
---|
37 | this.list_repository = docList;
|
---|
38 | }
|
---|
39 |
|
---|
40 | protected boolean isAcceptedFilename(String filename) {
|
---|
41 | if (filename_extensions == null) {
|
---|
42 | return false; // or should this be true??
|
---|
43 | }
|
---|
44 | for (int i=0; i<filename_extensions.size(); i++) {
|
---|
45 | if (filename.endsWith((String)filename_extensions.get(i))) {
|
---|
46 | return true;
|
---|
47 | }
|
---|
48 | }
|
---|
49 | return false;
|
---|
50 | }
|
---|
51 |
|
---|
52 | public boolean parseDocument(METSFile file)
|
---|
53 | {
|
---|
54 | String MIMEType = file.getMIMEType();
|
---|
55 | if (MIMEType == null ||
|
---|
56 | MIMEType.equals(this.preferred_mime_type)) {
|
---|
57 | URL location = file.getLocation();
|
---|
58 | return this.parseDocument(location);
|
---|
59 | }
|
---|
60 | return false;
|
---|
61 | }
|
---|
62 |
|
---|
63 | public boolean parseDocument(URL url)
|
---|
64 | {
|
---|
65 | String filename = null;
|
---|
66 |
|
---|
67 | if (url.getProtocol().equals("file")) {
|
---|
68 | filename = url.getPath();
|
---|
69 | }
|
---|
70 |
|
---|
71 | if (filename != null) {
|
---|
72 | if ( isAcceptedFilename(filename)) {
|
---|
73 |
|
---|
74 | System.out.println("Posting "+document_type+" Document " + filename);
|
---|
75 | AbstractDocument doc = DocumentFactory.createDocument(document_type, url);
|
---|
76 | this.list_repository.addDocument(doc);
|
---|
77 | return true;
|
---|
78 | }
|
---|
79 | } else {
|
---|
80 | // Get Mime type remotely, and then proceed if required
|
---|
81 | String mimeType = HTTPTools.getMIMEType(url);
|
---|
82 |
|
---|
83 | if (mimeType == this.preferred_mime_type) {
|
---|
84 | System.out.println("Posting "+document_type+" Document " + url.toString());
|
---|
85 |
|
---|
86 | AbstractDocument doc = DocumentFactory.createDocument(document_type, url);
|
---|
87 | this.list_repository.addDocument(doc);
|
---|
88 | return true;
|
---|
89 | }
|
---|
90 | }
|
---|
91 | return false;
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 |
|
---|