Angad has posted 2 posts at DZone. View Full User Profile

Internship Project: Schema Aware XML Hyperlinking

06.05.2008
| 8457 views |
  • submit to reddit

Varun Nischal and I are working on schema-aware hyperlinking support for XML (both for xsd and dtd's) in NetBeans IDE as part of our internship project at Sun Microsystems, Bangalore, under the mentorship of Rohan Ranade.

The goal of this effort is to make hyperlinking of XML tags possible in the XML Editor, in NetBeans. Implementation would take place in two phases/milestones. In it's first phase, we aim to implement hyperlinking within a DTD and DTD-constrained XML, and then this effort aims to achieve hyperlinking for XML tags, that conforms to the constraints defined by XML Schema.

As we all know, XML has a collection of tags, in a well defined structure which conforms to a particular Schema Language, i.e. DTD or XML Schema. There are few more types that exists. However, we shall focus on these two types.

An XML instance document that can conform to following schema languages;
  1. DTD
  2. XML Schema
  3. W3C XML Schema
  4. Relax NG
  5. ISO Schematron
  6. ISO NVDL

 

XML Schema

It must mention about the schema with the help of schemaLocation or noNamespaceSchemaLocation attribute at the root element of the document. The schemaLocation and noNamespaceSchemaLocation attributes can be used in a document to provide hints to find the physical location of schema documents which may be used for assessment and validation.

The value of schemaLocation consists of one or more pairs of URI references, separated by white space. The first member of each pair is a namespace name, and the second member of the pair is a hint describing where to find an appropriate schema document for that namespace. Where as, the value of noNamespaceSchemaLocation consists of one or more schemas separated by white space. Typically, schemaLocation is used for schemas with a target namespace and noNamespaceSchemaLocation is used for schemas with no target namespace.

Please refer the following Use Case 1 (element to XSD definition hyperlinking) AND/OR Schema Aware Code Completion, for more details.

Document Type Definition

If an XML instance conforms to a document-type-definition (DTD), then, the System URI would help locate the file stored somewhere. Its mentioned in the instance document inside at the root - <![DOCTYPE element_name Public "Namespace Name" "System URI"] />

The value of DOCTYPE tag consists of URI references, separated by white space. The first member of the pair is a namespace name, and the second member of the pair is a hint describing where to find an appropriate DTD document for that namespace.

Please refer following Use Cases, for more details.

Use Cases

Lets take an example, <element_name attr_name=value /> - this is a proper XML tag, where element_name and attr_name are defined in the schema language. Following are the various possible broadly classified cases;

  1. If user clicks on element_name, he will be taken to its respective schema/DTD definition within the same or different local file or even a web document. The schema definition may be located anywhere in the inheritance hierarchy of the schema's following the one included in that file.
  2. If user clicks on attr_name, then user is taken to the respective attribute's schema/DTD definition with same or web xml document (DTD or XSD).
  3. If the user clicks on a schema file's URL, that schema file will be loaded even if a web document.


Use Cases of DTD hyperlinking


Use case 1 (element to it's DTD definition):
Let's look at a simple example,

<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>
....

The above is part of the web.xml configuration file of a regular Spring Framework project.

Clicking on <web-app> will take the user to:

<!ELEMENT web-app (icon?, display-name?, description?, distributable?,
context-param*, filter*, filter-mapping*, listener*, servlet*,
servlet-mapping*, session-config?, mime-mapping*, welcome-file-list?,
error-page*, taglib*, resource-env-ref*, resource-ref*, security-constraint*,
login-config?, security-role*, env-entry*, ejb-ref*, ejb-local-ref*)>

in web-app_2_3.dtd as referenced by web.xml.

Use Case 2 (sub-element to it's definition within same DTD):

<!ELEMENT web-app (icon?, display-name?, description?, distributable?,
context-param*, filter*, filter-mapping*, listener*, servlet*,
servlet-mapping*, session-config?, mime-mapping*, welcome-file-list?,
error-page*, taglib*, resource-env-ref*, resource-ref*, security-constraint*,
login-config?, security-role*, env-entry*, ejb-ref*, ejb-local-ref*)>

Clicking on "icon" takes the user to:

<!ELEMENT icon (small-icon?, large-icon?)>

Use Case 3 (opening external DTD declaration document):

Clicking on http://java.sun.com/dtd/web-app_2_3.dtd takes you to the respective file (it downloads the file in the background, then opens it in another tab):

<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">

 

Use Case of XSD (XML Schema Definition) Hyperlinking


Use Case 1 (element to XSD definition hyperlinking) -

In the following code from beans.xml,

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

When the user clicks any of the import element tags, he is taken to:

<xsd:element name="import">
<xsd:annotation>
<xsd:documentation source="java:org.springframework.core.io.Resource"><![CDATA[
Specifies an XML bean definition resource to import.
]]></xsd:documentation>
</xsd:annotation>
<xsd:complexType>
<xsd:complexContent>
<xsd:restriction base="xsd:anyType">
<xsd:attribute name="resource" type="xsd:string" use="required">
<xsd:annotation>
<xsd:documentation><![CDATA[
The relative resource location of the XML (bean definition) file to import,
for example "myImport.xml" or "includes/myImport.xml" or "../myImport.xml".
]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
</xsd:restriction>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>

in spring-beans-2.0.xsd

Use Case 2 (sub-element to it's definition within same XSD) -

In the following code definition of the beans element:

<xsd:element name="beans">
<xsd:annotation>
<xsd:documentation><![CDATA[
The top level (typically root) element. Allows the definition
of default values for all nested bean definitions.
]]></xsd:documentation>
</xsd:annotation>
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="description" minOccurs="0"/>

On clicking the sub-element "description", the user is taken to it's definition within the same XSD:

<xsd:element name="description">
<xsd:annotation>
<xsd:documentation><![CDATA[
Contains informative text describing the purpose of the enclosing
element.
Used primarily for user documentation of XML bean definition documents.
]]></xsd:documentation>
</xsd:annotation>
<xsd:complexType mixed="true">
<xsd:choice minOccurs="0" maxOccurs="unbounded"/>
</xsd:complexType>
</xsd:element>


Use Case 3 (attribute to its definition within same XSD) -

In the following example from XMLSchema.xsd:

<xs:complexType name="topLevelComplexType">
<xs:complexContent>
<xs:restriction base="xs:complexType">
<xs:sequence>
<xs:element ref="xs:annotation" minOccurs="0"/>
<xs:group ref="xs:complexTypeModel"/>
</xs:sequence>
<xs:attribute name="name" type="xs:NCName" use="required"/>
<xs:anyAttribute namespace="##other" processContents="lax"/>
</xs:restriction>
</xs:complexContent>
</xs:complexType>

When the user clicks on the "base" attribute for example, he is taken to it's definition:

<xs:attribute name="base" type="xs:QName" use="required"/>

Further, if we click on "name", we get to:

<xs:attribute name="name" type="xs:NCName"/>

If we click on "type", we get to:

<xs:attribute name="type" type="xs:QName"/>

..and so on

Use Case 4 (opening external XSD decalaration document) -

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

<xsd:schema xmlns="http://www.springframework.org/schema/beans"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.springframework.org/schema/beans">

<xsd:import namespace="http://www.w3.org/XML/1998/namespace"/>

In the above code, if we click on http://www.w3.org/XML/1998/namespace, we get to xml.xsd.

 

For Phase-1, we our working on implementing hyperlinking of subelement of an element in DTD to it's definition within the same DTD. (use case 2 under DTD use cases).

An email sent to dev@openide.netbeans.org yesterday:


We are working on schema aware hyperlinking support for XML (both for xsd and dtd's) in NetBeans.

For the first phase, I am working on implementing hyperlinking of subelement of an element in DTD to it's definition within the same DTD.

For example in a DTD file /http://java.sun.com/dtd/web-app_2_3.dtd:/

..
/<!ELEMENT web-app (icon?, display-name?, description?, distributable?,
context-param*, filter*, filter-mapping*, listener*, servlet*,
servlet-mapping*, session-config?, mime-mapping*, welcome-file-list?,
error-page*, taglib*, resource-env-ref*, resource-ref*, security-constraint*,
login-config?, security-role*, env-entry*, ejb-ref*, ejb-local-ref*)>
..

/Clicking on /"icon"/ takes the user to:/

<!ELEMENT icon (small-icon?, large-icon?)>

/..within the same file.

For implementing this, at first we thought we would use the Lexer API to define our own language definition for DTD's, but apparently, NetBeans provides a DTD editor with lot of features like syntax highlighting DTD documents, "Check DTD", "Generate CSS", and even generate DOM tree scanner support.

This basically indicates that there is already some lexical support for DTD's in netbeans.

On some hacking around, we found out that the API we need resides at:

/org.netbeans.modules.xml.text.syntax.DTDKit

/and we retrieved tokens like so:
/
BaseDocument doc = (BaseDocument) document;
DTDKit kit = new DTDKit();
ExtSyntaxSupport syntaxSupport = (ExtSyntaxSupport) kit.createSyntaxSupport(doc);

TokenItem tok = syntaxSupport.getTokenChain(offset, Math.min(offset + 1, document.getLength())); /
/

/It's not certain that this is the right way to do this, as we are creating our own SyntaxSupport.

What is the proper way to use the DTD support in netbeans for solving our purpose?

Regards,
Angad Singh

 

References -
[1] http://www.netbeans.org/features/soa/wsdl-xml.html
[2] http://www.netbeans.org/features/all.html#soa
[3] http://en.wikipedia.org/wiki/XML_schema
[4] http://hg.netbeans.org/main/rev/587eedb4328

Schema Aware Code Completion -
[1] http://wiki.netbeans.org/SchemaAwareCodeCompletion
[2] http://blogs.sun.com/gridbag/entry/a_smarter_xml_completion_in
[3] http://blogs.sun.com/gridbag/entry/a_smarter_xml_completion_in1

Most Important -
[1] http://www.netbeans.org/kb/60/soa/xmltools-start.html
[2] http://xml.netbeans.org/
[3] http://platform.netbeans.org/tutorials/60/nbm-xmleditor.html
[4] http://wiki.netbeans.org/DevFaqModulesLayerFile
[5] Geertjan's Blog - http://blogs.sun.com/geertjan/entry/sun_tech_days_day_1

Published at DZone with permission of its author, Angad Singh.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Frank Marsh replied on Sat, 2009/08/08 - 2:25am

hyperlinking of XML tags possible in the XML Editor, will definitely be a great achievement and will help a lot.

teeth whitening products

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.