/* * 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 getLinks() throws APIException { List links = null; Map htmlStrings = getAnnotatedStrings(WithHTMLTags.class); if (!htmlStrings.isEmpty()) { links = SnookerURLUtils.parseUrls(htmlStrings); } return links; } @Override public List getURLs() throws APIException { List urls = null; Map 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 getAnnotatedStrings( Class annotationClass ) { Map 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 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 getAnnotatedMethodStrings( Class annotationClass ) { Map 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 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; } }