1
1
package fr .free .nrw .commons .category ;
2
2
3
- import androidx .annotation .NonNull ;
4
-
5
3
import org .w3c .dom .Element ;
6
4
import org .w3c .dom .Node ;
7
5
import org .w3c .dom .NodeList ;
8
6
9
- import java .text .ParseException ;
10
- import java .text .SimpleDateFormat ;
11
7
import java .util .ArrayList ;
12
8
import java .util .Collections ;
13
- import java .util .Date ;
14
9
import java .util .List ;
15
10
16
- import javax .annotation .Nullable ;
17
-
18
- import fr .free .nrw .commons .Media ;
19
- import fr .free .nrw .commons .utils .StringUtils ;
20
- import timber .log .Timber ;
21
-
22
11
public class CategoryImageUtils {
23
12
24
- /**
25
- * The method iterates over the child nodes to return a list of Media objects
26
- * @param childNodes
27
- * @return
28
- */
29
- public static List <Media > getMediaList (NodeList childNodes ) {
30
- List <Media > categoryImages = new ArrayList <>();
31
-
32
- for (int i = 0 ; i < childNodes .getLength (); i ++) {
33
- Node node = childNodes .item (i );
34
-
35
- if (getFileName (node ).substring (0 , 5 ).equals ("File:" )) {
36
- categoryImages .add (getMediaFromPage (node ));
37
- }
38
- }
39
-
40
- return categoryImages ;
41
- }
42
-
43
13
/**
44
14
* The method iterates over the child nodes to return a list of Subcategory name
45
15
* sorted alphabetically
@@ -56,27 +26,6 @@ public static List<String> getSubCategoryList(NodeList childNodes) {
56
26
return subCategories ;
57
27
}
58
28
59
- /**
60
- * Creates a new Media object from the XML response as received by the API
61
- * @param node
62
- * @return
63
- */
64
- public static Media getMediaFromPage (Node node ) {
65
- Media media = new Media (null ,
66
- getImageUrl (node ),
67
- getFileName (node ),
68
- getDescription (node ),
69
- getDataLength (node ),
70
- getDateCreated (node ),
71
- getDateCreated (node ),
72
- getCreator (node )
73
- );
74
-
75
- media .setLicense (getLicense (node ));
76
-
77
- return media ;
78
- }
79
-
80
29
/**
81
30
* Extracts the filename of the uploaded image
82
31
* @param document
@@ -87,162 +36,4 @@ private static String getFileName(Node document) {
87
36
return element .getAttribute ("title" );
88
37
}
89
38
90
- /**
91
- * Extracts the image description for that particular upload
92
- * @param document
93
- * @return
94
- */
95
- private static String getDescription (Node document ) {
96
- return getMetaDataValue (document , "ImageDescription" );
97
- }
98
-
99
- /**
100
- * Extracts license information from the image meta data
101
- * @param document
102
- * @return
103
- */
104
- private static String getLicense (Node document ) {
105
- return getMetaDataValue (document , "License" );
106
- }
107
-
108
- /**
109
- * Returns the parsed value of artist from the response
110
- * The artist information is returned as a HTML string from the API. Using HTML parser to parse the HTML
111
- * @param document
112
- * @return
113
- */
114
- @ NonNull
115
- private static String getCreator (Node document ) {
116
- String artist = getMetaDataValue (document , "Artist" );
117
- if (StringUtils .isNullOrWhiteSpace (artist )) {
118
- return "" ;
119
- }
120
- return StringUtils .getParsedStringFromHtml (artist );
121
- }
122
-
123
- /**
124
- * Returns the parsed date of creation of the image
125
- * @param document
126
- * @return
127
- */
128
- private static Date getDateCreated (Node document ) {
129
- String dateTime = getMetaDataValue (document , "DateTime" );
130
- if (dateTime != null && !dateTime .equals ("" )) {
131
- SimpleDateFormat format = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss" );
132
- try {
133
- return format .parse (dateTime );
134
- } catch (ParseException e ) {
135
- Timber .d ("Error occurred while parsing date %s" , dateTime );
136
- return new Date ();
137
- }
138
- }
139
- return new Date ();
140
- }
141
-
142
- /**
143
- * @param document
144
- * @return Returns the url attribute from the imageInfo node
145
- */
146
- private static String getImageUrl (Node document ) {
147
- Element element = (Element ) getImageInfo (document );
148
- if (element != null ) {
149
- return element .getAttribute ("url" );
150
- }
151
- return null ;
152
- }
153
-
154
- /**
155
- * Takes the node document and gives out the attribute length from the node document
156
- * @param document
157
- * @return
158
- */
159
- private static long getDataLength (Node document ) {
160
- Element element = (Element ) document ;
161
- if (element != null ) {
162
- String length = element .getAttribute ("length" );
163
- if (length != null && !length .equals ("" )) {
164
- return Long .parseLong (length );
165
- }
166
- }
167
- return 0L ;
168
- }
169
-
170
- /**
171
- * Generic method to get the value of any meta as returned by the getMetaData function
172
- * @param document node document as returned by API
173
- * @param metaName the name of meta node to be returned
174
- * @return
175
- */
176
- private static String getMetaDataValue (Node document , String metaName ) {
177
- Element metaData = getMetaData (document , metaName );
178
- if (metaData != null ) {
179
- return metaData .getAttribute ("value" );
180
- }
181
- return null ;
182
- }
183
-
184
- /**
185
- * Generic method to return an element taking the node document and metaName as input
186
- * @param document node document as returned by API
187
- * @param metaName the name of meta node to be returned
188
- * @return
189
- */
190
- @ Nullable
191
- private static Element getMetaData (Node document , String metaName ) {
192
- Node extraMetaData = getExtraMetaData (document );
193
- if (extraMetaData != null ) {
194
- Node node = getNode (extraMetaData , metaName );
195
- if (node != null ) {
196
- return (Element ) node ;
197
- }
198
- }
199
- return null ;
200
- }
201
-
202
- /**
203
- * Extracts extmetadata from the response XML
204
- * @param document
205
- * @return
206
- */
207
- @ Nullable
208
- private static Node getExtraMetaData (Node document ) {
209
- Node imageInfo = getImageInfo (document );
210
- if (imageInfo != null ) {
211
- return getNode (imageInfo , "extmetadata" );
212
- }
213
- return null ;
214
- }
215
-
216
- /**
217
- * Extracts the ii node from the imageinfo node
218
- * @param document
219
- * @return
220
- */
221
- @ Nullable
222
- private static Node getImageInfo (Node document ) {
223
- Node imageInfo = getNode (document , "imageinfo" );
224
- if (imageInfo != null ) {
225
- return getNode (imageInfo , "ii" );
226
- }
227
- return null ;
228
- }
229
-
230
- /**
231
- * Takes a parent node as input and returns a child node if present
232
- * @param node parent node
233
- * @param nodeName child node name
234
- * @return
235
- */
236
- @ Nullable
237
- public static Node getNode (Node node , String nodeName ) {
238
- NodeList childNodes = node .getChildNodes ();
239
- for (int i = 0 ; i < childNodes .getLength (); i ++) {
240
- Node nodeItem = childNodes .item (i );
241
- Element item = (Element ) nodeItem ;
242
- if (item .getTagName ().equals (nodeName )) {
243
- return nodeItem ;
244
- }
245
- }
246
- return null ;
247
- }
248
39
}
0 commit comments