View Javadoc

1   /*
2    *
3    *   Copyright 2005 The Apache Software Foundation.
4    *
5    *  Licensed under the Apache License, Version 2.0 (the "License");
6    *  you may not use this file except in compliance with the License.
7    *  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   *
17   */
18  package org.apache.commons.scxml.env;
19  
20  import java.util.Iterator;
21  import java.util.LinkedList;
22  
23  import org.apache.commons.scxml.model.Transition;
24  import org.apache.commons.scxml.model.TransitionTarget;
25  
26  /***
27   * Helper methods for Commons SCXML logging.
28   *
29   */
30  public final class LogUtils {
31  
32      /***
33       * Create a human readable log view of this transition.
34       *
35       * @param from The source TransitionTarget
36       * @param to The destination TransitionTarget
37       * @param transition The Transition that is taken
38       * @return String The human readable log entry
39       */
40      public static String transToString(final TransitionTarget from,
41              final TransitionTarget to, final Transition transition) {
42          StringBuffer buf = new StringBuffer("transition (");
43          buf.append("event = ").append(transition.getEvent());
44          buf.append(", cond = ").append(transition.getCond());
45          buf.append(", from = ").append(getTTPath(from));
46          buf.append(", to = ").append(getTTPath(to));
47          buf.append(')');
48          return buf.toString();
49      }
50  
51      /***
52       * Write out this TransitionTarget location in a XPath style format.
53       *
54       * @param tt The TransitionTarget whose "path" is to needed
55       * @return String The XPath style location of the TransitionTarget within
56       *                the SCXML document
57       */
58      public static String getTTPath(final TransitionTarget tt) {
59          TransitionTarget parent = tt.getParent();
60          if (parent == null) {
61              return "/" + tt.getId();
62          } else {
63              LinkedList pathElements = new LinkedList();
64              pathElements.addFirst(tt);
65              while (parent != null) {
66                  pathElements.addFirst(parent);
67                  parent = parent.getParent();
68              }
69              StringBuffer names = new StringBuffer();
70              for (Iterator i = pathElements.iterator(); i.hasNext();) {
71                  TransitionTarget pathElement = (TransitionTarget) i.next();
72                  names.append('/').append(pathElement.getId());
73              }
74              return names.toString();
75          }
76      }
77  
78      /***
79       * Discourage instantiation since this is a utility class.
80       */
81      private LogUtils() {
82          super();
83      }
84  
85  }