ingenias.editor.persistence.PersistenceManager Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of nodereled Show documentation
Show all versions of nodereled Show documentation
A simple node-relationship editor
/**
* Copyright (C) 2010 Jorge J. Gomez-Sanz
*
* This file is part of the INGENME tool. INGENME is an open source meta-editor
* which produces customized editors for user-defined modeling languages
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation version 3 of the License
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see
**/
package ingenias.editor.persistence;
import ingenias.editor.Editor;
import ingenias.editor.GUIResources;
import ingenias.editor.GraphManager;
import ingenias.editor.IDEState;
import ingenias.editor.IDEUpdater;
import ingenias.editor.ModelJGraph;
import ingenias.editor.ObjectManager;
import ingenias.editor.Preferences;
import ingenias.editor.ProjectProperty;
import ingenias.editor.RelationshipManager;
import ingenias.editor.actions.HistoryManager;
import ingenias.editor.editionmode.EmbeddedAndPopupCellEditor;
import ingenias.editor.entities.NAryEdgeEntity;
import ingenias.editor.widget.DnDJTree;
import ingenias.exception.CannotLoad;
import ingenias.exception.InvalidProjectProperty;
import ingenias.exception.UnknowFormat;
import ingenias.exception.VersionNotFound;
import ingenias.generator.browser.BrowserImp;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.RandomAccessFile;
import java.lang.reflect.InvocationTargetException;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.channels.OverlappingFileLockException;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Properties;
import java.util.Vector;
import javax.swing.JFileChooser;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
import org.apache.xerces.parsers.DOMParser;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
/**
* Description of the Class
*
* @author developer
* @created 7 de agosto de 2003
*/
public class PersistenceManager {
private static GraphLoad gl;
private static ObjectLoad ol;
private static RelationshipLoad rl;
private static PropertyLoad pl;
/**
* Constructor for the PersistenceManager object
*/
public PersistenceManager() {
}
/**
* Description of the Method
*
* @param abs
* Description of the Parameter
* @exception UnknowFormat
* Description of the Exception
* @exception CannotLoad
* Description of the Exception
*/
public void restorePreferences(IDEState ids, GUIResources resources, IDEUpdater updater) throws UnknowFormat,
CannotLoad {
JFileChooser jfc = new JFileChooser();
File homedir = jfc.getCurrentDirectory();
String filename = homedir.getPath() + "/.idk/idkproperties.xml";
if (new File(filename).exists()) {
try {
DOMParser parser = new DOMParser();
// Parse the Document
// and traverse the DOM
parser.parse("file:" + filename);
Document doc = parser.getDocument();
NodeList nl = doc.getElementsByTagName("preferences");
Preferences prefs = Preferences.fromXML(nl.item(0));
ids.prefs = prefs;
ids.getLastFiles().clear();
nl = nl.item(0).getChildNodes();
for (int k = 0; k < nl.getLength(); k++) {
Node n = nl.item(k);
if (n.getNodeName().equals("lastfile")) {
if (n.getChildNodes().getLength() > 0) {
Node file = n.getChildNodes().item(0);
if (file.getNodeType() == Node.TEXT_NODE) {
String path = file.getNodeValue();
HistoryManager.updateHistory(new File(path), resources, ids,updater);
}
}
}
if (n.getNodeName().equals("lastimage")) {
if (n.getChildNodes().getLength() > 0) {
Node file = n.getChildNodes().item(0);
if (file.getNodeType() == Node.TEXT_NODE) {
String path = file.getNodeValue();
HistoryManager.updateHistory(new File(path), resources, ids,updater);
}
}
}
}
// deleteBadRelationships(gm);
}
/*
* catch (java.io.FileNotFoundException fnf) { throw new
* CannotLoad("File " + filename + " not found"); }
*/catch (java.io.IOException ioe) {
throw new CannotLoad("File " + filename
+ " could not be loaded. " + ioe.getMessage());
} catch (org.xml.sax.SAXException se) {
se.printStackTrace();
throw new UnknowFormat("File " + filename
+ " is not valid xml." + se.getMessage());
}
}
}
/**
* Description of the Method
*
* @param ids
* Description of the Parameter
*/
public void savePreferences(IDEState ids) {
try {
File homedir = new File(System.getProperty("user.home"));
String filename = homedir.getPath() + "/.idk/idkproperties.xml";
if (!new File(homedir.getPath() + "/.idk").exists() ){
new File(homedir.getPath() + "/.idk").mkdir();
}
java.io.FileOutputStream fos = new java.io.FileOutputStream(
filename);
fos
.write("\n\n"
.getBytes());
Vector v = ids.getLastFiles();
for (int k = 0; k < v.size(); k++) {
File current = (File) v.elementAt(k);
if (current!=null)
fos.write(("" + ingenias.generator.util.Conversor.replaceInvalidChar(current.getPath()) + " \n").getBytes());
}
if (ids.getCurrentImageFolder() != null) {
fos.write((""
+ ingenias.generator.util.Conversor
.replaceInvalidChar(ids.getCurrentImageFolder()
.getPath()) + " \n")
.getBytes());
}
fos.write(ids.prefs.toXML().getBytes());
fos.write(" ".getBytes());
fos.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
/**
* Description of the Method
*
* @param input
* Description of Parameter
* @return Description of the Returned Value
* @exception ingenias.exception.UnknowFormat
* Description of Exception
* @exception ingenias.exception.DamagedFormat
* Description of Exception
* @exception CannotLoad
* Description of Exception
*/
public IDEState mergeFile(String input, IDEState ids,GUIResources resources)
throws ingenias.exception.UnknowFormat,
ingenias.exception.DamagedFormat, CannotLoad {
try {
DOMParser parser = new DOMParser();
// Parse the Document
// and traverse the DOM
parser.parse(new InputSource(new FileInputStream(input)));
Document doc = parser.getDocument();
NodeList reltags = doc.getElementsByTagName("relationship");
NodeList objects = doc.getElementsByTagName("object");
NodeList nodesGLX = doc.getElementsByTagName("node");
NodeList models = doc.getElementsByTagName("model");
Vector rels = RelationshipManager
.getRelationshipsVector(ids.gm);
Hashtable trels = new Hashtable();
for (NAryEdgeEntity nedge : rels) {
trels.put(nedge.getId(), nedge);
}
for (int k = 0; k < reltags.getLength(); k++) {
String id = reltags.item(k).getAttributes().getNamedItem("id")
.getNodeValue();
String nid = id;
while (trels.containsKey(nid)
|| ids.om.findUserObject(nid).size() > 0) {
nid = nid + "_";
}
if (!id.equals(nid)) {
reltags.item(k).getAttributes().getNamedItem("id")
.setNodeValue(nid);
renameNodes(nodesGLX, id, nid);
renameNodes(objects, id, nid);
}
}
for (int k = 0; k < models.getLength(); k++) {
String id = models.item(k).getAttributes().getNamedItem("id")
.getNodeValue();
String nid = id;
while (ids.gm.getModel(nid) != null) {
nid = nid + "_";
}
if (!id.equals(nid)) {
models.item(k).getAttributes().getNamedItem("id")
.setNodeValue(nid);
renameNodes(models, id, nid);
renameNodes(objects, id, nid);
}
}
String version = "1.0";
try {
version = this.getVersion(doc);
} catch (VersionNotFound vnf) {
// vnf.printStackTrace();
}
this.setVersion(version,ids);
// ol.restoreObject(ids.om, ids.gm, doc);
restoreObjects(ids.om, ids.gm, doc);
rl.restoreRelationships(ids.om, ids.gm, doc);
try {
gl.restoreModels(ids, resources,doc);
} catch (ingenias.exception.CannotLoadDiagram cld) {
throw new ingenias.exception.DamagedFormat(cld.getMessage());
}
Vector rels1 = RelationshipManager
.getRelationshipsVector(ids.gm);
HashSet trels1 = new HashSet();
for (NAryEdgeEntity nedge : rels) {
trels1.add(nedge.getId());
}
int n = 1;
// deleteBadRelationships(gm);
} catch (java.io.FileNotFoundException fnf) {
throw new CannotLoad("File " + input + " not found");
} catch (java.io.IOException ioe) {
throw new CannotLoad("File " + input + " could not be loaded. "
+ ioe.getMessage());
} catch (org.xml.sax.SAXException se) {
throw new UnknowFormat("File " + input + " is not valid xml."
+ se.getMessage());
} catch (UnknownVersion uv) {
throw new ingenias.exception.CannotLoad(
"File "
+ input
+ " version is not recognised. Try downloading a new version of the IDE in http://ingenias.sourceforge.net");
} catch (ClassNotFoundException cnf) {
cnf.printStackTrace();
} catch (NoSuchMethodException nsme) {
nsme.printStackTrace();
} catch (IllegalAccessException iae) {
iae.printStackTrace();
} catch (InstantiationException ie) {
ie.printStackTrace();
} catch (InvocationTargetException ite) {
ite.printStackTrace();
}
return ids;
}
private void renameNodes(NodeList nodesGLX, String id, String nid) {
for (int k = 0; k < nodesGLX.getLength(); k++) {
if (nodesGLX.item(k).getAttributes() != null
&& nodesGLX.item(k).getAttributes().getNamedItem("id") != null) {
String atid = nodesGLX.item(k).getAttributes().getNamedItem(
"id").getNodeValue();
if (atid.equals(id)) {
nodesGLX.item(k).getAttributes().getNamedItem("id")
.setNodeValue(nid);
}
}
}
}
/**
* Description of the Method
*
* @param input
* Description of Parameter
* @return Description of the Returned Value
* @exception ingenias.exception.UnknowFormat
* Description of Exception
* @exception ingenias.exception.DamagedFormat
* Description of Exception
* @exception CannotLoad
* Description of Exception
*/
public synchronized void loadWithoutListeners(String input,GUIResources resources, Properties oldProperties, IDEState ids)
throws ingenias.exception.UnknowFormat,
ingenias.exception.DamagedFormat, CannotLoad {
FileChannel channel=null;
FileLock lock=null;
if (!new File(System.getProperty("user.home")+"/.idk").exists())
new File(System.getProperty("user.home")+"/.idk").mkdir();
File lockFile=new File(System.getProperty("user.home")+"/.idk/lock");
if (!lockFile.exists())
try {
lockFile.createNewFile();
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
try {
channel = new RandomAccessFile(lockFile, "rw").getChannel();
// Use the file channel to create a lock on the file.
// This method blocks until it can retrieve the lock.
// Being the load method synchronized, it ensures in-jvm consistency. The lock ensures out-jvm consistency
int timeout=0;
while (timeout<10000 && lock==null){
try {
lock = channel.lock();
} catch (OverlappingFileLockException ofl){
try {
Thread.currentThread().sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
timeout=timeout+500;
}
}
if (timeout>=10000)
throw new CannotLoad("Could not acquire a lock on file "+input);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
try {
RelationshipManager.clearRelationships();
try {
DOMParser parser = new DOMParser();
// Parse the Document
// and traverse the DOM
InputSource is = new InputSource(new FileInputStream(input));
is.setEncoding("UTF-8");
parser.parse(is);
Document doc = parser.getDocument();
String version = "1.0";
try {
version = this.getVersion(doc);
} catch (VersionNotFound vnf) {
// vnf.printStackTrace();
}
this.setVersion(version,ids);
ids.prop.putAll(oldProperties);
// ol.restoreObject(ids.om, ids.gm, doc);
restoreObjects(ids.om, ids.gm, doc);
rl.restoreRelationships(ids.om, ids.gm, doc);
try {
gl.restoreModels(ids, resources,doc);
Vector mjg = ids.gm.getUOModels();
for (int k = 0; k < mjg.size(); k++) {
mjg.elementAt(k).setSelectionCells(new Object[0]);
mjg.elementAt(k).setUI(new EmbeddedAndPopupCellEditor(ids,resources));
}
} catch (ingenias.exception.CannotLoadDiagram cld) {
throw new ingenias.exception.DamagedFormat(cld.getMessage());
}
NodeList leafpackages = doc.getElementsByTagName("leafpackages");
if (leafpackages != null && leafpackages.getLength() > 0) {
NodeList paths = leafpackages.item(0).getChildNodes();
ids.gm.toExpad = new Vector();
for (int k = 0; k < paths.getLength(); k++) {
if (paths.item(k).getNodeName().equalsIgnoreCase("path")) {
NodeList pathToAdd = paths.item(k).getChildNodes();
Vector pathlist = new Vector();
for (int j = 0; j < pathToAdd.getLength(); j++) {
if (pathToAdd.item(j).getNodeName()
.equalsIgnoreCase("package")) {
String pname = pathToAdd.item(j)
.getAttributes().getNamedItem("id")
.getNodeValue();
ids.gm.addPackage(pathlist.toArray(), pname);
pathlist.add(pname);
}
}
TreePath tp = getPath(pathlist, ids.gm.arbolProyecto).getParentPath();
ids.gm.toExpad.add(tp);
ids.gm.getArbolProyecto().expandPath(tp);
}
}
for (TreePath tp:ids.gm.toExpad){
Vector