Code Completion & Validation for your custom XML Editor

Update: If you prefer to read this in spanish, here’s the spanish translation of this article. It has been kindly provided by Maria Ramos.

I’ve just added XML Serialization to my JBox2D-Editor. It’s really very simple to do that using JAXB. But what was even nicer, is that I can create an XML-Editor for my format in NetBeans with Syntax Highlighting, Code Completion, Syntax Checking, Navigator and Validation with almost no coding required. Here’s how it looks:

And his is how it works:

1. Create a “New Project” -> “NetBeans Modules” -> “Module Suite” with a nice name, like e.g. “Sample XML Editor”. Navigate to the “Modules” tab, right click and “Add new…”. Choose a nice name and base package, e.g. “Sampe XML Support” and “de.eppleton.demos.xmleditor”.

2. In your Module Project launch the “New …” -> “Module Development” -> “File Type” – Wizard. As a format choose “text/x-sample+xml” and make “.smpl” the extension. Obviously you can replace “.smpl” and sample with your own strings.

3. In the next step enter “Sample” as the prefix and decide if you want to use multiview. It doesn’t matter for this micro-tutorial. A template file and SampleDataObject will be created. Open the template file and add this:

<!--?xml version="1.0" encoding="UTF-8" standalone="yes"?-->

4. Back in your project, in the constructor of SampleDataObject add this to enable validation and SyntaxChecking. (You also need to set a Dependency on XML Tools API):

InputSource inputSource = DataObjectAdapters.inputSource(this);
CheckXMLCookie checkCookie = new CheckXMLSupport(inputSource);
ValidateXMLCookie validateXMLCookie = new ValidateXMLSupport(inputSource);

5. You can now launch your project and create your first .smpl file the editor will have “Check XML” and “Validate XML” in it’s context menu. Validate works, but tells you the document is invalid, since it can’t find the schema, which is correct. Syntax Highlighting? Check! Syntax Checking? Check! Validation? Check!

6. Now create a “New…” -> “XML Layer” and add this to it:

<!--?xml version="1.0" encoding="UTF-8"?-->

7. Now launch the application again and you’ll see the Navigator Panel is working. Navigator? Check!

8. Now let’s create a “New…” -> “XML” -> “XML Schema (empty)” called “Sample” with this content:

<!--?xml version="1.0"?-->
<!-- To change this template, choose Tools | Templates and open the template in the editor. -->

9. Now we’ll preregister it in NetBeans. Create a “New…” -> “XML” -> “XML Document” with name “UserXMLCatalog” and add this:

10. And register this Catalog File in the Layer.xml:

<!--?xml version="1.0" encoding="UTF-8"?-->

That’s it. A full blown XML Editor for your custom XML Format that also validates your file against your beautiful new Schema.
The complete project is available on Github together with other NetBeans examples: Enjoy!

Hinterlasse eine Antwort

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind markiert *

Du kannst folgende HTML-Tags benutzen: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>