Difference between revisions of "EscapeXML"

From Second Life Wiki
Jump to navigation Jump to search
(New page: =====EscapeXML===== <lsl> // Escape standard reserved XML chars with entities // Copyright 2008, JB Kraft // Released under BSD license // http://www.opensource.org/licenses/bsd-license.p...)
 
Line 13: Line 13:
string escapeXML( string str )
string escapeXML( string str )
{
{
     list escapeChars = ["&","&amp;","\"","&quot;","'","&#039","<","&lt;",">","&gt;"];
     list escapeChars = ["&","&amp;","\"","&quot;","'","&#039;","<","&lt;",">","&gt;"];
      
      
     integer i;
     integer i;

Revision as of 23:18, 8 March 2008

EscapeXML

<lsl> // Escape standard reserved XML chars with entities // Copyright 2008, JB Kraft // Released under BSD license // http://www.opensource.org/licenses/bsd-license.php


// ------------------------------------------------------------------------ // escape standard reserved XML chars with entities // ------------------------------------------------------------------------ string escapeXML( string str ) {

   list escapeChars = ["&","&","\"",""","'","'","<","<",">",">"];
   
   integer i;
   integer ndx;
   for( i = 0; i < llStringLength( str ); i++ ) {
       ndx = llListFindList( escapeChars, [llGetSubString(str, i, i)]);
       if( ndx != -1 ) {
           // start
           if( i == 0 ) {
               str = llList2String( escapeChars, ndx+1) + llGetSubString( str, i+1, -1 );
           }
           // end
           else if( i == llStringLength( str ) - 1) {
               str = llGetSubString( str, 0, -2) + llList2String( escapeChars, ndx+1);
           }
           //  middle
           else {
               str = llGetSubString( str, 0, i-1) + llList2String( escapeChars, ndx+1) 
                   + llGetSubString( str, i+1, -1 );
           }
           i += llStringLength( llList2String( escapeChars, ndx+1));
       }
   }
   return str;

}

// ------------------------------------------------------------------------ // D E F A U L T // ------------------------------------------------------------------------ default {

   state_entry()
   {
       llSay(0, escapeXML("<Black> & \"White\" & 'Red' & <Green>"));
   }

}


</lsl>