<HTML>
<HEAD>
<!-- Copyright 2000, 2001, 2002, 2003 Macromedia, Inc. All rights reserved. -->

<TITLE>ASP JavaScript Translator</TITLE>

<meta http-equiv="Content-Type" content="text/html; charset=">

<script SRC="../Shared/MM/Scripts/CMN/quickString.js"></SCRIPT>
<SCRIPT SRC="../Shared/Common/Scripts/dwscripts.js"></SCRIPT>
<SCRIPT SRC="TransData.js"></SCRIPT>
<SCRIPT SRC="TMCallback.js"></SCRIPT>
<SCRIPT SRC="Translator.js"></SCRIPT>
<SCRIPT SRC="TranslationManager.js"></SCRIPT>

<SCRIPT>

// *************** GLOBALS VARS *****************

var SERVER_MODEL_NAME = "JSP";
var SERVER_MODEL_FOLDER = "JSP";
var TRANSLATOR_CLASS  = "MM_JSPSCRIPT";
var TRANSLATOR_NAME   = SERVER_MODEL_NAME;

var SUCCESS_MARKER    = "<!-- MMDW:success -->";

var BEGIN_LOCK        = "<mm:beginlock translatorClass=\"MM_LIVE_DATA\" type=\"" + TRANSLATOR_CLASS + "\" orig=\"%s\">";
var END_LOCK          = "<mm:endlock>";
var TRANSLATED_ATTR   = "mmvisible=false";

// Specify which tags are jsp
var prefixArray = new Array();
prefixArray[0] = "jsp:";
prefixArray[1] = "jspx:";
prefixArray[2] = "java:";
prefixArray[3] = "javax:";
prefixArray[4] = "servlet:";
prefixArray[5] = "sun:";
prefixArray[6] = "sunw:";

// Specify which tags cannot contain editable HTML in the returned doc
var nonHtmlArray = new Array();
nonHtmlArray[0] = "jsp:plugin";


var debugTranslator   = false;
var macBeforeFileName = "Desktop:BeforeTranslation.txt";
var macAfterFileName  = "Desktop:AfterTranslation.txt";
var winBeforeFileName = "C:\\BeforeTranslation.txt";
var winAfterFileName  = "C:\\AfterTranslation.txt";

var liveDebugTranslator   = false;
var liveMacBeforeFileName = "Desktop:LiveBeforeTranslation.txt";
var liveMacAfterFileName  = "Desktop:LiveAfterTranslation.txt";
var liveWinBeforeFileName = "C:\\LiveBeforeTranslation.txt";
var liveWinAfterFileName  = "C:\\LiveAfterTranslation.txt";


// ******************* API **********************

//--------------------------------------------------------------------
// FUNCTION:
//   getTranslatorInfo
//
// DESCRIPTION:
//   Provides information about the translator and the files it can
//   act upon
//
// ARGUMENTS:
//   none
//
// RETURNS:
//   JavaScript array
//--------------------------------------------------------------------

function getTranslatorInfo()
{
  returnArray = new Array(9);

  returnArray[0] = TRANSLATOR_CLASS;   // The TRANSLATOR_CLASS
  returnArray[1] = TRANSLATOR_NAME;    // The title
  returnArray[2] = "1" 				   // The number of extensions
  returnArray[3] = "%JSP%" 		   	   // Extensions - indicate that we run on any
									   // extension for the JSP document type
									   // as defined in MMDocumentTypes.xml
  returnArray[4] = "2";                // The number of expressions
  returnArray[5] = "<%";               // Expression for script
  returnArray[6] = "<jsp:";            // Expression for jsp tags
  returnArray[7] = "byString";         // Run if doc contains expr
  returnArray[8] = "50";               // priority order to apply translator

  return returnArray
}


//--------------------------------------------------------------------
// FUNCTION:
//   translateMarkup
//
// DESCRIPTION:
//   This function performs the translation.
//
// ARGUMENTS:
//   docNameStr - string - URL of the document to be translated
//   siteRootStr - string - URL of the site root
//   inStr - string - the contents of the document
//
// RETURNS:
//   string - the translated contents of the document
//--------------------------------------------------------------------

function translateMarkup(docNameStr, siteRootStr, inStr)
{
  var outStr = "";

  var serverModel = "";
  if (dw.getDocumentDOM())
  {
    serverModel = dw.getDocumentDOM().serverModel.getFolderName();
    if (serverModel == "")
    {
      serverModel = dw.getDocumentDOM().getServerModelFolderNameFromDoc();
    }
  }

  if (serverModel == SERVER_MODEL_FOLDER &&
      inStr.length > 0)
  {

    if (debugTranslator)
    {
      if (dwscripts.IS_MAC)
        DWfile.write( macBeforeFileName, inStr );
      else
        DWfile.write( winBeforeFileName, inStr );
    }

    outStr = miniTranslateMarkup(docNameStr, siteRootStr, inStr);

    // update Server Behavior list
    dw.serverBehaviorInspector.findAllServerBehaviors();

    if (debugTranslator)
    {
      if (dwscripts.IS_MAC)
        DWfile.write( macAfterFileName, outStr );
      else
        DWfile.write( winAfterFileName, outStr );
    }

  }

  return outStr;
}


//--------------------------------------------------------------------
// FUNCTION:
//   miniTranslateMarkup
//
// DESCRIPTION:
//   This function translates the given string.  It is broken out
//   so that other functions can call the translator.
//
// ARGUMENTS:
//   docNameStr - string - URL of the document to be translated
//   siteRootStr - string - URL of the site root
//   inStr - string - the contents of the document
//
// RETURNS:
//   string
//--------------------------------------------------------------------

function miniTranslateMarkup(docNameStr, siteRootStr, inStr)
{
  var outStr = "";

  // translate
  if (inStr.indexOf("<%") != -1 || inStr.indexOf("<jsp:") != -1)
  {
    var TM = new TranslationManager(TRANSLATOR_CLASS, SERVER_MODEL_FOLDER, "");
    TM.serverModelAlwaysCheckTag = myAlwaysCheckTag;
    TM.serverModelAlwaysCheckAttribute = myAlwaysCheckAttribute;
    //TM.defaultDirectiveTag = "MM_JSPSCRIPT";
    outStr = TM.translate(inStr);
  }

  return outStr;
}


//--------------------------------------------------------------------
// FUNCTION:
//   myAlwaysCheckTag
//
// DESCRIPTION:
//   This function is called by the translation manager to determine
//   if the given tag should be checked for dynamic info.
//
// ARGUMENTS:
//   tagName - string - the name of the tag to check
//
// RETURNS:
//   boolean
//--------------------------------------------------------------------

function myAlwaysCheckTag(tagName)
{
  return (tagName.substr(0, 4) == "jsp:");
}


//--------------------------------------------------------------------
// FUNCTION:
//   myAlwaysCheckAttribute
//
// DESCRIPTION:
//   This function is called by the translation manager to determine
//   if the given attribute code should be checked for dynamic info.
//
// ARGUMENTS:
//   code - string - the code to check
//
// RETURNS:
//   boolean
//--------------------------------------------------------------------

function myAlwaysCheckAttribute(code)
{
  var result = (code.indexOf("<%") != -1 ||
                code.indexOf("<jsp:") != -1);
  return result;
}


//--------------------------------------------------------------------
// FUNCTION:
//   liveDataTranslateMarkup
//
// DESCRIPTION:
//   This function performs the translation while in live data mode
//
// ARGUMENTS:
//   docNameStr - string - the URL of the document to be translated
//   siteRootStr - string - the URL of the site root
//   inStr - string - the contents of the document
//
// RETURNS:
//   string - the translated contents of the document
//--------------------------------------------------------------------

function liveDataTranslateMarkup( docNameStr, siteRootStr, inStr )
{
  var outStr = "";

  if (dw.getDocumentDOM() &&
      dw.getDocumentDOM().serverModel.getFolderName() == SERVER_MODEL_FOLDER &&
      inStr.length > 0 &&
      siteRootStr.length > 0)
  {

    if (liveDebugTranslator)
    {
      if (dwscripts.IS_MAC)
        DWfile.write( liveMacBeforeFileName, inStr );
      else
        DWfile.write( liveWinBeforeFileName, inStr );
    }

    var success = true;
    var translated = "";

    // Send init tags to the server and request requst them
    var initTags = dreamweaver.getLiveDataInitTags();
    if (initTags.length > 0)
    {
      // Add a marker at the end so we can check for successful completion
      initTags = initTags + SUCCESS_MARKER;

      // Send file to server and get the response
      translated = dreamweaver.liveDataTranslate(docNameStr, initTags);

      // If we can't find our marker, report that an error occurred
      if (translated.data.lastIndexOf(SUCCESS_MARKER) == -1)
      {
        dreamweaver.setLiveDataError(translated.data);
        success = false;
      }
    }

    // if initialization was successful, send the document
    if (success)
    {
      // Wrap labels around the server directives
      LiveDataTranslator.initialize();
      LiveDataTranslator.beginPreTranslate("<%", "%>", prefixArray, nonHtmlArray);
      dreamweaver.scanSourceString(inStr, LiveDataTranslator);
      var labels = LiveDataTranslator.endPreTranslate(inStr);

      // Add a marker at the end so we can check for successful completion
      var labelsAndMarker = labels + SUCCESS_MARKER;

      // If we send a file with Mac-style "\r" line endings to a Unix server,
      // the server will ignore the "\r" characters (line ending is "\n" on
      // Unix) and send back a file with no line endings.  Instead, we'll
      // convert line endings to "\r\n" before sending to the server.
      labelsAndMarker = LiveDataTranslator.setLineEndingsToMatch("\r\n", labelsAndMarker);

      // Send file to server and get the response
      translated = dreamweaver.liveDataTranslate(docNameStr, labelsAndMarker);

      // If the scripts on the page were not executed or there are no scripts
      // on the page, then just send the page through the normal translator
      if (translated.data == labels)
      {
        return translateMarkup(docNameStr, siteRootStr, inStr);
      }

      // If we can't find our marker, report that an error occurred
      var index = translated.data.lastIndexOf(SUCCESS_MARKER);
      if (index == -1)
      {
        // Request page without the labels, so we get the right line numbers
        translated = dreamweaver.liveDataTranslate(docNameStr, inStr);

         // Tell Dreamweaver to display an error
        dreamweaver.setLiveDataError(translated.data);

        LiveDataTranslator.cleanup();
        success = false;
      }
    }

    // if the page was returned successfully, get the returned string
    if (success)
    {
      translated.data = translated.data.substr(0, index);

      // Some HTTP servers change the line endings in the file
      translated.data = LiveDataTranslator.setLineEndingsToMatch(inStr, translated.data);

      // If the scripts on the page were executed and there are scripts
      // on the page, then remove the labels and add lock tags
      if (translated.data != labels)
      {
        LiveDataTranslator.beginPostTranslate();
        dreamweaver.scanSourceString(translated.data, LiveDataTranslator);
        outStr = LiveDataTranslator.endPostTranslate(inStr, translated.data, BEGIN_LOCK, END_LOCK, TRANSLATED_ATTR);
        LiveDataTranslator.cleanup();
      }
    }

    if (liveDebugTranslator)
    {
      if (dwscripts.IS_MAC)
        DWfile.write( liveMacAfterFileName, outStr );
      else
        DWfile.write( liveWinAfterFileName, outStr );
    }

  }

  return outStr;
}

</SCRIPT>

</HEAD>

<BODY>
</BODY>
</HTML>
