1 | /************************************************************************************ |
---|
2 | * Copyright (C) 2008 by Politehnica University of Bucharest and Rutgers University |
---|
3 | * All rights reserved. |
---|
4 | * Refer to LICENSE for terms and conditions of use. |
---|
5 | ***********************************************************************************/ |
---|
6 | package vnsim.applications.vitp.utils; |
---|
7 | |
---|
8 | import java.util.*; |
---|
9 | |
---|
10 | public class URIParser { |
---|
11 | //this class contains static methods, that help parsing a URI; |
---|
12 | |
---|
13 | //URI Format: "/type/tag?[cnt=*&tout=*]¶m1=*¶m2=*..." |
---|
14 | |
---|
15 | //all the methods return null if the format of the URI is wrong |
---|
16 | |
---|
17 | public static String getType(String uri) { |
---|
18 | if(uri==null) return null; |
---|
19 | if(!uri.startsWith("/")) return null; |
---|
20 | int i1=uri.indexOf('/',1); |
---|
21 | if(i1==-1) return null; |
---|
22 | return uri.substring(1,i1); |
---|
23 | } |
---|
24 | |
---|
25 | public static String getTag(String uri) { |
---|
26 | if(uri==null) return null; |
---|
27 | if(!uri.startsWith("/")) return null; |
---|
28 | int i1=uri.indexOf('/',1); |
---|
29 | if(i1==-1) return null; |
---|
30 | int i2=uri.indexOf('?',i1); |
---|
31 | if(i2==-1) return uri.substring(i1+1); |
---|
32 | return uri.substring(i1+1,i2); |
---|
33 | } |
---|
34 | |
---|
35 | public static Vector<String> getRCExpressions(String uri){ |
---|
36 | //returns a vector of all expressions within the brackets; |
---|
37 | //the corresponding service should do more parsing inside |
---|
38 | //each expression, since it could have particular formats |
---|
39 | //(e.g with < > *) |
---|
40 | |
---|
41 | if(uri==null) return null; |
---|
42 | if(!uri.startsWith("/")) return null; |
---|
43 | int i1=uri.indexOf('/',1); |
---|
44 | if(i1==-1) return null; |
---|
45 | int i2=uri.indexOf('?',i1); |
---|
46 | if(i2==-1) return null; |
---|
47 | if(uri.charAt(i2+1)!='[') return null; |
---|
48 | int index=i2+2; |
---|
49 | int finIndex=uri.indexOf(']',index); |
---|
50 | if(finIndex==-1) return null; |
---|
51 | String rcs=uri.substring(index,finIndex); |
---|
52 | Vector<String> ret=new Vector<String>(); |
---|
53 | StringTokenizer st=new StringTokenizer(rcs,"&"); |
---|
54 | while(st.hasMoreTokens()) { |
---|
55 | ret.addElement(st.nextToken()); |
---|
56 | } |
---|
57 | return ret; |
---|
58 | } |
---|
59 | |
---|
60 | public static Vector<String> getParams(String uri){ |
---|
61 | //returns the parameters after the [] as a vector of Strings |
---|
62 | //the corresponding service should do more parsing inside |
---|
63 | //each expression, since it could have particular formats |
---|
64 | //(e.g with < > *) |
---|
65 | if(uri==null) return null; |
---|
66 | if(!uri.startsWith("/")) return null; |
---|
67 | int i1=uri.indexOf('/',1); |
---|
68 | if(i1==-1) return null; |
---|
69 | i1=uri.indexOf('?',i1); |
---|
70 | if(i1==-1) return null; |
---|
71 | i1=uri.indexOf('[',i1); |
---|
72 | if(i1==-1) return null; |
---|
73 | i1=uri.indexOf(']',i1); |
---|
74 | if(i1==-1) return null; |
---|
75 | Vector<String> ret=new Vector<String>(); |
---|
76 | i1++; |
---|
77 | if(i1>=uri.length()) return ret; |
---|
78 | if(uri.charAt(i1)!='&') return null; |
---|
79 | StringTokenizer st=new StringTokenizer(uri.substring(i1),"&"); |
---|
80 | while(st.hasMoreTokens()) { |
---|
81 | ret.addElement(st.nextToken()); |
---|
82 | } |
---|
83 | return ret; |
---|
84 | } |
---|
85 | |
---|
86 | public static String getURIFromParts(String type, String tag, Vector h1, Vector h2){ |
---|
87 | if(type==null || tag==null || h1==null || h2==null) |
---|
88 | return null; |
---|
89 | String ret="/"+type+"/"+tag+"?["; |
---|
90 | for(int i=0;i<h1.size();i++) { |
---|
91 | ret=ret+((String)h1.elementAt(i)); |
---|
92 | if(i<h1.size()-1) |
---|
93 | ret=ret+"&"; |
---|
94 | } |
---|
95 | ret=ret+"]"; |
---|
96 | for(int i=0;i<h2.size();i++) { |
---|
97 | ret=ret+"&"+((String)h2.elementAt(i)); |
---|
98 | } |
---|
99 | return ret; |
---|
100 | } |
---|
101 | } |
---|