
import ch.qos.logback.classic.joran.JoranConfigurator;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.core.util.StatusPrinter;
import ch.qos.logback.core.joran.spi.JoranException;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;

import javax.sound.sampled.*;
import javax.sound.midi.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.Options;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.w3c.dom.bootstrap.DOMImplementationRegistry;
import org.w3c.dom.Document;
import org.w3c.dom.DOMConfiguration;
import org.w3c.dom.Element;
import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.LSOutput;
import org.w3c.dom.ls.LSSerializer;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;


import org.xml.sax.InputSource;


// set classpath=.;%classpath%
// set classpath=.\*;%classpath%
// javac crkodrt2Config.java
//
// java crkodrt2Config
//

public class crkodrt2Config
{

  static Logger c2_Logger;
  static int NumberOfStackTraceElementsToOutput = 20;
  static NodeList nlGEBTNNodeList;
  static Node nGEBTNitem0NodeInstance;
  static Node nGEBTNitem0FirstChildNodeInstance;
  static NodeList nlChartNodeList;
  static int intChartNodeListLength;
  static Node nChartNodeInstance;
  static NodeList nlChartChildNodeList;
  static int intChartChildNodeListLength;
  static Node nChartChildNodeInstance;
  static short shNodeType01;
  static String strNodeName01;
  static String strNodeValue01;
  static String strNodeValue03;
  static NodeList nlChart_XXChildNodeList;
  static int intChart_XXChildNodeListLength;
  static Node nChart_XXChildNodeInstance;
  static String strNodeName02;
  static short shNodeType02;
  static String strNodeValue02;
  static Receiver DesiredReceiver;
  static String TIMING_CLOCK_DivisorString;
  static int TIMING_CLOCK_Divisor;
  static String DesiredMidiDeviceInfoName;
  static FileInputStream fisPrintChartDataFileInputStream;
  static DocumentBuilderFactory dbfPCD_DocumentBuilderFactoryInstance;
  static DocumentBuilderFactory dbfC2C_DocumentBuilderFactoryInstance;
  static DocumentBuilder dbPCD_DocumentBuilderInstance;
  static DocumentBuilder dbC2C_DocumentBuilderInstance;
  static Document dC2C_DocumentInstance;
  static Element eC2C_RootElementInstance;
  static String strCrkodrt2ConfigPathname;
  static String Crkodrt2ConfigXmlData;
  static StringReader Crkodrt2ConfigStringReader;
  static InputSource Crkodrt2ConfigInputSource;
  static String Crkodrt2Config_DesiredMidiDeviceInfoName_ByRecordPathname;
  static org.w3c.dom.Document Crkodrt2Config_DesiredMidiDeviceInfoName_ByRecordDocument;
  static String crkodrt2ConfigPathname;
  static org.w3c.dom.Document crkodrt2ConfigDocument;


    public crkodrt2Config()
    {

    }



    public static void main(String[] CommandLineArguments)
    {

      // get a Logger so that we can send stuff to slf4j

      c2_Logger = LoggerFactory.getLogger(crkodrt2Config.class);
      LoggerContext LoggerContextInstance = (LoggerContext) LoggerFactory.getILoggerFactory();

      try
      {

        JoranConfigurator JoranConfiguratorInstance = new JoranConfigurator();
        JoranConfiguratorInstance.setContext(LoggerContextInstance);
        // Call context.reset() to clear any previous configuration, e.g. default
        // configuration. For multi-step configuration, omit calling context.reset().
        LoggerContextInstance.reset();
        String TempPathname = "./crkodrt2Config.logback.xml";
        System.err.println("crkodrt2Config.main:JoranConfiguratorInstance.doConfigure(" + TempPathname + ")");
        JoranConfiguratorInstance.doConfigure(TempPathname);
        StatusPrinter.printInCaseOfErrorsOrWarnings(LoggerContextInstance);

      }
      catch (Exception e)
      {
        System.err.println("c2_ERROR:" + e.getMessage());
        StackTraceElement[] steArray = e.getStackTrace();
        int steArraySize = java.lang.reflect.Array.getLength(steArray);
        for(int x = 0;x < NumberOfStackTraceElementsToOutput;x++)
        {
          if(x < steArraySize)
          {
            System.err.println("c2_ERROR:" + steArray[x].toString());
          }
        }
        System.exit(1);
      }


      try
      {








        strCrkodrt2ConfigPathname = "./crkodrt2Config.xml";
        dbfC2C_DocumentBuilderFactoryInstance = DocumentBuilderFactory.newInstance();

        Crkodrt2ConfigXmlData = LoadFileIntoString(strCrkodrt2ConfigPathname);

        // parse the Crkodrt2Config xml file into a DOM

        dbC2C_DocumentBuilderInstance = dbfC2C_DocumentBuilderFactoryInstance.newDocumentBuilder();
        Crkodrt2ConfigStringReader = new StringReader(Crkodrt2ConfigXmlData);
        Crkodrt2ConfigInputSource = new InputSource(Crkodrt2ConfigStringReader);
        dC2C_DocumentInstance = dbC2C_DocumentBuilderInstance.parse(Crkodrt2ConfigInputSource);

        // get the root element, which should be <Crkodrt2Config>

        eC2C_RootElementInstance = dC2C_DocumentInstance.getDocumentElement();

        // get the Crkodrt2Config data

        DesiredMidiDeviceInfoName = FindAndReturnXmlDataAssociatedWithTag(eC2C_RootElementInstance, "DesiredMidiDeviceInfoName");
        c2_Logger.info("crkodrt2Config:main:DesiredMidiDeviceInfoName:" + DesiredMidiDeviceInfoName);

        // get the TIMING_CLOCK_Divisor

        TIMING_CLOCK_DivisorString = FindAndReturnXmlDataAssociatedWithTag(eC2C_RootElementInstance, "TIMING_CLOCK_Divisor");
        TIMING_CLOCK_Divisor = Integer.parseInt(TIMING_CLOCK_DivisorString);
        c2_Logger.info("crkodrt2Config:main:TIMING_CLOCK_Divisor:" + TIMING_CLOCK_Divisor);


        System.out.println("here are the midi interfaces that are present");
        System.out.println();

        // find the desired midi device

        MidiDevice.Info[] MidiDeviceInfoArray = MidiSystem.getMidiDeviceInfo();
        int MidiDeviceInfoArraySize = java.lang.reflect.Array.getLength(MidiDeviceInfoArray);
        c2_Logger.info("crkodrt2Config:main:MidiDeviceInfoArraySize:" + MidiDeviceInfoArraySize);
        MidiDevice.Info DesiredMidiDeviceInfo = null;
        for
        (
         int MidiDeviceInfoArrayIndex = 0;
         MidiDeviceInfoArrayIndex < MidiDeviceInfoArraySize;
         MidiDeviceInfoArrayIndex++
        )
        {
          MidiDevice.Info CurrentMidiDeviceInfo = MidiDeviceInfoArray[MidiDeviceInfoArrayIndex];
          String CurrentDescription = CurrentMidiDeviceInfo.getDescription();
          String CurrentName = CurrentMidiDeviceInfo.getName();
          String CurrentVendor = CurrentMidiDeviceInfo.getVendor();
          String CurrentVersion = CurrentMidiDeviceInfo.getVersion();
          c2_Logger.info("crkodrt2Config:main:MidiDeviceInfoArray[" +
                             MidiDeviceInfoArrayIndex +
                             "]" +
                             ":CurrentDescription:" +   CurrentDescription +
                             ":CurrentName:" +          CurrentName        +
                             ":CurrentVendor:" +        CurrentVendor      +
                             ":CurrentVersion:" +       CurrentVersion
                            );
          System.out.println(MidiDeviceInfoArrayIndex +
                             ":" +   CurrentDescription +
                             ":" +          CurrentName        +
                             ":" +        CurrentVendor      +
                             ":" +       CurrentVersion
                            );



          // c2_Logger.info("crkodrt2Config:main:if(CurrentName.compareTo(" + DesiredMidiDeviceInfoName + ") == 0):" + CurrentName);
          // if(CurrentName.compareTo(DesiredMidiDeviceInfoName) == 0)
          // {
          //   DesiredMidiDeviceInfo = CurrentMidiDeviceInfo;
          // }
        }

        System.out.println();
        System.out.println("type the number of the one you wish to use and then press Enter");
        System.out.println();

        Scanner ScannerInstance = new Scanner(System.in);

        String TempString = ScannerInstance.nextLine();

        int MidiDeviceInfoArrayIndex = Integer.parseInt(TempString);
        MidiDevice.Info CurrentMidiDeviceInfo = MidiDeviceInfoArray[MidiDeviceInfoArrayIndex];
        DesiredMidiDeviceInfo = CurrentMidiDeviceInfo;
        String CurrentDescription = CurrentMidiDeviceInfo.getDescription();
        String CurrentName = CurrentMidiDeviceInfo.getName();
        String CurrentVendor = CurrentMidiDeviceInfo.getVendor();
        String CurrentVersion = CurrentMidiDeviceInfo.getVersion();
        System.out.println();
        System.out.println("you selected");
        System.out.println();
        System.out.println(MidiDeviceInfoArrayIndex +
                           ":" +   CurrentDescription +
                           ":" +          CurrentName        +
                           ":" +        CurrentVendor      +
                           ":" +       CurrentVersion
                          );


        c2_Logger.info("crkodrt2Config:main:DesiredMidiDevice = MidiSystem.getMidiDevice(DesiredMidiDeviceInfo)");
        MidiDevice DesiredMidiDevice = MidiSystem.getMidiDevice(DesiredMidiDeviceInfo);
        c2_Logger.info("crkodrt2Config:main:DesiredMidiDevice.open()");
        DesiredMidiDevice.open();
        c2_Logger.info("crkodrt2Config:main:DesiredReceiver = DesiredMidiDevice.getReceiver()");
        DesiredReceiver = DesiredMidiDevice.getReceiver();








        // build the pathname to the Crkodrt2Config_DesiredMidiDeviceInfoName_ByRecord Xml and load the contents of that file into a string

        Crkodrt2Config_DesiredMidiDeviceInfoName_ByRecordPathname = "Crkodrt2Config_DesiredMidiDeviceInfoName_ByRecord.xml";
        c2_Logger.info("crkodrt2Config:main:Crkodrt2Config_DesiredMidiDeviceInfoName_ByRecordXmlData = LoadFileIntoString(" + Crkodrt2Config_DesiredMidiDeviceInfoName_ByRecordPathname + ")");
        String Crkodrt2Config_DesiredMidiDeviceInfoName_ByRecordXmlData = LoadFileIntoString(Crkodrt2Config_DesiredMidiDeviceInfoName_ByRecordPathname);

        Crkodrt2Config_DesiredMidiDeviceInfoName_ByRecordXmlData = Crkodrt2Config_DesiredMidiDeviceInfoName_ByRecordXmlData.replace("DMDIN_Dummy", CurrentName );

        // instantiate a Dom Document object with the Crkodrt2Config_DesiredMidiDeviceInfoName_ByRecord data

        DocumentBuilderFactory Crkodrt2Config_DesiredMidiDeviceInfoName_ByRecordDocumentBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder Crkodrt2Config_DesiredMidiDeviceInfoName_ByRecordDocumentBuilder = Crkodrt2Config_DesiredMidiDeviceInfoName_ByRecordDocumentBuilderFactory.newDocumentBuilder();
        StringReader Crkodrt2Config_DesiredMidiDeviceInfoName_ByRecordStringReader = new StringReader(Crkodrt2Config_DesiredMidiDeviceInfoName_ByRecordXmlData);
        InputSource Crkodrt2Config_DesiredMidiDeviceInfoName_ByRecordInputSource = new InputSource(Crkodrt2Config_DesiredMidiDeviceInfoName_ByRecordStringReader);
        c2_Logger.info("crkodrt2Config:main:Crkodrt2Config_DesiredMidiDeviceInfoName_ByRecordDocument = Crkodrt2Config_DesiredMidiDeviceInfoName_ByRecordDocumentBuilder.parse[Crkodrt2Config_DesiredMidiDeviceInfoName_ByRecordInputSource]");
        Crkodrt2Config_DesiredMidiDeviceInfoName_ByRecordDocument = Crkodrt2Config_DesiredMidiDeviceInfoName_ByRecordDocumentBuilder.parse(Crkodrt2Config_DesiredMidiDeviceInfoName_ByRecordInputSource);

        // build the pathname to the crkodrt2Config Xml and load the contents of that file into a string

        crkodrt2ConfigPathname = "crkodrt2Config.xml";
        c2_Logger.info("crkodrt2Config:main:crkodrt2ConfigXmlData = LoadFileIntoString(" + crkodrt2ConfigPathname + ")");
        String crkodrt2ConfigXmlData = LoadFileIntoString(crkodrt2ConfigPathname);

        // instantiate a Dom Document object with the crkodrt2Config data

        DocumentBuilderFactory crkodrt2ConfigDocumentBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder crkodrt2ConfigDocumentBuilder = crkodrt2ConfigDocumentBuilderFactory.newDocumentBuilder();
        StringReader crkodrt2ConfigStringReader = new StringReader(crkodrt2ConfigXmlData);
        InputSource crkodrt2ConfigInputSource = new InputSource(crkodrt2ConfigStringReader);
        c2_Logger.info("crkodrt2Config:main:crkodrt2ConfigDocument = crkodrt2ConfigDocumentBuilder.parse[crkodrt2ConfigInputSource]");
        crkodrt2ConfigDocument = crkodrt2ConfigDocumentBuilder.parse(crkodrt2ConfigInputSource);


        // find the <Record> node in the Crkodrt2Config_DesiredMidiDeviceInfoName_ByRecordTemplate

        c2_Logger.info("crkodrt2Config:main:RecordNodeList = Crkodrt2Config_DesiredMidiDeviceInfoName_ByRecordDocument.getElementsByTagName(\"Record\")");
        NodeList RecordNodeList = Crkodrt2Config_DesiredMidiDeviceInfoName_ByRecordDocument.getElementsByTagName("Record");
        int RecordNodeListLength = RecordNodeList.getLength();
        c2_Logger.info("crkodrt2Config:main:RecordNodeListLength:" + RecordNodeListLength);


        // get the <Record> Node ... there should only be one

        org.w3c.dom.Node CurrentRecordNode = RecordNodeList.item(0);
        short CurrentRecordNodeType = CurrentRecordNode.getNodeType();
        c2_Logger.info("crkodrt2Config:main:CurrentRecordNodeType:" + CurrentRecordNodeType);

        // create the Crkodrt2Config verb name that we are looking for

        String DesiredDesiredMidiDeviceInfoNameValue = "DesiredMidiDeviceInfoName";


        // find the <Crkodrt2Config> node in the crkodrt2Config

        c2_Logger.info("crkodrt2Config:main:Crkodrt2ConfigNodeList = crkodrt2ConfigDocument.getElementsByTagName(\"Crkodrt2Config\")");
        NodeList Crkodrt2ConfigNodeList = crkodrt2ConfigDocument.getElementsByTagName("Crkodrt2Config");
        int Crkodrt2ConfigNodeListLength = Crkodrt2ConfigNodeList.getLength();
        c2_Logger.info("crkodrt2Config:main:Crkodrt2ConfigNodeListLength:" + Crkodrt2ConfigNodeListLength);


        // get the <Crkodrt2Config> Node ... there should only be one

        org.w3c.dom.Node CurrentCrkodrt2ConfigNode = Crkodrt2ConfigNodeList.item(0);
        short CurrentCrkodrt2ConfigNodeType = CurrentCrkodrt2ConfigNode.getNodeType();
        c2_Logger.info("crkodrt2Config:main:CurrentCrkodrt2ConfigNodeType:" + CurrentCrkodrt2ConfigNodeType);

        // import the Crkodrt2Config_DesiredMidiDeviceInfoName_ByRecordTemplate into the Crkodrt2Config dom

        c2_Logger.info("crkodrt2Config:main:NewRecordNode = crkodrt2ConfigDocument.importNode(CurrentRecordNode, true)");
        Node NewRecordNode = crkodrt2ConfigDocument.importNode(CurrentRecordNode, true);

        // loop through the Nodes in the crkodrt2Config until we find the node that contains the DesiredMidiDeviceInfoName that we are looking for

        org.w3c.dom.Element CurrentCrkodrt2ConfigElement = (org.w3c.dom.Element)CurrentCrkodrt2ConfigNode;
        c2_Logger.info("crkodrt2Config:main:Crkodrt2ConfigRootNode = crkodrt2ConfigDocument.getFirstChild()");
        Node Crkodrt2ConfigRootNode = crkodrt2ConfigDocument.getFirstChild();
        c2_Logger.info("crkodrt2Config:main:Crkodrt2ConfigRecordNodeList = Crkodrt2ConfigRootNode.getChildNodes()");
        NodeList Crkodrt2ConfigRecordNodeList = Crkodrt2ConfigRootNode.getChildNodes();
        int Crkodrt2ConfigRecordNodeListLength = Crkodrt2ConfigRecordNodeList.getLength();
        c2_Logger.info("crkodrt2Config:main:Crkodrt2ConfigRecordNodeListLength:" + Crkodrt2ConfigRecordNodeListLength);
        boolean DesiredNodeFoundFlag = false;
        int Crkodrt2ConfigRecordNodeListIndex = 0;
        while(
              (Crkodrt2ConfigRecordNodeListIndex < Crkodrt2ConfigRecordNodeListLength) &&
              ( ! DesiredNodeFoundFlag )
             )
        {
          org.w3c.dom.Node CurrentCPRecordNode = Crkodrt2ConfigRecordNodeList.item(Crkodrt2ConfigRecordNodeListIndex);
          short CurrentCPRecordNodeType = CurrentCPRecordNode.getNodeType();
          String CurrentCPRecordNodeName = CurrentCPRecordNode.getNodeName();
          c2_Logger.info("crkodrt2Config:main:CurrentCPRecordNodeType:" + CurrentCPRecordNodeType + ":if(CurrentCPRecordNodeName.compareTo(\"Record\") == 0):" + CurrentCPRecordNodeName);

          // we are only interested in <Record> elements

          if(CurrentCPRecordNodeName.compareTo("Record") == 0)
          {

            // drill down into the <Record> and extract the DesiredMidiDeviceInfoName

            org.w3c.dom.Element CurrentElement = (org.w3c.dom.Element)CurrentCPRecordNode;

            c2_Logger.info("crkodrt2Config:main:DesiredMidiDeviceInfoNameNodeList = CurrentElement.getElementsByTagName(\"DesiredMidiDeviceInfoName\")");
            NodeList DesiredMidiDeviceInfoNameNodeList = CurrentElement.getElementsByTagName("DesiredMidiDeviceInfoName");
            int DesiredMidiDeviceInfoNameNodeListLength = DesiredMidiDeviceInfoNameNodeList.getLength();
            c2_Logger.info("crkodrt2Config:main:DesiredMidiDeviceInfoNameNodeListLength:" + DesiredMidiDeviceInfoNameNodeListLength);
            org.w3c.dom.Node CurrentDesiredMidiDeviceInfoNameNode = DesiredMidiDeviceInfoNameNodeList.item(0);
            short CurrentDesiredMidiDeviceInfoNameNodeType = CurrentDesiredMidiDeviceInfoNameNode.getNodeType();
            c2_Logger.info("crkodrt2Config:main:CurrentDesiredMidiDeviceInfoNameNodeType:" + CurrentDesiredMidiDeviceInfoNameNodeType);
            if(CurrentDesiredMidiDeviceInfoNameNode.hasChildNodes())
            {
              org.w3c.dom.Text CurrentText = (org.w3c.dom.Text)CurrentDesiredMidiDeviceInfoNameNode.getFirstChild();
              String CurrentDesiredMidiDeviceInfoName = CurrentText.getWholeText();

              // // if this is the correct node, replace it with the new data, and write the file back out
              //
              // c2_Logger.info("crkodrt2Config:main:if(DesiredDesiredMidiDeviceInfoNameValue.compareTo(" + CurrentDesiredMidiDeviceInfoName + "))");
              // if(DesiredDesiredMidiDeviceInfoNameValue.compareTo(CurrentDesiredMidiDeviceInfoName) == 0)
              // {


                DesiredNodeFoundFlag = true;

                // replace the node with the modified Crkodrt2Config_DesiredMidiDeviceInfoName_ByRecordTemplate node

                c2_Logger.info("crkodrt2Config:main:org.w3c.dom.Node ParentNode = CurrentCPRecordNode.getParentNode()");
                org.w3c.dom.Node ParentNode = CurrentCPRecordNode.getParentNode();
                c2_Logger.info("crkodrt2Config:main:ParentNode:" + ParentNode.getNodeName());
                c2_Logger.info("crkodrt2Config:main:ParentNode.replaceChild(NewRecordNode, CurrentCPRecordNode)");
                ParentNode.replaceChild(NewRecordNode, CurrentCPRecordNode);

                // write the data back to the file

                c2_Logger.info("crkodrt2Config:main:DOMImplementationRegistryInstance = DOMImplementationRegistry.newInstance()");
                DOMImplementationRegistry DOMImplementationRegistryInstance = DOMImplementationRegistry.newInstance();
                c2_Logger.info("crkodrt2Config:main:DOMImplementationLSInstance = (DOMImplementationLS) DOMImplementationRegistryInstance.getDOMImplementation(\"XML 3.0 LS 3.0\")");
                DOMImplementationLS DOMImplementationLSInstance = (DOMImplementationLS) DOMImplementationRegistryInstance.getDOMImplementation("XML 3.0 LS 3.0");
                if (DOMImplementationLSInstance == null)
                {
                  c2_Logger.error("crkodrt2Config:main:c2_ERROR:No DOMImplementation found !");
                }

                c2_Logger.info("crkodrt2Config:main:DOMImplementationLSInstance:" + DOMImplementationLSInstance.getClass().getName());
                c2_Logger.info("crkodrt2Config:main:LSSerializerInstance = DOMImplementationLSInstance.createLSSerializer()");
                LSSerializer LSSerializerInstance = DOMImplementationLSInstance.createLSSerializer();

                c2_Logger.info("crkodrt2Config:main:DOMConfiguration DOMConfigurationInstance = LSSerializerInstance.getDomConfig()");
                DOMConfiguration DOMConfigurationInstance = LSSerializerInstance.getDomConfig();
                c2_Logger.info("crkodrt2Config:main:DOMConfigurationInstance.setParameter(\"format-pretty-print\", Boolean.TRUE)");
                DOMConfigurationInstance.setParameter("format-pretty-print", Boolean.TRUE);


                c2_Logger.info("crkodrt2Config:main:LSOutputInstance = DOMImplementationLSInstance.createLSOutput()");
                LSOutput LSOutputInstance = DOMImplementationLSInstance.createLSOutput();
                c2_Logger.info("crkodrt2Config:main:LSOutputInstance.setEncoding(\"UTF-8\")");
                LSOutputInstance.setEncoding("UTF-8");

                FileWriter LSOutputCharacterStreamFileWriter;
                BufferedWriter LSOutputCharacterStreamBufferedWriter;

                c2_Logger.info("crkodrt2Config:main:LSOutputCharacterStreamFileWriter = new FileWriter(" + crkodrt2ConfigPathname + ")");
                LSOutputCharacterStreamFileWriter = new FileWriter(crkodrt2ConfigPathname);
                c2_Logger.info("crkodrt2Config:main:");
                LSOutputCharacterStreamBufferedWriter = new BufferedWriter(LSOutputCharacterStreamFileWriter);

                c2_Logger.info("crkodrt2Config:main:LSOutputInstance.setCharacterStream(LSOutputCharacterStreamBufferedWriter)");
                LSOutputInstance.setCharacterStream(LSOutputCharacterStreamBufferedWriter);
                c2_Logger.info("crkodrt2Config:main:LSSerializerInstance.write(crkodrt2ConfigDocument, LSOutputInstance)");
                LSSerializerInstance.write(crkodrt2ConfigDocument, LSOutputInstance);

                LSOutputCharacterStreamBufferedWriter.close();
                LSOutputCharacterStreamFileWriter.close();

              // }
            }
          }

          Crkodrt2ConfigRecordNodeListIndex++;
        }





















      }
      catch (Exception e)
      {
        c2_Logger.error("Crkodrt2Config_DesiredMidiDeviceInfoName_ByRecord.main:c2_ERROR:" + e.getMessage());
        StackTraceElement[] steArray = e.getStackTrace();
        int steArraySize = java.lang.reflect.Array.getLength(steArray);
        for(int x = 0;x < NumberOfStackTraceElementsToOutput;x++)
        {
          if(x < steArraySize)
          {
            c2_Logger.error("Crkodrt2Config_DesiredMidiDeviceInfoName_ByRecord.main:c2_ERROR:" + steArray[x].toString());
          }
        }
        System.exit(1);
      }

    }






















  // function to load a file into a String
  // call with:
  //                 LFIS_LoadFileIntoStringFileAbsolutePathname = the absolute path where the file can be found
  // returns:
  //                 String containing the contents of the file

  public static String LoadFileIntoString(String LFIS_LoadFileIntoStringFileAbsolutePathname)
  {
    try
    {




      String LoadFileIntoStringReturnString;
      File LoadFileIntoStringFileFile;
      FileReader LoadFileIntoStringFileFileReader;
      BufferedReader LoadFileIntoStringFileBufferedReader;
      String TempString;

      // read in the file

      // c2_Logger.info("CurrentDatabaseName:" + CurrentDatabaseName + ":ProcessIdString:" + ProcessIdString + ":LoadFileIntoString:LoadFileIntoStringFileReader = new File(" + LFIS_LoadFileIntoStringFileAbsolutePathname + ")");
      LoadFileIntoStringFileFile = new File(LFIS_LoadFileIntoStringFileAbsolutePathname);
      if( LoadFileIntoStringFileFile.exists() )
      {
        // c2_Logger.info("CurrentDatabaseName:" + CurrentDatabaseName + ":ProcessIdString:" + ProcessIdString + ":LoadFileIntoString:LoadFileIntoStringFileReader = new FileReader(LoadFileIntoStringFileFile)");
        LoadFileIntoStringFileFileReader = new FileReader(LoadFileIntoStringFileFile);
        // c2_Logger.info("CurrentDatabaseName:" + CurrentDatabaseName + ":ProcessIdString:" + ProcessIdString + ":LoadFileIntoString:LoadFileIntoStringFileBufferedReader = new BufferedReader[LoadFileIntoStringFileFileReader]");
        LoadFileIntoStringFileBufferedReader = new BufferedReader(LoadFileIntoStringFileFileReader);
        LoadFileIntoStringReturnString = "";
        TempString = LoadFileIntoStringFileBufferedReader.readLine();
        while(TempString != null)
        {
          LoadFileIntoStringReturnString = LoadFileIntoStringReturnString + TempString + "\n";
          TempString = LoadFileIntoStringFileBufferedReader.readLine();
        }
        LoadFileIntoStringFileBufferedReader.close();
        LoadFileIntoStringFileFileReader.close();
      }
      else
      {
        // c2_Logger.warn("CurrentDatabaseName:" + CurrentDatabaseName + ":ProcessIdString:" + ProcessIdString + ":LoadFileIntoString:c2_WARNING:" + LFIS_LoadFileIntoStringFileAbsolutePathname + ":DOES NOT EXIST!");
        LoadFileIntoStringReturnString = null;
      }
      return LoadFileIntoStringReturnString;

    }
    catch (Exception e)
    {
      c2_Logger.error("LoadFileIntoString:LFIS_ERROR:" + e.getMessage());
      StackTraceElement[] steArray = e.getStackTrace();
      int steArraySize = java.lang.reflect.Array.getLength(steArray);
      for(int x = 0;x < NumberOfStackTraceElementsToOutput;x++)
      {
        if(x < steArraySize)
        {
          c2_Logger.error("LoadFileIntoString:LFIS_ERROR:" + steArray[x].toString());
        }
      }
      return null;
    }
  }























  // this function starts at a given XML Element
  // it traverses the DOM searching for the first element matching the name given
  // and returns the data associated with that element
  // if the data child element does not exist, then it returns an empty string

  public static String FindAndReturnXmlDataAssociatedWithTag(Element eTopElementInstance, String strDesiredXmlTagString)
  {

    String strFARXDAWTNodeName;
    short shFARXDAWTNodeType;
    String strFARXDAWTNodeValue;

    try
    {
      nlGEBTNNodeList = eTopElementInstance.getElementsByTagName(strDesiredXmlTagString);
      strFARXDAWTNodeValue = "";
      if(nlGEBTNNodeList.getLength() > 0)
      {
        nGEBTNitem0NodeInstance = nlGEBTNNodeList.item(0);
        if(nGEBTNitem0NodeInstance.hasChildNodes())
        {
          nGEBTNitem0FirstChildNodeInstance = nGEBTNitem0NodeInstance.getFirstChild();
          strFARXDAWTNodeName = nGEBTNitem0FirstChildNodeInstance.getNodeName();
          shFARXDAWTNodeType = nGEBTNitem0FirstChildNodeInstance.getNodeType();
          strFARXDAWTNodeValue = nGEBTNitem0FirstChildNodeInstance.getNodeValue();
        }
      }
    }
    catch (Exception e)
    {
      c2_Logger.error("FindAndReturnXmlDataAssociatedWithTag:FARXDAWT_ERROR:" + e.getMessage());
      StackTraceElement[] steArray = e.getStackTrace();
      int steArraySize = java.lang.reflect.Array.getLength(steArray);
      for(int x = 0;x < NumberOfStackTraceElementsToOutput;x++)
      {
        if(x < steArraySize)
        {
          c2_Logger.error("FindAndReturnXmlDataAssociatedWithTag:FARXDAWT_ERROR:" + steArray[x].toString());
        }
      }
      return null;
    }

    return strFARXDAWTNodeValue;
  }




























}
