protected virtual string GetDirectoryForClass(IClassMap classMap) { ISourceMap sourceMap = classMap.GetDocSourceMap(); if (sourceMap == null) { throw new MappingException("Can't find Document Source Map for '" + classMap.GetFullName() + "' in the npersist xml mapping file!"); // do not localize } string directory = sourceMap.DocPath; if (!(Directory.Exists(directory))) { throw new NPersistException("Can't find directory: " + directory); // do not localize } directory += @"\" + classMap.GetFullName(); if (!(Directory.Exists(directory))) { try { Directory.CreateDirectory(directory); } catch (Exception ex) { throw new NPersistException("Could not create directory '" + directory + "': " + ex.Message, ex); // do not localize } } return(directory); }
protected virtual XmlNode LoadObjectNodeFromDomainFile(object obj, IClassMap classMap) { string fileName = GetFileNamePerDomain(obj, classMap); this.Context.LogManager.Debug(this, "Saving object to file", "File: " + fileName + ", Object Type: " + obj.GetType().ToString()); // do not localize XmlDocument xmlDoc; if (File.Exists(fileName)) { xmlDoc = GetXmlDocument(fileName); } else if (HasXmlDocument(fileName)) { xmlDoc = GetXmlDocument(fileName); } else { xmlDoc = CreateDomainXmlDocument(classMap, fileName); } XmlNode xmlRoot = xmlDoc.SelectSingleNode(classMap.GetDocSourceMap().GetDocRoot()); XmlNode xmlClass = GetNode(xmlRoot, classMap.GetDocRoot()); XmlNode xmlObject = GetNodeForObject(xmlClass, obj, classMap); return(xmlObject); }
protected virtual string GetDirectoryForDomain(IClassMap classMap) { ISourceMap sourceMap = classMap.GetDocSourceMap(); if (sourceMap == null) { throw new MappingException("Can't find Document Source Map for '" + classMap.GetFullName() + "' in the npersist xml mapping file!"); // do not localize } string directory = sourceMap.DocPath; if (!(Directory.Exists(directory))) { throw new NPersistException("Can't find directory: " + directory); // do not localize } return(directory); }
protected virtual void DeserializeDomain(ref object obj, IClassMap classMap, string xml) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(xml); XmlNode xmlRoot = xmlDoc.SelectSingleNode(classMap.GetDocSourceMap().GetDocRoot()); XmlNode xmlClass = xmlRoot.SelectSingleNode(classMap.GetDocRoot()); XmlNode xmlObject = FindNodeForObject(xmlClass, obj, classMap); if (xmlObject == null) { obj = null; } else { DeserializeObject(obj, classMap, xmlObject); } }
protected virtual XmlDocument CreateObjectXmlDocument(IClassMap classMap, string fileName) { ISourceMap sourceMap = classMap.GetDocSourceMap(); MemoryStream ms = new MemoryStream(); XmlTextWriter xmlWriter = new XmlTextWriter( ms, Encoding.GetEncoding(sourceMap.GetDocEncoding())); xmlWriter.Formatting = Formatting.Indented; xmlWriter.WriteStartDocument(false); xmlWriter.WriteDocType("Object", null, null, null); xmlWriter.WriteComment("Serialized object"); // do not localize xmlWriter.WriteStartElement(classMap.GetDocElement(), null); xmlWriter.WriteEndElement(); xmlWriter.WriteEndDocument(); //Write the XML to file and close the writer xmlWriter.Flush(); string xml = Encoding.GetEncoding("utf-8").GetString(ms.ToArray(), 0, (int)ms.Length); xmlWriter.Close(); //Obs fulhack!! Jag får ett konstigt skräptecken i början!! xml = xml.Substring(1); XmlDocument xmlDocument = new XmlDocument(); xmlDocument.LoadXml(xml); CachedXmlDocument cachedXmlDocument = new CachedXmlDocument(xmlDocument, fileName); cachedXmlDocuments[fileName] = cachedXmlDocument; return(xmlDocument); }
protected virtual XmlNode LoadObjectNodeFromDomainFile(object obj, IClassMap classMap) { string fileName = GetFileNamePerDomain(classMap); LogMessage message = new LogMessage("Saving object to file", "File: {0}, Object Type: {1}" , fileName , obj.GetType()); this.Context.LogManager.Debug(this, message); // do not localize XmlDocument xmlDoc; if (File.Exists(fileName)) xmlDoc = GetXmlDocument(fileName); else if (HasXmlDocument(fileName)) xmlDoc = GetXmlDocument(fileName); else xmlDoc = CreateDomainXmlDocument(classMap, fileName); XmlNode xmlRoot = xmlDoc.SelectSingleNode(classMap.GetDocSourceMap().GetDocRoot() ); XmlNode xmlClass = GetNode(xmlRoot, classMap.GetDocRoot() ); XmlNode xmlObject = GetNodeForObject(xmlClass, obj, classMap ); return xmlObject; }
protected virtual string GetDirectoryForDomain(IClassMap classMap) { ISourceMap sourceMap = classMap.GetDocSourceMap(); if (sourceMap == null) throw new MappingException("Can't find Document Source Map for '" + classMap.GetFullName() + "' in the npersist xml mapping file!"); // do not localize string directory = sourceMap.DocPath; if (!(Directory.Exists(directory))) throw new NPersistException("Can't find directory: " + directory); // do not localize return directory; }
protected virtual string GetDirectoryForClass(IClassMap classMap) { ISourceMap sourceMap = classMap.GetDocSourceMap(); if (sourceMap == null) throw new MappingException("Can't find Document Source Map for '" + classMap.GetFullName() + "' in the npersist xml mapping file!"); // do not localize string directory = sourceMap.DocPath; if (!(Directory.Exists(directory))) throw new NPersistException("Can't find directory: " + directory); // do not localize directory += @"\" + classMap.GetFullName(); if (!(Directory.Exists(directory))) { try { Directory.CreateDirectory(directory); } catch (Exception ex) { throw new NPersistException("Could not create directory '" + directory + "': " + ex.Message, ex); // do not localize } } return directory; }
protected virtual void DeserializeDomain(Type type, IList listToFill, IClassMap classMap, string xml) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(xml); XmlNode xmlRoot = xmlDoc.SelectSingleNode(classMap.GetDocSourceMap().GetDocRoot()); XmlNode xmlClass = xmlRoot.SelectSingleNode(classMap.GetDocRoot()); XmlNodeList xmlObjects = FindNodesForObjects(xmlClass, classMap); if (xmlObjects != null) { foreach (XmlNode xmlObject in xmlObjects) { object obj = InstantiateObject(type, classMap, xmlObject); DeserializeObject(obj, classMap, xmlObject); listToFill.Add(obj); } } }
protected virtual void DeserializeDomain(ref object obj, IClassMap classMap, string xml) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(xml); XmlNode xmlRoot = xmlDoc.SelectSingleNode(classMap.GetDocSourceMap().GetDocRoot()); XmlNode xmlClass = xmlRoot.SelectSingleNode(classMap.GetDocRoot()); XmlNode xmlObject = FindNodeForObject(xmlClass, obj, classMap); if (xmlObject == null) obj = null; else DeserializeObject(obj, classMap, xmlObject); }
protected virtual XmlDocument CreateObjectXmlDocument(IClassMap classMap, string fileName) { ISourceMap sourceMap = classMap.GetDocSourceMap(); MemoryStream ms = new MemoryStream() ; XmlTextWriter xmlWriter = new XmlTextWriter( ms, Encoding.GetEncoding( sourceMap.GetDocEncoding() )); xmlWriter.Formatting = Formatting.Indented; xmlWriter.WriteStartDocument(false); xmlWriter.WriteDocType("Object",null,null,null); xmlWriter.WriteComment("Serialized object"); // do not localize xmlWriter.WriteStartElement(classMap.GetDocElement(), null); xmlWriter.WriteEndElement(); xmlWriter.WriteEndDocument(); //Write the XML to file and close the writer xmlWriter.Flush(); string xml = Encoding.GetEncoding( "utf-8" ).GetString(ms.ToArray(),0,(int)ms.Length); xmlWriter.Close(); //Obs fulhack!! Jag får ett konstigt skräptecken i början!! xml = xml.Substring(1); XmlDocument xmlDocument = new XmlDocument() ; xmlDocument.LoadXml(xml); CachedXmlDocument cachedXmlDocument = new CachedXmlDocument(xmlDocument, fileName) ; cachedXmlDocuments[fileName] = cachedXmlDocument; return xmlDocument; }
protected virtual XmlDocument CreateDomainXmlDocument(IClassMap classMap, string fileName) { ISourceMap sourceMap = classMap.GetDocSourceMap(); MemoryStream ms = new MemoryStream() ; XmlTextWriter xmlWriter = new XmlTextWriter( ms, Encoding.GetEncoding( sourceMap.GetDocEncoding() )); xmlWriter.Formatting = Formatting.Indented; xmlWriter.WriteStartDocument(false); xmlWriter.WriteDocType("Domain",null,null,null); xmlWriter.WriteComment("Serialized domain objects"); // do not localize xmlWriter.WriteStartElement(sourceMap.GetDocRoot() , null); xmlWriter.WriteEndElement(); xmlWriter.WriteEndDocument(); //Write the XML to file and close the writer xmlWriter.Flush(); string xml = Encoding.GetEncoding( "utf-8" ).GetString(ms.ToArray(),0,(int)ms.Length); xmlWriter.Close(); //TODO: ugly hack!! Do this right way to avoid extra char in the beginning!! xml = xml.Substring(1); XmlDocument xmlDocument = new XmlDocument() ; xmlDocument.LoadXml(xml); CachedXmlDocument cachedXmlDocument = new CachedXmlDocument(xmlDocument, fileName) ; cachedXmlDocuments[fileName] = cachedXmlDocument; return xmlDocument; }