001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.commons.scxml.env;
018    
019    import java.util.Iterator;
020    import java.util.LinkedList;
021    
022    import org.apache.commons.scxml.model.Transition;
023    import org.apache.commons.scxml.model.TransitionTarget;
024    
025    /**
026     * Helper methods for Commons SCXML logging.
027     *
028     */
029    public final class LogUtils {
030    
031        /**
032         * Create a human readable log view of this transition.
033         *
034         * @param from The source TransitionTarget
035         * @param to The destination TransitionTarget
036         * @param transition The Transition that is taken
037         * @return String The human readable log entry
038         */
039        public static String transToString(final TransitionTarget from,
040                final TransitionTarget to, final Transition transition) {
041            StringBuffer buf = new StringBuffer("transition (");
042            buf.append("event = ").append(transition.getEvent());
043            buf.append(", cond = ").append(transition.getCond());
044            buf.append(", from = ").append(getTTPath(from));
045            buf.append(", to = ").append(getTTPath(to));
046            buf.append(')');
047            return buf.toString();
048        }
049    
050        /**
051         * Write out this TransitionTarget location in a XPath style format.
052         *
053         * @param tt The TransitionTarget whose "path" is to needed
054         * @return String The XPath style location of the TransitionTarget within
055         *                the SCXML document
056         */
057        public static String getTTPath(final TransitionTarget tt) {
058            TransitionTarget parent = tt.getParent();
059            if (parent == null) {
060                return "/" + tt.getId();
061            } else {
062                LinkedList pathElements = new LinkedList();
063                pathElements.addFirst(tt);
064                while (parent != null) {
065                    pathElements.addFirst(parent);
066                    parent = parent.getParent();
067                }
068                StringBuffer names = new StringBuffer();
069                for (Iterator i = pathElements.iterator(); i.hasNext();) {
070                    TransitionTarget pathElement = (TransitionTarget) i.next();
071                    names.append('/').append(pathElement.getId());
072                }
073                return names.toString();
074            }
075        }
076    
077        /**
078         * Discourage instantiation since this is a utility class.
079         */
080        private LogUtils() {
081            super();
082        }
083    
084    }