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.scxml.env;
18  
19  import java.util.Iterator;
20  import java.util.LinkedList;
21  
22  import org.apache.commons.scxml.model.Transition;
23  import org.apache.commons.scxml.model.TransitionTarget;
24  
25  /***
26   * Helper methods for Commons SCXML logging.
27   *
28   */
29  public final class LogUtils {
30  
31      /***
32       * Create a human readable log view of this transition.
33       *
34       * @param from The source TransitionTarget
35       * @param to The destination TransitionTarget
36       * @param transition The Transition that is taken
37       * @return String The human readable log entry
38       */
39      public static String transToString(final TransitionTarget from,
40              final TransitionTarget to, final Transition transition) {
41          StringBuffer buf = new StringBuffer("transition (");
42          buf.append("event = ").append(transition.getEvent());
43          buf.append(", cond = ").append(transition.getCond());
44          buf.append(", from = ").append(getTTPath(from));
45          buf.append(", to = ").append(getTTPath(to));
46          buf.append(')');
47          return buf.toString();
48      }
49  
50      /***
51       * Write out this TransitionTarget location in a XPath style format.
52       *
53       * @param tt The TransitionTarget whose "path" is to needed
54       * @return String The XPath style location of the TransitionTarget within
55       *                the SCXML document
56       */
57      public static String getTTPath(final TransitionTarget tt) {
58          TransitionTarget parent = tt.getParent();
59          if (parent == null) {
60              return "/" + tt.getId();
61          } else {
62              LinkedList pathElements = new LinkedList();
63              pathElements.addFirst(tt);
64              while (parent != null) {
65                  pathElements.addFirst(parent);
66                  parent = parent.getParent();
67              }
68              StringBuffer names = new StringBuffer();
69              for (Iterator i = pathElements.iterator(); i.hasNext();) {
70                  TransitionTarget pathElement = (TransitionTarget) i.next();
71                  names.append('/').append(pathElement.getId());
72              }
73              return names.toString();
74          }
75      }
76  
77      /***
78       * Discourage instantiation since this is a utility class.
79       */
80      private LogUtils() {
81          super();
82      }
83  
84  }