##### Coverage Report - org.apache.commons.scxml.env.LogUtils

Classes in this File | Line Coverage | Branch Coverage | Complexity
LogUtils |
91% |
100% |
| 2.3333333333333335;2.333

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 | 1 |
             StringBuffer buf = new StringBuffer("transition (");
42 | 1 |
             buf.append("event = ").append(transition.getEvent());
43 | 1 |
             buf.append(", cond = ").append(transition.getCond());
44 | 1 |
             buf.append(", from = ").append(getTTPath(from));
45 | 1 |
             buf.append(", to = ").append(getTTPath(to));
46 | 1 |
             buf.append(')');
47 | 1 |
             return buf.toString();
48 |  |
         }
49 |  |
50 |  |
         /**
51 |  |
          * Write out this TransitionTarget location in a XPath style format.
52 |  |
          *
53 |  |
          * @param tt The TransitionTarget whose &quot;path&quot; 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 | 4 |
             TransitionTarget parent = tt.getParent();
59 | 4 |
             if (parent == null) {
60 | 3 |
                 return "/" + tt.getId();
61 |  |
             } else {
62 | 1 |
                 LinkedList pathElements = new LinkedList();
63 | 1 |
                 pathElements.addFirst(tt);
64 | 3 |
                 while (parent != null) {
65 | 2 |
                     pathElements.addFirst(parent);
66 | 2 |
                     parent = parent.getParent();
67 |  |
                 }
68 | 1 |
                 StringBuffer names = new StringBuffer();
69 | 1 |
                 for (Iterator i = pathElements.iterator(); i.hasNext();) {
70 | 3 |
                     TransitionTarget pathElement = (TransitionTarget) i.next();
71 | 3 |
                     names.append('/').append(pathElement.getId());
72 |  |
                 }
73 | 1 |
                 return names.toString();
74 |  |
             }
75 |  |
         }
76 |  |
77 |  |
         /**
78 |  |
          * Discourage instantiation since this is a utility class.
79 |  |
          */
80 |  |
         private LogUtils() {
81 | 0 |
             super();
82 | 0 |
         }
83 |  |
84 |  |
     }