1 | /* |
2 | Copyright (C) 2004 MySQL AB |
3 | |
4 | This program is free software; you can redistribute it and/or modify |
5 | it under the terms of the GNU General Public License version 2 as |
6 | published by the Free Software Foundation. |
7 | |
8 | This program is distributed in the hope that it will be useful, |
9 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 | GNU General Public License for more details. |
12 | |
13 | You should have received a copy of the GNU General Public License |
14 | along with this program; if not, write to the Free Software |
15 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
16 | |
17 | */ |
18 | package com.mysql.management; |
19 | |
20 | import java.io.PrintStream; |
21 | import java.util.LinkedHashMap; |
22 | import java.util.Map; |
23 | |
24 | import com.mysql.management.util.Utils; |
25 | |
26 | /* |
27 | * TODO Replace this with something less fragile than text parsing. Perhaps an |
28 | * api could be added to the server? |
29 | */ |
30 | |
31 | /** |
32 | * This class is final simply as a hint to the compiler, it may be un-finalized |
33 | * safely. |
34 | * |
35 | * @author Eric Herman <eric@mysql.com> |
36 | * @version $Id: HelpOptionsParser.java,v 1.22 2005/10/25 19:11:16 eherman Exp $ |
37 | */ |
38 | final class HelpOptionsParser { |
39 | |
40 | private static final String DIVIDER = "--------------------------------- -----------------------------"; |
41 | |
42 | private static final String END_TEXT = "To see what values a running"; |
43 | |
44 | private static final String NO_DEFAULT_VALUE = "(No default value)"; |
45 | |
46 | private PrintStream err; |
47 | |
48 | private Utils utils; |
49 | |
50 | HelpOptionsParser(PrintStream err, Utils utils) { |
51 | this.err = err; |
52 | this.utils = utils; |
53 | } |
54 | |
55 | public Map getOptionsFromHelp(String help) { |
56 | String trimmed = trimToOptions(help); |
57 | |
58 | Map map = new LinkedHashMap(); |
59 | String[] lines = utils.str().splitLines(trimmed); |
60 | for (int i = 0; i < lines.length; i++) { |
61 | String line = lines[i]; |
62 | if (line.indexOf(' ') <= 0) { |
63 | continue; |
64 | } |
65 | String key = line.substring(0, line.indexOf(' ')); |
66 | String val = line.substring(key.length()).trim(); |
67 | if (val.equals(NO_DEFAULT_VALUE)) { |
68 | val = ""; |
69 | } |
70 | map.put(key, val); |
71 | } |
72 | |
73 | map.remove("help"); |
74 | map.remove("verbose"); |
75 | |
76 | return map; |
77 | } |
78 | |
79 | String trimToOptions(String help) { |
80 | boolean success = false; |
81 | try { |
82 | String trimmedHelp = trimToOptionsInner(help); |
83 | success = true; |
84 | return trimmedHelp; |
85 | } finally { |
86 | if (!success) { |
87 | synchronized (err) { |
88 | printMsg(err, "parsing unseccessful:"); |
89 | printMsg(err, "===== BEGIN MYSQLD HELP OPTIONS TEXT ====="); |
90 | err.println(help); |
91 | printMsg(err, "===== END MYSQLD HELP OPTIONS TEXT ====="); |
92 | } |
93 | } |
94 | } |
95 | } |
96 | |
97 | private String trimToOptionsInner(String help) { |
98 | int dividerPos = help.indexOf(DIVIDER); |
99 | int start = dividerPos + DIVIDER.length(); |
100 | int stop = help.indexOf(END_TEXT); |
101 | if (dividerPos == -1) { |
102 | synchronized (err) { |
103 | printMsg(err, "Divider=\"" + DIVIDER + "\""); |
104 | printMsg(err, "found at: " + dividerPos); |
105 | printMsg(err, "Start Position:" + start); |
106 | printMsg(err, "End Text=\"" + END_TEXT + "\""); |
107 | printMsg(err, "found at: " + stop); |
108 | printMsg(err, "HELP TEXT BEGIN"); |
109 | printMsg(err, help); |
110 | printMsg(err, "HELP TEXT END"); |
111 | } |
112 | throw new RuntimeException("could not parse help text"); |
113 | } |
114 | if (stop < start) { |
115 | stop = help.length(); |
116 | } |
117 | String options = help.substring(start, stop); |
118 | return options + System.getProperty("line.separator"); |
119 | } |
120 | |
121 | private void printMsg(PrintStream ps, String msg) { |
122 | synchronized (ps) { |
123 | ps.print("["); |
124 | ps.print(utils.str().shortClassName(this)); |
125 | ps.print("] "); |
126 | ps.println(msg); |
127 | } |
128 | } |
129 | } |