Adding DOM Elements to XML file

My objective is to programatically add dom elements to a web.xml file, like servlets, servlet-mappings and filters.

First using intellij idea dev-kit plugin I have generated to dom model for web.xml 2.4 xsd file

Then I wrote following peice of code to get hold of web.xml file inside the project directory. And try to add new servlet dom element to it.

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    
@Override
    public void
actionPerformed(AnActionEvent anActionEvent) {
        Project project = anActionEvent.getProject();
        PsiFile[] filesByName = FilenameIndex.getFilesByName(anActionEvent.getProject(), "web.xml", GlobalSearchScopesCore.directoryScope(anActionEvent.getProject(), anActionEvent.getProject().getBaseDir(), true));
        XmlFile xmlFile = (XmlFile) filesByName[0];

        DomManagerImpl domManager = (DomManagerImpl) DomManager.getDomManager(anActionEvent.getProject());
        domManager.registerFileDescription(new DomFileDescription(WebApp.class, "web-app"));

        DomElement rootElement = domManager.getFileElement(xmlFile).getRootElement();

        if
((rootElement instanceof WebApp)) {
            final WebApp webapp = (WebApp) rootElement;


            ApplicationManager.getApplication().runWriteAction(new Runnable() {
                @Override
                public void
run() {
                    Servlet servlet = webapp.addServlet();
                    servlet.getServletName().setStringValue("ussdReceiver");
                    servlet.getServletClass().setStringValue("UssdMoReceiver");

                }
            });


            JOptionPane.showMessageDialog(null, "Hello world - " + webapp.getServletMappings().get(0).getUrlPattern().getRawText());
        }


    }

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


How ever when I run the event which trigger this action. I get following exception.

java.lang.NullPointerException
 at com.intellij.psi.impl.source.tree.ChangeUtil.decodeInformation(ChangeUtil.java:91)
 at com.intellij.psi.impl.source.tree.ChangeUtil.decodeInformation(ChangeUtil.java:83)
 at com.intellij.psi.impl.source.tree.CompositePsiElement.addAfter(CompositePsiElement.java:162)
 at com.intellij.util.xml.impl.DomInvocationHandler.addEmptyTag(DomInvocationHandler.java:828)
 at com.intellij.util.xml.impl.DomInvocationHandler.addCollectionChild(DomInvocationHandler.java:795)
 at com.intellij.util.xml.impl.AddChildInvocation.invoke(AddChildInvocation.java:43)
 at com.intellij.util.xml.impl.DomInvocationHandler.invoke(DomInvocationHandler.java:689)
 at com.intellij.myframework.model.WebApp$$EnhancerByCGLIB$$d89a8446.addServlet(<generated>)
 at TestPsi$1.run(TestPsi.java:49)
 at com.intellij.openapi.application.impl.ApplicationImpl.runWriteAction(ApplicationImpl.java:938)
 at TestPsi.actionPerformed(TestPsi.java:46)
 at com.intellij.openapi.actionSystem.impl.ChameleonAction.actionPerformed(ChameleonAction.java:54)
 at com.intellij.openapi.actionSystem.ex.ActionUtil.performActionDumbAware(ActionUtil.java:164)
 at com.intellij.openapi.actionSystem.impl.ActionMenuItem$ActionTransmitter$1.run(ActionMenuItem.java:312)
 at com.intellij.openapi.wm.impl.FocusManagerImpl.runOnOwnContext(FocusManagerImpl.java:926)
 at com.intellij.openapi.wm.impl.IdeFocusManagerImpl.runOnOwnContext(IdeFocusManagerImpl.java:124)
 at com.intellij.openapi.actionSystem.impl.ActionMenuItem$ActionTransmitter.actionPerformed(ActionMenuItem.java:282)
 at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
 at com.intellij.openapi.actionSystem.impl.ActionMenuItem.fireActionPerformed(ActionMenuItem.java:109)
 at com.intellij.ui.plaf.beg.BegMenuItemUI.doClick(BegMenuItemUI.java:512)
 at com.intellij.ui.plaf.beg.BegMenuItemUI.access$300(BegMenuItemUI.java:44)
 at com.intellij.ui.plaf.beg.BegMenuItemUI$MyMouseInputHandler.mouseReleased(BegMenuItemUI.java:532)
 at java.awt.Component.processMouseEvent(Component.java:6525)
 at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
 at java.awt.Component.processEvent(Component.java:6290)
 at java.awt.Container.processEvent(Container.java:2234)
 at java.awt.Component.dispatchEventImpl(Component.java:4881)
 at java.awt.Container.dispatchEventImpl(Container.java:2292)



What is the proper way to achieve this. What am I missing here.
1 comment
Comment actions Permalink

It's not the latest IDEA 14.0.x, is it? (line numbers don't seem to match) Which IDEA build are you running your plugin in? Or, what's in "ChangeUtil.java:91" line in that version?

0

Please sign in to leave a comment.