<commit_before>/*******************************************************************************
* Copyright 2015 <NAME> http://galenframework.com
* 
* 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 com.galenframework.api;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import com.galenframework.browser.Browser;
import com.galenframework.rainbow4j.Rainbow4J;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.ObjectMapper;

public class PageDump {

    private final static Logger LOG = LoggerFactory.getLogger(PageDump.class);

    private String pageName;
    private String title;
    private Map<String, Element> items = new HashMap<String, Element>();

    public void setTitle(String title) {
        this.title = title;
    }

    public String getTitle() {
        return title;
    }

    public void addElement(Element element) {
        items.put(element.getObjectName(), element);
    }

    public Map<String, Element> getItems() {
        return items;
    }

    public void setItems(Map<String, Element> items) {
        this.items = items;
    }

    public void exportAsJson(File file) throws IOException {
        makeSureFileExists(file);
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.writeValue(file, this);
    }

    public void exportAsHtml(String title, File file) throws IOException {
        makeSureFileExists(file);
        ObjectMapper objectMapper = new ObjectMapper();
        String jsonText = objectMapper.writeValueAsString(this);

        String template = IOUtils.toString(getClass().getResourceAsStream("/pagedump/page.html"));

        String htmlText = template.replace("${title}", title);
        htmlText = htmlText.replace("${json}", jsonText);


        FileUtils.writeStringToFile(file, htmlText);
    }

    public void makeSureFileExists(File file) throws IOException {
        if (!file.exists()) {
            if (!file.createNewFile()) {
                throw new RuntimeException("Couldn't create file: " + file.getAbsolutePath());
            }
        }
    }

    public void exportAllScreenshots(Browser browser, File reportFolder) throws IOException {

        File screenshotOriginalFile = browser.createScreenshot();

        FileUtils.copyFile(screenshotOriginalFile, new File(reportFolder.getAbsolutePath() + File.separator + "page.png"));

        BufferedImage image = Rainbow4J.loadImage(screenshotOriginalFile.getAbsolutePath());


        File objectsFolder = new File(reportFolder.getAbsolutePath() + File.separator + "objects");
        objectsFolder.mkdirs();

        for (Element element : items.values()) {
            if (element.hasImage) {
                int[] area = element.getArea();

                try {
                    BufferedImage subImage = image.getSubimage(area[0], area[1], area[2], area[3]);
                    Rainbow4J.saveImage(subImage, new File(objectsFolder.getAbsolutePath() + File.separator + element.getObjectName() + ".png"));
                }
                catch (Exception ex) {
                    LOG.error("Got error during saving image", ex);
                }
            }
        }<commit_msg>Crop subimages on image borders.<commit_after>/*******************************************************************************
* Copyright 2015 <NAME> http://galenframework.com
*
* 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 com.galenframework.api;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import com.galenframework.browser.Browser;
import com.galenframework.rainbow4j.Rainbow4J;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.ObjectMapper;

public class PageDump {

    private final static Logger LOG = LoggerFactory.getLogger(PageDump.class);

    private String pageName;
    private String title;
    private Map<String, Element> items = new HashMap<String, Element>();

    public void setTitle(String title) {
        this.title = title;
    }

    public String getTitle() {
        return title;
    }

    public void addElement(Element element) {
        items.put(element.getObjectName(), element);
    }

    public Map<String, Element> getItems() {
        return items;
    }

    public void setItems(Map<String, Element> items) {
        this.items = items;
    }

    public void exportAsJson(File file) throws IOException {
        makeSureFileExists(file);
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.writeValue(file, this);
    }

    public void exportAsHtml(String title, File file) throws IOException {
        makeSureFileExists(file);
        ObjectMapper objectMapper = new ObjectMapper();
        String jsonText = objectMapper.writeValueAsString(this);

        String template = IOUtils.toString(getClass().getResourceAsStream("/pagedump/page.html"));

        String htmlText = template.replace("${title}", title);
        htmlText = htmlText.replace("${json}", jsonText);


        FileUtils.writeStringToFile(file, htmlText);
    }

    public void makeSureFileExists(File file) throws IOException {
        if (!file.exists()) {
            if (!file.createNewFile()) {
                throw new RuntimeException("Couldn't create file: " + file.getAbsolutePath());
            }
        }
    }

    public void exportAllScreenshots(Browser browser, File reportFolder) throws IOException {

        File screenshotOriginalFile = browser.createScreenshot();

        FileUtils.copyFile(screenshotOriginalFile, new File(reportFolder.getAbsolutePath() + File.separator + "page.png"));

        BufferedImage image = Rainbow4J.loadImage(screenshotOriginalFile.getAbsolutePath());

        File objectsFolder = new File(reportFolder.getAbsolutePath() + File.separator + "objects");
        objectsFolder.mkdirs();

        for (Element element : items.values()) {
            if (element.hasImage) {
                int[] area = element.getArea();
                int availableHeight = image.getHeight() - area[1];
                int availableWidth = image.getWidth() - area[0];
                int subimageHeight = Math.min(area[2], availableHeight);
                int subimageWidth = Math.min(area[3], availableWidth);

                try {
                    BufferedImage subImage = image.getSubimage(area[0], area[1], subimageHeight, subimageWidth);
                    Rainbow4J.saveImage(subImage, new File(objectsFolder.getAbsolutePath() + File.separator + element.getObjectName() + ".png"));
                }
                catch (Exception ex) {
                    LOG.error("Got error during saving image", ex);
                }
            }
        }
{'fraction_non_alphanumeric': 0.1346293165901956, 'fraction_numerical': 0.004588263704419222, 'mean_word_length': 3.145645645645646, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 4}, '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': '6077895', 'n_tokens_mistral': 2207, 'n_tokens_neox': 2086, 'n_words': 574}