Defining a DOM Model
A DOM model consists of interfaces extending DomElement, one for each kind of XML tag, and a file description that binds the root interface to XML files. This page describes how to register the file description and how to declare tag content, attributes, and sub-tags in the interfaces. For an overview of how the model relates to DOM and XML PSI classes, see the key classes diagram.
File Description
To register DOM files, extend DomFileDescription, passing the root element interface and the root tag name to its constructor. Register it in plugin.xml using com.intellij.dom.fileMetaData extension point with the following attributes:
implementation- theDomFileDescriptioninheritor.rootTagName- the unqualified root tag name. Omit it if the set of root tag names is not known in advance; then the IDE callsDomFileDescription.isMyFile()to determine whether the description applies to a file.domVersion- increase it whenever the logic determining which files the description applies to changes.stubVersion- specify it if the model uses stubs, and increase it whenever the stub structure changes.
To restrict the description to some of the files with the given root tag name, for example, by namespace, override isMyFile(XmlFile).
Override the following DomFileDescription methods to configure the model:
initializeFileDescription()- register namespace policies and type choosers.createAnnotator()- provide highlighting annotations.getIdentityScope()- define the scope in which element names must be unique.getResolveScope()- define the scope in which references are resolved.getFileIcon()- provide an icon for the files.
DOM Element Types
Getters in DOM interfaces return DOM elements representing tags and attributes. The following diagram shows the element types and how they relate:
A tag with attributes or sub-tags is represented by a plugin interface extending
DomElement, likeFooandBar. Its getters return the DOM elements of its sub-tags, either as a single element or as a list.A tag containing only a value is represented by
GenericDomValue<T>, so the plugin doesn't need to define an interface for it.An attribute is represented by
GenericAttributeValue<T>, which extendsGenericDomValue<T>with access to the underlying XML attribute.Both convert values between XML text and
Tusing aConverter<T>.
Tag Content
XML PSI refers to tag content as tag value, and this page uses the same term. To read and change a tag value, add a getter and a setter to the interface:
getValue() and setValue() are the default names of tag value accessors. To use custom method names, annotate the methods with @TagValue:
These accessors work with String values, as XML tag content is always text. A model can also operate with integers, booleans, enums, class names (represented as PsiClass), or Java types (PsiType). In such cases, change the accessor types to the required ones.
Custom Value Types
For other types, annotate the accessor methods with @Convert and specify a class extending Converter<T>, where T is the custom type. A converter converts values between String and T. If a value can't be converted (for example, "foo" to Integer), the converter can return null. The converter class must have a no-argument constructor.
For enum types, the default converter searches for enum constants with the names specified in XML. Sometimes, XML values are not valid Java identifiers. For example, the CMP version in EJB can be "1.x" or "2.x", which can't be used as enum constant names. In such cases, let the enum implement NamedEnum and return the value matching the XML content from getValue():
Value-Only Tags
Besides a value, an XML tag can have attributes and sub-tags. However, many tags (according to DTD or Schema) contain only a value. DOM represents such tags with GenericDomValue<T>, which provides the following methods:
getValue()andsetValue()- get and set the tag value converted toT.getStringValue()andsetStringValue()- get and set the tag value as aString, without conversion toT.getRawText()- returns the tag value text exactly as specified in XML, without any conversions or substitutions.getConverter()- returns theConverterused to convert values betweenStringandT.
Specify a particular T when using this interface. The String accessors are useful, for example, when T is PsiClass, and the UI needs to highlight invalid values: getStringValue() returns the string from the XML file even if it can't be resolved to a class, and the converter provides the error message via getErrorMessage().
Attributes
Attribute values can be read and set, and they can have different types. DOM represents attributes with GenericAttributeValue<T>, which extends GenericDomValue<T> and additionally provides access to the underlying XML PSI:
getXmlAttribute()- returns the underlyingXmlAttribute.getXmlAttributeValue()- returns the underlyingXmlAttributeValue.
For example, an attribute named some-class with a value of type PsiClass is declared with the @Attribute annotation as follows:
The returned value supports getting and setting the value, resolving the PsiClass, getting its String representation, etc. If the method name matches the attribute name according to the name strategy, the annotation can be omitted:
If the attribute type is not PsiClass, but a custom T requiring a converter, annotate the getter with @Convert.
The attribute getter never returns null, even if the attribute isn't specified in XML. In this case, its getValue(), getStringValue(), and getXmlAttribute() methods return null, but the DOM element exists and is valid. To remove an existing underlying attribute, call DomElement.undefine(): the XML attribute is deleted, while the GenericAttributeValue remains valid.
Name Strategy
DomNameStrategy specifies how to convert accessor names without the get, set, or is prefixes to XML element names. Specify the strategy class in the @NameStrategy annotation on a DOM element interface. All descendants and children of this interface use this strategy. The default strategy is HyphenNameStrategy, which delimits words with hyphens, as in the sample above. Another common strategy is JavaNameStrategy, which follows the Java naming convention. With it, the attribute name in the example would be someClass.
Children: Fixed Number
Tags often have at most one sub-tag with a given name, for example, <ejb-name>, <ejb-class>, or <cmp-field> in tags defining entity EJBs. To access such children, declare getters with a return type extending DomElement:
To designate such children explicitly, use the @SubTag annotation. Its value attribute specifies the tag name. If it is not specified, the name is derived from the method name using the current name strategy.
For some sub-tags, their presence matters rather than their content, for example, <unchecked> in EJB method permissions: if the sub-tag exists, permissions are unchecked; otherwise, they are checked. Represent such sub-tags with a GenericDomValue<Boolean> child. By default, its getValue() returns true if the tag value is "true", false if it is "false", and null otherwise. To change this, specify indicator = true in the @SubTag annotation. Then getValue() returns true if the tag exists, and false otherwise.
Some tags contain a fixed number (greater than one) of sub-tags with the same name. For example, an EJB relation has two roles, each represented by an <ejb-relationship-role> tag. Representing them as a collection would require checking the number of elements on every access. Instead, DOM supports declaring a separate getter for each sub-tag, with its index:
The first method returns the DOM element for the first <ejb-relationship-role> sub-tag, and the second method returns the element for the second one. Such children are called fixed-number children, as DTD or Schema defines a fixed number of sub-tags with the given name. Most often, this number is 1; for EJB relations, it is 2. Like attributes, fixed-number children exist regardless of whether the underlying tag exists. To delete their tags, call undefine().
Converters can be specified for GenericDomValue children in the same way as for attributes.
Children: Collections
DTDs and Schemas often define children with the same tag name and no fixed upper limit of their number. Accessors for such children return a Collection or List of a type extending DomElement. To derive the tag name using the name strategy, use the plural form in the method name, for example:
To specify the tag name explicitly, use the @SubTagList annotation.
Returned collections can't be modified directly. To delete an element from a collection, call undefine() on it. This removes the tag and makes the element invalid (DomElement.isValid() returns false). This behavior differs from fixed-number children and attributes, which remain valid after undefine(). Also, unlike them, collection children always have valid underlying XML tags.
DOM creates all elements internally, so it is not possible to add an existing DOM element to a collection. Instead, declare methods in the parent element that add a child to the collection:
Note the singular form of "Entity" in the method names, as these methods add a single element. The returned element can then be modified: define its value, children, etc.
Mixed Collections
Another common case is a collection of tags with different names mixed in arbitrary order. To work with it, define collection getters for all tag names in the mixed collection and an additional getter annotated with @SubTagsList:
The annotation is mandatory, as DOM can't derive several tag names from one method name.
To add elements to a mixed collection, declare an "add" method for each possible tag name:
The index parameter in the last example is the index in the merged collection, not in the collection of <bar> tags.
Mapping Summary
The following table summarizes how XML constructs are declared in DOM interfaces and how the resulting DOM elements behave.
XML construct | Declaration example | Annotation, if the name can't be derived | Element exists without XML | After |
|---|---|---|---|---|
|
| - | - | |
|
| Yes | Stays valid | |
|
| Yes | Stays valid | |
|
| Yes | Stays valid | |
|
| Yes | Stays valid | |
|
| No | Becomes invalid | |
|
| No | Becomes invalid |
Required Children
A common error case is a missing required sub-tag or attribute. To mark a child as required, annotate its getter with @Required. For collection getters, this annotation means that the collection must not be empty (corresponding to + in DTD). Creating a new element with required fixed-number or attribute children also creates their tags or attributes in XML. Missing required children are highlighted automatically.
Type Choosers
A collection can contain same-named tags that have different structures or even different types in the DTD or Schema. For example, JSF managed beans can be of three types, all extending ManagedBean:
MapEntriesBean, if the<managed-bean>tag contains a<map-entries>sub-tagListEntriesBean, if it contains a<list-entries>sub-tagPropertyBeanotherwise
Each element of the list returned by List<ManagedBean> getManagedBeans() should implement the specific interface: MapEntriesBean, ListEntriesBean, or PropertyBean.
To choose the interface that a DOM element implements depending on its tag, extend TypeChooser and implement its methods:
chooseType()chooses the type (most often a class) for the given tag.distinguishTag()does the reverse: it modifies the tag so that the DOM element created from it later (for example, after the user reopens the project) implements the same interface, and no model data is lost.getChooserTypes()returns all types thatchooseType()can return.
To register the TypeChooser, call registerTypeChooser() in the overridden DomFileDescription.initializeFileDescription() method.
Namespace Support
Annotate the DOM model with @Namespace and register the namespace key mapping via DomFileDescription.registerNamespacePolicy() in DomFileDescription.initializeFileDescription().
Dynamic Definition
To extend existing DOM models at runtime, implement DomExtender<T>. Register it in the extenderClass attribute of com.intellij.dom.extender extension point, where domClass specifies the extended DOM class T. DomExtensionsRegistrar provides methods to register dynamic attributes and children.
If the contributed elements depend on anything other than plain XML file content (framework version, libraries in the classpath, etc.), return false from DomExtender.supportsStubs().
Implementation
A model sometimes needs functionality that isn't directly connected to XML, but relates to the program logic. The DOM element interface is the most appropriate place for such functionality.
In the simplest case, a method returns the same value as another getter in this element or in one of its children. Declare such a method and annotate it with @PropertyAccessor, specifying the path of property names (getter names without the get or is prefixes):
In this case, the second method returns the same value as the first one. With the "foo.bar.name" path in the annotation, DOM would call getFoo().getBar().getName() and return the result. Such annotations are useful when extending an interface that is inconsistent with the model, or when extracting a common super-interface from two model interfaces with differently named children that have the same meaning (for example, <ejb-ref> and <ejb-local-ref>).
More often, the model requires custom logic implemented in code. Add the required methods to the interface, then create an abstract class implementing the interface, and implement only the added methods that are not directly connected to the XML model. The class must have a no-argument constructor.
Register the class using com.intellij.dom.implementation extension point. DOM then generates at runtime a class that implements the interface and extends the abstract class.
DOM Stubs
DOM elements can be stubbed, so that costly access to XML PSI is not necessary (see Indexing and PSI Stubs for a similar feature for custom languages). To stub performance-relevant elements, annotate their tag or attribute getters with @Stubbed. Set the stubVersion attribute of the file description registration, and increase it whenever the usage of @Stubbed annotations changes in the DOM hierarchy to trigger rebuilding stubs during indexing.
IDE Support
Plugin DevKit supports the following features for working with DOM-related code:
DomElement- provide implicit usages for all DOM-related methods defined in inheriting classes (to suppress the "unused method" warning)DomElementVisitor- provide implicit usages for all DOM-related visitor methods defined in inheriting classes (to suppress the "unused method" warning)