KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > webEditor > util > escapeChars


1 /* $Id: escapeChars.java,v 1.1 2001/04/30 20:51:54 agarcia3 Exp $
2     webEditor. The new way in content management
3     Copyright (C) 2001 Alfredo Garcia
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12     GNU General Public License for more details.
13 */

14
15 package webEditor.util;
16
17 import java.io.*;
18
19 import org.w3c.dom.*;
20 import org.apache.xerces.dom.*;
21 import org.apache.regexp.RE;
22
23 /**
24  * Special characters escaping utilities.
25  *
26  * @author <a HREF="mailto:agarcia@mundofree.com">Alfredo Garcia</a>
27  */

28 public class escapeChars
29 {
30    
31    /**
32     * Add the scaping character (slash) to the input string
33     * @param in Input String
34     * @return String
35     */

36    public String JavaDoc addSlashes (String JavaDoc in)
37    {
38     String JavaDoc out = null;
39 try {
40     RE r = new RE("'");
41     out = r.subst (in, "\\'");
42
43 } catch (Exception JavaDoc e) {
44     e.printStackTrace();
45 }
46     return (out);
47    }
48
49    /**
50     * Remove the scaping character (slash) to the input string
51     * @param in Input String
52     * @return String
53     */

54    public String JavaDoc removeSlashes (String JavaDoc in)
55    {
56     String JavaDoc out = null;
57 try {
58     RE r = new RE("\\'");
59     out = r.subst (in, "'");
60
61 } catch (Exception JavaDoc e) {
62     e.printStackTrace();
63 }
64     return (out);
65    }
66
67    /**
68     * Escape special characters into the DOM tree
69     * @param doc DOM tree of the document
70     * @return Document New DOM tree
71     */

72    public Document escapeDoc (Document doc)
73    {
74     String JavaDoc rootTag = null;
75     String JavaDoc tagName = null;
76     String JavaDoc tagValue = null;
77
78     Element root = doc.getDocumentElement();
79     rootTag = root.getTagName();
80     NodeList list = doc.getElementsByTagName(rootTag).item(0).getChildNodes();
81
82     // Now, we escape the text value of all the nodes's tree
83
for (int i=0; i < list.getLength(); i++) {
84         Node source = list.item(i);
85
86         tagValue = source.getLastChild().getNodeValue();
87         source.getLastChild().setNodeValue (this.addSlashes(tagValue));
88     }
89
90     return (doc);
91    }
92
93
94    
95 }
96
Popular Tags