View Javadoc

1   /*
2    * joey-gen and its relative products are published under the terms
3    * of the Apache Software License.
4    * 
5    * Created on 2004/11/09 12:51:23
6    */
7   package org.asyrinx.joey.gen.model.command;
8   
9   import java.util.ArrayList;
10  import java.util.List;
11  
12  import org.apache.commons.logging.LogFactory;
13  import org.asyrinx.brownie.core.log.DispatchLog;
14  import org.asyrinx.joey.gen.model.Element;
15  import org.asyrinx.joey.gen.model.ElementVisitor;
16  
17  /***
18   * @author takeshi
19   */
20  public class Command implements ElementVisitor {
21  
22      private boolean strict = true;
23  
24      protected final List errors = new ArrayList();
25  
26      protected final DispatchLog log = new DispatchLog(LogFactory.getLog(this.getClass()), "info");
27  
28      /***
29       *  
30       */
31      public Command() {
32          super();
33      }
34  
35      protected String element2String(Element element, String msg) {
36          if (element != null)
37              msg += " @" + element.getFullName();
38          msg += " (by command '" + this.getClass().getName() + "')";
39          return msg;
40      }
41  
42      protected void addError(String msg) {
43          addError(null, msg);
44      }
45  
46      protected void addError(Element element, String msg) {
47          addError(element, msg, isStrict());
48      }
49  
50      protected void addError(Element element, String msg, boolean exit) {
51          msg = element2String(element, msg);
52          final ValidationError error = new ValidationError(msg, element);
53          if (exit) {
54              throw error;
55          } else {
56              log.warn(msg);
57              errors.add(error);
58          }
59      }
60  
61      protected void log(String msg) {
62          log(null, msg);
63      }
64  
65      protected void log(Element element, String msg) {
66          msg = element2String(element, msg);
67          log.log(msg);
68      }
69  
70      /***
71       * @return Returns the strict.
72       */
73      public boolean isStrict() {
74          return strict;
75      }
76  
77      /***
78       * @param strict
79       *            The strict to set.
80       */
81      public void setStrict(boolean strict) {
82          this.strict = strict;
83      }
84  
85      /*
86       * (non-Javadoc)
87       * 
88       * @see org.asyrinx.joey.gen.model.ElementVisitor#visit(org.asyrinx.joey.gen.model.Element)
89       */
90      public void execute(Element element) {
91          throw new UnsupportedOperationException("subclass must override");
92      }
93  
94      /*
95       * (non-Javadoc)
96       * 
97       * @see org.asyrinx.joey.gen.model.ElementVisitor#visit(org.asyrinx.joey.gen.model.Element)
98       */
99      public void visit(Element element) {
100         throw new UnsupportedOperationException();
101     }
102 
103     public List getErrors() {
104         return errors;
105     }
106 }