1/*
2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package com.sun.tools.internal.xjc.util;
27
28import java.text.ParseException;
29import java.util.regex.Matcher;
30import java.util.regex.Pattern;
31
32/**
33 * Utility class to parse a string
34 *
35 * @author Kohsuke Kawaguchi
36 */
37public final class StringCutter {
38    private final String original;
39    private String s;
40    private boolean ignoreWhitespace;
41
42    public StringCutter(String s, boolean ignoreWhitespace) {
43        this.s = this.original = s;
44        this.ignoreWhitespace = ignoreWhitespace;
45    }
46
47    public void skip(String regexp) throws ParseException {
48        next(regexp);
49    }
50
51    public String next(String regexp) throws ParseException {
52        trim();
53        Pattern p = Pattern.compile(regexp);
54        Matcher m = p.matcher(s);
55        if(m.lookingAt()) {
56            String r = m.group();
57            s = s.substring(r.length());
58            trim();
59            return r;
60        } else
61            throw error();
62    }
63
64    private ParseException error() {
65        return new ParseException(original,original.length()-s.length());
66    }
67
68    public String until(String regexp) throws ParseException {
69        Pattern p = Pattern.compile(regexp);
70        Matcher m = p.matcher(s);
71        if(m.find()) {
72            String r =  s.substring(0,m.start());
73            s = s.substring(m.start());
74            if(ignoreWhitespace)
75                r = r.trim();
76            return r;
77        } else {
78            // return everything left
79            String r = s;
80            s = "";
81            return r;
82        }
83    }
84
85    public char peek() {
86        return s.charAt(0);
87    }
88
89    private void trim() {
90        if(ignoreWhitespace)
91            s = s.trim();
92    }
93
94    public int length() {
95        return s.length();
96    }
97}
98