Skip to content

Commit 95bd831

Browse files
committed
Fix issues caused by intellij class loader
1 parent 0595124 commit 95bd831

File tree

1 file changed

+68
-49
lines changed

1 file changed

+68
-49
lines changed

Utils/azuretools-core/src/com/microsoft/azuretools/azurecommons/util/ParserXMLUtility.java

Lines changed: 68 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -53,29 +53,35 @@ public final class ParserXMLUtility {
5353
/**
5454
* Parses XML file and returns XML document.
5555
*
56-
* @param fileName
57-
* .
56+
* @param fileName .
5857
* @return XML document or <B>null</B> if error occurred
5958
* @throws Exception
6059
*/
6160
public static Document parseXMLFile(final String fileName)
6261
throws Exception {
63-
DocumentBuilder docBuilder;
64-
Document doc = null;
65-
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
66-
.newInstance();
67-
docBuilderFactory.setIgnoringElementContentWhitespace(true);
68-
docBuilder = docBuilderFactory.newDocumentBuilder();
69-
File xmlFile = new File(fileName);
70-
doc = docBuilder.parse(xmlFile);
71-
return doc;
62+
final ClassLoader current = Thread.currentThread().getContextClassLoader();
63+
try {
64+
Thread.currentThread().setContextClassLoader(ParserXMLUtility.class.getClassLoader());
65+
// fixes https://dev.azure.com/mseng/VSJava/_workitems/edit/1796447
66+
// refers https://jetbrains.org/intellij/sdk/docs/basics/plugin_structure/plugin_class_loaders.html
67+
DocumentBuilder docBuilder;
68+
Document doc = null;
69+
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
70+
.newInstance();
71+
docBuilderFactory.setIgnoringElementContentWhitespace(true);
72+
docBuilder = docBuilderFactory.newDocumentBuilder();
73+
File xmlFile = new File(fileName);
74+
doc = docBuilder.parse(xmlFile);
75+
return doc;
76+
} finally {
77+
Thread.currentThread().setContextClassLoader(current);
78+
}
7279
}
7380

7481
/**
7582
* Parses Input Stream and returns XML document.
7683
*
77-
* @param inputStream
78-
* .
84+
* @param inputStream .
7985
* @return XML document or <B>null</B> if error occurred
8086
* @throws Exception
8187
*/
@@ -182,14 +188,16 @@ public static void writeFile(InputStream inStream, OutputStream out)
182188

183189
}
184190

185-
/** Generic API to delete elements from DOM */
191+
/**
192+
* Generic API to delete elements from DOM
193+
*/
186194
public static void deleteElement(Document doc, String expr)
187195
throws XPathExpressionException {
188196
if (doc == null) {
189197
throw new IllegalArgumentException(INVALID_ARG);
190198
} else {
191-
XPath xPath = XPathFactory.newInstance().newXPath();
192-
Element element = (Element) xPath.evaluate(expr, doc,
199+
XPath path = XPathFactory.newInstance().newXPath();
200+
Element element = (Element) path.evaluate(expr, doc,
193201
XPathConstants.NODE);
194202

195203
if (element != null) {
@@ -200,33 +208,42 @@ public static void deleteElement(Document doc, String expr)
200208
}
201209
}
202210

203-
/** This API evaluates XPath expression and return the result as a String. */
211+
/**
212+
* This API evaluates XPath expression and return the result as a String.
213+
*/
204214
public static String getExpressionValue(Document doc, String expr)
205215
throws XPathExpressionException {
206-
if (doc == null || expr == null || expr.isEmpty())
216+
if (doc == null || expr == null || expr.isEmpty()) {
207217
throw new IllegalArgumentException(INVALID_ARG);
218+
}
208219

209-
XPath xPath = XPathFactory.newInstance().newXPath();
210-
return xPath.evaluate(expr, doc);
220+
XPath path = XPathFactory.newInstance().newXPath();
221+
return path.evaluate(expr, doc);
211222
}
212223

213-
/** This API evaluates XPath expression and sets the value. */
224+
/**
225+
* This API evaluates XPath expression and sets the value.
226+
*/
214227
public static void setExpressionValue(Document doc, String expr,
215-
String value) throws XPathExpressionException {
216-
if (doc == null || expr == null || expr.isEmpty())
228+
String value) throws XPathExpressionException {
229+
if (doc == null || expr == null || expr.isEmpty()) {
217230
throw new IllegalArgumentException(INVALID_ARG);
231+
}
218232

219-
XPath xPath = XPathFactory.newInstance().newXPath();
220-
Node node = (Node) xPath.evaluate(expr, doc, XPathConstants.NODE);
233+
XPath path = XPathFactory.newInstance().newXPath();
234+
Node node = (Node) path.evaluate(expr, doc, XPathConstants.NODE);
221235
node.setNodeValue(value);
222236
}
223237

224238
protected static boolean isEpPortEqualOrInRange(String oldPort,
225-
String newPort) {
239+
String newPort) {
226240
boolean isEqual = false;
227241
try {
228242

229-
int oldMin, oldMax, newMin, newMax;
243+
int oldMin = 0;
244+
int oldMax = 0;
245+
int newMin = 0;
246+
int newMax = 0;
230247
if (oldPort.contains("-")) {
231248
String[] rang = oldPort.split("-");
232249
oldMin = Integer.valueOf(rang[0]);
@@ -282,25 +299,28 @@ protected static boolean isValidRange(String range) {
282299
return isValid;
283300
}
284301

285-
/** Generic API to update or create DOM elements */
302+
/**
303+
* Generic API to update or create DOM elements
304+
*/
286305
public static Element updateOrCreateElement(Document doc, String expr,
287-
String parentNodeExpr, String elementName, boolean firstChild,
288-
Map<String, String> attributes)
306+
String parentNodeExpr, String elementName, boolean firstChild,
307+
Map<String, String> attributes)
289308
throws Exception {
290309

291310
if (doc == null) {
292311
throw new IllegalArgumentException(INVALID_ARG);
293312
} else {
294-
XPath xPath = XPathFactory.newInstance().newXPath();
313+
XPath path = XPathFactory.newInstance().newXPath();
295314
Element element = null;
296-
if (expr != null)
297-
element = (Element) xPath.evaluate(expr, doc,
315+
if (expr != null) {
316+
element = (Element) path.evaluate(expr, doc,
298317
XPathConstants.NODE);
318+
}
299319

300320
// If element doesn't exist create one
301321
if (element == null) {
302322
element = doc.createElement(elementName);
303-
Element parentElement = (Element) xPath.evaluate(
323+
Element parentElement = (Element) path.evaluate(
304324
parentNodeExpr, doc, XPathConstants.NODE);
305325
if (firstChild) {
306326
parentElement.insertBefore(
@@ -323,20 +343,23 @@ public static Element updateOrCreateElement(Document doc, String expr,
323343
}
324344
}
325345

326-
/** Generic API to update or create DOM elements */
346+
/**
347+
* Generic API to update or create DOM elements
348+
*/
327349
public static Element createElement(Document doc, String expr,
328-
Element parentElement, String elementName, boolean firstChild,
329-
Map<String, String> attributes)
350+
Element parentElement, String elementName, boolean firstChild,
351+
Map<String, String> attributes)
330352
throws Exception {
331353

332354
if (doc == null) {
333355
throw new IllegalArgumentException(INVALID_ARG);
334356
} else {
335-
XPath xPath = XPathFactory.newInstance().newXPath();
357+
XPath path = XPathFactory.newInstance().newXPath();
336358
Element element = null;
337-
if (expr != null)
338-
element = (Element) xPath.evaluate(expr, doc,
359+
if (expr != null) {
360+
element = (Element) path.evaluate(expr, doc,
339361
XPathConstants.NODE);
362+
}
340363

341364
// If element doesn't exist create one
342365
if (element == null) {
@@ -364,22 +387,18 @@ public static Element createElement(Document doc, String expr,
364387

365388
/**
366389
* API checks if a node is already present in the XML document
390+
*
367391
* @param doc
368392
* @param nodeExpression
369-
* @return
370393
* @throws Exception
371394
*/
372395
public static boolean doesNodeExists(Document doc, String nodeExpression) throws Exception {
373-
if (nodeExpression == null ) {
396+
if (nodeExpression == null) {
374397
throw new IllegalArgumentException(INVALID_ARG);
375398
} else {
376-
XPath xPath = XPathFactory.newInstance().newXPath();
377-
Element element = (Element) xPath.evaluate(nodeExpression, doc, XPathConstants.NODE);
378-
if (element == null) {
379-
return false;
380-
} else {
381-
return true;
382-
}
399+
XPath path = XPathFactory.newInstance().newXPath();
400+
Element element = (Element) path.evaluate(nodeExpression, doc, XPathConstants.NODE);
401+
return element != null;
383402
}
384403
}
385404
}

0 commit comments

Comments
 (0)