KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > management > loading > MLetParser


1 /*
2  * @(#)MLetParser.java 1.26 04/04/16
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package javax.management.loading;
9
10
11 // java import
12
import java.net.URL JavaDoc;
13 import java.net.URLConnection JavaDoc;
14 import java.net.MalformedURLException JavaDoc;
15 import java.io.Reader JavaDoc;
16 import java.io.IOException JavaDoc;
17 import java.io.BufferedReader JavaDoc;
18 import java.io.InputStreamReader JavaDoc;
19 import java.io.File JavaDoc;
20 import java.util.Hashtable JavaDoc;
21 import java.util.Vector JavaDoc;
22
23 import com.sun.jmx.trace.Trace;
24
25
26 /**
27  * This class is used for parsing URLs.
28  *
29  * @since 1.5
30  */

31 class MLetParser {
32
33 /*
34   * ------------------------------------------
35   * PRIVATE VARIABLES
36   * ------------------------------------------
37   */

38     
39     /**
40      * The current character
41      */

42     private int c;
43
44     /**
45      * Tag to parse.
46      */

47     private static String JavaDoc tag = "mlet";
48     
49     /**
50      * The name of this class to be used for trace messages
51      */

52     private String JavaDoc dbgTag = "MLetParser";
53     
54     
55   /*
56   * ------------------------------------------
57   * CONSTRUCTORS
58   * ------------------------------------------
59   */

60
61     /**
62      * Create an MLet parser object
63      */

64     public MLetParser() {
65     }
66     
67     /*
68      * ------------------------------------------
69      * PUBLIC METHODS
70      * ------------------------------------------
71      */

72     
73     /**
74      * Scan spaces.
75      */

76     public void skipSpace(Reader JavaDoc in) throws IOException JavaDoc {
77     while ((c >= 0) && ((c == ' ') || (c == '\t') || (c == '\n') || (c == '\r'))) {
78         c = in.read();
79     }
80     }
81     
82     /**
83      * Scan identifier
84      */

85     public String JavaDoc scanIdentifier(Reader JavaDoc in) throws IOException JavaDoc {
86     StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
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     /**
100      * Scan tag
101      */

102     public Hashtable JavaDoc scanTag(Reader JavaDoc in) throws IOException JavaDoc {
103     Hashtable JavaDoc atts = new Hashtable JavaDoc();
104     skipSpace(in);
105     while (c >= 0 && c != '>') {
106         String JavaDoc att = scanIdentifier(in);
107         String JavaDoc 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 JavaDoc buf = new StringBuffer JavaDoc();
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     /**
138      * Scan an html file for <mlet> tags
139      */

140     public Vector JavaDoc parse(URL JavaDoc url) throws IOException JavaDoc {
141     String JavaDoc mth = "parse";
142     // Warning Messages
143
String JavaDoc requiresNameWarning = "<param name=... value=...> tag requires name parameter.";
144     String JavaDoc paramOutsideWarning = "<param> tag outside <mlet> ... </mlet>.";
145     String JavaDoc requiresCodeWarning = "<mlet> tag requires either code or object parameter.";
146     String JavaDoc requiresJarsWarning = "<mlet> tag requires archive parameter.";
147     
148     URLConnection JavaDoc conn;
149     
150     conn = url.openConnection();
151     Reader JavaDoc in = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(conn.getInputStream(), "UTF-8"));
152     
153     // The original URL may have been redirected - this
154
// sets it to whatever URL/codebase we ended up getting
155
//
156
url = conn.getURL();
157     
158     Vector JavaDoc mlets = new Vector JavaDoc();
159     Hashtable JavaDoc atts = null;
160     
161     Vector JavaDoc types = new Vector JavaDoc();
162     Vector JavaDoc values = new Vector JavaDoc();
163     
164     // debug("parse","*** Parsing " + url );
165
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 JavaDoc nm = scanIdentifier(in);
174             if (nm.equalsIgnoreCase(tag)) {
175             if (atts != null) {
176                 // Constructor parameters
177
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 JavaDoc(url, atts));
182             }
183             atts = null;
184             types.removeAllElements();
185             values.removeAllElements();
186             }
187         } else {
188             String JavaDoc nm = scanIdentifier(in);
189             if (nm.equalsIgnoreCase("arg")) {
190             Hashtable JavaDoc t = scanTag(in);
191             String JavaDoc att = (String JavaDoc) t.get("type");
192             if (att == null) {
193                 if (isTraceOn()) {
194                 trace(mth, requiresNameWarning);
195                 }
196                 throw new IOException JavaDoc(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 JavaDoc(paramOutsideWarning);
205                 }
206             }
207             String JavaDoc val = (String JavaDoc) t.get("value");
208             if (val == null) {
209                 if (isTraceOn()) {
210                 trace(mth, requiresNameWarning);
211                 }
212                 throw new IOException JavaDoc(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 JavaDoc(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 JavaDoc(requiresCodeWarning);
233                 }
234                 if (atts.get("archive") == null) {
235                 if (isTraceOn()) {
236                     trace(mth, requiresJarsWarning);
237                 }
238                 atts = null;
239                 throw new IOException JavaDoc(requiresJarsWarning);
240                 }
241             }
242             }
243         }
244         }
245     }
246     in.close();
247     return mlets;
248     }
249
250     /**
251      * Parse the document pointed by the URL urlname
252      */

253     public Vector JavaDoc parseURL(String JavaDoc urlname) throws IOException JavaDoc {
254     // Parse the document
255
//
256
URL JavaDoc url = null;
257     if (urlname.indexOf(':') <= 1) {
258         String JavaDoc userDir = System.getProperty("user.dir");
259         String JavaDoc 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 JavaDoc(prot + userDir.replace(File.separatorChar, '/') + "/");
268         url = new URL JavaDoc(url, urlname);
269     } else {
270         url = new URL JavaDoc(urlname);
271     }
272     // Return list of parsed MLets
273
//
274
return parse(url);
275     }
276
277     /*
278      * ------------------------------------------
279      * PRIVATE METHODS
280      * ------------------------------------------
281      */

282     
283     // TRACES & DEBUG
284
//---------------
285

286     private boolean isTraceOn() {
287         return Trace.isSelected(Trace.LEVEL_TRACE, Trace.INFO_MLET);
288     }
289
290     private void trace(String JavaDoc clz, String JavaDoc func, String JavaDoc info) {
291         Trace.send(Trace.LEVEL_TRACE, Trace.INFO_MLET, clz, func, info);
292     }
293
294     private void trace(String JavaDoc func, String JavaDoc 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 JavaDoc clz, String JavaDoc func, String JavaDoc info) {
303         Trace.send(Trace.LEVEL_DEBUG, Trace.INFO_MLET, clz, func, info);
304     }
305
306     private void debug(String JavaDoc func, String JavaDoc info) {
307         debug(dbgTag, func, info);
308     }
309
310 }
311
Popular Tags