1 package net.sourceforge.phpdt.sql.view.bookmark;
3 import java.util.Collections;
4 import java.util.Vector;
6 import org.w3c.dom.Element;
7 import org.w3c.dom.NodeList;
9 import net.sourceforge.phpdt.sql.Messages;
10 import net.sourceforge.phpdt.sql.sql.metadata.MetaDataXMLInterface;
11 import net.sourceforge.phpdt.sql.sql.metadata.ObjectMetaData;
13 public class SubsetNode implements TreeNode {
14 private String name = null;
15 private Vector children = new Vector();
17 public SubsetNode(SubsetNode param) {
19 children.addAll(param.children);
23 public ObjectMetaData getMetaData() {
24 return null; //no metadata implementation for now
29 public SubsetNode(String name) {
33 public void setName(String name) {
37 public String getName(){
41 public Object[] getChildren() {
42 return children.toArray();
45 public Object getParent() {
49 public boolean hasChildren() {
50 if (children != null && children.size() > 0) {
56 public void setChildren(Vector children) {
57 for (int i = 0; i < children.size(); i++) {
58 Object obj = children.elementAt(i);
61 Collections.sort(children);
62 this.children = children;
65 public void isValid(Object child) {
66 boolean valid = false;
67 if (child instanceof ObjectNode ) {
71 throw new RuntimeException("Invalid SubsetNode child: " + child.getClass().getName()); //$NON-NLS-1$
77 * Finds a child of the SubsetNode with the said name
79 * @return the TreeNode found. null if none
81 public TreeNode find(String name){
82 for (int i = 0; i < children.size(); i++) {
83 Object obj = children.elementAt(i);
84 if (obj instanceof TreeNode){
85 TreeNode node = (TreeNode) obj;
86 if (name.equals(node.getName())) return node;
92 public boolean add(ObjectNode object){
93 if (children.indexOf(object) >= 0) return false;
98 public boolean addReplace(ObjectNode object){
99 children.remove(object);
103 public boolean remove(ObjectNode object){
104 return children.remove(object);
107 public void exportXML(Element root) {
108 Element sub = MetaDataXMLInterface.createElementText(root,Messages.getString("ExportXMLAction.Subset"), ""); //$NON-NLS-1$ //$NON-NLS-2$
109 MetaDataXMLInterface.createElementText(sub,Messages.getString("ExportXMLAction.SubsetName"), getName()); //$NON-NLS-1$
110 for (int i = 0; i < children.size(); i++) {
111 Object obj = children.elementAt(i);
112 if (obj instanceof ObjectNode){
113 ((ObjectNode) obj).exportXML(sub);
120 * Imports a set of XML tags (parsed into a XMLDocument) into a Subset
121 * @param root Document to get the data from
122 * @param replace True if you want to replace already-existing elements with the same name, false if not
124 public void importXML(Element root, boolean replace) {
126 String name = MetaDataXMLInterface.getElementText(root, Messages.getString("ExportXMLAction.SubsetName")); //$NON-NLS-1$
127 if (name == "") return; //$NON-NLS-1$
130 importElementXML(root, replace, Messages.getString("ExportXMLAction.Table")); //$NON-NLS-1$
131 importElementXML(root, replace, Messages.getString("ExportXMLAction.View")); //$NON-NLS-1$
138 * Imports one type of element from a XMLDocument (possibly a part of one) into a Subset
139 * @param root Document to get the data from
140 * @param replace True if you want to replace already-existing elements with the same name, false if not
141 * @param type The type of element to import, has to correspond with the XML tag
143 private void importElementXML(Element root, boolean replace, String type) {
144 // We get all the tags named as the type
145 NodeList tables = root.getElementsByTagName(type);
146 for (int i = 0; i < tables.getLength(); i++) {
147 Element table = (Element) tables.item(i);
148 ObjectNode objectNode = ObjectNode.importXML(table, this);
149 if (replace)addReplace(objectNode);
150 else add(objectNode);