~ubuntu-branches/ubuntu/karmic/apgdiff/karmic

« back to all changes in this revision

Viewing changes to src/main/java/cz/startnet/utils/pgdiff/parsers/ParserUtils.java

  • Committer: Bazaar Package Importer
  • Author(s): Christoph Berg
  • Date: 2008-09-09 15:42:54 UTC
  • Revision ID: james.westby@ubuntu.com-20080909154254-458sv7ew1rczdal1
Tags: upstream-1.2
ImportĀ upstreamĀ versionĀ 1.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * $Id: ParserUtils.java 80 2007-09-01 20:25:45Z fordfrog $
 
3
 */
 
4
package cz.startnet.utils.pgdiff.parsers;
 
5
 
 
6
import cz.startnet.utils.pgdiff.schema.PgDatabase;
 
7
 
 
8
import java.util.regex.Pattern;
 
9
 
 
10
 
 
11
/**
 
12
 * Parser utilities.
 
13
 *
 
14
 * @author fordfrog
 
15
 * @version $Id: ParserUtils.java 80 2007-09-01 20:25:45Z fordfrog $
 
16
 */
 
17
public class ParserUtils {
 
18
    /**
 
19
     * Creates a new instance of ParserUtils.
 
20
     */
 
21
    private ParserUtils() {
 
22
        super();
 
23
    }
 
24
 
 
25
    /**
 
26
     * Returns position of last character of single command within
 
27
     * larger command (like CREATE TABLE). Last character is either ',' or
 
28
     * ')'. If no such character is found and method reaches the end of the
 
29
     * command then position after the last character in the command is
 
30
     * returned.
 
31
     *
 
32
     * @param command command
 
33
     * @param start start position
 
34
     *
 
35
     * @return end position of the command
 
36
     */
 
37
    public static int getCommandEnd(final String command, final int start) {
 
38
        int bracesCount = 0;
 
39
        boolean singleQuoteOn = false;
 
40
        int charPos = start;
 
41
 
 
42
        for (; charPos < command.length(); charPos++) {
 
43
            final char chr = command.charAt(charPos);
 
44
 
 
45
            if (chr == '(') {
 
46
                bracesCount++;
 
47
            } else if (chr == ')') {
 
48
                if (bracesCount == 0) {
 
49
                    break;
 
50
                } else {
 
51
                    bracesCount--;
 
52
                }
 
53
            } else if (chr == '\'') {
 
54
                singleQuoteOn ^= singleQuoteOn;
 
55
            } else if ((chr == ',') && !singleQuoteOn && (bracesCount == 0)) {
 
56
                break;
 
57
            }
 
58
        }
 
59
 
 
60
        return charPos;
 
61
    }
 
62
 
 
63
    /**
 
64
     * Returns object name from optionally schema qualified name.
 
65
     *
 
66
     * @param name optionally schema qualified name
 
67
     *
 
68
     * @return name of the object
 
69
     */
 
70
    public static String getObjectName(final String name) {
 
71
        final String result;
 
72
        final int pos = name.indexOf('.');
 
73
 
 
74
        if (pos == -1) {
 
75
            result = name;
 
76
        } else {
 
77
            result = name.substring(pos + 1);
 
78
        }
 
79
 
 
80
        return result;
 
81
    }
 
82
 
 
83
    /**
 
84
     * Returns schema name from optionally schema qualified name.
 
85
     *
 
86
     * @param name optionally schema qualified name
 
87
     * @param database database
 
88
     *
 
89
     * @return name of the schema
 
90
     */
 
91
    public static String getSchemaName(
 
92
        final String name,
 
93
        final PgDatabase database) {
 
94
        final String result;
 
95
        final int pos = name.indexOf('.');
 
96
 
 
97
        if (pos == -1) {
 
98
            result = database.getDefaultSchema().getName();
 
99
        } else {
 
100
            result = name.substring(0, pos);
 
101
        }
 
102
 
 
103
        return result;
 
104
    }
 
105
 
 
106
    /**
 
107
     * Removes semicolon from the end of the <code>command</code>, but
 
108
     * only if <code>command</code> ends with semicolon.
 
109
     *
 
110
     * @param command command
 
111
     *
 
112
     * @return original <code>string</code> without last character and trimmed
 
113
     */
 
114
    public static String removeLastSemicolon(final String command) {
 
115
        final String result;
 
116
 
 
117
        if (command.endsWith(";")) {
 
118
            result = command.substring(0, command.length() - 1).trim();
 
119
        } else {
 
120
            result = command;
 
121
        }
 
122
 
 
123
        return result;
 
124
    }
 
125
 
 
126
    /**
 
127
     * Removes substring from <code>string</code> based on
 
128
     * <code>start</code> and <code>end</code> position.
 
129
     *
 
130
     * @param string string
 
131
     * @param start start position of substring
 
132
     * @param end offset after the last character of the substring
 
133
     *
 
134
     * @return <code>string</code> without given substring
 
135
     */
 
136
    public static String removeSubString(
 
137
        final String string,
 
138
        final int start,
 
139
        final int end) {
 
140
        final String result;
 
141
 
 
142
        if (start == 0) {
 
143
            result = string.substring(end).trim();
 
144
        } else {
 
145
            result =
 
146
                string.substring(0, start).trim() + " "
 
147
                + string.substring(end).trim();
 
148
        }
 
149
 
 
150
        return result;
 
151
    }
 
152
 
 
153
    /**
 
154
     * Removes <code>subString</code> from <code>string</code>. The
 
155
     * removal is performed case insensitive.
 
156
     *
 
157
     * @param string string
 
158
     * @param subString substring
 
159
     *
 
160
     * @return <code>string</code> without <code>subString</code>
 
161
     */
 
162
    public static String removeSubString(
 
163
        final String string,
 
164
        final String subString) {
 
165
        return Pattern.compile(subString, Pattern.CASE_INSENSITIVE)
 
166
                      .matcher(string).replaceAll("");
 
167
    }
 
168
}