Source code: gov/lanl/Utility/ResourceNameTranslate.java
1 /*
2 * ResourceNameComponentStr.java
3 *
4 * Created on February 7, 2002, 4:28 PM
5 */
6 // Intellectual Properties Notice
7
8 /**
9 * ***********************************
10 * Copyright Notice
11 * Copyright (c) 1999,2000,2001,2002 Regents of the University of California. All rights reserved.
12 *
13 * DISCLAIMER
14 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS
15 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
17 * SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
18 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
20 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
22 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
23 * DAMAGE.
24 * ************************************
25 */
26 package gov.lanl.Utility;
27
28 import org.omg.DfResourceAccessDecision.InvalidResourceName;
29 import org.omg.DfResourceAccessDecision.ResourceName;
30 import org.omg.DfResourceAccessDecision.ResourceNameComponent;
31
32 import java.util.StringTokenizer;
33 import java.util.Vector;
34
35 /**
36 * This class converts a String representation of a ResourceNameComponent into the appropriate
37 * structure and vice versa. This is particularly useful for mapping String names into the RAD space.
38 * e.g. Id.10001/field.BACTERIOLOGY maps to a ResourceNameComponent array of two elements. The element
39 * before the period is the name_string and the element after the period is the value_string
40 * This follows the specification for Stringified NameComponents as specified in the updated NamingService
41 * This seems appropriate because the ResourceNameComponent was borrowed from that specification.
42 * @author $Author: dwforslund $
43 * @version $Id: ResourceNameTranslate.java,v 1.5 2002/04/25 23:09:18 dwforslund Exp $
44 */
45 public class ResourceNameTranslate {
46 private static String SEPARATOR = "://";
47 private static org.apache.log4j.Logger cat =
48 org.apache.log4j.Logger.getLogger(ResourceNameTranslate.class.getName());
49
50 /** Creates a new instance of ResourceNameTranslate */
51 private ResourceNameTranslate() {
52 }
53
54 /**
55 * Check to see if Resource string is a valid ResourceName
56 * @param resource string containing the ResourceName
57 * @return boolean result of test
58 */
59 public static boolean isValid(String resource) {
60 try {
61 toResource(resource);
62 return true;
63 } catch (InvalidResourceName name) {
64 return false;
65 }
66 }
67
68 /**
69 * Extend convention to handle resource name
70 * prepend the authority as though it were a protocol.
71 * @param resource ResourceName to be stringified
72 * @return String representation of input ResourceName
73 */
74 public static String toString(ResourceName resource) {
75 return resource.resource_naming_authority + SEPARATOR + toString(resource.resource_name_component_list);
76 }
77
78 /**
79 * Convert resourceName string into a full resourceName
80 * @param resource string with the full resourceName
81 * @return ResourceName
82 */
83 public static ResourceName toResource(String resource) throws InvalidResourceName {
84 int i = resource.indexOf(SEPARATOR);
85 if (i == -1) throw new InvalidResourceName(); // ensure that the leading string is present
86 ResourceName resourceName = new ResourceName();
87 resourceName.resource_naming_authority = resource.substring(0, i);
88
89 resourceName.resource_name_component_list = toName(resource.substring(i + SEPARATOR.length()));
90 return resourceName;
91
92 }
93
94 /**
95 * Convert ResourceNameComponent[] into a String
96 * It will quote any literal '.', '/', and '\' characters by adding a preceding '\'
97 * @param ResourceNameComponentList to be converted to String
98 * @return String containing Stringified ResourceNameComponent
99 */
100 public static String toString(ResourceNameComponent[] ResourceNameComponentList) {
101 StringBuffer buf = new StringBuffer();
102 for (int i = 0; i < ResourceNameComponentList.length; i++) {
103 // System.out.println(ResourceNameComponentList[i].name_string + ","+ResourceNameComponentList[i].value_string);
104 StringTokenizer st1 = new StringTokenizer(ResourceNameComponentList[i].name_string, "./\\", true);
105 StringTokenizer st2 = new StringTokenizer(ResourceNameComponentList[i].value_string, "./\\", true);
106 StringBuffer buf1 = new StringBuffer();
107 StringBuffer buf2 = new StringBuffer();
108
109 while (st1.hasMoreTokens()) {
110 String s = st1.nextToken();
111 if (s.equals("."))
112 buf1.append("\\.");
113 else if (s.equals("/"))
114 buf1.append("\\/");
115 else if (s.equals("\\"))
116 buf1.append("\\\\");
117 else
118 buf1.append(s);
119 }
120 while (st2.hasMoreTokens()) {
121 String s = st2.nextToken();
122 if (s.equals("."))
123 buf2.append("\\.");
124 else if (s.equals("/"))
125 buf2.append("\\/");
126 else if (s.equals("\\"))
127 buf2.append("\\\\");
128 else
129 buf2.append(s);
130 }
131
132 buf.append(buf1.toString());
133 if (!ResourceNameComponentList[i].value_string.equals("")) {
134 buf.append(".");
135 buf.append(buf2.toString());
136 }
137 if (i < ResourceNameComponentList.length - 1)
138 buf.append("/");
139
140 }
141 return buf.toString();
142 }
143
144 /**
145 * function to turn string into a ResourceNameComponent array
146 * The characters '.', '/' and '\' can be included in the string values if they are preceded with '\'
147 * @param resourceName to be converted to ResourceNameComponent
148 * @return ResourceNameComponent[]
149 *
150 */
151 public static ResourceNameComponent[] toName(String resourceName) throws InvalidResourceName {
152 StringTokenizer strTok = new StringTokenizer(resourceName, "./\\", true);
153 Vector name = new Vector();
154 Vector value = new Vector();
155 StringBuffer buf = new StringBuffer();
156
157 while (strTok.hasMoreTokens()) {
158
159 String s = strTok.nextToken();
160 // System.out.println("s: '"+s+"'");
161 if (s.equals("\\")) {
162
163 buf.append(strTok.nextToken());
164 // System.out.println("adding quoted: "+buf);
165
166
167 } else if (s.equals(".")) {
168 value.addElement(buf.toString());
169 // System.out.println("adding 1st element to value "+buf);
170 buf = new StringBuffer();
171 } else if (s.equals("/")) {
172 value.addElement(buf.toString());
173 name.addElement(value);
174 // System.out.println("adding 2 element to value " + buf);
175 buf = new StringBuffer();
176 value = new Vector();
177 } else {
178 buf.append(s);
179 // System.out.println(" appending "+s);
180 }
181 }
182 //System.out.println("adding 2nd value "+buf);
183 value.addElement(buf.toString());
184 // System.out.println("value length: "+value.size());
185 name.addElement(value);
186
187
188
189 // create the naming context
190 //
191 //ResourceNameComponent[] oldNcName = null;
192 ResourceNameComponent[] ncName = new ResourceNameComponent[name.size()];
193 for (int i = 0; i < name.size(); i++) {
194 // oldNcName = ncName;
195 Vector v = (Vector) name.elementAt(i);
196 ncName[i] = new ResourceNameComponent();
197 //for (int j=0 ; j < i ; j++)
198 // ncName[j] = oldNcName[j];
199 ncName[i].value_string = "";
200 ncName[i].name_string = "";
201 if (v != null) {
202 if (v.size() > 1) ncName[i].value_string = (String) v.elementAt(1);
203 ;
204 ncName[i].name_string = (String) v.elementAt(0);
205 ;
206 }
207 }
208 return ncName;
209 }
210
211 /**
212 * Static test of translation
213 */
214 static void main(String[] argv) {
215 try {
216 ResourceName rn = ResourceNameTranslate.toResource(argv[0]);
217
218 cat.debug("in: " + argv[0] + "\n" + "out: " + ResourceNameTranslate.toString(rn));
219
220 } catch (InvalidResourceName e) {
221 System.out.println(argv[0] + " is an invalid resource name");
222 }
223 }
224
225 }