Geertjan's Blog

  • June 23, 2007

Integrating the XML Editor for Your File Type (Part 1)

Geertjan Wielenga
Product Manager
Thanks to help from Vadiraj, I now know how to open non-XML files in the XML editor. Sometimes you have XML content in a file that does not end in ".xml". How do you let the IDE treat it like an XML file? Below, you see a file with the extension ".test" open in the XML editor. You know it is open there because of the syntax coloring and indentation, and because of the additional menu items that relate specifically to XML files:

How to do this:

  1. Use New File Type wizard, which will give you various classes. The only class we will need to change is the XxxDataObject.
  2. Add a dependency on XML Tools API.
  3. In the data object, change the extension class from MultiDataObject to XMLDataObject.
  4. Fill out the constructor with additional functionality by providing cookies for checking XML, validating XML, and transforming XML via XSLT, like this:
    public TestDataObject(FileObject pf, TestDataLoader loader) throws DataObjectExistsException, IOException {
    super(pf, loader);
    CookieSet cookies = getCookieSet();
    InputSource is = DataObjectAdapters.inputSource(this);
    Source source = DataObjectAdapters.source(this);
    cookies.add(new CheckXMLSupport(is));
    cookies.add(new ValidateXMLSupport(is));
    cookies.add(new TransformableSupport(source));
    cookies.add((Node.Cookie) DataEditorSupport.create(this, getPrimaryEntry(), cookies));
  5. Make sure the import statements are correct:
    import java.io.IOException;
    import javax.xml.transform.Source;
    import org.netbeans.spi.xml.cookies.CheckXMLSupport;
    import org.netbeans.spi.xml.cookies.DataObjectAdapters;
    import org.netbeans.spi.xml.cookies.TransformableSupport;
    import org.netbeans.spi.xml.cookies.ValidateXMLSupport;
    import org.openide.filesystems.FileObject;
    import org.openide.loaders.DataObjectExistsException;
    import org.openide.loaders.XMLDataObject;
    import org.openide.nodes.CookieSet;
    import org.openide.nodes.Node;
    import org.openide.text.DataEditorSupport;
    import org.xml.sax.InputSource;

That's it. You're done. Your file will now be treated as an XML file, like all other XML files.

Important. Make sure to read part 2 as well.

Join the discussion

Comments ( 9 )
  • Laszlo Kishalmi Saturday, June 23, 2007
    Well, it's fine. However assing a new extension to:
    Tools > Options > Advanced Options > IDE Configuration > System > Object Types > XML Object > Extensions and MIME Types is a bit easier.
  • Geertjan Saturday, June 23, 2007
    You're missing the point. My blog entry is about creating plugins for NetBeans IDE or for other applications on the NetBeans Platform. In those scenarios, one often wants to have the user open a file as an XML document, even when the extension is not ".xml". You shouldn't expect the user to have to configure anything (or as little as possible) after installing your application. And, potentially, the application doesn't even contain an Options window, so they wouldn't even be able to do what you're suggesting.
  • Vadiraj Saturday, June 23, 2007
    Thanks Geertjan for this entry. I think somebody needs to update the Developer FAQ link that talks about it and it looks like outdated.
  • TGMG Saturday, June 23, 2007
    Is it possible to do a similar thing but for the java editor?
  • Laszlo Kishalmi Sunday, June 24, 2007
    Well, in module writing context you are right.
  • Geertjan Sunday, June 24, 2007
    TGMG, that is definitely possible. Can you give me a use case? Why would you want to do this?
  • TGMG Sunday, June 24, 2007
    It is for my netbeans game editor called G-Java. I want to be able to open .script files as .java files
    I would like to be able to use drag and drop for ".script" files. Aswell as a custom icon.
    But I don't know if this will effect the code completion in anyway, as if I type myscript. will it come up with the methods etc? Or does netbeans read the java file?
    Thank you.
  • Geertjan Sunday, June 24, 2007
    What do you want to gain from the Java editor? The coloring? Or anything else? If the coloring, does your script file consist of Java code? If yes, why isn't it a Java file? I'm sorry, currently I don't understand enough to be able to help. Maybe you can drop me an e-mail at geertjan DOT wielenga AT sun DOT com and we can talk about it. Send an example script file too.
  • dima Tuesday, August 19, 2008

    Can I use ValidateXMLSupport for validating xml string? I have JUnit test:


    String xml = "<?xml version=\\"1.0\\" encoding=\\"UTF-8\\" ?> " +


    InputSource saxIS = new InputSource(new StringReader(xml));

    CheckXMLSupport cxs = new CheckXMLSupport(saxIS);

    boolean res = cxs.checkXML(null);

    assertTrue(res); // it's work

    ValidateXMLSupport vxs = new ValidateXMLSupport(saxIS);

    boolean res2 = vxs.validateXML(null);

    assertTrue(res2); //res2 is always false

    xml - is valid xml string, but res2 is always false.

Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.Captcha