EscapeXML

From Second Life Wiki
Jump to navigation Jump to search
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>