Geertjan's Blog

  • August 17, 2005

Generate Your Own Code Completion in NetBeans IDE

Geertjan Wielenga
Product Manager
Something I discovered yesterday is how easy it is to add code completion functionality to any XML file. Say, for example, you work a lot with datasource configuration files for JBoss. It's really easy to set things up so that, while typing, a little drop-down such as the following appears to suggest tags to use:

To create that list, all you need is a DTD file. Code completion for XML files is based on the DTD file declared in the second line of the XML file. But, there's more: you can use the IDE to generate your DTD file, using the XML file as its basis. So, right-click inside the Source Editor while the XML file that contains the tags you need is open. Then choose Generate DTD. Once you've generated the DTD file, you need to add the DTD declaration to the top of any XML file that should use the DTD file. For example, here's my DTD declaration for Pointbase datasource configuration files in JBoss:

<!DOCTYPE datasources PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "pointbase-ds.dtd">

That's all I had to do -- then, thanks to the DTD file, code completion is available for any JBoss datasource configuration file that references my DTD file. (There are several open issues in this area. The one that tripped me up is that you need to manually save a file before generating the DTD. This is because the DTD is generated based on the saved version, and no autosave is currently carried out.) I've done the same thing for the Shakespeare files that I've been retrieving from the Shakespeare web service that I discussed in some recent blog entries:

By the way, something else I've discovered is how to read XML files. Previously, I'd written the retrieved Shakespeare string to an XML file. However, what if I don't want to display the whole retrieved string, but only part thereof -- for example, only the play or the speaker? My user interface would be something like this:

To implement this, I needed to read the returned XML string. I knew what was being returned, i.e., the tags were known to me, so based on that I identifed the tags and assigned their content to a string. So, this is what I added to the servlet to make it possible to identify the play and the speaker from the returned Shakespeare quotation (the code snippet below starts with the invocation on the getSpeech operation, which is made available by the web service):

String s = getShakespeareSoap().getSpeech(quotation);
String startPlayTag = "<PLAY>";
String endPlayTag = "</PLAY>";
int startPlay = s.indexOf(startPlayTag) + startPlayTag.length();
int endPlay = s.indexOf(endPlayTag);
String play = s.substring(startPlay, endPlay);
String startSpeakerTag = "<SPEAKER>";
String endSpeakerTag = "</SPEAKER>";
int startSpeaker = s.indexOf(startSpeakerTag) + startSpeakerTag.length();
int endSpeaker = s.indexOf(endSpeakerTag);
String speaker = s.substring(startSpeaker, endSpeaker);

Join the discussion

Comments ( 4 )
  • Kovi Wednesday, August 17, 2005
    You could also use JDOM (www.jdom.org) to get a proper XML document from that String and play with that Document. :)
  • Geertjan Thursday, August 18, 2005
    Thanks for the tip! I'm going to look into it. But can you explain what the advantage of that approach is? (I can already see one disadvantage -- using an external library. My current solution doesn't need anything special to be added to the classpath.)
  • Vivek Tuesday, October 3, 2006
    Hi Geertjan,
    can we refer "XML Schema" file instead of "DTD" file for XML code completion in Netbeans IDE? and how?
    I have a schema file which will be referred by some xml files. How can I register that schema file to XML Schema catalogs without having the DTD file.
    Thanks in Advance,
  • Geertjan Tuesday, October 3, 2006
    I'm pretty sure that isn't possible, but I could be wrong -- please write to nbusers@netbeans.org and ask.
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.