user1144004
user1144004

Reputation: 302

Detail Formatter: code snippet cannot refer to other classes whose import is not declared

I am using Eclipse Oxygen.3a

I have a class which is a representation of an XML file. This class does not have toString() overridden. I want to use Detail Formatter to show the pretty-print format of the XML it is representing.

Detail Formatter code snippet:

try{
        String xml = getXmlString(); // This method returns single line string representation of the XML
        final InputSource src = new InputSource(new StringReader(xml));
        final Node document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(src).getDocumentElement();
        final Boolean keepDeclaration = Boolean.valueOf(xml.startsWith("<?xml"));
        final DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
        final DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
        final LSSerializer writer = impl.createLSSerializer();

        writer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE); // Set this to true if the output needs to be beautified.
        writer.getDomConfig().setParameter("xml-declaration", keepDeclaration); // Set this to true if the declaration is needed to be outputted.

        return writer.writeToString(document);
    }
    catch (Exception e) {
        return getXmlString();
    }

The problem is that, the class for which I am defining this does not have import declaration for many of the classes I am using in the code snippet. This results in below error:

Detail formatter error:
    InputSource cannot be resolved to a type
    InputSource cannot be resolved to a type
    StringReader cannot be resolved to a type
    Node cannot be resolved to a type
    DOMImplementationRegistry cannot be resolved to a type
    DOMImplementationRegistry cannot be resolved
    DOMImplementationLS cannot be resolved to a type
    DOMImplementationLS cannot be resolved to a type
    LSSerializer cannot be resolved to a type

How can this be achieved? If Detail Formatter cannot support this, is there any other way to achieve this?

NOTE This class is a legacy class for which I am trying to write Detail Formatter. I have the source code but I cannot make any changes in this class.

Upvotes: 0

Views: 227

Answers (1)

Daniel Gorodowienko
Daniel Gorodowienko

Reputation: 60

Use full class path.
For example:

java.lang.String xml = getXmlString();

Upvotes: 3

Related Questions