/* * Created on Apr 12, 2003 * * To change the template for this generated file go to * Window>Preferences>Java>Code Generation>Code and Comments */ package net.raepple.plugin; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.eclipse.core.resources.IFile; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Status; import org.eclipse.jface.dialogs.ErrorDialog; import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.jface.wizard.Wizard; import org.eclipse.swt.widgets.Shell; import org.w3c.dom.Document; import org.w3c.dom.NodeList; /** * @author raepple * * This class initializes the wizard and calculates the number of * elements when user clicked finish. * */ public class XMLElementCountWizard extends Wizard { private IFile xmlFile; private XMLElementCountPage page1; /** * */ public XMLElementCountWizard(IFile xmlFile) { super(); setWindowTitle("XML Element Count Plugin"); this.xmlFile = xmlFile; } /** * Adds pages to the wizard and initialize them. * */ public void addPages() { page1 = new XMLElementCountPage(); addPage(page1); } /* (non-Javadoc) * @see org.eclipse.jface.wizard.IWizard#performFinish() */ public boolean performFinish() { int numberOfElements = 0; String errorMessage = ""; // get XML Parser from JAXP factory DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); // configure factory factory.setValidating(false); factory.setNamespaceAware(false); factory.setIgnoringComments(true); Shell shell = new Shell(); Document document = null; try { DocumentBuilder parser = factory.newDocumentBuilder(); document = parser.parse(this.xmlFile.getContents()); } catch (Exception e) { errorMessage = e.getMessage(); IStatus status = new Status(IStatus.ERROR, XMLProcessing.PLUGIN_ID, 0, errorMessage, e); ErrorDialog.openError( shell, "XML Element Count", "Exception occured: " + errorMessage, status); return false; } NodeList nodeList = document.getElementsByTagName(page1.getElementName()); numberOfElements = nodeList.getLength(); MessageDialog.openInformation( shell, "XML Element Count", "Elements with name " + page1.getElementName() + " found: " + numberOfElements); return false; } }