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