01: /*
02:
03: * <copyright>
04: *
05: * Copyright 2002-2007 BBNT Solutions, LLC
06: * under sponsorship of the Defense Advanced Research Projects
07: * Agency (DARPA).
08: *
09: * You can redistribute this software and/or modify it under the
10: * terms of the Cougaar Open Source License as published on the
11: * Cougaar Open Source Website (www.cougaar.org).
12: *
13: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
14: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
15: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
16: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
17: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
18: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
19: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24: *
25: * </copyright>
26:
27: */
28:
29: package org.cougaar.qos.qrs;
30:
31: import java.util.StringTokenizer;
32:
33: import org.cougaar.qos.ResourceStatus.ResourceDescriptionParseException;
34: import org.cougaar.qos.ResourceStatus.ResourceNode;
35:
36: public class PathParser {
37:
38: public static ResourceNode parseNode(String spec) {
39: String[] args;
40: int left_paren = spec.indexOf('(');
41: int right_paren = spec.indexOf(')');
42: String kind;
43: if (left_paren < 0 || right_paren < 0) {
44: kind = spec;
45: args = new String[0];
46: } else {
47: kind = spec.substring(0, left_paren);
48: if (left_paren + 1 < right_paren) {
49: String arg_spec = spec.substring(left_paren + 1,
50: right_paren);
51: StringTokenizer tk = new StringTokenizer(arg_spec, ",");
52: int count = tk.countTokens();
53: args = new String[count];
54: for (int i = 0; i < count; i++) {
55: args[i] = tk.nextToken();
56: }
57: } else {
58: args = new String[0];
59: }
60: }
61: ResourceNode node = new ResourceNode();
62: node.kind = kind;
63: node.parameters = args;
64: return node;
65: }
66:
67: public static ResourceNode[] parsePath(String spec)
68: throws ResourceDescriptionParseException {
69: // kind(arg, ..., arg):...:kind(arg, ..., arg)
70: ResourceNode[] path = null;
71: try {
72: StringTokenizer tk = new StringTokenizer(spec, ":");
73: int count = tk.countTokens();
74: path = new ResourceNode[count];
75: for (int i = 0; i < count; i++) {
76: path[i] = parseNode(tk.nextToken());
77: }
78: } catch (Exception ex) {
79: throw new ResourceDescriptionParseException(spec, ex
80: .getMessage());
81: }
82:
83: return path;
84: }
85:
86: }
|