Problem with the old version of xercesImpl.jar

Hello,

As i am working with reading and writing of XML file and i am using classes available under the package org.w3c.dom.*. MPS have xercesImpl.jar available under lib folder which is getting loaded when i start MPS. Cause of this i am getting NoSuchMethodException for the code as below.

DOMImplementationLSdomImplementation=(DOMImplementationLS)doc.getImplementation();
LSSerializerlsSerializer=domImplementation.createLSSerializer();

When i use xercesImpl-2.6.1.jar specific to my Solution the problem resolves. Is there any specific reason ,
MPS using a older version of xercesImpl? I guises many will be using org.w3c.dom.*(As comes with JDK) and availability of xercesImpl.jar in the lib folder is problematic, unless MPS changes the current class loader implementation. 

As i can see many issues are logged related to the same:
eg: https://mps-support.jetbrains.com/hc/en-us/community/posts/206614495-Java-Class-Cast-Exception-in-MPS-Class-Loader-Issue-

Please share if anybody have information about this.

Thanks & Regards,
Sanjit Kumar Mishra

0
4 comments
Avatar
Permanently deleted user

I have also experienced this issue. I have used the ClassLoader trick as a workaround but I definitely think this issue should be solved.

0

@Federico Tomassetti: ClassLoader trick does not work always example:

//////////////////////////// CODE COMENT //////////////////////////////

//This coding is correct but will not work with current MPS//

//////////////////////////////////////////////////////////////////////////////////////

ClassLoader cl=Thread.currentThread().getContextClassLoader(); 
try{
Thread.currentThread().setContextClassLoader(SequenceDiagramTestUtil.class.getClassLoader());
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.newDocument();
DOMImplementationLS domImplementation = (DOMImplementationLS)doc.getImplementation();

// Creating document object logic goes here

TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();

DOMSource source = new DOMSource(document);
StreamResult result = new StreamResult(System.out);
transformer.transform(source, result);
}
catch(IOExceptionio e){
    ioe.printStackTrace();
}
catch(ParserConfigurationException pcee){
    pcee.printStackTrace();
}
finally{
    Thread.currentThread().setContextClassLoader(cl);
}

To make it work we have to use DOMImplementationLS or any other implementation which converts dom to xml file.
0
Avatar
Permanently deleted user

Following works for me on MPS 3.3.5

private void createDocument()throws ParserConfigurationException,IOException,SAXException{ 
  File file=new File(path);
ClassLoader cl=Thread.currentThread().getContextClassLoader();
try{
  Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
DocumentBuilderFactory documentBuilderFactory=DocumentBuilderFactory.newInstance();
document=documentBuilderFactory.newDocumentBuilder().parse(file);
}finally{
  Thread.currentThread().setContextClassLoader(cl);
}
}
0

@m sch95: Yes this is the class loader trick to avoid Class cast exception but there are other problems as i discussed in my question and one of the comment related to xercesImpl.jar implementation in MPS.

0

Please sign in to leave a comment.