Revert "Added Netbeans project."
[cql-java-moved-to-github.git] / src / org / z3950 / zing / cql / Utils.java
1 // $Id: Utils.java,v 1.2 2002-11-06 00:05:58 mike Exp $
2
3 package org.z3950.zing.cql;
4
5
6 /**
7  * Utility functions for the org.z3950.zing.cql package.
8  * Not intended for use outside this package.
9  *
10  * @version     $Id: Utils.java,v 1.2 2002-11-06 00:05:58 mike Exp $
11  */
12 class Utils {
13     static String indent(int level) {
14         String x = "";
15         while (level-- > 0) {
16             x += "  ";
17         }
18         return x;
19     }
20
21     // XML Quote --
22     //  s/&/&/g;
23     //  s/</&lt;/g;
24     //  s/>/&gt;/g;
25     // This is hideously inefficient, but I just don't see a better
26     // way using the standard JAVA library.
27     //
28     static String xq(String str) {
29         str = replaceString(str, "&", "&amp;");
30         str = replaceString(str, "<", "&lt;");
31         str = replaceString(str, ">", "&gt;");
32         return str;
33     }
34
35     // I can't _believe_ I have to write this by hand in 2002 ...
36     static String replaceString(String str, String from, String to) {
37         StringBuffer sb = new StringBuffer();
38         int ix;                 // index of next `from'
39         int offset = 0;         // index of previous `from' + length(from)
40
41         while ((ix = str.indexOf(from, offset)) != -1) {
42             sb.append(str.substring(offset, ix));
43             sb.append(to);
44             offset = ix + from.length();
45         }
46
47         // End of string: append last bit and we're done
48         sb.append(str.substring(offset));
49         return sb.toString();
50     }
51 }