001: package org.objectweb.celtix.tools.common.toolspec.parser;
002:
003: import java.util.Collection;
004: import java.util.HashSet;
005: import java.util.Iterator;
006: import java.util.Set;
007: import java.util.logging.Level;
008: import java.util.logging.Logger;
009:
010: import org.objectweb.celtix.common.logging.LogUtils;
011:
012: public class ErrorVisitor {
013: public static final long serialVersionUID = 1L;
014:
015: private static final Logger LOG = LogUtils
016: .getL7dLogger(ErrorVisitor.class);
017:
018: private final Set<Object> errors = new HashSet<Object>();
019:
020: public static class MissingOption implements CommandLineError {
021: private final Option o;
022:
023: public MissingOption(Option op) {
024: this .o = op;
025: }
026:
027: public String toString() {
028: return "Missing option: " + o.getPrimarySwitch();
029: }
030:
031: public Option getOption() {
032: return o;
033: }
034:
035: public String getOptionSwitch() {
036: return o.getPrimarySwitch();
037: }
038: }
039:
040: public static class DuplicateOption implements CommandLineError {
041: private final String option;
042:
043: public DuplicateOption(String opt) {
044: option = opt;
045: }
046:
047: public String toString() {
048: return "Duplicated option: " + option;
049: }
050:
051: public String getOptionSwitch() {
052: return option;
053: }
054: }
055:
056: public static class DuplicateArgument implements CommandLineError {
057: private final String argument;
058:
059: public DuplicateArgument(String arg) {
060: this .argument = arg;
061: }
062:
063: public String toString() {
064: return "Duplicated argument: " + argument;
065: }
066:
067: public String getOptionSwitch() {
068: return argument;
069: }
070: }
071:
072: public static class UnexpectedOption implements CommandLineError {
073: private final String option;
074:
075: public UnexpectedOption(String opt) {
076: this .option = opt;
077: }
078:
079: public String toString() {
080: return "Unexpected option: " + option;
081: }
082:
083: public String getOptionSwitch() {
084: return option;
085: }
086: }
087:
088: public static class UnexpectedArgument implements CommandLineError {
089: private final String arg;
090:
091: public UnexpectedArgument(String a) {
092: this .arg = a;
093: }
094:
095: public String toString() {
096: return "Unexpected argument: " + arg;
097: }
098:
099: public String getArgument() {
100: return arg;
101: }
102: }
103:
104: public static class InvalidOption implements CommandLineError {
105: private final String option;
106:
107: public InvalidOption(String opt) {
108: this .option = opt;
109: }
110:
111: public String toString() {
112: return "Invalid option: " + option
113: + " is missing its associated argument";
114: }
115:
116: public String getOptionSwitch() {
117: return option;
118: }
119: }
120:
121: public static class MissingArgument implements CommandLineError {
122: private final String arg;
123:
124: public MissingArgument(String a) {
125: this .arg = a;
126: }
127:
128: public String toString() {
129: return "Missing argument: " + arg;
130: }
131:
132: public String getArgument() {
133: return arg;
134: }
135: }
136:
137: public static class UserError implements CommandLineError {
138: private final String msg;
139:
140: public UserError(String m) {
141: this .msg = m;
142: }
143:
144: public String toString() {
145: return msg;
146: }
147:
148: public String getMessage() {
149: return msg;
150: }
151: }
152:
153: public Collection getErrors() {
154: return errors;
155: }
156:
157: public void add(CommandLineError err) {
158: if (LOG.isLoggable(Level.INFO)) {
159: LOG.info("Adding error: " + err);
160: }
161: errors.add(err);
162: }
163:
164: public String toString() {
165: StringBuffer res = new StringBuffer();
166:
167: for (Iterator it = errors.iterator(); it.hasNext();) {
168: res.append(it.next().toString()).append(
169: System.getProperty("line.separator"));
170: }
171: return res.toString();
172: }
173:
174: }
|