[LIB-8] URL and HTML tag processing, new entities fields
[snooker-score-api.git] / src / main / java / org / hedgecode / snooker / json / JsonURLEntity.java
diff --git a/src/main/java/org/hedgecode/snooker/json/JsonURLEntity.java b/src/main/java/org/hedgecode/snooker/json/JsonURLEntity.java
new file mode 100644 (file)
index 0000000..b625f52
--- /dev/null
@@ -0,0 +1,168 @@
+/*
+ * Copyright (c) 2017. Developed by Hedgecode.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.hedgecode.snooker.json;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.hedgecode.snooker.SnookerURLUtils;
+import org.hedgecode.snooker.annotation.IsURL;
+import org.hedgecode.snooker.annotation.WithHTMLTags;
+import org.hedgecode.snooker.api.APIException;
+import org.hedgecode.snooker.api.SnookerURL;
+import org.hedgecode.snooker.api.URLEntity;
+
+/**
+ * Abstract URL Entity.
+ *
+ * @author Dmitry Samoshin aka gotty
+ */
+public abstract class JsonURLEntity implements URLEntity {
+
+    @Override
+    public List<SnookerURL> getLinks() throws APIException {
+        List<SnookerURL> links = null;
+        Map<String, String> htmlStrings = getAnnotatedStrings(WithHTMLTags.class);
+        if (!htmlStrings.isEmpty()) {
+            links = SnookerURLUtils.parseUrls(htmlStrings);
+        }
+        return links;
+    }
+
+    @Override
+    public List<SnookerURL> getURLs() throws APIException {
+        List<SnookerURL> urls = null;
+        Map<String, String> urlStrings = getAnnotatedStrings(IsURL.class);
+        urlStrings.putAll(getAnnotatedMethodStrings(IsURL.class));
+        if (!urlStrings.isEmpty()) {
+            urls = SnookerURLUtils.assignUrls(urlStrings);
+        }
+        return urls;
+    }
+
+    @Override
+    public SnookerURL getURL(String name) throws APIException {
+        String urlString = getAnnotatedString(IsURL.class, name);
+        if (urlString == null) {
+            urlString = getAnnotatedMethodString(IsURL.class, name);
+        }
+        if (urlString != null && !urlString.isEmpty()) {
+            return SnookerURLUtils.assignUrl(name, urlString);
+        }
+        return null;
+    }
+
+    @Override
+    public String withoutTags(String name) throws APIException {
+        String tagString = getAnnotatedString(WithHTMLTags.class, name);
+        if (tagString != null && !tagString.isEmpty()) {
+            return SnookerURLUtils.cutTags(tagString);
+        }
+        return null;
+    }
+
+    private Map<String, String> getAnnotatedStrings(
+            Class<? extends Annotation> annotationClass
+    ) {
+        Map<String, String> result = new HashMap<>();
+        Class<?> clazz = getClass();
+        for (Field field : clazz.getDeclaredFields()) {
+            if (field.isAnnotationPresent(annotationClass)
+                    && field.getType().equals(String.class))
+            {
+                field.setAccessible(true);
+                try {
+                    result.put(
+                            field.getName(),
+                            (String) field.get(this)
+                    );
+                } catch (Exception ignored) {
+                }
+            }
+        }
+        return result;
+    }
+
+    private String getAnnotatedString(
+            Class<? extends Annotation> annotationClass,
+            String fieldName
+    ) {
+        String result = null;
+        Class<?> clazz = getClass();
+        for (Field field : clazz.getDeclaredFields()) {
+            if (field.isAnnotationPresent(annotationClass)
+                    && field.getName().equals(fieldName)
+                    && field.getType().equals(String.class))
+            {
+                field.setAccessible(true);
+                try {
+                    result = (String) field.get(this);
+                } catch (Exception ignored) {
+                }
+            }
+        }
+        return result;
+    }
+
+    private Map<String, String> getAnnotatedMethodStrings(
+            Class<? extends Annotation> annotationClass
+    ) {
+        Map<String, String> result = new HashMap<>();
+        Class<?> clazz = getClass();
+        for (Method method : clazz.getMethods()) {
+            if (method.isAnnotationPresent(annotationClass)
+                    && method.getReturnType().equals(String.class)
+                    && method.getParameterCount() == 0)
+            {
+                try {
+                    result.put(
+                            method.getName(),
+                            (String) method.invoke(this)
+                    );
+                } catch (Exception ignored) {
+                }
+            }
+        }
+        return result;
+    }
+
+    private String getAnnotatedMethodString(
+            Class<? extends Annotation> annotationClass,
+            String methodName
+    ) {
+        String result = null;
+        Class<?> clazz = getClass();
+        for (Method method : clazz.getMethods()) {
+            if (method.isAnnotationPresent(annotationClass)
+                    && method.getName().equals(methodName)
+                    && method.getReturnType().equals(String.class)
+                    && method.getParameterCount() == 0)
+            {
+                try {
+                    result = (String) method.invoke(this);
+                } catch (Exception ignored) {
+                }
+            }
+        }
+        return result;
+    }
+
+}