<commit_before>/*
 * Copyright 2013 Red Hat, Inc. and/or its affiliates.
 *
 * Licensed under the Eclipse Public License version 1.0, available at
 * http://www.eclipse.org/legal/epl-v10.html
 */

package org.jboss.forge.addon.resource;

import java.net.MalformedURLException;
import java.net.URL;

import org.jboss.forge.furnace.util.Strings;

/**
 * Generates {@link URLResource} objects
 * 
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * 
 */
public class URLResourceGenerator implements ResourceGenerator<URLResource, Object>
{
   @Override
   public boolean handles(Class<?> type, Object resource)
   {
      if (resource == null)
      {
         return false;
      }
      else if (resource instanceof URL)
      {
         return true;
      }
      else if (Strings.isURL(resource.toString()))
      {
         try
         {
            new URL(resource.toString());
            return true;
         }
         catch (MalformedURLException e)
         {
         }
      }
      return false;
   }

   @Override
   @SuppressWarnings("unchecked")
   public <T extends Resource<Object>> T getResource(ResourceFactory factory, Class<URLResource> type, Object resource)
   {
      URL url = null;
      if (resource instanceof URL)
      {
         url = (URL) resource;
      }
      else if (Strings.isURL(resource.toString()))
      {
         try
         {<commit_msg>FORGE-1699 - Performance improvement of URL validation.<commit_after>/*
 * Copyright 2013 Red Hat, Inc. and/or its affiliates.
 *
 * Licensed under the Eclipse Public License version 1.0, available at
 * http://www.eclipse.org/legal/epl-v10.html
 */

package org.jboss.forge.addon.resource;

import java.net.MalformedURLException;
import java.net.URL;

import org.jboss.forge.furnace.util.Strings;

/**
 * Generates {@link URLResource} objects
 *
 * @author <a href="mailto:<EMAIL>"><NAME>aldi</a>
 *
 */
public class URLResourceGenerator implements ResourceGenerator<URLResource, Object>
{
   @Override
   public boolean handles(Class<?> type, Object resource)
   {
      boolean result;
      if (resource == null)
      {
         result = false;
      }
      else if (resource instanceof URL)
      {
         result = true;
      }
      else
      {
         result = Strings.isURL(resource.toString());
      }
      return result;
   }

   @Override
   @SuppressWarnings("unchecked")
   public <T extends Resource<Object>> T getResource(ResourceFactory factory, Class<URLResource> type, Object resource)
   {
      URL url = null;
      if (resource instanceof URL)
      {
         url = (URL) resource;
      }
      else if (Strings.isURL(resource.toString()))
      {
         try
         {
{'fraction_non_alphanumeric': 0.11272321428571429, 'fraction_numerical': 0.00744047619047619, 'mean_word_length': 2.4081115335868186, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2408982', 'n_tokens_mistral': 801, 'n_tokens_neox': 778, 'n_words': 199}