Archive

Archive for the ‘XML’ Category

Writing an Eclipse Plug-in (Part 9): Custom Project: Defining a Custom File Type

October 25, 2009 Leave a comment

So things have been going swimmingly for the last 8 parts. Time to return to our roots: the original project.

But first: time to floss. Open customplugin MANIFEST.MF and click on the light bulb to fix the warning. It should now display:

MANIFEST.MF
...
Export-Package: customplugin,
 customplugin.natures,
 customplugin.perspectives,
 customplugin.projects,
 customplugin.wizards

Also, return to the Overview tab and click on the Externalize Strings Wizard. That should take care of externalizing 4 strings from the MANIFEST.MF and plugin.xml files. Click Finish.

There. My teeth feel so much better.

Okay, now on to the serious stuff.

The custom project will use 3 files types (for now):

  1. Schema definition
  2. Deployment descriptor
  3. Java source code

The two descriptor files will be XML files. They are what we need to define.

The current tasks are to:

  • Define a custom project file type for the Schema Definition.
  • Define a custom project file type for the Deployment Definition.

There are two things we need to know before defining the file type:

  • how can we determine the type of a file?
  • can we leverage existing Eclipse functionality?

The butler did it. Metaphorically anyway. There is no need to wait until the end of the story to be told what the answer is: Eclipse can associate the content type of an XML file by looking inside it and reading the root element.

By configuring the content type extension with a base-type of org.eclipse.core.runtime.xml and selecting a parser of type org.eclipse.core.runtime.content.XMLRootElementContentDescriber2 we can create an extensive list of content types that are all XML files, but have different file formats. Very convenient. Very cool. That’s two verys.

Define a Custom Project File Type for the Schema Definition.

First:

  1. Open customplugin –> plugin.xml –> Extensions –> Add –> contenttypes
  2. Select org.eclipse.core.contenttype.contentTypes
  3. Click Finish
  4. Enter:
    • ID: customplugin.contenttype
  5. Select org.eclipse.core.contenttype.contentTypes –> new –> content-type
  6. Enter:
    • id: customplugin.contenttype.schema
    • name: Hidden Clause Schema Definition
    • base-type: org.eclipse.core.runtime.xml
    • file-extensions: xml
    • priority: normal
  7. Select Schema Definition –> new –> describer
  8. Enter:
    • class: org.eclipse.core.runtime.content.XMLRootElementContentDescriber2
  9. Select org.eclipse.core.runtime.content.XMLRootElementContentDescriber2 –> new –> parameter
  10. Enter:
    • name: element
    • value: hc-schema

Start the runtime workbench and open Window –> Preferences –> General –> Content Types –> Text –> XML. Hidden Clause Schema Definition should be listed as an XML content type.

customplugin-part-9-preferences-with-new-contenttype

From the Resource Perspective create a project, custom or otherwise. Create a file and name it special-schema.xml. Enter the following into the file:

<hc-schema>
  <tables>
  </tables>
</hc-schema>

Right click on the file and select Properties –> Resource. The file should be listed as Hidden Clause Schema Definition (the name we gave it above).

customplugin-part-9-properties-file-hc-schema-def

Define a Custom Project File Type for the Deployment Definition.

Time for the second file type:

  1. Select org.eclipse.core.contenttype.contentTypes –> new –> content-type
  2. Enter:
    • id: customplugin.contenttype.deployment
    • name: Hidden Clause Deployment Definition
    • base-type: org.eclipse.core.runtime.xml
    • file-extensions: xml
    • priority: normal
  3. Select Schema Definition –> new –> describer
  4. Enter:
    • class: org.eclipse.core.runtime.content.XMLRootElementContentDescriber2
  5. Select org.eclipse.core.runtime.content.XMLRootElementContentDescriber2 –> new –> parameter
  6. Enter:
    • name: element
    • value: hc-deployment

Start the runtime workbench and open Window –> Preferences –> General –> Content Types –> Text –> XML. Hidden Clause Deployment Definition should be listed as an XML content type.

customplugin-part-9-preferences-with-contenttype-deployment

From the Resource Perspective, using the same project as before create another file and name it special-dep.xml. Enter the following into the file:

<hc-deployment>
  <tables>
  </tables>
</hc-deployment>

Right click on the file and select Properties –> Resource. The file should be listed as Hidden Clause Deployment Definition.

customplugin-part-9-properties-file-hc-deploy-def

Take a bow.

Cleaning Up

Return to the Overview tab of plugin.xml and click on the Externalize Strings Wizard. Oh, look: the two custom file type names are there.

  • Assign the key content-type.name.schema to Hidden Clause Schema Definition
  • Assign the key content-type.name.deployment to Hidden Clause Deployment Definition
  • Click Finish

I think we might have missed a spot behind the ears, but they feel dry so I think we are okay.

What Just Happened?

Well, adding two new content types was pretty straightforward. Some would say that we should have done this directly in XML. Others think we should learn latin in school. Others think we should go back to the oceans.

Personally, I prefer to use editors to control configuration files; it keeps me from doing something complicated like…forgetting a closing brace, or misspelling an element name.

References

The Eclipse help files were somewhat useful, but not as useful as just trying it out. This is one of the few times where stumbling around in the room is actually useful.

Of course the Eclipse Tip: Define Custom Content Types to Identify Your Data Files was rather helpful in pointing out the use of org.eclipse.core.runtime.content.XMLRootElementContentDescriber2 as an existing content type XML parser that also supports namespaces. Very cool. Very useful.

The cat is alive.

Help! I Can’t Find An Executable Version of XML Copy Editor for 64-bit Kubuntu!

September 18, 2009 5 comments

[I hate when things change; like when Dr. Who died and became someone else. Very disturbing. Anyway, the links to XML Copy Editor listed below don’t work as the maintainer of the getdeb site is no longer updating software for anything prior to Ubuntu 9.04. The getdeb XML Copy Editor drop was part of the Jaunty Jackolope distro which I guess was before 9.04. Any software for prior versions is still available on the getdeb archive site at http://old.getdeb.net/ and the 64-bit version of XML Copy Editor can be found at http://old.getdeb.net/release/4263. Sorry for any confusion this might have caused even though I didn’t cause it and the maintainer of getdeb didn’t mean to. So get over it. Download XML Copy Editor and be done with it.]

[Another update: 3/12/10: Going to http://old.getdeb.net/ does not appear to work. Apparently they are working on the legacy side of their site. Be patient.]

And now for something completely different. By different I mean short.

If you have been looking for a great XML editor I would recommend XML Copy Editor. The original goal of this post was to show you how to download the code and all of the dependencies needed to run XML Copy Editor. The last time I downloaded XML Copy Editor it did not come in a ready-to-run form for Kubuntu. I had to download all sorts of dependencies and cut the heads off of a few chickens to get it to work, but work it did and I was happy.

After installing Kubuntu again I found that I needed to revisit the installation of XML Copy Editor. Wouldn’t you know? I forgot to take notes on how I did it. And it was not trivial the last time I did it.

As it turns out there is a web site that contains ready-to-run, pre-baked versions of all sorts of program including XML Copy Editor. The site is getdeb and I also recommend it as a place to find software you might have thought was unavailable on Ubuntu.

I happen to run Kubuntu 64-bit so a search for XML Copy Editor returned XML Copy Editor for Ubuntu Jaunty 32 bit. Scrolling down the page revealed a version available for Ubuntu Jaunty 64 bit which exactly matched my need.

The cat was alive.

These are a few of my favorite things

XML Copy Editor does a number of things rather well:

  • Checks well-formedness
  • Validates the XML using DTDs, XSDs, RELAX NG and others
  • Applies XSL transforms so you can test what it is you are transforming
  • Executes arbitrary XPath on the current file
  • Supports the creation of 26 XML-related file types

Do I have to mention that it also uses colored syntax, element folding and a built in web browser?

XML Copy Editor: download it from getdeb. Install it. Use it.