Ejemplo n.º 1
0
        // don't be scared by the absolute numbers for the pieces of the document
        // they can be changed, they ONLY refer to the structure that we
        // use in the DEFAULT_DOC.
        // On reading the XML document, we use element names
        public override XmlDocument GetXml(SerializableUniverse serializablePiece)
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(DEFAULT_DOC); // READ IN THE TEXT BELOW WHICH IS THE
            // STRUCTURE OF THE DOCUMENT
            XmlElement kbaseNode = (XmlElement)doc.FirstChild;

            XmlElement versionNode = (XmlElement)kbaseNode.FirstChild;
            XmlText text = doc.CreateTextNode(serialize(serializablePiece.Version));
            versionNode.AppendChild(text);

            XmlElement propertiesNode = (XmlElement)kbaseNode.ChildNodes[1];
            foreach (int id in serializablePiece.propertyIds)
            {
                makeChildXmlAndAppend(doc, propertiesNode, id);
            }

            XmlElement propertySetsNode = (XmlElement)kbaseNode.ChildNodes[2];
            foreach (int id in serializablePiece.propertySetIds)
            {
                makeChildXmlAndAppend(doc, propertySetsNode, id);
            }

            if (serializablePiece.UnknownPieceToReserialize != null) {
                XmlNodeList unknowns = serializablePiece.UnknownPieceToReserialize as XmlNodeList;
                foreach (XmlNode child in unknowns) {
                    XmlNode childClone = doc.ImportNode(child, true);
                    kbaseNode.AppendChild(childClone);
                }
            }

            XmlElement topLevelIdsNode = (XmlElement)kbaseNode.ChildNodes[3];
            foreach (int id in serializablePiece.topLevelIds)
            {
                makeChildXmlAndAppend(doc, topLevelIdsNode, id);
            }

            XmlElement snippetsNode = (XmlElement)kbaseNode.ChildNodes[4]; // explanation of nums above
            foreach (SerializableSnippet snippet in serializablePiece.snippets.Values)
            {
                makeSnippetXmlAndAppend(doc, snippetsNode, snippet);
            }

            Encryption encryption = Universe.Instance.encryption;
            if (encryption.On)
            {
                XmlEncryption.EncryptElement(doc.FirstChild, encryption.Password, true);
            }

            return doc;
        }
Ejemplo n.º 2
0
 public void Save(SerializableUniverse serializablePiece, string path)
 {
     System.IO.TextWriter writer = null;
     try
     {
         writer = new System.IO.StreamWriter(path);
         Save(serializablePiece,writer);
     }
     finally
     {
         if (writer != null)
             writer.Close();
     }
 }
Ejemplo n.º 3
0
 public void Save(SerializableUniverse serializablePiece, string path)
 {
     System.IO.TextWriter writer = null;
     bool undoable = false;
     FileInfo backupFile = new FileInfo(path + ".bak");
     FileInfo file = new FileInfo(path);
     try
     {
         // make a backup of the old file first
         // backup the old backup file if it exists
         if (backupFile.Exists)
             backupFile.Delete();
         if (file.Exists)
             file.MoveTo(backupFile.FullName);
         undoable = true;
         writer = new System.IO.StreamWriter(path);
         Save(serializablePiece,writer);
         undoable = false;
     } catch (Exception e) {
         if (undoable) {
             if (file.Exists)
             {
                 // the writer could lock up the file, so we close it
                 // just in case
                 if (writer != null)
                     writer.Close();
                 file.Delete();
             }
             if (backupFile.Exists)
                 backupFile.MoveTo(file.FullName);
         }
         throw e;
     }
     finally
     {
         if (writer != null)
             writer.Close();
     }
 }
Ejemplo n.º 4
0
        public override XmlDocument GetXml(SerializableUniverse serializablePiece)
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(DEFAULT_DOC); // READ IN THE TEXT BELOW WHICH IS THE
            // STRUCTURE OF THE DOCUMENT
            XmlElement kbaseNode = (XmlElement)doc.FirstChild;

            XmlElement versionNode = (XmlElement)kbaseNode.FirstChild;
            XmlText text = doc.CreateTextNode(serialize(serializablePiece.Version));
            versionNode.AppendChild(text);

            XmlElement propertiesNode = (XmlElement)kbaseNode.ChildNodes[1];
            foreach (Snippet snippet in Universe.Instance.propertiesPane.getProperties())
            {
                makeChildXmlAndAppend(doc, propertiesNode, snippet.Id);
            }

            XmlElement topLevelIdsNode = (XmlElement)kbaseNode.ChildNodes[2];
            foreach (int id in serializablePiece.topLevelIds)
            {
                makeChildXmlAndAppend(doc, topLevelIdsNode, id);
            }

            XmlElement snippetsNode = (XmlElement)kbaseNode.ChildNodes[3];
            foreach (SerializableSnippet snippet in serializablePiece.snippets.Values)
            {
                makeSnippetXmlAndAppend(doc, snippetsNode, snippet);
            }

            Encryption encryption = Universe.Instance.encryption;
            if (encryption.On)
            {
                XmlEncryption.EncryptElement(doc.FirstChild, encryption.Password, true);
            }

            return doc;
        }
Ejemplo n.º 5
0
 public SerializableUniverse GetSerializablePiece()
 {
     ArrayList snippets = new ArrayList(this.Count);
     foreach (SnippetTNode node in this) {
         snippets.Add(node.Snippet);
     }
     SerializableUniverse serializable = new SerializableUniverse(snippets);
     serializable.Version = Version;
     return serializable;
 }
Ejemplo n.º 6
0
 public override object OnSelectedNodesDrag()
 {
     try
     {
         // if we're client server, this is actually just a shell...
         // see OnDragDrop which will, if we're not pasting to another
         // running TheKbase.exe, use a locally cached version that
         // has not been serialized at all
         SerializableUniverse serializable = new SerializableUniverse(GetSelectedNodesAsSnippets());
         // cache our serializable so we can figure out if the drag is local
         hashCodeOfNodesBeingDragged = serializable.GetHashCode();
         return serializable;
     }
     catch (Exception e2)
     {
         MainForm.ShowError(e2);
         return null;
     }
 }
Ejemplo n.º 7
0
 void Paste(SerializableUniverse serializable, Snippet to)
 {
     List<Snippet> newSnippets = serializable.Restore(to, true);
     foreach (Snippet snippet in newSnippets)
     {
         snippet.UI.GetInstanceUnder(to).Select();
     }
 }
Ejemplo n.º 8
0
 public void Save(SerializableUniverse serializablePiece, System.IO.TextWriter writer)
 {
     XmlDocument doc = GetXml(serializablePiece);
     doc.Save(writer);
 }
Ejemplo n.º 9
0
 public abstract XmlDocument GetXml(SerializableUniverse serializablePiece);
Ejemplo n.º 10
0
        /// <summary>
        /// returns true if save suceeds, will NOT call saveas!
        /// </summary>
        /// <returns></returns>
        public bool Save()
        {
            bool success = false;
            detailPane.Save();

            SerializableUniverse universe = new SerializableUniverse(ModelGateway.TopLevelSnippet.Children);
            try
            {
                Logger.Log("Saving TheKBase File to " + Path);
                DateTime marker = DateTime.Now;
                universe.Save(Path);
                mainForm.SetStatus("Sucessfully saved to " + Path + "");
                success = true;
                ModelGateway.Dirty = false;
                Logger.LogTimer(marker, "Saving file");
            }
            catch (Exception ex)
            {
                MainForm.ShowError(ex, "Error saving");
            }
            return success;
        }
Ejemplo n.º 11
0
 /// <summary>
 /// </summary>
 /// <returns></returns>
 public void Export(string xsl, string outputFile)
 {
     detailPane.Save();
     SerializableUniverse universe = new SerializableUniverse(ModelGateway.TopLevelSnippet.Children);
     universe.Export(xsl, outputFile);
 }
Ejemplo n.º 12
0
        public override SerializableUniverse Restore(string path)
        {
            XmlReader reader = null;
            try
            {
                SerializableUniverse retVal = new SerializableUniverse();
                XmlDocument doc = new XmlDocument();
                reader = new XmlTextReader(path);
                doc.Load(reader);

                XmlElement kbaseNode = (XmlElement)doc.GetElementsByTagName("Kbase")[0];

                if (kbaseNode.FirstChild.Name.Equals(XmlEncryption.ENCRYPTED_NODE_NAME)) {
                    string password = null;
                    bool success = false;
                    XmlNode encryptedNode = kbaseNode.FirstChild;
                    while (!success)
                    {
                        password = Universe.Instance.encryption.SolicitPasswordOnLoad(password);
                        if (password == null)
                            throw new Exception("User cancelled.");
                        try
                        {
                            XmlEncryption.DecryptElement(encryptedNode, password);
                            Universe.Instance.encryption.Password = password;
                            success = true;
                        }
                        catch (Exception ex) {
                            Logger.Log("Failed decryption " + ex.Message);
                        }
                    }
                }

                // version node
                XmlElement versionNode = (XmlElement)kbaseNode.GetElementsByTagName("Version")[0];
                XmlText text = (XmlText)versionNode.FirstChild;
                retVal.Version = deserializeDateTime(text.Value);

                XmlElement topLevelIdNode = (XmlElement)kbaseNode.GetElementsByTagName("TopLevelIds")[0];
                foreach (XmlElement idXml in topLevelIdNode.ChildNodes)
                {
                    int id = HandleId(idXml);
                    retVal.topLevelIds.Add(id);
                }

                XmlElement snippetsNode = (XmlElement)kbaseNode.GetElementsByTagName("Snippets")[0];
                foreach (XmlElement snippetXml in snippetsNode.ChildNodes)
                {
                    SerializableSnippet sSnippet = HandleSnippet(snippetXml);
                    retVal.snippets.Add(sSnippet.Id,sSnippet);
                }

                XmlElement propertiesNode = (XmlElement)kbaseNode.GetElementsByTagName("Properties")[0];
                if (propertiesNode != null) // for backwards compatability
                {
                    foreach (XmlElement idXml in propertiesNode.ChildNodes)
                    {
                        int id = HandleId(idXml);
                        retVal.propertyIds.Add(id);
                    }
                }

                return retVal;
            }
            finally
            {
                if (reader!=null)
                    reader.Close();
            }
        }
Ejemplo n.º 13
0
 public SerializableUniverse GetSerializablePiece()
 {
     List<Kbase.Model.Snippet> snippets = new List<Kbase.Model.Snippet>(this.Count);
     foreach (SnippetTNode node in this) {
         snippets.Add(node.Snippet);
     }
     SerializableUniverse serializable = new SerializableUniverse(snippets);
     serializable.Version = Version;
     return serializable;
 }