package net.sourceforge.phpdt.sql.actions; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.Iterator; import org.apache.crimson.tree.XmlDocument; import org.eclipse.jface.action.Action; import org.eclipse.jface.action.IAction; import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.StructuredSelection; import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.FileDialog; import org.eclipse.ui.IViewActionDelegate; import org.eclipse.ui.IViewPart; import org.w3c.dom.Element; import net.sourceforge.phpdt.sql.Messages; import net.sourceforge.phpdt.sql.sql.metadata.MetaDataXMLInterface; import net.sourceforge.phpdt.sql.sql.metadata.ObjectMetaData; import net.sourceforge.phpdt.sql.view.BookmarkView; import net.sourceforge.phpdt.sql.view.SubsetView; import net.sourceforge.phpdt.sql.view.bookmark.BookmarkNode; import net.sourceforge.phpdt.sql.view.bookmark.ObjectNode; import net.sourceforge.phpdt.sql.view.bookmark.SubsetNode; import net.sourceforge.phpdt.sql.view.bookmark.TableNode; import net.sourceforge.phpdt.sql.view.bookmark.TreeNode; import net.sourceforge.phpdt.sql.view.bookmark.ViewNode; /** * @author root * * To change this generated comment edit the template variable "typecomment": * Window>Preferences>Java>Templates. * To enable and disable the creation of type comments go to * Window>Preferences>Java>Code Generation. */ public class ExportXMLAction extends Action implements IViewActionDelegate { IViewPart view; /** * @see org.eclipse.ui.IViewActionDelegate#init(IViewPart) */ public void init(IViewPart view) { this.view = view; } /** * @see org.eclipse.ui.IActionDelegate#run(IAction) */ public void run(IAction action) { run(); } public void run() { FileDialog dialog = new FileDialog(view.getSite().getShell(), SWT.SAVE); dialog.setFilterExtensions(new String[]{"*.xml","*.*"}); //$NON-NLS-1$ //$NON-NLS-2$ dialog.setFilterNames(new String[]{Messages.getString("filedialog.xmlFiles"),Messages.getString("filedialog.xmlFiles")}); //$NON-NLS-1$ //$NON-NLS-2$ String filename = dialog.open(); if (filename == null) return; /*Check for the presence of a "." - either indicates an * extension has been provided or that a filename with a '.' * has been specified - if the latter, it is assumed the user * knows what they're doing - could be dangerous! :-) */ if (filename.indexOf(".") ==0) filename += ".xml"; File file = new File(filename); System.out.println(Messages.getString("ExportXMLAction.XMLExporting") + file); //$NON-NLS-1$ FileOutputStream out = null; try { out = new FileOutputStream(file); } catch (FileNotFoundException e1) { MessageDialog.openConfirm(view.getSite().getShell(), Messages.getString("ExportXMLAction.CannotOpenFile"), //$NON-NLS-1$ Messages.getString("ExportXMLAction.CannotOpenFileMessage") + filename+ //$NON-NLS-1$ Messages.getString("ExportXMLAction.CannotOpenFileExplain")); //$NON-NLS-1$ e1.printStackTrace(); return; } XmlDocument doc = new XmlDocument(); StructuredSelection selection = null; if (view instanceof BookmarkView){ BookmarkView bookmarkView = (BookmarkView) view; selection = bookmarkView.getSelection(); } else if (view instanceof SubsetView){ SubsetView subsetView = (SubsetView) view; selection = subsetView.getSelection(); } ExportXMLSelection(doc, selection); try { doc.write(out); out.close(); } catch (IOException e) { e.printStackTrace(); } } public static void ExportXMLSelection(XmlDocument doc, StructuredSelection selection) { Element root = (Element) doc.appendChild(doc.createElement(Messages.getString("ExportXMLAction.Metadata"))); //$NON-NLS-1$ MetaDataXMLInterface.createElementText(root, Messages.getString("ExportXMLAction.Author"), //$NON-NLS-1$ Messages.getString("ExportXMLAction.PHPEclipseSQL")); //$NON-NLS-1$ MetaDataXMLInterface.createElementText(root, Messages.getString("ExportXMLAction.Version"), //$NON-NLS-1$ Messages.getString("ExportXMLAction.XMLVersionNumber")); //$NON-NLS-1$ Iterator iter = selection.iterator(); while (iter.hasNext()) { TreeNode current = (TreeNode) iter.next(); if (current instanceof SubsetNode){ SubsetNode subset = (SubsetNode) current; MetaDataXMLInterface.createElementText(root, Messages.getString("ExportXMLAction.Subset"), //$NON-NLS-1$ subset.getName()); //$NON-NLS-1$ Object[] children = subset.getChildren(); for (int i = 0; i < children.length; i++) { TreeNode objectNode = (TreeNode) children[i]; ExportXMLAction.exportObject(root, objectNode); } } ExportXMLAction.exportObject(root, current); } } /** * @see org.eclipse.ui.IActionDelegate#selectionChanged(IAction, ISelection) */ public void selectionChanged(IAction action, ISelection selection) { } /** * Exports a TreeNode metadata representation to an XmlDocument, based on a an already-created root Element * @param doc The XmlDocument to receive the metadata representation * @param node The node with the metadata to export * @param root The root element (already present in the XmlDocument) that will hold the metadata */ public static void exportObject(Element root, TreeNode node) { XmlDocument doc = (XmlDocument) root.getOwnerDocument(); String name = node.getName(); Element table = null; if (node instanceof TableNode) table = (Element) root.appendChild(doc.createElement(Messages.getString("ExportXMLAction.Table"))); //$NON-NLS-1$ else if (node instanceof ViewNode) table = (Element) root.appendChild(doc.createElement(Messages.getString("ExportXMLAction.View"))); //$NON-NLS-1$ else if (node instanceof ObjectNode) table = (Element) root.appendChild(doc.createElement(Messages.getString("ExportXMLAction.View"))); //$NON-NLS-1$ else return; MetaDataXMLInterface.createElementText(table,Messages.getString("ExportXMLAction.TableName"), name); //$NON-NLS-1$ if (node instanceof TableNode || node instanceof ViewNode){ BookmarkNode bookmark = (BookmarkNode) node.getParent(); if (bookmark != null){ MetaDataXMLInterface.createElementText(table,Messages.getString("ExportXMLAction.BookmarkName"), bookmark.getName()); //$NON-NLS-1$ } } ObjectMetaData objMetaData = node.getMetaData(); if (objMetaData != null) MetaDataXMLInterface.metaDataToXML(objMetaData, doc, table); } }