1 /***
2 *
3 * Copyright 2004 Protique Ltd
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * 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 **/
18
19 package org.codehaus.activemq.filter;
20
21 import javax.jms.Destination;
22 import javax.jms.JMSException;
23 import javax.jms.Message;
24 import java.util.ArrayList;
25 import java.util.List;
26
27 /***
28 * Helper class for decomposing a Destination into a number of paths
29 *
30 * @version $Revision: 1.2 $
31 */
32 public class DestinationPath {
33 protected static final char SEPARATOR = '.';
34
35 public static String[] getDestinationPaths(String subject) {
36 List list = new ArrayList();
37 int previous = 0;
38 int lastIndex = subject.length() - 1;
39 while (true) {
40 int idx = subject.indexOf(SEPARATOR, previous);
41 if (idx < 0) {
42 list.add(subject.substring(previous, lastIndex + 1));
43 break;
44 }
45 list.add(subject.substring(previous, idx));
46 previous = idx + 1;
47 }
48 String[] answer = new String[list.size()];
49 list.toArray(answer);
50 return answer;
51 }
52
53 public static String[] getDestinationPaths(Message message) throws JMSException {
54 return getDestinationPaths(message.getJMSDestination());
55 }
56
57 public static String[] getDestinationPaths(Destination destination) {
58 return getDestinationPaths(destination.toString());
59 }
60
61 /***
62 * Converts the paths to a single String seperated by dots.
63 *
64 * @param paths
65 * @return
66 */
67 public static String toString(String[] paths) {
68 StringBuffer buffer = new StringBuffer();
69 for (int i = 0; i < paths.length; i++) {
70 if (i > 0) {
71 buffer.append(SEPARATOR);
72 }
73 String path = paths[i];
74 if (path == null) {
75 buffer.append("*");
76 }
77 else {
78 buffer.append(path);
79 }
80 }
81 return buffer.toString();
82 }
83 }