[LIB-8] URL and HTML tag processing, new entities fields
[snooker-score-api.git] / src / main / java / org / hedgecode / snooker / json / JsonURLEntity.java
1 /*
2  * Copyright (c) 2017. Developed by Hedgecode.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.hedgecode.snooker.json;
18
19 import java.lang.annotation.Annotation;
20 import java.lang.reflect.Field;
21 import java.lang.reflect.Method;
22 import java.util.HashMap;
23 import java.util.List;
24 import java.util.Map;
25
26 import org.hedgecode.snooker.SnookerURLUtils;
27 import org.hedgecode.snooker.annotation.IsURL;
28 import org.hedgecode.snooker.annotation.WithHTMLTags;
29 import org.hedgecode.snooker.api.APIException;
30 import org.hedgecode.snooker.api.SnookerURL;
31 import org.hedgecode.snooker.api.URLEntity;
32
33 /**
34  * Abstract URL Entity.
35  *
36  * @author Dmitry Samoshin aka gotty
37  */
38 public abstract class JsonURLEntity implements URLEntity {
39
40     @Override
41     public List<SnookerURL> getLinks() throws APIException {
42         List<SnookerURL> links = null;
43         Map<String, String> htmlStrings = getAnnotatedStrings(WithHTMLTags.class);
44         if (!htmlStrings.isEmpty()) {
45             links = SnookerURLUtils.parseUrls(htmlStrings);
46         }
47         return links;
48     }
49
50     @Override
51     public List<SnookerURL> getURLs() throws APIException {
52         List<SnookerURL> urls = null;
53         Map<String, String> urlStrings = getAnnotatedStrings(IsURL.class);
54         urlStrings.putAll(getAnnotatedMethodStrings(IsURL.class));
55         if (!urlStrings.isEmpty()) {
56             urls = SnookerURLUtils.assignUrls(urlStrings);
57         }
58         return urls;
59     }
60
61     @Override
62     public SnookerURL getURL(String name) throws APIException {
63         String urlString = getAnnotatedString(IsURL.class, name);
64         if (urlString == null) {
65             urlString = getAnnotatedMethodString(IsURL.class, name);
66         }
67         if (urlString != null && !urlString.isEmpty()) {
68             return SnookerURLUtils.assignUrl(name, urlString);
69         }
70         return null;
71     }
72
73     @Override
74     public String withoutTags(String name) throws APIException {
75         String tagString = getAnnotatedString(WithHTMLTags.class, name);
76         if (tagString != null && !tagString.isEmpty()) {
77             return SnookerURLUtils.cutTags(tagString);
78         }
79         return null;
80     }
81
82     private Map<String, String> getAnnotatedStrings(
83             Class<? extends Annotation> annotationClass
84     ) {
85         Map<String, String> result = new HashMap<>();
86         Class<?> clazz = getClass();
87         for (Field field : clazz.getDeclaredFields()) {
88             if (field.isAnnotationPresent(annotationClass)
89                     && field.getType().equals(String.class))
90             {
91                 field.setAccessible(true);
92                 try {
93                     result.put(
94                             field.getName(),
95                             (String) field.get(this)
96                     );
97                 } catch (Exception ignored) {
98                 }
99             }
100         }
101         return result;
102     }
103
104     private String getAnnotatedString(
105             Class<? extends Annotation> annotationClass,
106             String fieldName
107     ) {
108         String result = null;
109         Class<?> clazz = getClass();
110         for (Field field : clazz.getDeclaredFields()) {
111             if (field.isAnnotationPresent(annotationClass)
112                     && field.getName().equals(fieldName)
113                     && field.getType().equals(String.class))
114             {
115                 field.setAccessible(true);
116                 try {
117                     result = (String) field.get(this);
118                 } catch (Exception ignored) {
119                 }
120             }
121         }
122         return result;
123     }
124
125     private Map<String, String> getAnnotatedMethodStrings(
126             Class<? extends Annotation> annotationClass
127     ) {
128         Map<String, String> result = new HashMap<>();
129         Class<?> clazz = getClass();
130         for (Method method : clazz.getMethods()) {
131             if (method.isAnnotationPresent(annotationClass)
132                     && method.getReturnType().equals(String.class)
133                     && method.getParameterCount() == 0)
134             {
135                 try {
136                     result.put(
137                             method.getName(),
138                             (String) method.invoke(this)
139                     );
140                 } catch (Exception ignored) {
141                 }
142             }
143         }
144         return result;
145     }
146
147     private String getAnnotatedMethodString(
148             Class<? extends Annotation> annotationClass,
149             String methodName
150     ) {
151         String result = null;
152         Class<?> clazz = getClass();
153         for (Method method : clazz.getMethods()) {
154             if (method.isAnnotationPresent(annotationClass)
155                     && method.getName().equals(methodName)
156                     && method.getReturnType().equals(String.class)
157                     && method.getParameterCount() == 0)
158             {
159                 try {
160                     result = (String) method.invoke(this);
161                 } catch (Exception ignored) {
162                 }
163             }
164         }
165         return result;
166     }
167
168 }