<commit_before>
module Camel {

  /**
   * Looks up the route XML for the given context and selected route and
   * processes the selected route's XML with the given function
   */
  export function processRouteXml(workspace:Workspace, jolokia, folder, onRoute) {
    var selectedRouteId = getSelectedRouteId(workspace, folder);
    var mbean = getSelectionCamelContextMBean(workspace);

    function onRouteXml(response) {
      var route = null;
      var data = response ? response.value : null;
      if (data) {
        var doc = $.parseXML(data);
        console.log("Finding route for ID " + selectedRouteId);
        var routes = $(doc).find("route[id='" + selectedRouteId + "']");
        if (routes && routes.length) {
          route = routes[0];
        }
      }
      onRoute(route);
    }

    if (mbean && selectedRouteId) {
      jolokia.request(
              {type: 'exec', mbean: mbean, operation: 'dumpRoutesAsXml()'},
              onSuccess(onRouteXml, {error: onRouteXml}));
    } else {
      if (!selectedRouteId) {
        console.log("No selectedRouteId when trying to lazy load the route!")
      }
      onRoute(null);
    }
  }

  /**
   * Returns the URI string for the given EIP pattern node or null if it is not applicable
   */
  export function getRouteNodeUri(node) {
    var uri = null;
    if (node) {
      uri = node.getAttribute("uri");
      if (!uri) {
        var ref = node.getAttribute("ref");
        if (ref) {
          uri = "ref:" + ref;
        }
      }
    }
    return uri;
  }

  export function getRouteNodeJSON(routeXmlNode, answer = {}) {
    if (routeXmlNode) {
      angular.forEach(routeXmlNode.attributes, (attr) => {
        answer[attr.name] = attr.value;
      });

      // lets look for nested elements and convert those
      // explicitly looking for expressions
      $(routeXmlNode).children("*").each((idx, element) => {
        var nodeName = element.nodeName;
        var langSettings = Camel.camelLanguageSettings(nodeName);
        if (langSettings) {
          // TODO the expression key could be anything really; how should we know?
          answer["expression"] = {
            language: nodeName,
            expression: element.textContent
          };
        } else {
          var nested = getRouteNodeJSON(element);
          if (nested) {
            answer[nodeName] = nested;
          }
        }
      });
    }
    return answer;
  }

  export function getRouteNodeIcon(nodeSettings) {
    var imageName = nodeSettings["icon"] || "generic24.png";
    return url("/app/camel/img/" + imageName);
  }

  export function getSelectedRouteNode(workspace:Workspace) {
    var selection = workspace.selection;
    return (selection && jmxDomain === selection.domain) ? selection["routeXmlNode"] : null;
  }

  /**
   * Looks up the given node name in the Camel schema
   */
  export function getCamelSchema(nodeId) {
    return Forms.lookupDefinition(nodeId, _apacheCamelModel);
  }

  /**
   * Looks up the Camel language settings for the given language name
   */
  export function camelLanguageSettings(nodeName) {
    return _apacheCamelModel.languages[nodeName];
  }

  export function loadCamelTree(xml: string) {
    var doc = $.parseXML(xml);

    // TODO get id from camelContext
    var id = "camelContext";
    var folder = new Folder(id);
    folder.addClass = "org-apache-camel-context";
    folder.domain = Camel.jmxDomain;
    folder.typeName = "context";

    var context = $(doc).find("camelContext");
    if (!context || !context.length) {
      context = $(doc).find("routes");
    }

    if (context && context.length) {
      $(context).children("route").each((idx, route) => {
        // TODO add a route node!!
          var id = route.getAttribute("id") || "route" + idx;
          var routeFolder = new Folder(id);
          routeFolder.addClass = "org-apache-camel-route";
          routeFolder.typeName = "routes";
          routeFolder.domain = Camel.jmxDomain;
          folder.children.push(routeFolder);

          addRouteChildren(routeFolder, route);
      });
    }
    return folder;
  }

  /**
   * Adds the route children to the given folder for each step in the route
   */
  export function addRouteChildren(folder: Folder, route) {
    folder.children = [];
    $(route).children("*").each((idx, n) => {
      var nodeName = n.nodeName;
      if (nodeName) {
        var nodeSettings = getCamelSchema(nodeName);
        if (nodeSettings) {
          var label = nodeSettings["title"] || nodeName;
          var uri = getRouteNodeUri(n);
          if (uri) {
            label += " " + uri;
          }
          var tooltip = nodeSettings["tooltip"] || nodeSettings["description"] || label;
          var imageUrl = getRouteNodeIcon(nodeSettings);<commit_msg>associate the route XML with the route node<commit_after>
module Camel {

  /**
   * Looks up the route XML for the given context and selected route and
   * processes the selected route's XML with the given function
   */
  export function processRouteXml(workspace:Workspace, jolokia, folder, onRoute) {
    var selectedRouteId = getSelectedRouteId(workspace, folder);
    var mbean = getSelectionCamelContextMBean(workspace);

    function onRouteXml(response) {
      var route = null;
      var data = response ? response.value : null;
      if (data) {
        var doc = $.parseXML(data);
        console.log("Finding route for ID " + selectedRouteId);
        var routes = $(doc).find("route[id='" + selectedRouteId + "']");
        if (routes && routes.length) {
          route = routes[0];
        }
      }
      onRoute(route);
    }

    if (mbean && selectedRouteId) {
      jolokia.request(
              {type: 'exec', mbean: mbean, operation: 'dumpRoutesAsXml()'},
              onSuccess(onRouteXml, {error: onRouteXml}));
    } else {
      if (!selectedRouteId) {
        console.log("No selectedRouteId when trying to lazy load the route!")
      }
      onRoute(null);
    }
  }

  /**
   * Returns the URI string for the given EIP pattern node or null if it is not applicable
   */
  export function getRouteNodeUri(node) {
    var uri = null;
    if (node) {
      uri = node.getAttribute("uri");
      if (!uri) {
        var ref = node.getAttribute("ref");
        if (ref) {
          uri = "ref:" + ref;
        }
      }
    }
    return uri;
  }

  export function getRouteNodeJSON(routeXmlNode, answer = {}) {
    if (routeXmlNode) {
      angular.forEach(routeXmlNode.attributes, (attr) => {
        answer[attr.name] = attr.value;
      });

      // lets look for nested elements and convert those
      // explicitly looking for expressions
      $(routeXmlNode).children("*").each((idx, element) => {
        var nodeName = element.nodeName;
        var langSettings = Camel.camelLanguageSettings(nodeName);
        if (langSettings) {
          // TODO the expression key could be anything really; how should we know?
          answer["expression"] = {
            language: nodeName,
            expression: element.textContent
          };
        } else {
          var nested = getRouteNodeJSON(element);
          if (nested) {
            answer[nodeName] = nested;
          }
        }
      });
    }
    return answer;
  }

  export function getRouteNodeIcon(nodeSettings) {
    var imageName = nodeSettings["icon"] || "generic24.png";
    return url("/app/camel/img/" + imageName);
  }

  export function getSelectedRouteNode(workspace:Workspace) {
    var selection = workspace.selection;
    return (selection && jmxDomain === selection.domain) ? selection["routeXmlNode"] : null;
  }

  /**
   * Looks up the given node name in the Camel schema
   */
  export function getCamelSchema(nodeId) {
    return Forms.lookupDefinition(nodeId, _apacheCamelModel);
  }

  /**
   * Looks up the Camel language settings for the given language name
   */
  export function camelLanguageSettings(nodeName) {
    return _apacheCamelModel.languages[nodeName];
  }

  export function loadCamelTree(xml: string) {
    var doc = $.parseXML(xml);

    // TODO get id from camelContext
    var id = "camelContext";
    var folder = new Folder(id);
    folder.addClass = "org-apache-camel-context";
    folder.domain = Camel.jmxDomain;
    folder.typeName = "context";

    var context = $(doc).find("camelContext");
    if (!context || !context.length) {
      context = $(doc).find("routes");
    }

    if (context && context.length) {
      $(context).children("route").each((idx, route) => {
        // TODO add a route node!!
          var id = route.getAttribute("id") || "route" + idx;
          var routeFolder = new Folder(id);
          routeFolder.addClass = "org-apache-camel-route";
          routeFolder.typeName = "routes";
          routeFolder.domain = Camel.jmxDomain;
          folder.children.push(routeFolder);

          addRouteChildren(routeFolder, route);
      });
    }
    return folder;
  }

  /**
   * Adds the route children to the given folder for each step in the route
   */
  export function addRouteChildren(folder: Folder, route) {
    folder.children = [];
    folder["routeXmlNode"] = route;
    $(route).children("*").each((idx, n) => {
      var nodeName = n.nodeName;
      if (nodeName) {
        var nodeSettings = getCamelSchema(nodeName);
        if (nodeSettings) {
          var label = nodeSettings["title"] || nodeName;
          var uri = getRouteNodeUri(n);
          if (uri) {
            label += " " + uri;
          }
          var tooltip = nodeSettings["tooltip"] || nodeSettings["description"] || label;
          var imageUrl = getRouteNodeIcon(nodeSettings);
{'fraction_non_alphanumeric': 0.12141967621419676, 'fraction_numerical': 0.0006226650062266501, 'mean_word_length': 2.447942754919499, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '2489018', 'n_tokens_mistral': 2766, 'n_tokens_neox': 2735, 'n_words': 728}