XML Models (DOM)
The Document Object Model (DOM) in the IntelliJ Platform is an API for working with DTD- or Schema-based XML models. It is intended for plugins that provide framework or server integrations, or a UI for editing XML files.
The reader is expected to be familiar with Java, the IntelliJ Platform XML PSI (classes XmlTag, XmlFile, XmlTagValue, etc.), and IntelliJ Platform plugin development basics (services, extension points).
XML PSI vs DOM
Working with XML through PSI usually means taking an XmlFile, getting its root tag, and then finding the required sub-tag by a path of tag names. Typing these string names everywhere is tedious and error-prone. Consider the following XML:
The goal is to read the content of the second <bar> element, namely "239".
Chained calls like the following are not correct, because each call may return null:
Code that handles all null values looks like this:
DOM simplifies this code. It only requires defining interfaces that extend DomElement, for example:
Next, bind the root tag name to the MyRoot interface with a file description. DomManager then provides the file element. The following code gets the "239" value:
DomManager.getFileElement() returns null if the file is not described by a file description with the given root interface. A model is often used in several places, and re-creating it each time would be inefficient. DOM caches the model, so subsequent calls to getFileElement() return the same instance. This makes it possible to obtain the root element once and reuse it.
Below the file element, the code is safe from NullPointerException: DOM guarantees that every method accessing a tag child returns a non-null element, even if the corresponding sub-tag doesn't exist. Based on the interfaces, DOM generates the code for accessing the correct sub-tags and creating model elements at runtime. Sub-tag names and element types are derived from method names, return types, and method annotations, if any. In most cases, annotations can be omitted, as in the example above.
Architecture Overview
Layers
DOM is a layer on top of the XML PSI:
XML file text: the file content, as edited by the user or modified by code.
XML PSI: the IntelliJ Platform parses the file text into a tree of PSI elements:
XmlFile,XmlTag,XmlAttribute, etc. The tree is updated incrementally when the text changes.DOM: DOM doesn't parse XML itself. It creates DOM elements on top of the PSI tree and implements the plugin's interfaces by reading and modifying the underlying PSI elements. As a result, setters modify the XML PSI and thus the file text, so they must be called in a write action, as for any PSI modification. Changes in the file text become visible in the model once the PSI is updated. DOM elements can exist even if the underlying PSI element doesn't, for example, for an attribute that isn't specified in the file.
Model definition: the plugin provides the DOM interfaces and a file description that binds them to XML files.
Plugin code and IDE features: the plugin works with typed DOM elements instead of XML PSI. The IDE uses the same model for error highlighting and resolving references in the XML editor.
Key Classes
The following diagram shows the key DOM classes:
The plugin defines
MyRootand the other model interfaces by extendingDomElement, and registersMyFileDescription, which binds the root tag name toMyRoot.DomManageris the entry point:getFileElement()returns theDomFileElementof anXmlFile, whosegetRootElement()returns the root DOM element.Dashed arrows lead to the underlying XML PSI elements: the file, a tag, or an attribute. They may be
nullif the corresponding tag or attribute doesn't exist in the file.
Attributes and tags containing only a value are represented by special DomElement inheritors, described in DOM Element Types.
DOM Topics
Defining a DOM Model describes how to register a DOM file and how to represent tag content, attributes, and sub-tags in DOM interfaces.
Working with DOM Elements describes the runtime API of DOM elements: PSI connection, validity, reflection, events, visitors, and models spanning several files.
DOM Highlighting and Resolving describes error highlighting and resolving references between DOM elements.
DOM-Based GUI describes the deprecated API for building Swing forms bound to DOM.
Sample Plugins
The following bundled open-source plugins make (heavy) use of DOM:
Explore third-party plugins using DOM on IntelliJ Platform Explorer.