001: package net.sourceforge.squirrel_sql.plugins.graph.xmlbeans;
002:
003: import net.sourceforge.squirrel_sql.client.session.ISession;
004: import net.sourceforge.squirrel_sql.fw.xml.XMLBeanWriter;
005: import net.sourceforge.squirrel_sql.fw.xml.XMLBeanReader;
006: import net.sourceforge.squirrel_sql.fw.xml.XMLException;
007: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
008: import net.sourceforge.squirrel_sql.fw.util.StringManager;
009: import net.sourceforge.squirrel_sql.plugins.graph.GraphMainPanelTab;
010: import net.sourceforge.squirrel_sql.plugins.graph.GraphPlugin;
011:
012: import java.io.File;
013: import java.io.FilenameFilter;
014: import java.io.IOException;
015: import java.io.FileNotFoundException;
016:
017: public class GraphXmlSerializer {
018:
019: private static final StringManager s_stringMgr = StringManagerFactory
020: .getStringManager(GraphXmlSerializer.class);
021:
022: private static final String XML_BEAN_POSTFIX = ".graph.xml";
023:
024: private GraphPlugin _plugin;
025: private ISession _session;
026: private String _graphFile;
027:
028: /**
029: * Either graphPane or graphFileName might be null.
030: */
031: public GraphXmlSerializer(GraphPlugin plugin, ISession session,
032: GraphMainPanelTab graphPane, String graphFileName) {
033: try {
034: _plugin = plugin;
035: _session = session;
036: String url = _session.getAlias().getUrl();
037:
038: if (null == graphFileName) {
039: _graphFile = getFileName(plugin
040: .getPluginUserSettingsFolder().getPath(), url,
041: graphPane.getTitle());
042: } else {
043: _graphFile = graphFileName;
044: }
045: } catch (Exception e) {
046: throw new RuntimeException(e);
047: }
048: }
049:
050: public void write(GraphControllerXmlBean xmlBean) {
051: try {
052: XMLBeanWriter bw = new XMLBeanWriter(xmlBean);
053: bw.save(_graphFile);
054:
055: String[] params = { xmlBean.getTitle(), _graphFile };
056: // i18n[graph.graphSaved=Graph "{0}" saved to "{1}"]
057: String msg = s_stringMgr.getString("graph.graphSaved",
058: params);
059:
060: _session.showMessage(msg);
061: } catch (Exception e) {
062: throw new RuntimeException(e);
063: }
064: }
065:
066: public GraphControllerXmlBean read() {
067: try {
068: XMLBeanReader br = new XMLBeanReader();
069: br.load(_graphFile, this .getClass().getClassLoader());
070: return (GraphControllerXmlBean) br.iterator().next();
071: } catch (Exception e) {
072: throw new RuntimeException(e);
073: }
074: }
075:
076: private String getFileName(String path, String url, String title) {
077: final String filePrefix = javaNormalize(url) + "."
078: + javaNormalize(title);
079: File p = new File(path);
080:
081: String buf = filePrefix;
082: for (int i = 1; prefixExists(p, buf); ++i) {
083: buf = filePrefix + "_" + i;
084: }
085:
086: return path + File.separator + buf + XML_BEAN_POSTFIX;
087: }
088:
089: private boolean prefixExists(File path, final String filePrefix) {
090: File[] files = path.listFiles(new FilenameFilter() {
091: public boolean accept(File dir, String name) {
092: if (name.toLowerCase().equals(
093: filePrefix.toLowerCase() + XML_BEAN_POSTFIX)) {
094: return true;
095: }
096: return false;
097: }
098: });
099: return 0 < files.length;
100: }
101:
102: private static String javaNormalize(String text) {
103: StringBuffer buf = new StringBuffer(text.length());
104:
105: if (Character.isJavaIdentifierStart(text.charAt(0))) {
106: buf.append(text.charAt(0));
107: } else {
108: buf.append('_');
109: }
110:
111: for (int i = 1; i < text.length(); ++i) {
112: if (Character.isLetterOrDigit(text.charAt(i))) {
113: buf.append(text.charAt(i));
114: } else {
115: buf.append('_');
116: }
117: }
118:
119: String ret = buf.toString();
120:
121: return ret;
122: }
123:
124: public static GraphXmlSerializer[] getGraphXmSerializers(
125: GraphPlugin plugin, ISession session) {
126: try {
127: File settingsPath = plugin.getPluginUserSettingsFolder();
128: final String urlPrefix = javaNormalize(session.getAlias()
129: .getUrl())
130: + ".";
131:
132: File[] graphXmlFiles = settingsPath
133: .listFiles(new FilenameFilter() {
134: public boolean accept(File dir, String name) {
135: if (name.startsWith(urlPrefix)) {
136: return true;
137: }
138: return false;
139: }
140: });
141:
142: GraphXmlSerializer[] ret = new GraphXmlSerializer[graphXmlFiles.length];
143: for (int i = 0; i < graphXmlFiles.length; i++) {
144: ret[i] = new GraphXmlSerializer(plugin, session, null,
145: graphXmlFiles[i].getPath());
146: }
147:
148: return ret;
149:
150: } catch (IOException e) {
151: throw new RuntimeException(e);
152: }
153: }
154:
155: public void rename(String newName) {
156: try {
157: String url = _session.getAlias().getUrl();
158: String newGraphFile = getFileName(_plugin
159: .getPluginUserSettingsFolder().getPath(), url,
160: newName);
161: (new File(_graphFile)).renameTo(new File(newGraphFile));
162:
163: String[] params = { _graphFile, newGraphFile };
164: // i18n[graph.graphRenamed=Renamed "{0}" to "{1}"]
165: _session.showMessage(s_stringMgr.getString(
166: "graph.graphRenamed", params));
167:
168: _graphFile = newGraphFile;
169: } catch (Exception e) {
170: throw new RuntimeException(e);
171: }
172:
173: }
174:
175: public void remove() {
176: (new File(_graphFile)).delete();
177:
178: String[] params = { _graphFile };
179: // i18n[graph.graphRemoved=Removed graph file "{0}"]
180: _session.showMessage(s_stringMgr.getString(
181: "graph.graphRemoved", params));
182:
183: }
184: }
|