bug 1365831, error for creating new file with empty new PHP file template
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / actions / ExportXMLAction.java
1 package com.quantum.actions;
2
3
4 import java.io.FileOutputStream;
5 import java.io.IOException;
6 import java.util.Iterator;
7
8 import javax.xml.parsers.ParserConfigurationException;
9
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;
19
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;
28
29 /**
30  * @author root
31  */
32 public class ExportXMLAction extends Action implements IViewActionDelegate {
33     
34         IViewPart view;
35         /**
36          * @see org.eclipse.ui.IViewActionDelegate#init(IViewPart)
37          */
38         public void init(IViewPart view) {
39                 this.view = view;
40         }
41
42         /**
43          * @see org.eclipse.ui.IActionDelegate#run(IAction)
44          */
45         public void run(IAction action) {
46                 run();
47         }
48                 
49         public void run() {
50                 
51                 FileOutputStream out = ViewHelper.askSaveFile("exportxml", view.getSite().getShell());
52                 if (out == null)
53                         return;
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();
61                 }
62
63                 try {
64             Document doc = XMLHelper.createEmptyDocument();
65             exportXMLSelection(doc, selection);
66             try {
67                 XMLHelper.write(out, doc);
68             } finally {
69                         out.close();
70             }
71         } catch (ParserConfigurationException e) {
72             e.printStackTrace();
73                 } catch (IOException e) {
74                         e.printStackTrace();
75                 }
76
77                 
78         }
79
80         /**
81          * Exports to an XmlDocument the items selected in a StructuredSelection.
82          * @param doc
83          * @param selection
84          */
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);                                 
104 //                              }
105 //                      } else {
106                 exportObject(root, current);                    
107 //                      }
108                 }
109                 
110         }
111
112         /**
113          * @see org.eclipse.ui.IActionDelegate#selectionChanged(IAction, ISelection)
114          */
115         public void selectionChanged(IAction action, ISelection selection) {
116         }
117
118         /**
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
123          */
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());
128                 }
129         }
130 }