2 * Copyright (c) 2017. Developed by Hedgecode.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.hedgecode.snooker.json;
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;
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;
34 * Abstract URL Entity.
36 * @author Dmitry Samoshin aka gotty
38 public abstract class JsonURLEntity implements URLEntity {
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);
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);
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);
67 if (urlString != null && !urlString.isEmpty()) {
68 return SnookerURLUtils.assignUrl(name, urlString);
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);
82 private Map<String, String> getAnnotatedStrings(
83 Class<? extends Annotation> annotationClass
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))
91 field.setAccessible(true);
95 (String) field.get(this)
97 } catch (Exception ignored) {
104 private String getAnnotatedString(
105 Class<? extends Annotation> annotationClass,
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))
115 field.setAccessible(true);
117 result = (String) field.get(this);
118 } catch (Exception ignored) {
125 private Map<String, String> getAnnotatedMethodStrings(
126 Class<? extends Annotation> annotationClass
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)
138 (String) method.invoke(this)
140 } catch (Exception ignored) {
147 private String getAnnotatedMethodString(
148 Class<? extends Annotation> annotationClass,
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)
160 result = (String) method.invoke(this);
161 } catch (Exception ignored) {