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    
018    package org.apache.activemq.filter;
019    
020    import java.util.ArrayList;
021    import java.util.List;
022    
023    import javax.jms.JMSException;
024    
025    import org.apache.activemq.command.ActiveMQDestination;
026    import org.apache.activemq.command.Message;
027    
028    /**
029     * Helper class for decomposing a Destination into a number of paths
030     * 
031     * 
032     */
033    public final class DestinationPath {
034        protected static final char SEPARATOR = '.';
035    
036        private DestinationPath() {    
037        }
038        
039        public static String[] getDestinationPaths(String subject) {
040            List<String> list = new ArrayList<String>();
041            int previous = 0;
042            int lastIndex = subject.length() - 1;
043            while (true) {
044                int idx = subject.indexOf(SEPARATOR, previous);
045                if (idx < 0) {
046                    list.add(subject.substring(previous, lastIndex + 1));
047                    break;
048                }
049                list.add(subject.substring(previous, idx));
050                previous = idx + 1;
051            }
052            String[] answer = new String[list.size()];
053            list.toArray(answer);
054            return answer;
055        }
056    
057        public static String[] getDestinationPaths(Message message) throws JMSException {
058            return getDestinationPaths(message.getDestination());
059        }
060    
061        public static String[] getDestinationPaths(ActiveMQDestination destination) {
062            return getDestinationPaths(destination.getPhysicalName());
063        }
064    
065        /**
066         * Converts the paths to a single String seperated by dots.
067         * 
068         * @param paths
069         * @return
070         */
071        public static String toString(String[] paths) {
072            StringBuffer buffer = new StringBuffer();
073            for (int i = 0; i < paths.length; i++) {
074                if (i > 0) {
075                    buffer.append(SEPARATOR);
076                }
077                String path = paths[i];
078                if (path == null) {
079                    buffer.append("*");
080                } else {
081                    buffer.append(path);
082                }
083            }
084            return buffer.toString();
085        }
086    }