<commit_before>/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.opens.tanaguru.ruleimplementation;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.imageio.ImageIO;
import org.apache.log4j.Logger;
import org.opens.tanaguru.contentadapter.util.URLIdentifier;
import org.opens.tanaguru.processor.SSPHandler;

/**
 * This class is used to determine if an image is decorative
 * @author jkowalczyk
 */
public class ImageChecker {

    private URLIdentifier uRLIdentifier = null;
    private static ImageChecker imageChecker = null;

    private ImageChecker() {

    }

    public static ImageChecker getInstance() {
        if (imageChecker == null) {
            imageChecker = new ImageChecker();
        }
        return imageChecker;
    }

    public boolean isDecorativeImage(SSPHandler sspHandler, String imgUrl)  {
        boolean isMonoColorImg = true;
        boolean isMonoDimension = false;
        boolean isDecorativeImg = false;
        try {
            URL src = new URL(sspHandler.getSSP().getURI());
            uRLIdentifier.setUrl(src);

            BufferedImage image = null;
            try {
                image = ImageIO.read(uRLIdentifier.resolve(imgUrl));
            } catch (IOException ex) {
                Logger.getLogger(ImageChecker.class.getName()).
                        warn(ex.getMessage() + " " + uRLIdentifier.resolve(imgUrl));
            } catch (IllegalArgumentException ex){
                Logger.getLogger(ImageChecker.class.getName()).
                        warn(ex.getMessage() + " " + uRLIdentifier.resolve(imgUrl));
            } 

            int rgbColor = -1;
            if (image != null) {
                if (image.getWidth() < 1 || image.getHeight() < 1) {
                    isMonoDimension = true;
                } else {
                    for (int i = 0; i < image.getWidth(); i++) {
                        for (int j = 0; j < image.getHeight(); j++) {
                            if (i == 0 && j == 0) {
                                rgbColor = image.getRGB(i, j);
                            }
                            if (rgbColor != image.getRGB(i, j)) {
                                i = image.getWidth();
                                j = image.getHeight();
                                isMonoColorImg = false;
                            }
                        }
                    }
                }
                if (isMonoColorImg || isMonoDimension) {
                    isDecorativeImg = true;
                }
            }
            
        } catch (MalformedURLException ex) {
            Logger.getLogger(ImageChecker.class.getName()).
                    warn(ex.getMessage() + " " + uRLIdentifier.resolve(imgUrl));
        }
        return isDecorativeImg;
    }

    public void setUrlIdentifier(URLIdentifier urlIdentifier) {
        this.uRLIdentifier = urlIdentifier;
    }

}<commit_msg>01021 Rule from AccessiWeb2 implementation<commit_after>/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.opens.tanaguru.ruleimplementation;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.imageio.ImageIO;
import org.apache.log4j.Logger;
import org.opens.tanaguru.contentadapter.util.URLIdentifier;
import org.opens.tanaguru.processor.SSPHandler;

/**
 * This class is used to determine if an image is decorative
 * @author jkowalczyk
 */
public class ImageChecker {

    private URLIdentifier uRLIdentifier = null;
    private static ImageChecker imageChecker = null;

    private ImageChecker() {

    }

    public static ImageChecker getInstance() {
        if (imageChecker == null) {
            imageChecker = new ImageChecker();
        }
        return imageChecker;
    }

    public boolean isDecorativeImage(SSPHandler sspHandler, String imgUrl)  {
        Logger.getLogger(ImageChecker.class.getName()).
                    debug("Testing if image " + uRLIdentifier.resolve(imgUrl) + " is decorative");
        long beginDate = System.currentTimeMillis();
        boolean isMonoColorImg = true;
        boolean isMonoDimension = false;
        boolean isDecorativeImg = false;
        try {
            URL src = new URL(sspHandler.getSSP().getURI());
            uRLIdentifier.setUrl(src);

            BufferedImage image = null;
            try {
                image = ImageIO.read(uRLIdentifier.resolve(imgUrl));
            } catch (IOException ex) {
                Logger.getLogger(ImageChecker.class.getName()).
                        warn(ex.getMessage() + " " + uRLIdentifier.resolve(imgUrl));
            } catch (IllegalArgumentException ex){
                Logger.getLogger(ImageChecker.class.getName()).
                        warn(ex.getMessage() + " " + uRLIdentifier.resolve(imgUrl));
            } 

            int rgbColor = -1;
            if (image != null) {
                if (image.getWidth() < 1 || image.getHeight() < 1) {
                    isMonoDimension = true;
                } else {
                    for (int i = 0; i < image.getWidth(); i++) {
                        for (int j = 0; j < image.getHeight(); j++) {
                            if (i == 0 && j == 0) {
                                rgbColor = image.getRGB(i, j);
                            }
                            if (rgbColor != image.getRGB(i, j)) {
                                i = image.getWidth();
                                j = image.getHeight();
                                isMonoColorImg = false;
                            }
                        }
                    }
                }
                if (isMonoColorImg || isMonoDimension) {
                    isDecorativeImg = true;
                }
            }
            
        } catch (MalformedURLException ex) {
            Logger.getLogger(ImageChecker.class.getName()).
                    warn(ex.getMessage() + " " + uRLIdentifier.resolve(imgUrl));
        }
        Long processDuration = new Long(System.currentTimeMillis() - beginDate);
        Logger.getLogger(ImageChecker.class.getName()).
                    debug("image tested in " + processDuration.toString() + " ms");
        return isDecorativeImg;
    }

    public void setUrlIdentifier(URLIdentifier urlIdentifier) {
        this.uRLIdentifier = urlIdentifier;
    }

}
{'fraction_non_alphanumeric': 0.09960462287104623, 'fraction_numerical': 0.0033454987834549877, 'mean_word_length': 1.6161495624502784, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5399635', 'n_tokens_mistral': 1745, 'n_tokens_neox': 1693, 'n_words': 377}