1 7 8 package javax.management.loading; 9 10 11 import java.net.URL ; 13 import java.net.URLConnection ; 14 import java.net.MalformedURLException ; 15 import java.io.Reader ; 16 import java.io.IOException ; 17 import java.io.BufferedReader ; 18 import java.io.InputStreamReader ; 19 import java.io.File ; 20 import java.util.Hashtable ; 21 import java.util.Vector ; 22 23 import com.sun.jmx.trace.Trace; 24 25 26 31 class MLetParser { 32 33 38 39 42 private int c; 43 44 47 private static String tag = "mlet"; 48 49 52 private String dbgTag = "MLetParser"; 53 54 55 60 61 64 public MLetParser() { 65 } 66 67 72 73 76 public void skipSpace(Reader in) throws IOException { 77 while ((c >= 0) && ((c == ' ') || (c == '\t') || (c == '\n') || (c == '\r'))) { 78 c = in.read(); 79 } 80 } 81 82 85 public String scanIdentifier(Reader in) throws IOException { 86 StringBuffer buf = new StringBuffer (); 87 while (true) { 88 if (((c >= 'a') && (c <= 'z')) || 89 ((c >= 'A') && (c <= 'Z')) || 90 ((c >= '0') && (c <= '9')) || (c == '_')) { 91 buf.append((char)c); 92 c = in.read(); 93 } else { 94 return buf.toString(); 95 } 96 } 97 } 98 99 102 public Hashtable scanTag(Reader in) throws IOException { 103 Hashtable atts = new Hashtable (); 104 skipSpace(in); 105 while (c >= 0 && c != '>') { 106 String att = scanIdentifier(in); 107 String val = ""; 108 skipSpace(in); 109 if (c == '=') { 110 int quote = -1; 111 c = in.read(); 112 skipSpace(in); 113 if ((c == '\'') || (c == '\"')) { 114 quote = c; 115 c = in.read(); 116 } 117 StringBuffer buf = new StringBuffer (); 118 while ((c > 0) && 119 (((quote < 0) && (c != ' ') && (c != '\t') && 120 (c != '\n') && (c != '\r') && (c != '>')) 121 || ((quote >= 0) && (c != quote)))) { 122 buf.append((char)c); 123 c = in.read(); 124 } 125 if (c == quote) { 126 c = in.read(); 127 } 128 skipSpace(in); 129 val = buf.toString(); 130 } 131 atts.put(att.toLowerCase(), val); 132 skipSpace(in); 133 } 134 return atts; 135 } 136 137 140 public Vector parse(URL url) throws IOException { 141 String mth = "parse"; 142 String requiresNameWarning = "<param name=... value=...> tag requires name parameter."; 144 String paramOutsideWarning = "<param> tag outside <mlet> ... </mlet>."; 145 String requiresCodeWarning = "<mlet> tag requires either code or object parameter."; 146 String requiresJarsWarning = "<mlet> tag requires archive parameter."; 147 148 URLConnection conn; 149 150 conn = url.openConnection(); 151 Reader in = new BufferedReader (new InputStreamReader (conn.getInputStream(), "UTF-8")); 152 153 url = conn.getURL(); 157 158 Vector mlets = new Vector (); 159 Hashtable atts = null; 160 161 Vector types = new Vector (); 162 Vector values = new Vector (); 163 164 while(true) { 166 c = in.read(); 167 if (c == -1) 168 break; 169 if (c == '<') { 170 c = in.read(); 171 if (c == '/') { 172 c = in.read(); 173 String nm = scanIdentifier(in); 174 if (nm.equalsIgnoreCase(tag)) { 175 if (atts != null) { 176 if ((types.size() == values.size()) && ((!types.isEmpty()) && (!values.isEmpty()))) { 178 atts.put("types", types.clone()); 179 atts.put("values", values.clone()); 180 } 181 mlets.addElement(new MLetContent (url, atts)); 182 } 183 atts = null; 184 types.removeAllElements(); 185 values.removeAllElements(); 186 } 187 } else { 188 String nm = scanIdentifier(in); 189 if (nm.equalsIgnoreCase("arg")) { 190 Hashtable t = scanTag(in); 191 String att = (String ) t.get("type"); 192 if (att == null) { 193 if (isTraceOn()) { 194 trace(mth, requiresNameWarning); 195 } 196 throw new IOException (requiresNameWarning); 197 } else { 198 if (atts != null) { 199 types.addElement(att); 200 } else { 201 if (isTraceOn()) { 202 trace(mth, paramOutsideWarning); 203 } 204 throw new IOException (paramOutsideWarning); 205 } 206 } 207 String val = (String ) t.get("value"); 208 if (val == null) { 209 if (isTraceOn()) { 210 trace(mth, requiresNameWarning); 211 } 212 throw new IOException (requiresNameWarning); 213 } else { 214 if (atts != null) { 215 values.addElement(val); 216 } else { 217 if (isTraceOn()) { 218 trace(mth, paramOutsideWarning); 219 } 220 throw new IOException (paramOutsideWarning); 221 } 222 } 223 } 224 else { 225 if (nm.equalsIgnoreCase(tag)) { 226 atts = scanTag(in); 227 if (atts.get("code") == null && atts.get("object") == null) { 228 if (isTraceOn()) { 229 trace(mth, requiresCodeWarning); 230 } 231 atts = null; 232 throw new IOException (requiresCodeWarning); 233 } 234 if (atts.get("archive") == null) { 235 if (isTraceOn()) { 236 trace(mth, requiresJarsWarning); 237 } 238 atts = null; 239 throw new IOException (requiresJarsWarning); 240 } 241 } 242 } 243 } 244 } 245 } 246 in.close(); 247 return mlets; 248 } 249 250 253 public Vector parseURL(String urlname) throws IOException { 254 URL url = null; 257 if (urlname.indexOf(':') <= 1) { 258 String userDir = System.getProperty("user.dir"); 259 String prot; 260 if (userDir.charAt(0) == '/' || 261 userDir.charAt(0) == File.separatorChar) { 262 prot = "file:"; 263 } else { 264 prot = "file:/"; 265 } 266 url = 267 new URL (prot + userDir.replace(File.separatorChar, '/') + "/"); 268 url = new URL (url, urlname); 269 } else { 270 url = new URL (urlname); 271 } 272 return parse(url); 275 } 276 277 282 283 286 private boolean isTraceOn() { 287 return Trace.isSelected(Trace.LEVEL_TRACE, Trace.INFO_MLET); 288 } 289 290 private void trace(String clz, String func, String info) { 291 Trace.send(Trace.LEVEL_TRACE, Trace.INFO_MLET, clz, func, info); 292 } 293 294 private void trace(String func, String info) { 295 trace(dbgTag, func, info); 296 } 297 298 private boolean isDebugOn() { 299 return Trace.isSelected(Trace.LEVEL_DEBUG, Trace.INFO_MLET); 300 } 301 302 private void debug(String clz, String func, String info) { 303 Trace.send(Trace.LEVEL_DEBUG, Trace.INFO_MLET, clz, func, info); 304 } 305 306 private void debug(String func, String info) { 307 debug(dbgTag, func, info); 308 } 309 310 } 311 | Popular Tags |