1 package net.sourceforge.phpdt.sql.actions;
5 import java.io.FileNotFoundException;
6 import java.io.FileOutputStream;
7 import java.io.IOException;
8 import java.util.Iterator;
10 import org.apache.crimson.tree.XmlDocument;
11 import org.eclipse.jface.action.Action;
12 import org.eclipse.jface.action.IAction;
13 import org.eclipse.jface.dialogs.MessageDialog;
14 import org.eclipse.jface.viewers.ISelection;
15 import org.eclipse.jface.viewers.StructuredSelection;
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.widgets.FileDialog;
18 import org.eclipse.ui.IViewActionDelegate;
19 import org.eclipse.ui.IViewPart;
20 import org.w3c.dom.Element;
22 import net.sourceforge.phpdt.sql.Messages;
23 import net.sourceforge.phpdt.sql.sql.metadata.MetaDataXMLInterface;
24 import net.sourceforge.phpdt.sql.sql.metadata.ObjectMetaData;
25 import net.sourceforge.phpdt.sql.view.BookmarkView;
26 import net.sourceforge.phpdt.sql.view.SubsetView;
27 import net.sourceforge.phpdt.sql.view.bookmark.BookmarkNode;
28 import net.sourceforge.phpdt.sql.view.bookmark.ObjectNode;
29 import net.sourceforge.phpdt.sql.view.bookmark.SubsetNode;
30 import net.sourceforge.phpdt.sql.view.bookmark.TableNode;
31 import net.sourceforge.phpdt.sql.view.bookmark.TreeNode;
32 import net.sourceforge.phpdt.sql.view.bookmark.ViewNode;
37 * To change this generated comment edit the template variable "typecomment":
38 * Window>Preferences>Java>Templates.
39 * To enable and disable the creation of type comments go to
40 * Window>Preferences>Java>Code Generation.
42 public class ExportXMLAction extends Action implements IViewActionDelegate {
45 * @see org.eclipse.ui.IViewActionDelegate#init(IViewPart)
47 public void init(IViewPart view) {
52 * @see org.eclipse.ui.IActionDelegate#run(IAction)
54 public void run(IAction action) {
60 FileDialog dialog = new FileDialog(view.getSite().getShell(), SWT.SAVE);
61 dialog.setFilterExtensions(new String[]{"*.xml","*.*"}); //$NON-NLS-1$ //$NON-NLS-2$
62 dialog.setFilterNames(new String[]{Messages.getString("filedialog.xmlFiles"),Messages.getString("filedialog.xmlFiles")}); //$NON-NLS-1$ //$NON-NLS-2$
64 String filename = dialog.open();
65 if (filename == null) return;
66 /*Check for the presence of a "." - either indicates an
67 * extension has been provided or that a filename with a '.'
68 * has been specified - if the latter, it is assumed the user
69 * knows what they're doing - could be dangerous! :-)
71 if (filename.indexOf(".") ==0) filename += ".xml";
72 File file = new File(filename);
73 System.out.println(Messages.getString("ExportXMLAction.XMLExporting") + file); //$NON-NLS-1$
74 FileOutputStream out = null;
76 out = new FileOutputStream(file);
77 } catch (FileNotFoundException e1) {
78 MessageDialog.openConfirm(view.getSite().getShell(), Messages.getString("ExportXMLAction.CannotOpenFile"), //$NON-NLS-1$
79 Messages.getString("ExportXMLAction.CannotOpenFileMessage") + filename+ //$NON-NLS-1$
80 Messages.getString("ExportXMLAction.CannotOpenFileExplain")); //$NON-NLS-1$
84 XmlDocument doc = new XmlDocument();
85 StructuredSelection selection = null;
86 if (view instanceof BookmarkView){
87 BookmarkView bookmarkView = (BookmarkView) view;
88 selection = bookmarkView.getSelection();
89 } else if (view instanceof SubsetView){
90 SubsetView subsetView = (SubsetView) view;
91 selection = subsetView.getSelection();
94 ExportXMLSelection(doc, selection);
98 } catch (IOException e) {
105 public static void ExportXMLSelection(XmlDocument doc, StructuredSelection selection) {
106 Element root = (Element) doc.appendChild(doc.createElement(Messages.getString("ExportXMLAction.Metadata"))); //$NON-NLS-1$
107 MetaDataXMLInterface.createElementText(root, Messages.getString("ExportXMLAction.Author"), //$NON-NLS-1$
108 Messages.getString("ExportXMLAction.PHPEclipseSQL")); //$NON-NLS-1$
109 MetaDataXMLInterface.createElementText(root, Messages.getString("ExportXMLAction.Version"), //$NON-NLS-1$
110 Messages.getString("ExportXMLAction.XMLVersionNumber")); //$NON-NLS-1$
111 Iterator iter = selection.iterator();
112 while (iter.hasNext()) {
113 TreeNode current = (TreeNode) iter.next();
114 if (current instanceof SubsetNode){
115 SubsetNode subset = (SubsetNode) current;
116 MetaDataXMLInterface.createElementText(root, Messages.getString("ExportXMLAction.Subset"), //$NON-NLS-1$
117 subset.getName()); //$NON-NLS-1$
118 Object[] children = subset.getChildren();
119 for (int i = 0; i < children.length; i++) {
120 TreeNode objectNode = (TreeNode) children[i];
121 ExportXMLAction.exportObject(root, objectNode);
124 ExportXMLAction.exportObject(root, current);
130 * @see org.eclipse.ui.IActionDelegate#selectionChanged(IAction, ISelection)
132 public void selectionChanged(IAction action, ISelection selection) {
136 * Exports a TreeNode metadata representation to an XmlDocument, based on a an already-created root Element
137 * @param doc The XmlDocument to receive the metadata representation
138 * @param node The node with the metadata to export
139 * @param root The root element (already present in the XmlDocument) that will hold the metadata
141 public static void exportObject(Element root, TreeNode node) {
143 XmlDocument doc = (XmlDocument) root.getOwnerDocument();
144 String name = node.getName();
145 Element table = null;
146 if (node instanceof TableNode)
147 table = (Element) root.appendChild(doc.createElement(Messages.getString("ExportXMLAction.Table"))); //$NON-NLS-1$
148 else if (node instanceof ViewNode)
149 table = (Element) root.appendChild(doc.createElement(Messages.getString("ExportXMLAction.View"))); //$NON-NLS-1$
150 else if (node instanceof ObjectNode)
151 table = (Element) root.appendChild(doc.createElement(Messages.getString("ExportXMLAction.View"))); //$NON-NLS-1$
154 MetaDataXMLInterface.createElementText(table,Messages.getString("ExportXMLAction.TableName"), name); //$NON-NLS-1$
156 if (node instanceof TableNode || node instanceof ViewNode){
157 BookmarkNode bookmark = (BookmarkNode) node.getParent();
158 if (bookmark != null){
159 MetaDataXMLInterface.createElementText(table,Messages.getString("ExportXMLAction.BookmarkName"), bookmark.getName()); //$NON-NLS-1$
163 ObjectMetaData objMetaData = node.getMetaData();
164 if (objMetaData != null)
165 MetaDataXMLInterface.metaDataToXML(objMetaData, doc, table);