import java.io.File;
import java.io.FileInputStream;
-import java.io.FileOutputStream;
import java.util.Properties;
import java.util.Vector;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.Viewer;
+import org.w3c.dom.Element;
+import org.w3c.dom.NodeList;
import net.sourceforge.phpdt.sql.bookmarks.Bookmark;
+import net.sourceforge.phpdt.sql.sql.metadata.MetaDataXMLInterface;
public class BookmarkContentProvider implements ITreeContentProvider {
private Vector bookmarks = new Vector();
private BookmarkContentProvider() {
}
- public void importBookmarks(File file) {
- hasChanged = true;
- System.out.println("Importing Bookmarks: Loading from file: " + file);
- try {
- Properties props = new Properties();
- FileInputStream in = new FileInputStream(file);
- props.load(in);
- in.close();
- fromProperties(false, props);
- } catch (Throwable e) {
- e.printStackTrace();
- }
- }
+ /**
+ * Imports the bookmars from a File, supposed to be in XML format and have the correct tags.
+ * @param file
+ */
public void load(File file) {
- System.out.println("Bookmarks: Loading from file: " + file);
+ System.out.println("Bookmarks: Loading from file: " + file); //$NON-NLS-1$
try {
Properties props = new Properties();
FileInputStream in = new FileInputStream(file);
e.printStackTrace();
}
}
- public void save(File file) {
- System.out.println("Bookmarks: Saving to file: " + file);
- try {
- Properties props = getProperties();
- FileOutputStream out = new FileOutputStream(file);
- props.store(out, "");
- out.close();
- } catch (Throwable e) {
- e.printStackTrace();
+
+ /**
+ * Exports a Bookmark data to an XMLDocument Element
+ * The complementary function is importXML()
+ * @param root The Element to fill up with the bookmark info
+ */
+ public void exportXML(Element root) {
+ System.out.println("Bookmarks: Saving to Element"); //$NON-NLS-1$
+ Element bookmarkRoot = MetaDataXMLInterface.createElementText(root,"bookmarks", ""); //$NON-NLS-1$ //$NON-NLS-2$
+ for (int i = 0; i < bookmarks.size(); i++) {
+ Bookmark b = (Bookmark) bookmarks.elementAt(i);
+ Element bookmark = MetaDataXMLInterface.createElementText(bookmarkRoot,"bookmark", ""); //$NON-NLS-1$ //$NON-NLS-2$
+ MetaDataXMLInterface.createElementText(bookmark,"name", b.getName()); //$NON-NLS-1$
+ MetaDataXMLInterface.createElementText(bookmark,"username", b.getUsername()); //$NON-NLS-1$
+ MetaDataXMLInterface.createElementText(bookmark,"password", b.getPassword()); //$NON-NLS-1$
+ MetaDataXMLInterface.createElementText(bookmark,"schema", b.getSchema()); //$NON-NLS-1$
+ MetaDataXMLInterface.createElementText(bookmark,"connect", b.getConnect()); //$NON-NLS-1$
+ MetaDataXMLInterface.createElementText(bookmark,"driver", b.getDriver()); //$NON-NLS-1$
+ MetaDataXMLInterface.createElementText(bookmark,"type", b.getType()); //$NON-NLS-1$
+ MetaDataXMLInterface.createElementText(bookmark,"driverLocation", b.getDriverFile()); //$NON-NLS-1$
}
}
-
+
+ /**
+ * Imports a Bookmark data from an XMLDocument Element
+ * The complementary function is exportXML()
+ * @param root The Element from which to load
+ */
+ public void importXML(Element root) {
+ System.out.println("Bookmarks: Loading from Element"); //$NON-NLS-1$
+ Vector newBookmarks = new Vector();
+ NodeList nodes = root.getElementsByTagName("bookmark"); //$NON-NLS-1$
+ for (int i = 0; i < nodes.getLength(); i++) {
+ Bookmark bookmark = new BookmarkNode();
+ Element column = (Element) nodes.item(i);
+
+ String name = MetaDataXMLInterface.getElementText(column,"name"); //$NON-NLS-1$
+ if (name == null) break;
+ bookmark.setName(name);
+
+ MetaDataXMLInterface.getElementText(column,"name"); //$NON-NLS-1$
+ bookmark.setUsername(MetaDataXMLInterface.getElementText(column,"username")); //$NON-NLS-1$
+ bookmark.setPassword(MetaDataXMLInterface.getElementText(column,"password")); //$NON-NLS-1$
+ bookmark.setConnect(MetaDataXMLInterface.getElementText(column,"connect")); //$NON-NLS-1$
+ bookmark.setDriver(MetaDataXMLInterface.getElementText(column,"driver")); //$NON-NLS-1$
+ bookmark.setSchema(MetaDataXMLInterface.getElementText(column,"schema")); //$NON-NLS-1$
+ bookmark.setType(MetaDataXMLInterface.getElementText(column,"type")); //$NON-NLS-1$
+ bookmark.setDriverFile(MetaDataXMLInterface.getElementText(column,"driverLocation")); //$NON-NLS-1$
+ System.out.println(bookmark.toString());
+ if (!bookmark.isEmpty()) {
+ newBookmarks.addElement(bookmark);
+ }
+ }
+ bookmarks = newBookmarks;
+ }
+
public Object[] getChildren(Object parentElement) {
if (parentElement.equals(Root.ROOT)) {
return bookmarks.toArray();
}
return Root.EMPTY_ARRAY;
}
-
+ /**
+ * Finds a Bookmark with the specified name, and returs it. Null if nothing found.
+ * @param name
+ * @return
+ */
+ public BookmarkNode find(String name){
+ for (int i = 0; i < bookmarks.size(); i++) {
+ BookmarkNode b = (BookmarkNode) bookmarks.elementAt(i);
+ if (b.getName().equals(name)) return b;
+ }
+ return null;
+ }
+
public Object[] getElements(Object inputElement) {
return getChildren(inputElement);
}
int i = 0;
while (true) {
Bookmark bookmark = new BookmarkNode();
- String name = props.getProperty(i + ".name");
+ String name = props.getProperty(i + ".name"); //$NON-NLS-1$
if (name == null) {
break;
}
bookmark.setName(name);
- bookmark.setUsername(props.getProperty(i + ".username"));
- bookmark.setPassword(props.getProperty(i + ".password"));
- bookmark.setConnect(props.getProperty(i + ".connect"));
- bookmark.setDriver(props.getProperty(i + ".driver"));
- String schema = props.getProperty(i + ".schema");
+ bookmark.setUsername(props.getProperty(i + ".username")); //$NON-NLS-1$
+ bookmark.setPassword(props.getProperty(i + ".password")); //$NON-NLS-1$
+ bookmark.setConnect(props.getProperty(i + ".connect")); //$NON-NLS-1$
+ bookmark.setDriver(props.getProperty(i + ".driver")); //$NON-NLS-1$
+ String schema = props.getProperty(i + ".schema"); //$NON-NLS-1$
if (schema != null) {
bookmark.setSchema(schema);
} else {
- bookmark.setSchema("");
+ bookmark.setSchema(""); //$NON-NLS-1$
}
- String type = props.getProperty(i + ".type");
+ String type = props.getProperty(i + ".type"); //$NON-NLS-1$
if (type != null) {
bookmark.setType(type);
} else {
- bookmark.setType("");
+ bookmark.setType(""); //$NON-NLS-1$
}
- String driverFile = props.getProperty(i + ".driverLocation");
+ String driverFile = props.getProperty(i + ".driverLocation"); //$NON-NLS-1$
if (driverFile != null) {
bookmark.setDriverFile(driverFile);
} else {
- bookmark.setDriverFile("");
+ bookmark.setDriverFile(""); //$NON-NLS-1$
}
System.out.println(bookmark.toString());
if (!bookmark.isEmpty()) {
Properties props = new Properties();
for (int i = 0; i < bookmarks.size(); i++) {
Bookmark b = (Bookmark) bookmarks.elementAt(i);
- props.put(i + ".name", b.getName());
- props.put(i + ".username", b.getUsername());
- props.put(i + ".password", b.getPassword());
- props.put(i + ".schema", b.getSchema());
- props.put(i + ".connect", b.getConnect());
- props.put(i + ".driver", b.getDriver());
- props.put(i + ".type", b.getType());
- props.put(i + ".driverLocation", b.getDriverFile());
+ props.put(i + ".name", b.getName()); //$NON-NLS-1$
+ props.put(i + ".username", b.getUsername()); //$NON-NLS-1$
+ props.put(i + ".password", b.getPassword()); //$NON-NLS-1$
+ props.put(i + ".schema", b.getSchema()); //$NON-NLS-1$
+ props.put(i + ".connect", b.getConnect()); //$NON-NLS-1$
+ props.put(i + ".driver", b.getDriver()); //$NON-NLS-1$
+ props.put(i + ".type", b.getType()); //$NON-NLS-1$
+ props.put(i + ".driverLocation", b.getDriverFile()); //$NON-NLS-1$
}
return props;
}