-
Notifications
You must be signed in to change notification settings - Fork 0
/
WordResource.java
132 lines (113 loc) · 4 KB
/
WordResource.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package com.dynamicpdf.api;
import java.io.File;
import java.io.InputStream;
/**
* Represents a word resource.
*/
public class WordResource extends Resource {
private ResourceType type = ResourceType.WORD;
private String mimeType;
/**
* Initializes a new instance of the <code>WordResource</code> class.
* @param filePath The word file path.
*/
public WordResource(String filePath) {
this(filePath, null);
}
/**
* Initializes a new instance of the <code>WordResource</code> class.
* @param filePath The word file path.
* @param resourceName The resource name with file extension.
*/
public WordResource(String filePath, String resourceName) {
super(filePath, resourceName);
if ((resourceName == null || resourceName.isBlank()) == false) {
setMimeType();
} else if (resourceName != null) {
throw new EndpointException("Unsupported file type or invalid file extension.");
}
}
/**
* Initializes a new instance of the <code>WordResource</code> class.
* @param value The byte array of the word file.
* @param resourceName The resource name with file extension.
*/
public WordResource(byte[] value, String resourceName) {
super(value, resourceName);
if ((resourceName == null || resourceName.isBlank()) == false) {
setMimeType();
} else if (resourceName != null) {
throw new EndpointException("Unsupported file type or invalid file extension.");
}
}
/**
* Initializes a new instance of the <code>WordResource</code> class.
* @param data The stream of the word file.
* @param resourceName The resource name with file extension.
*/
public WordResource(InputStream data, String resourceName) {
super(data, resourceName);
if ((resourceName == null || resourceName.isBlank()) == false) {
setMimeType();
} else if (resourceName != null) {
throw new EndpointException("Unsupported file type or invalid file extension.");
}
}
/**
* Sets the resource name.
* @param value The resource name.
* <p>The resource name should be specified with a file extension.</p>
*/
public void setResourceName(String value) {
if ((value == null || value.isBlank()) == true
|| (value.contains(".") && value.length() > value.lastIndexOf('.') + 1) == false) {
throw new EndpointException("Invalid resource name.");
}
super.setResourceName(value);
setMimeType();
}
ResourceType getType() {
return type;
}
void setMimeType(String value) {
mimeType = value;
}
String getMimeType() {
return mimeType;
}
String getFileExtension() {
String inputFileExtension = "";
if ((getResourceName() == null || getResourceName().isBlank()) == false) {
if ((getResourceName().contains(".")
&& getResourceName().trim().length() > getResourceName().lastIndexOf('.') + 1) == true) {
inputFileExtension = getResourceName().substring(getResourceName().lastIndexOf('.')).toLowerCase();
} else {
throw new EndpointException("Invalid resource name.");
}
} else if ((getFilePath() == null || getFilePath().isBlank()) == false) {
String fileName = new File(getFilePath()).getName();
if ((fileName.contains(".") && fileName.trim().length() > fileName.lastIndexOf('.') + 1) == true) {
inputFileExtension = fileName.substring(fileName.lastIndexOf('.')).toLowerCase();
} else {
throw new EndpointException("Invalid file path specified.");
}
} else {
throw new EndpointException("Invalid file path or resource name.");
}
if (inputFileExtension.equals(".doc")) {
mimeType = "application/msword";
return ".doc";
} else if (inputFileExtension.equals(".docx") && getData()[0] == 0x50 && getData()[1] == 0x4b) {
mimeType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
return ".docx";
} else if (inputFileExtension.equals(".odt") && getData()[0] == 0x50 && getData()[1] == 0x4b) {
mimeType = "application/vnd.oasis.opendocument.text";
return ".odt";
} else {
throw new EndpointException("Unsupported file type or invalid file extension.");
}
}
private void setMimeType() {
getFileExtension();
}
}