View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  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  package org.apache.commons.scxml2.env;
18  
19  import org.apache.commons.scxml2.model.Transition;
20  import org.apache.commons.scxml2.model.TransitionTarget;
21  
22  /**
23   * Helper methods for Commons SCXML logging.
24   *
25   */
26  public final class LogUtils {
27  
28      /**
29       * Create a human readable log view of this transition.
30       *
31       * @param from The source TransitionTarget
32       * @param to The destination TransitionTarget
33       * @param transition The Transition that is taken
34       * @return String The human readable log entry
35       */
36      public static String transToString(final TransitionTarget from,
37              final TransitionTarget to, final Transition transition, String event) {
38          StringBuffer buf = new StringBuffer("(");
39          buf.append("event = ").append(event);
40          buf.append(", cond = ").append(transition.getCond());
41          buf.append(", from = ").append(getTTPath(from));
42          buf.append(", to = ").append(getTTPath(to));
43          buf.append(')');
44          return buf.toString();
45      }
46  
47      /**
48       * Write out this TransitionTarget location in a XPath style format.
49       *
50       * @param tt The TransitionTarget whose "path" is to needed
51       * @return String The XPath style location of the TransitionTarget within
52       *                the SCXML document
53       */
54      public static String getTTPath(final TransitionTarget tt) {
55          StringBuilder sb = new StringBuilder("/");
56          for (int i = 0; i < tt.getNumberOfAncestors(); i++) {
57              sb.append(tt.getAncestor(i).getId());
58              sb.append("/");
59          }
60          sb.append(tt.getId());
61          return sb.toString();
62      }
63  
64      /**
65       * Discourage instantiation since this is a utility class.
66       */
67      private LogUtils() {
68          super();
69      }
70  
71  }