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"?> <sample xmlns="http://www.eppleton.de/schemas/sample" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.eppleton.de/schemas/sample http://www.eppleton.de/schemas/sample.xsd"> </sample>
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); getCookieSet().add(checkCookie); ValidateXMLCookie validateXMLCookie = new ValidateXMLSupport(inputSource); getCookieSet().add(validateXMLCookie);
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"?> <!DOCTYPE filesystem PUBLIC "-//NetBeans//DTD Filesystem 1.2//EN" "http://www.netbeans.org/dtds/filesystem-1_2.dtd"> <filesystem> <folder name="Navigator"> <folder name="Panels"> <folder name="text"> <folder name="x-box2d+xml"> <file name="org-netbeans-modules-xml-text-navigator-XMLNavigatorPanel.instance"/> </folder> </folder> </folder> </folder> </filesystem>
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. --> <xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.eppleton.de/schemas/sample" elementFormDefault="qualified"> <xs:element name="sample" type="xs:string"/> </xs:schema>
9. Now we’ll preregister it in NetBeans. Create a “New…” -> “XML” -> “XML Document” with name “UserXMLCatalog” and add this:
<!DOCTYPE catalog PUBLIC "-//OASIS//DTD Entity Resolution XML Catalog V1.0//EN" "http://www.oasis-open.org/committees/entity/release/1.0/catalog.dtd"> <catalog xmlns="urn:oasis:names:tc:entity:xmlns:xml:catalog"> <system systemId="http://www.eppleton.de/schemas/sample.xsd" uri="nbresloc:de/eppleton/demo/xmleditor/sample.xsd"/> </catalog>
10. And register this Catalog File in the Layer.xml:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE filesystem PUBLIC "-//NetBeans//DTD Filesystem 1.2//EN" "http://www.netbeans.org/dtds/filesystem-1_2.dtd"> <filesystem> <folder name="xml"> <folder name="catalogs"> <file name="UserXMLCatalog.xml" url="UserXMLCatalog.xml"/> </folder> </folder> <folder name="Navigator"> <folder name="Panels"> <folder name="text"> <folder name="x-box2d+xml"> <file name="org-netbeans-modules-xml-text-navigator-XMLNavigatorPanel.instance"/> </folder> </folder> </folder> </folder> </filesystem>
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: https://github.com/eppleton/netbeans-examples Enjoy!