<commit_before>package org.timepedia.chronoscope.client.browser;

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;

import org.timepedia.chronoscope.client.Chart;
import org.timepedia.chronoscope.client.Overlay;
import org.timepedia.chronoscope.client.XYDataset;
import org.timepedia.chronoscope.client.XYPlot;
import org.timepedia.chronoscope.client.canvas.View;
import org.timepedia.chronoscope.client.canvas.ViewReadyCallback;
import org.timepedia.chronoscope.client.overlays.Marker;
import org.timepedia.chronoscope.client.overlays.OverlayClickListener;

import java.util.Date;

/**
 * Helper class used to parse microformat annotations in the DOM and turn those
 * into charts.
 *
 * @author <NAME> &lt;<EMAIL>&gt;
 */
public class Microformats {

  static Command microformatReadyListener;

  public static void setMicroformatsReadyListener(Command rdy) {
    microformatReadyListener = rdy;
  }

  private static class MicroformatViewReadyCallback
      implements ViewReadyCallback {

    private final Element[] links;

    private final String id;

    private String chartId;

    private final XYDataset[] ds;

    private final Element elt;

    private MicroformatCountdownLatch latch;

    public MicroformatViewReadyCallback(Element[] links, String id,
        String chartId, XYDataset[] ds, Element elt,
        MicroformatCountdownLatch latch) {
      this.links = links;
      this.id = id;
      this.chartId = chartId;
      this.ds = ds;
      this.elt = elt;
      this.latch = latch;
    }

    public void onViewReady(View view) {
      Chart chart = Chronoscope.getChartById(id);

      for (int j = 0; j < links
          .length; j++) {
        String href = DOM.getElementAttribute(links[j], "href");
        int hash = href != null ? href.indexOf("#") : -1;
        if (hash != -1) {
          href = href.substring(hash);
        }

        if (href != null && href.startsWith("#" + chartId)) {

          String date = href.substring(("#" + chartId + ":").length());
          String label = getAttribute(links[j], "accesskey");
          if (label == null) {
            char c[] = new char[1];
            c[0] = (char) ('A' + charCounter);

            label = new String(c);
          }
          final Element[] infoWindow = getElementsByClassName(links[j], "span",
              CMF_PREFIX + "-infowindow");
          final Marker m = new Marker((double) Date.parse(date),
              (ds[0].getRangeTop() - ds[0].getRangeBottom()) / 2, label, 0);
          attachOnClick(links[j], m);

          if (infoWindow != null && infoWindow[0] != null) {
            m.addOverlayClickListener(new OverlayClickListener() {
              public void onOverlayClick(Overlay overlay, int x, int y) {
                m.openInfoWindow(DOM.getInnerHTML(infoWindow[0]));
              }
            });
          }
          XYPlot plot = chart.getPlot();
          plot.addOverlay(m);
          String[] hdrs = getTableHeaders(elt);
          if (hdrs != null && hdrs.length > 1) {
            plot.getDomainAxis().setLabel(hdrs[0]);
            for (int k = 1; k < hdrs.length; k++) {
              plot.getRangeAxis(k - 1).setLabel(hdrs[k]);
            }
          }
        }
      }
      latch.decrement();
      chart.redraw();
    }
  }

  private static final char charCounter = 0;

  private static final String CMF_PREFIX = "cmf";

  private static final String CMF_CHART = CMF_PREFIX + "-chart";

  public static void clickMicroformatLink(Element link, Marker marker) {
    marker.click(1, 1);
  }

  public static native JavaScriptObject importMicroformatTable0(String id,
      Element table) /*-{

          var label="";
  
          var thead = table.getElementsByTagName("thead");
          var labels=[], axes=[], dateformat=null;
          var colgroup = table.getElementsByTagName("colgroup");
          var rangeFormats = [];
          var i = 0;
          if(colgroup && colgroup.item(0)) {
            var cols = colgroup.item(0).getElementsByTagName("col");
            if(cols && cols.length > 0) {
              var datecol=cols.item(0);
              if(datecol.className == "cmf-dateformat") {
                dateformat = datecol.getAttribute('title');
              }
              for(i=1; i<cols.length; i++) {
                var rcol = cols.item(i);
                if(rcol.className == "cmf-numberformat") {
                  rangeFormats[i] = rcol.getAttribute("title");
                }
              }
            }
          }
          
          if(thead && thead.item(0)) {
            var r = thead.item(0).getElementsByTagName("tr");
            var c= r.item(0).getElementsByTagName("th");
            for(i=0; i<c.length; i++) {
               label = c.item(i).innerHTML;
               labels.push(label);
               var axis=c.item(i).getAttribute("axis");
               if(!axis) { axis="default"; }
               axes.push(axis);
            }
          }
          var tbody = table.getElementsByTagName("tbody");

          if(tbody && tbody.item(0))
          {
            var rowList = tbody.item(0).getElementsByTagName("tr");

            var i;
            var domain=[], range=[];
            var numCols = rowList.item(0).getElementsByTagName("td").length;
            for(i=0; i<numCols-1; i++) {
                range.push([]);
            }
            for(i = 0; i<rowList.length; i++)
            {
             var colList = rowList.item(i).getElementsByTagName("td");
             var date = colList.item(0).innerHTML;
             var t = @org.timepedia.chronoscope.client.data.DateParser::parse(Ljava/lang/String;Ljava/lang/String;)(dateformat, date);
             domain.push(t/1000);
              for(j = 1; j<colList.length; j++) {
                  range[j-1].push(parseFloat(colList.item(j).innerHTML));
              }
            }
            var result=[];
            for(i=0; i<numCols-1; i++) {
               result.push({ id: id, domain: domain, range: range[i], label: labels[i+1], axis: axes[i+1],
               format: rangeFormats[i+1]});
            }
            return result;
          }
          return [];
       }-*/;

  public static native JavaScriptObject importTable(String id) /*-{

       var table = $doc.getElementById(id);
       var rowList = table.getElementsByTagName("tr");
       var i;
       var domain=[], range=[];
       for(i = 0; i<rowList.length; i++)
       {
          var colList = rowList.item(i).getElementsByTagName("td");
          domain.push(colList.item(0).innerHTML);
          range.push(colList.item(1).innerHTML);
       }<commit_msg>Microformat parser no longer needs to scale domain values by 1000<commit_after>package org.timepedia.chronoscope.client.browser;

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;

import org.timepedia.chronoscope.client.Chart;
import org.timepedia.chronoscope.client.Overlay;
import org.timepedia.chronoscope.client.XYDataset;
import org.timepedia.chronoscope.client.XYPlot;
import org.timepedia.chronoscope.client.canvas.View;
import org.timepedia.chronoscope.client.canvas.ViewReadyCallback;
import org.timepedia.chronoscope.client.overlays.Marker;
import org.timepedia.chronoscope.client.overlays.OverlayClickListener;

import java.util.Date;

/**
 * Helper class used to parse microformat annotations in the DOM and turn those
 * into charts.
 *
 * @author <NAME> &lt;<EMAIL>&gt;
 */
public class Microformats {

  static Command microformatReadyListener;

  public static void setMicroformatsReadyListener(Command rdy) {
    microformatReadyListener = rdy;
  }

  private static class MicroformatViewReadyCallback
      implements ViewReadyCallback {

    private final Element[] links;

    private final String id;

    private String chartId;

    private final XYDataset[] ds;

    private final Element elt;

    private MicroformatCountdownLatch latch;

    public MicroformatViewReadyCallback(Element[] links, String id,
        String chartId, XYDataset[] ds, Element elt,
        MicroformatCountdownLatch latch) {
      this.links = links;
      this.id = id;
      this.chartId = chartId;
      this.ds = ds;
      this.elt = elt;
      this.latch = latch;
    }

    public void onViewReady(View view) {
      Chart chart = Chronoscope.getChartById(id);

      for (int j = 0; j < links
          .length; j++) {
        String href = DOM.getElementAttribute(links[j], "href");
        int hash = href != null ? href.indexOf("#") : -1;
        if (hash != -1) {
          href = href.substring(hash);
        }

        if (href != null && href.startsWith("#" + chartId)) {

          String date = href.substring(("#" + chartId + ":").length());
          String label = getAttribute(links[j], "accesskey");
          if (label == null) {
            char c[] = new char[1];
            c[0] = (char) ('A' + charCounter);

            label = new String(c);
          }
          final Element[] infoWindow = getElementsByClassName(links[j], "span",
              CMF_PREFIX + "-infowindow");
          final Marker m = new Marker((double) Date.parse(date),
              (ds[0].getRangeTop() - ds[0].getRangeBottom()) / 2, label, 0);
          attachOnClick(links[j], m);

          if (infoWindow != null && infoWindow[0] != null) {
            m.addOverlayClickListener(new OverlayClickListener() {
              public void onOverlayClick(Overlay overlay, int x, int y) {
                m.openInfoWindow(DOM.getInnerHTML(infoWindow[0]));
              }
            });
          }
          XYPlot plot = chart.getPlot();
          plot.addOverlay(m);
          String[] hdrs = getTableHeaders(elt);
          if (hdrs != null && hdrs.length > 1) {
            plot.getDomainAxis().setLabel(hdrs[0]);
            for (int k = 1; k < hdrs.length; k++) {
              plot.getRangeAxis(k - 1).setLabel(hdrs[k]);
            }
          }
        }
      }
      latch.decrement();
      chart.redraw();
    }
  }

  private static final char charCounter = 0;

  private static final String CMF_PREFIX = "cmf";

  private static final String CMF_CHART = CMF_PREFIX + "-chart";

  public static void clickMicroformatLink(Element link, Marker marker) {
    marker.click(1, 1);
  }

  public static native JavaScriptObject importMicroformatTable0(String id,
      Element table) /*-{

          var label="";
  
          var thead = table.getElementsByTagName("thead");
          var labels=[], axes=[], dateformat=null;
          var colgroup = table.getElementsByTagName("colgroup");
          var rangeFormats = [];
          var i = 0;
          if(colgroup && colgroup.item(0)) {
            var cols = colgroup.item(0).getElementsByTagName("col");
            if(cols && cols.length > 0) {
              var datecol=cols.item(0);
              if(datecol.className == "cmf-dateformat") {
                dateformat = datecol.getAttribute('title');
              }
              for(i=1; i<cols.length; i++) {
                var rcol = cols.item(i);
                if(rcol.className == "cmf-numberformat") {
                  rangeFormats[i] = rcol.getAttribute("title");
                }
              }
            }
          }
          
          if(thead && thead.item(0)) {
            var r = thead.item(0).getElementsByTagName("tr");
            var c= r.item(0).getElementsByTagName("th");
            for(i=0; i<c.length; i++) {
               label = c.item(i).innerHTML;
               labels.push(label);
               var axis=c.item(i).getAttribute("axis");
               if(!axis) { axis="default"; }
               axes.push(axis);
            }
          }
          var tbody = table.getElementsByTagName("tbody");

          if(tbody && tbody.item(0))
          {
            var rowList = tbody.item(0).getElementsByTagName("tr");

            var i;
            var domain=[], range=[];
            var numCols = rowList.item(0).getElementsByTagName("td").length;
            for(i=0; i<numCols-1; i++) {
                range.push([]);
            }
            for(i = 0; i<rowList.length; i++)
            {
             var colList = rowList.item(i).getElementsByTagName("td");
             var date = colList.item(0).innerHTML;
             var t = @org.timepedia.chronoscope.client.data.DateParser::parse(Ljava/lang/String;Ljava/lang/String;)(dateformat, date);
             domain.push(t);
              for(j = 1; j<colList.length; j++) {
                  range[j-1].push(parseFloat(colList.item(j).innerHTML));
              }
            }
            var result=[];
            for(i=0; i<numCols-1; i++) {
               result.push({ id: id, domain: domain, range: range[i], label: labels[i+1], axis: axes[i+1],
               format: rangeFormats[i+1]});
            }
            return result;
          }
          return [];
       }-*/;

  public static native JavaScriptObject importTable(String id) /*-{

       var table = $doc.getElementById(id);
       var rowList = table.getElementsByTagName("tr");
       var i;
       var domain=[], range=[];
       for(i = 0; i<rowList.length; i++)
       {
          var colList = rowList.item(i).getElementsByTagName("td");
          domain.push(colList.item(0).innerHTML);
          range.push(colList.item(1).innerHTML);
       }
{'fraction_non_alphanumeric': 0.13257828114590273, 'fraction_numerical': 0.007402472425790214, 'mean_word_length': 2.078851412944394, 'pattern_counts': {'":': 2, '<': 25, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '5016006', 'n_tokens_mistral': 4128, 'n_tokens_neox': 3984, 'n_words': 836}