/* $Id: escapeChars.java,v 1.1 2001/04/30 20:51:54 agarcia3 Exp $
webEditor. The new way in content management
Copyright (C) 2001 Alfredo Garcia
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/
package webEditor.util;
import java.io.*;
import org.w3c.dom.*;
import org.apache.xerces.dom.*;
import org.apache.regexp.RE;
/**
* Special characters escaping utilities.
*
* @author <a href="mailto:agarcia@mundofree.com">Alfredo Garcia</a>
*/
public class escapeChars
{
/**
* Add the scaping character (slash) to the input string
* @param in Input String
* @return String
*/
public String addSlashes (String in)
{
String out = null;
try {
RE r = new RE("'");
out = r.subst (in, "\\'");
} catch (Exception e) {
e.printStackTrace();
}
return (out);
}
/**
* Remove the scaping character (slash) to the input string
* @param in Input String
* @return String
*/
public String removeSlashes (String in)
{
String out = null;
try {
RE r = new RE("\\'");
out = r.subst (in, "'");
} catch (Exception e) {
e.printStackTrace();
}
return (out);
}
/**
* Escape special characters into the DOM tree
* @param doc DOM tree of the document
* @return Document New DOM tree
*/
public Document escapeDoc (Document doc)
{
String rootTag = null;
String tagName = null;
String tagValue = null;
Element root = doc.getDocumentElement();
rootTag = root.getTagName();
NodeList list = doc.getElementsByTagName(rootTag).item(0).getChildNodes();
// Now, we escape the text value of all the nodes's tree
for (int i=0; i < list.getLength(); i++) {
Node source = list.item(i);
tagValue = source.getLastChild().getNodeValue();
source.getLastChild().setNodeValue (this.addSlashes(tagValue));
}
return (doc);
}
}