1 package com.quantum.actions;
4 import java.io.FileOutputStream;
5 import java.io.IOException;
6 import java.util.Iterator;
8 import javax.xml.parsers.ParserConfigurationException;
10 import com.quantum.Messages;
11 import com.quantum.model.xml.ModelToXMLConverter;
12 import com.quantum.sql.metadata.MetaDataXMLInterface;
13 import com.quantum.util.xml.XMLHelper;
14 import com.quantum.view.ViewHelper;
15 import com.quantum.view.bookmark.BookmarkView;
16 import com.quantum.view.bookmark.EntityNode;
17 import com.quantum.view.bookmark.TreeNode;
18 import com.quantum.view.subset.SubsetView;
20 import org.eclipse.jface.action.Action;
21 import org.eclipse.jface.action.IAction;
22 import org.eclipse.jface.viewers.ISelection;
23 import org.eclipse.jface.viewers.StructuredSelection;
24 import org.eclipse.ui.IViewActionDelegate;
25 import org.eclipse.ui.IViewPart;
26 import org.w3c.dom.Document;
27 import org.w3c.dom.Element;
32 public class ExportXMLAction extends Action implements IViewActionDelegate {
36 * @see org.eclipse.ui.IViewActionDelegate#init(IViewPart)
38 public void init(IViewPart view) {
43 * @see org.eclipse.ui.IActionDelegate#run(IAction)
45 public void run(IAction action) {
51 FileOutputStream out = ViewHelper.askSaveFile("exportxml", view.getSite().getShell());
54 StructuredSelection selection = null;
55 if (view instanceof BookmarkView){
56 BookmarkView bookmarkView = (BookmarkView) view;
57 selection = bookmarkView.getSelection();
58 } else if (view instanceof SubsetView){
59 SubsetView subsetView = (SubsetView) view;
60 selection = subsetView.getSelection();
64 Document doc = XMLHelper.createEmptyDocument();
65 exportXMLSelection(doc, selection);
67 XMLHelper.write(out, doc);
71 } catch (ParserConfigurationException e) {
73 } catch (IOException e) {
81 * Exports to an XmlDocument the items selected in a StructuredSelection.
85 public void exportXMLSelection(Document doc, StructuredSelection selection) {
86 Element root = (Element) doc.appendChild(doc.createElement(Messages.getString("ExportXMLAction.Metadata"))); //$NON-NLS-1$
87 MetaDataXMLInterface.createElementText(root, Messages.getString("ExportXMLAction.Author"), //$NON-NLS-1$
88 Messages.getString("ExportXMLAction.Quantum")); //$NON-NLS-1$
89 MetaDataXMLInterface.createElementText(root, Messages.getString("ExportXMLAction.Version"), //$NON-NLS-1$
90 Messages.getString("ExportXMLAction.XMLVersionNumber")); //$NON-NLS-1$
91 Iterator iter = selection.iterator();
92 while (iter.hasNext()) {
93 TreeNode current = (TreeNode) iter.next();
94 // TODO: reinstate this
95 // if (current instanceof SubsetNode){
96 // SubsetNode subset = (SubsetNode) current;
97 // MetaDataXMLInterface.createElementText(root, Messages.getString("ExportXMLAction.Subset"), //$NON-NLS-1$
98 // subset.getName()); //$NON-NLS-1$
99 // Object[] children = subset.getChildren();
100 // for (int i = 0; i < children.length; i++) {
101 // TreeNode objectNode = (TreeNode) children[i];
102 // if (objectNode instanceof SelectableNode)
103 // ExportXMLAction.exportObject(root, (SelectableNode)objectNode);
106 exportObject(root, current);
113 * @see org.eclipse.ui.IActionDelegate#selectionChanged(IAction, ISelection)
115 public void selectionChanged(IAction action, ISelection selection) {
119 * Exports a TreeNode metadata representation to an XmlDocument, based on a an already-created root Element
120 * @param doc The XmlDocument to receive the metadata representation
121 * @param node The node with the metadata to export
122 * @param root The root element (already present in the XmlDocument) that will hold the metadata
124 public void exportObject(Element root, TreeNode node) {
125 if (node instanceof EntityNode) {
126 EntityNode entityNode = (EntityNode) node;
127 ModelToXMLConverter.getInstance().convert(root, entityNode.getEntity());