PDA

View Full Version : Parsing problem with characters " & < > "



SuperLes2006
01-16-2006, 02:26 AM
I'm trying to parse a line from a text file (.svm file) but when the text
contains the characters & or < or > the text doesn't get parsed.
It works fine if the text doesn't contain those 3 characters however.
Whats the problem? Why won't it recognize those 3 characters?

The line to parse is
Notes=" any characters "

I made the function match everything between the quotes
and if nothing is between the quotes is will return **No Notes**



function findnotes( notes )
{
var notes // if no match, use this
var notesArray = notes.match(/".+"/gi);
if (notesArray)
{
notes = "";
for (var i = 0; i < notesArray.length; i++)
{
notes += notesArray[i];
}
}
else
{
notes = "**No Notes**";
}
return notes;
}

function convert_SVM_to_XML( p_inp, p_t )
{
sXML = "";
sXML += "<svm>";
fso = new ActiveXObject( "Scripting.FileSystemObject" );
try
{
tfRead = fso.OpenTextFile( p_inp.value, 1/*for reading*/, false );
}
catch(e)
{
alert("Hey Dumb Dumb! You need to browse for a setup!");
return;
}
while ( !tfRead.AtEndOfStream )
{
s = tfRead.ReadLine();
if( s == "[GENERAL]" )
{
s = tfRead.ReadLine();
sXML += "<par name='Notes'>" + findnotes( s ) + "</par>";
s = tfRead.ReadLine();

The output is xsl stylesheet:



function showPar( pmode, pdescname, pparname, punit, ponlyDiff )
{
ps = "";
if(( pmode == 1 ) || ( pmode == 2 ))
ps+=" "+pdescname+" <a style='font-size: 12px; font-weight:bold; color:"+arrColors[pmode]+";'><xsl:value-of select='svms/svm[1]/par[@name=\""+pparname+"\"]'/></a> "+punit+"<br/>";
else
{
if( ponlyDiff == 1 )
{
ps+=" <xsl:if test='svms/svm[1]/par[@name=\""+pparname+"\"] != svms/svm[2]/par[@name=\""+pparname+"\"]'>";
ps+=" "+pdescname+" <a style='font-size: 12px; font-weight:bold; color:"+arrColors[1]+";'><xsl:value-of select='svms/svm[1]/par[@name=\""+pparname+"\"]'/></a> <b>/</b> <a style='font-size: 12px; font-weight:bold; color:"+arrColors[2]+";'><xsl:value-of select='svms/svm[2]/par[@name=\""+pparname+"\"]'/></a> "+punit+"<br/>";
ps+=" </xsl:if>";
}
else
{
ps+=" <xsl:choose>";
ps+=" <xsl:when test='svms/svm[1]/par[@name=\""+pparname+"\"] != svms/svm[2]/par[@name=\""+pparname+"\"]'>";
ps+=" "+pdescname+" <a style='font-size: 12px; font-weight:bold; color:"+arrColors[1]+";'><xsl:value-of select='svms/svm[1]/par[@name=\""+pparname+"\"]'/></a> <b>/</b> <a style='font-size: 12px; font-weight:bold; color:"+arrColors[2]+";'><xsl:value-of select='svms/svm[2]/par[@name=\""+pparname+"\"]'/></a> "+punit+"<br/>";
ps+=" </xsl:when>";
ps+=" <xsl:otherwise>";
ps+=" "+pdescname+" <a style='font-size: 12px; font-weight:normal;'><xsl:value-of select='svms/svm[1]/par[@name=\""+pparname+"\"]'/></a> <b>/</b> <a style='font-size: 12px; font-weight:normal;'><xsl:value-of select='svms/svm[2]/par[@name=\""+pparname+"\"]'/></a> "+punit+"<br/>";
ps+=" </xsl:otherwise>";
ps+=" </xsl:choose>";
}
}
return ps;
}


function showFile( mode, onlyDiff )
{
oxml = new ActiveXObject('Microsoft.XMLDOM');
oxml.async = false;
strXML1 = "";
strXML2 = "";
if(( mode == 1 ) || ( mode == 3 ))
{
strXML1 = convert_SVM_to_XML( document.all.inpFile1 );
}
if(( mode == 2 ) || ( mode == 3 ))
{
strXML2 = convert_SVM_to_XML( document.all.inpFile2 );
}
oxml.loadXML( "<?xml version='1.0'?><svms>" + strXML1 + strXML2 + "</svms>" );
//alert(oxml.xml);
var xsldom=new ActiveXObject("Microsoft.XMLDOM");
xsldom.async=false;
strxsl="";
strxsl+="<?xml version='1.0'?>";
strxsl+="<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='3.0'>";
strxsl+="<xsl:template match='/'>";
strxsl+=" <table border='1' width='100%' style='background-color: black' height='100%' cellpadding='1' cellspacing='1' align='center'>";
strxsl+=" <tr>";
strxsl+=" <td align='right'>";
strxsl+=" <table width='240' height='100' style='background-color: gray' cellpadding='0' cellspacing='0' border='0'>";
strxsl+=" <tr height='20'><td>";
strxsl+=" <table cellpadding='2' cellspacing='1' width='100%' border='0' height='100%'>";
strxsl+=" <tr>";
strxsl+=" <td align='center' style='font-family: arial; font-size: 13px; font-weight: bold; color: #FFFFFF; background-color: gray;'>";
strxsl+=" SETUP NOTES";
strxsl+=" </td>";
strxsl+=" </tr>";
strxsl+=" </table>";
strxsl+=" </td></tr>";
strxsl+=" <tr><td>";
strxsl+=" <table cellpadding='2' cellspacing='1' width='100%' height='100%' border='0'>";
strxsl+=" <tr>";
strxsl+=" <td valign='top' style='font-family: Arial; font-size: 11px; font-weight: normal; color: black; background-color: white;'>";
strxsl+= showPar( mode, "", "Notes", "", onlyDiff );

Twey
01-16-2006, 05:09 PM
You need to XML-encode them.

notes = notes.replace(/&/, "&amp;").replace(/</, "&lt;").replace(/>/, "&gt;");

SuperLes2006
01-16-2006, 07:24 PM
Brilliant!!! That worked. Thanks :)
I also added /gi so it would recognize those special characters more than once.



function findnotes( notes )
{
var notes // if no match, use this
var notesArray = notes.match(/".+"/gi);
if (notesArray)
{
notes = "";
for (var i = 0; i < notesArray.length; i++)
{
notes += notesArray[i];
}
}
else
{
notes = "**No Notes**";
}
return notes.replace(/&/gi, "&amp;").replace(/</gi, "&lt;").replace(/>/gi, "&gt;"); notes;


Now...My only problem is that the output doesn't show more than one space at a time. :eek: For example:

Input : Notes="Just_____A__Test!"
I had to use _ for a space because this board eliminates the extra spaces too.

The output would be: "Just A Test!"

ARGH!!

Twey
01-16-2006, 09:02 PM
You only need g, not i; i is for case insensitivity, which doesn't apply here. The problem with your spaces is a rendering one. HTML automatically, on encountering more than one space, renders it as just one space, to avoid whitespace being shown on the site. Try:
notes = notes.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/ /g, "&nbsp;");

SuperLes2006
01-17-2006, 12:53 AM
Thanks Twey! Your right, I didn't need the i for case insensitivity :)

This code don't work though :(


.replace(/ /g, "&nbsp;");


but this Works like a charm!!!! ;)


.replace(/ /g, "&#160");

Thanks for all the help

Twey
01-17-2006, 04:21 PM
Really? Hm, odd. Ah well.