author | ohair |
Tue, 25 May 2010 15:58:33 -0700 | |
changeset 5506 | 202f599c92aa |
parent 715 | f16baef3a20e |
child 21656 | d4c777ccb1db |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 1999, 2008, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package javax.management.loading; |
|
27 |
||
28 |
import static com.sun.jmx.defaults.JmxProperties.MLET_LOGGER; |
|
29 |
||
30 |
import java.io.BufferedReader; |
|
31 |
import java.io.File; |
|
32 |
import java.io.IOException; |
|
33 |
import java.io.InputStreamReader; |
|
34 |
import java.io.Reader; |
|
35 |
import java.net.URL; |
|
36 |
import java.net.URLConnection; |
|
37 |
import java.util.ArrayList; |
|
38 |
import java.util.HashMap; |
|
39 |
import java.util.List; |
|
40 |
import java.util.Map; |
|
41 |
import java.util.logging.Level; |
|
42 |
||
43 |
/** |
|
44 |
* This class is used for parsing URLs. |
|
45 |
* |
|
46 |
* @since 1.5 |
|
47 |
*/ |
|
48 |
class MLetParser { |
|
49 |
||
50 |
/* |
|
51 |
* ------------------------------------------ |
|
52 |
* PRIVATE VARIABLES |
|
53 |
* ------------------------------------------ |
|
54 |
*/ |
|
55 |
||
56 |
/** |
|
57 |
* The current character |
|
58 |
*/ |
|
59 |
private int c; |
|
60 |
||
61 |
/** |
|
62 |
* Tag to parse. |
|
63 |
*/ |
|
64 |
private static String tag = "mlet"; |
|
65 |
||
66 |
||
67 |
/* |
|
68 |
* ------------------------------------------ |
|
69 |
* CONSTRUCTORS |
|
70 |
* ------------------------------------------ |
|
71 |
*/ |
|
72 |
||
73 |
/** |
|
74 |
* Create an MLet parser object |
|
75 |
*/ |
|
76 |
public MLetParser() { |
|
77 |
} |
|
78 |
||
79 |
/* |
|
80 |
* ------------------------------------------ |
|
81 |
* PUBLIC METHODS |
|
82 |
* ------------------------------------------ |
|
83 |
*/ |
|
84 |
||
85 |
/** |
|
86 |
* Scan spaces. |
|
87 |
*/ |
|
88 |
public void skipSpace(Reader in) throws IOException { |
|
89 |
while ((c >= 0) && ((c == ' ') || (c == '\t') || (c == '\n') || (c == '\r'))) { |
|
90 |
c = in.read(); |
|
91 |
} |
|
92 |
} |
|
93 |
||
94 |
/** |
|
95 |
* Scan identifier |
|
96 |
*/ |
|
97 |
public String scanIdentifier(Reader in) throws IOException { |
|
98 |
StringBuilder buf = new StringBuilder(); |
|
99 |
while (true) { |
|
100 |
if (((c >= 'a') && (c <= 'z')) || |
|
101 |
((c >= 'A') && (c <= 'Z')) || |
|
102 |
((c >= '0') && (c <= '9')) || (c == '_')) { |
|
103 |
buf.append((char)c); |
|
104 |
c = in.read(); |
|
105 |
} else { |
|
106 |
return buf.toString(); |
|
107 |
} |
|
108 |
} |
|
109 |
} |
|
110 |
||
111 |
/** |
|
112 |
* Scan tag |
|
113 |
*/ |
|
114 |
public Map<String,String> scanTag(Reader in) throws IOException { |
|
115 |
Map<String,String> atts = new HashMap<String,String>(); |
|
116 |
skipSpace(in); |
|
117 |
while (c >= 0 && c != '>') { |
|
118 |
if (c == '<') |
|
119 |
throw new IOException("Missing '>' in tag"); |
|
120 |
String att = scanIdentifier(in); |
|
121 |
String val = ""; |
|
122 |
skipSpace(in); |
|
123 |
if (c == '=') { |
|
124 |
int quote = -1; |
|
125 |
c = in.read(); |
|
126 |
skipSpace(in); |
|
127 |
if ((c == '\'') || (c == '\"')) { |
|
128 |
quote = c; |
|
129 |
c = in.read(); |
|
130 |
} |
|
131 |
StringBuilder buf = new StringBuilder(); |
|
132 |
while ((c > 0) && |
|
133 |
(((quote < 0) && (c != ' ') && (c != '\t') && |
|
134 |
(c != '\n') && (c != '\r') && (c != '>')) |
|
135 |
|| ((quote >= 0) && (c != quote)))) { |
|
136 |
buf.append((char)c); |
|
137 |
c = in.read(); |
|
138 |
} |
|
139 |
if (c == quote) { |
|
140 |
c = in.read(); |
|
141 |
} |
|
142 |
skipSpace(in); |
|
143 |
val = buf.toString(); |
|
144 |
} |
|
145 |
atts.put(att.toLowerCase(), val); |
|
146 |
skipSpace(in); |
|
147 |
} |
|
148 |
return atts; |
|
149 |
} |
|
150 |
||
151 |
/** |
|
152 |
* Scan an html file for <mlet> tags |
|
153 |
*/ |
|
154 |
public List<MLetContent> parse(URL url) throws IOException { |
|
155 |
String mth = "parse"; |
|
156 |
// Warning Messages |
|
157 |
String requiresTypeWarning = "<arg type=... value=...> tag requires type parameter."; |
|
158 |
String requiresValueWarning = "<arg type=... value=...> tag requires value parameter."; |
|
159 |
String paramOutsideWarning = "<arg> tag outside <mlet> ... </mlet>."; |
|
160 |
String requiresCodeWarning = "<mlet> tag requires either code or object parameter."; |
|
161 |
String requiresJarsWarning = "<mlet> tag requires archive parameter."; |
|
162 |
||
163 |
URLConnection conn; |
|
164 |
||
165 |
conn = url.openConnection(); |
|
166 |
Reader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), |
|
167 |
"UTF-8")); |
|
168 |
||
169 |
// The original URL may have been redirected - this |
|
170 |
// sets it to whatever URL/codebase we ended up getting |
|
171 |
// |
|
172 |
url = conn.getURL(); |
|
173 |
||
174 |
List<MLetContent> mlets = new ArrayList<MLetContent>(); |
|
175 |
Map<String,String> atts = null; |
|
176 |
||
177 |
List<String> types = new ArrayList<String>(); |
|
178 |
List<String> values = new ArrayList<String>(); |
|
179 |
||
180 |
// debug("parse","*** Parsing " + url ); |
|
181 |
while(true) { |
|
182 |
c = in.read(); |
|
183 |
if (c == -1) |
|
184 |
break; |
|
185 |
if (c == '<') { |
|
186 |
c = in.read(); |
|
187 |
if (c == '/') { |
|
188 |
c = in.read(); |
|
189 |
String nm = scanIdentifier(in); |
|
190 |
if (c != '>') |
|
191 |
throw new IOException("Missing '>' in tag"); |
|
192 |
if (nm.equalsIgnoreCase(tag)) { |
|
193 |
if (atts != null) { |
|
194 |
mlets.add(new MLetContent(url, atts, types, values)); |
|
195 |
} |
|
196 |
atts = null; |
|
197 |
types = new ArrayList<String>(); |
|
198 |
values = new ArrayList<String>(); |
|
199 |
} |
|
200 |
} else { |
|
201 |
String nm = scanIdentifier(in); |
|
202 |
if (nm.equalsIgnoreCase("arg")) { |
|
203 |
Map<String,String> t = scanTag(in); |
|
204 |
String att = t.get("type"); |
|
205 |
if (att == null) { |
|
206 |
MLET_LOGGER.logp(Level.FINER, |
|
207 |
MLetParser.class.getName(), |
|
208 |
mth, requiresTypeWarning); |
|
209 |
throw new IOException(requiresTypeWarning); |
|
210 |
} else { |
|
211 |
if (atts != null) { |
|
212 |
types.add(att); |
|
213 |
} else { |
|
214 |
MLET_LOGGER.logp(Level.FINER, |
|
215 |
MLetParser.class.getName(), |
|
216 |
mth, paramOutsideWarning); |
|
217 |
throw new IOException(paramOutsideWarning); |
|
218 |
} |
|
219 |
} |
|
220 |
String val = t.get("value"); |
|
221 |
if (val == null) { |
|
222 |
MLET_LOGGER.logp(Level.FINER, |
|
223 |
MLetParser.class.getName(), |
|
224 |
mth, requiresValueWarning); |
|
225 |
throw new IOException(requiresValueWarning); |
|
226 |
} else { |
|
227 |
if (atts != null) { |
|
228 |
values.add(val); |
|
229 |
} else { |
|
230 |
MLET_LOGGER.logp(Level.FINER, |
|
231 |
MLetParser.class.getName(), |
|
232 |
mth, paramOutsideWarning); |
|
233 |
throw new IOException(paramOutsideWarning); |
|
234 |
} |
|
235 |
} |
|
236 |
} else { |
|
237 |
if (nm.equalsIgnoreCase(tag)) { |
|
238 |
atts = scanTag(in); |
|
239 |
if (atts.get("code") == null && atts.get("object") == null) { |
|
240 |
MLET_LOGGER.logp(Level.FINER, |
|
241 |
MLetParser.class.getName(), |
|
242 |
mth, requiresCodeWarning); |
|
243 |
throw new IOException(requiresCodeWarning); |
|
244 |
} |
|
245 |
if (atts.get("archive") == null) { |
|
246 |
MLET_LOGGER.logp(Level.FINER, |
|
247 |
MLetParser.class.getName(), |
|
248 |
mth, requiresJarsWarning); |
|
249 |
throw new IOException(requiresJarsWarning); |
|
250 |
} |
|
251 |
} |
|
252 |
} |
|
253 |
} |
|
254 |
} |
|
255 |
} |
|
256 |
in.close(); |
|
257 |
return mlets; |
|
258 |
} |
|
259 |
||
260 |
/** |
|
261 |
* Parse the document pointed by the URL urlname |
|
262 |
*/ |
|
263 |
public List<MLetContent> parseURL(String urlname) throws IOException { |
|
264 |
// Parse the document |
|
265 |
// |
|
526
61ba2d5ea9da
6701459: Synchronization bug pattern found in javax.management.relation.RelationService
emcmanus
parents:
2
diff
changeset
|
266 |
URL url; |
2 | 267 |
if (urlname.indexOf(':') <= 1) { |
268 |
String userDir = System.getProperty("user.dir"); |
|
269 |
String prot; |
|
270 |
if (userDir.charAt(0) == '/' || |
|
271 |
userDir.charAt(0) == File.separatorChar) { |
|
272 |
prot = "file:"; |
|
273 |
} else { |
|
274 |
prot = "file:/"; |
|
275 |
} |
|
276 |
url = |
|
277 |
new URL(prot + userDir.replace(File.separatorChar, '/') + "/"); |
|
278 |
url = new URL(url, urlname); |
|
279 |
} else { |
|
280 |
url = new URL(urlname); |
|
281 |
} |
|
282 |
// Return list of parsed MLets |
|
283 |
// |
|
284 |
return parse(url); |
|
285 |
} |
|
286 |
||
287 |
} |