KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > tax > io > StringUtil


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.tax.io;
20
21 /**
22  *
23  * @author Libor Kramolis
24  * @version 0.1
25  */

26 public final class StringUtil {
27
28     public static boolean isWS (char ch) {
29         return ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r';
30     }
31
32     /**
33      * @param nest list of characters that symetrically delimit some inner token that
34      * can contain stop delimiter. e.g. <!ENTITY sdsd "sd>">
35      */

36     public static int skipDelimited (String JavaDoc text, int pos, char del1, char del2, String JavaDoc nest) {
37         char ch = text.charAt (pos);
38         if ( ch != del1) return -1;
39         do {
40             pos++;
41             ch = text.charAt (pos);
42             if (nest.indexOf (ch) >= 0) {
43                 pos = skipDelimited (text, pos, ch, ch, "");
44                 ch = text.charAt (pos);
45             }
46         } while (ch != del2);
47         return pos + 1;
48     }
49     
50     public static int skipDelimited (String JavaDoc text, int pos, String JavaDoc del1, String JavaDoc del2) {
51         if (text.startsWith (del1, pos)) {
52             int match = text.indexOf (del2, pos + del1.length ());
53             if (match == -1) return -1;
54             return match + del2.length ();
55         } else {
56             return -1;
57         }
58     }
59     
60     public static int skipWS (String JavaDoc text, int pos) {
61         if (isWS (text.charAt (pos))) {
62             return pos + 1;
63         } else {
64             return -1;
65         }
66     }
67     
68     
69     /**
70      * @param args the command line arguments
71      */

72     public static void main (String JavaDoc args[]) {
73         
74         String JavaDoc idtd = " <!-- klfh --> <!hjk \"fdsf\" ''>]>";
75         int pos = 0;
76         int now = 0;
77         int last = -1;
78         
79         System.err.println ("SkipWs" + skipWS (" k", pos));
80         
81         System.err.println ("SkipDelinitedchar" + skipDelimited ("< ' > '>", 0, '<', '>' ,"\"'"));
82         
83         System.err.println ("SkipDelinitedchar" + skipDelimited ("<!-- ' > '-->", 0, "<!--", "-->"));
84         
85         while (idtd.substring (pos).startsWith ("]>") == false && last != pos) {
86             
87             last = pos;
88             
89             for (now = 0; now != -1; now = skipWS (idtd, pos)) pos = now;
90             
91             for (now = 0; now != -1; now = skipDelimited (idtd, pos, "<!--", "-->")) {
92                 pos = now;
93                 for (now = 0; now != -1; now = skipWS (idtd, pos)) pos = now;
94             }
95             
96             for (now = 0; now != -1; now = skipDelimited (idtd, pos, '<', '>' , "\"'")) pos = now;
97             
98             // while(skipWS(idtd, pos));
99
// while(skipDelimited(idtd, pos, "<!--", "-->")) { while(skipWS(idtd, pos));};
100

101             // skipDelimited(idtd, pos, '%', ';' , "");
102
}
103         
104     }
105     
106 }
107
Popular Tags