All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.datacleaner.result.renderer.HtmlCrosstabRendererCallback Maven / Gradle / Ivy

There is a newer version: 6.0.0
Show newest version
/**
 * DataCleaner (community edition)
 * Copyright (C) 2014 Neopost - Customer Information Management
 *
 * This copyrighted material is made available to anyone wishing to use, modify,
 * copy, or redistribute it subject to the terms and conditions of the GNU
 * Lesser General Public License, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this distribution; if not, write to:
 * Free Software Foundation, Inc.
 * 51 Franklin Street, Fifth Floor
 * Boston, MA  02110-1301  USA
 */
package org.datacleaner.result.renderer;

import java.text.DecimalFormatSymbols;
import java.util.List;

import org.datacleaner.api.AnalyzerResult;
import org.datacleaner.result.AnnotatedRowsResult;
import org.datacleaner.result.Crosstab;
import org.datacleaner.result.CrosstabDimension;
import org.datacleaner.result.ResultProducer;
import org.datacleaner.result.html.DrillToDetailsBodyElement;
import org.datacleaner.result.html.HtmlFragment;
import org.datacleaner.result.html.HtmlRenderingContext;
import org.datacleaner.result.html.SimpleHtmlFragment;
import org.datacleaner.util.LabelUtils;

public class HtmlCrosstabRendererCallback implements CrosstabRendererCallback {

    private final StringBuilder sb;
    private final SimpleHtmlFragment htmlFragtment;
    private final RendererFactory rendererFactory;
    private final HtmlRenderingContext htmlRenderingContext;

    private int rowNumber;

    public HtmlCrosstabRendererCallback(RendererFactory rendererFactory, HtmlRenderingContext htmlRenderingContext) {
        this.rendererFactory = rendererFactory;
        this.htmlRenderingContext = htmlRenderingContext;
        this.sb = new StringBuilder();
        this.rowNumber = 0;
        this.htmlFragtment = new SimpleHtmlFragment();
    }

    @Override
    public void beginTable(Crosstab crosstab, List horizontalDimensions,
            List verticalDimensions) {
        sb.append("");
    }

    @Override
    public void endTable() {
        sb.append("
"); } @Override public void beginRow() { rowNumber++; if (rowNumber % 2 == 0) { sb.append(""); } else { sb.append(""); } } @Override public void endRow() { sb.append(""); } @Override public void horizontalHeaderCell(String category, CrosstabDimension dimension, int width) { if (width <= 0) { return; } if (width > 1) { sb.append(""); } else if (width == 1) { sb.append(""); } sb.append(toHtml(category)); sb.append(""); } @Override public void verticalHeaderCell(String category, CrosstabDimension dimension, int height) { if (height <= 0) { return; } if (height > 1) { sb.append(""); } else if (height == 1) { sb.append(""); } sb.append(toHtml(category)); sb.append(""); } @Override public void valueCell(Object value, ResultProducer drillToDetailResultProducer) { if (drillToDetailResultProducer == null) { simpleValueCell(value); return; } final AnalyzerResult drillResult = drillToDetailResultProducer.getResult(); if (drillResult == null) { simpleValueCell(value); return; } if (drillResult instanceof AnnotatedRowsResult && ((AnnotatedRowsResult) drillResult).getAnnotatedRowCount() == 0) { simpleValueCell(value); return; } final String drillElementId = htmlRenderingContext.createElementId(); final DrillToDetailsBodyElement drillBodyElement = new DrillToDetailsBodyElement(drillElementId, rendererFactory, drillResult); htmlFragtment.addBodyElement(drillBodyElement); final String invocation = drillBodyElement.toJavaScriptInvocation(); sb.append(""); sb.append(""); sb.append(toHtml(LabelUtils.getValueLabel(value))); sb.append(""); sb.append(""); } private void simpleValueCell(Object value) { sb.append(""); sb.append(toHtml(value)); sb.append(""); } public String toHtml(Object value) { String valueLabel = LabelUtils.getValueLabel(value); valueLabel = htmlRenderingContext.escapeHtml(valueLabel); if (value instanceof Number) { // mark the decimal point DecimalFormatSymbols decimalFormatSymbols = DecimalFormatSymbols.getInstance(); char decimalSeparator = decimalFormatSymbols.getDecimalSeparator(); int indexOfDecimalSeparator = valueLabel.lastIndexOf(decimalSeparator); if (indexOfDecimalSeparator != -1) { // add a around the decimal part. valueLabel = valueLabel.substring(0, indexOfDecimalSeparator) + "" + valueLabel.substring(indexOfDecimalSeparator) + ""; } } return valueLabel; } @Override public void emptyHeader(CrosstabDimension verticalDimension, CrosstabDimension horizontalDimension) { sb.append(""); } @Override public HtmlFragment getResult() { htmlFragtment.addBodyElement(sb.toString()); return htmlFragtment; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy