001 /**
002 *
003 * Copyright 2004 Protique Ltd
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * 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
019 package org.activemq.filter;
020
021 import javax.jms.Destination;
022 import javax.jms.JMSException;
023 import javax.jms.Message;
024 import java.util.ArrayList;
025 import java.util.List;
026
027 /**
028 * Helper class for decomposing a Destination into a number of paths
029 *
030 * @version $Revision: 1.1.1.1 $
031 */
032 public class DestinationPath {
033 protected static final char SEPARATOR = '.';
034
035 public static String[] getDestinationPaths(String subject) {
036 List list = new ArrayList();
037 int previous = 0;
038 int lastIndex = subject.length() - 1;
039 while (true) {
040 int idx = subject.indexOf(SEPARATOR, previous);
041 if (idx < 0) {
042 list.add(subject.substring(previous, lastIndex + 1));
043 break;
044 }
045 list.add(subject.substring(previous, idx));
046 previous = idx + 1;
047 }
048 String[] answer = new String[list.size()];
049 list.toArray(answer);
050 return answer;
051 }
052
053 public static String[] getDestinationPaths(Message message) throws JMSException {
054 return getDestinationPaths(message.getJMSDestination());
055 }
056
057 public static String[] getDestinationPaths(Destination destination) {
058 return getDestinationPaths(destination.toString());
059 }
060
061 /**
062 * Converts the paths to a single String seperated by dots.
063 *
064 * @param paths
065 * @return
066 */
067 public static String toString(String[] paths) {
068 StringBuffer buffer = new StringBuffer();
069 for (int i = 0; i < paths.length; i++) {
070 if (i > 0) {
071 buffer.append(SEPARATOR);
072 }
073 String path = paths[i];
074 if (path == null) {
075 buffer.append("*");
076 }
077 else {
078 buffer.append(path);
079 }
080 }
081 return buffer.toString();
082 }
083 }