001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.tax.io;
042:
043: /**
044: *
045: * @author Libor Kramolis
046: * @version 0.1
047: */
048: public final class StringUtil {
049:
050: public static boolean isWS(char ch) {
051: return ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r';
052: }
053:
054: /**
055: * @param nest list of characters that symetrically delimit some inner token that
056: * can contain stop delimiter. e.g. <!ENTITY sdsd "sd>">
057: */
058: public static int skipDelimited(String text, int pos, char del1,
059: char del2, String nest) {
060: char ch = text.charAt(pos);
061: if (ch != del1)
062: return -1;
063: do {
064: pos++;
065: ch = text.charAt(pos);
066: if (nest.indexOf(ch) >= 0) {
067: pos = skipDelimited(text, pos, ch, ch, "");
068: ch = text.charAt(pos);
069: }
070: } while (ch != del2);
071: return pos + 1;
072: }
073:
074: public static int skipDelimited(String text, int pos, String del1,
075: String del2) {
076: if (text.startsWith(del1, pos)) {
077: int match = text.indexOf(del2, pos + del1.length());
078: if (match == -1)
079: return -1;
080: return match + del2.length();
081: } else {
082: return -1;
083: }
084: }
085:
086: public static int skipWS(String text, int pos) {
087: if (isWS(text.charAt(pos))) {
088: return pos + 1;
089: } else {
090: return -1;
091: }
092: }
093:
094: /**
095: * @param args the command line arguments
096: */
097: public static void main(String args[]) {
098:
099: String idtd = " <!-- klfh --> <!hjk \"fdsf\" ''>]>";
100: int pos = 0;
101: int now = 0;
102: int last = -1;
103:
104: System.err.println("SkipWs" + skipWS(" k", pos));
105:
106: System.err.println("SkipDelinitedchar"
107: + skipDelimited("< ' > '>", 0, '<', '>', "\"'"));
108:
109: System.err.println("SkipDelinitedchar"
110: + skipDelimited("<!-- ' > '-->", 0, "<!--", "-->"));
111:
112: while (idtd.substring(pos).startsWith("]>") == false
113: && last != pos) {
114:
115: last = pos;
116:
117: for (now = 0; now != -1; now = skipWS(idtd, pos))
118: pos = now;
119:
120: for (now = 0; now != -1; now = skipDelimited(idtd, pos,
121: "<!--", "-->")) {
122: pos = now;
123: for (now = 0; now != -1; now = skipWS(idtd, pos))
124: pos = now;
125: }
126:
127: for (now = 0; now != -1; now = skipDelimited(idtd, pos,
128: '<', '>', "\"'"))
129: pos = now;
130:
131: // while(skipWS(idtd, pos));
132: // while(skipDelimited(idtd, pos, "<!--", "-->")) { while(skipWS(idtd, pos));};
133:
134: // skipDelimited(idtd, pos, '%', ';' , "");
135: }
136:
137: }
138:
139: }
|