private static void ImportRoot(XPathNavigator xpBase, GxiProfile p, GxiContext c) { ImportObject(xpBase, p, p.GroupXPath, null, GxiImporter.ImportGroup, c); if (p.EntriesInRoot) { ImportObject(xpBase, p, p.EntryXPath, null, GxiImporter.ImportEntry, c); } }
public GxiContext(GxiProfile p, PwDatabase pd, PwGroup pg, PwEntry pe) { m_pd = pd; m_pg = pg; m_pe = pe; m_dStringKeyRepl = GxiImporter.ParseRepl(p.StringKeyRepl); m_dStringValueRepl = GxiImporter.ParseRepl(p.StringValueRepl); m_dStringKeyRepl2 = GxiImporter.ParseRepl(p.StringKeyRepl2); m_dStringValueRepl2 = GxiImporter.ParseRepl(p.StringValueRepl2); m_dBinaryKeyRepl = GxiImporter.ParseRepl(p.BinaryKeyRepl); }
/// <summary> /// Import a stream into a database. Throws an exception if an error /// occurs. Do not call the base class method when overriding it. /// </summary> /// <param name="pwStorage">Data storage into which the data will be imported.</param> /// <param name="sInput">Input stream to read the data from.</param> /// <param name="slLogger">Status logger. May be <c>null</c>.</param> public virtual void Import(PwDatabase pwStorage, Stream sInput, IStatusLogger slLogger) { GxiProfile p = this.XmlProfile; if (p != null) { if (pwStorage == null) { throw new ArgumentNullException("pwStorage"); } GxiImporter.Import(pwStorage.RootGroup, sInput, p, pwStorage, slLogger); return; } throw new NotSupportedException(); }
// private static string QueryValueSafe(XPathNavigator xpBase, string strXPath, // bool bQueryName) // { // return (QueryValue(xpBase, strXPath, bQueryName) ?? string.Empty); // } private static void ImportObject(XPathNavigator xpBase, GxiProfile p, string strXPath, string strAltXPath, ImportObjectDelegate f, GxiContext c) { if (f == null) { Debug.Assert(false); return; } XPathNodeIterator xi = QueryNodes(xpBase, strXPath, strAltXPath); if (xi == null) { return; // No assert } foreach (XPathNavigator xp in xi) { f(xp, p, c); } }
private static string Preprocess(string strDoc, GxiProfile p) { string str = strDoc; if (str == null) { Debug.Assert(false); return(string.Empty); } if (p.RemoveInvalidChars) { str = StrUtil.SafeXmlString(str); } if (p.DecodeHtmlEntities) { str = XmlUtil.DecodeHtmlEntities(str); } return(str); }
public static void Import(PwGroup pgStorage, Stream s, GxiProfile p, PwDatabase pdContext, IStatusLogger sl) { if(pgStorage == null) throw new ArgumentNullException("pgStorage"); if(s == null) throw new ArgumentNullException("s"); if(p == null) throw new ArgumentNullException("p"); if(pdContext == null) throw new ArgumentNullException("pdContext"); // sl may be null // Import into virtual group first, in order to realize // an all-or-nothing import PwGroup pgVirt = new PwGroup(true, true); try { ImportPriv(pgVirt, s, p, pdContext, sl); } finally { s.Close(); } foreach(PwGroup pg in pgVirt.Groups) pgStorage.AddGroup(pg, true); foreach(PwEntry pe in pgVirt.Entries) pgStorage.AddEntry(pe, true); }
private static void ImportBinaryKvp(XPathNavigator xpBase, GxiProfile p, GxiContext c) { string strKey = QueryValue(xpBase, p.BinaryKeyXPath, p.BinaryKeyUseName); if (string.IsNullOrEmpty(strKey)) { return; } strKey = ApplyRepl(strKey, c.BinaryKeyRepl); if (strKey.Length == 0) { return; } string strValue = QueryValueSafe(xpBase, p.BinaryValueXPath); byte[] pbValue = null; if (p.BinaryValueEncoding == GxiBinaryEncoding.Base64) { pbValue = Convert.FromBase64String(strValue); } else if (p.BinaryValueEncoding == GxiBinaryEncoding.Hex) { pbValue = MemUtil.HexStringToByteArray(strValue); } else { Debug.Assert(false); } if (pbValue == null) { return; } c.Entry.Binaries.Set(strKey, new ProtectedBinary(false, pbValue)); }
private static void ImportEntry(XPathNavigator xpBase, GxiProfile p, GxiContext c) { PwEntry pe = new PwEntry(true, true); PwGroup pg = c.Group; // Not the database root group string strGroupPath = QueryValueSafe(xpBase, p.EntryGroupXPath); string strGroupPath2 = QueryValueSafe(xpBase, p.EntryGroupXPath2); if ((strGroupPath.Length > 0) && (strGroupPath2.Length > 0)) { Debug.Assert(p.EntryGroupSep.Length > 0); strGroupPath = strGroupPath + p.EntryGroupSep + strGroupPath2; } if (strGroupPath.Length > 0) { if (p.EntryGroupSep.Length == 0) { pg = pg.FindCreateGroup(strGroupPath, true); } else { pg = pg.FindCreateSubTree(strGroupPath, new string[1] { p.EntryGroupSep }, true); } } pg.AddEntry(pe, true); GxiContext cSub = c.ModifyWith(pe); ImportObject(xpBase, p, p.StringKvpXPath, null, GxiImporter.ImportStringKvp, cSub); ImportObject(xpBase, p, p.StringKvpXPath2, null, GxiImporter.ImportStringKvp2, cSub); ImportObject(xpBase, p, p.BinaryKvpXPath, null, GxiImporter.ImportBinaryKvp, cSub); }
public static void Import(PwGroup pgStorage, Stream s, GxiProfile p, PwDatabase pdContext, IStatusLogger sl) { if (pgStorage == null) { throw new ArgumentNullException("pgStorage"); } if (s == null) { throw new ArgumentNullException("s"); } if (p == null) { throw new ArgumentNullException("p"); } if (pdContext == null) { throw new ArgumentNullException("pdContext"); } // sl may be null // Import into virtual group first, in order to realize // an all-or-nothing import PwGroup pgVirt = new PwGroup(true, true); try { ImportPriv(pgVirt, s, p, pdContext, sl); } finally { s.Close(); } foreach (PwGroup pg in pgVirt.Groups) { pgStorage.AddGroup(pg, true); } foreach (PwEntry pe in pgVirt.Entries) { pgStorage.AddEntry(pe, true); } }
private static void ImportStringKvpEx(XPathNavigator xpBase, GxiProfile p, GxiContext c, bool bFirst) { string strKey = QueryValue(xpBase, (bFirst ? p.StringKeyXPath : p.StringKeyXPath2), (bFirst ? p.StringKeyUseName : p.StringKeyUseName2)); if (string.IsNullOrEmpty(strKey)) { return; } strKey = ApplyRepl(strKey, (bFirst ? c.StringKeyRepl : c.StringKeyRepl2)); if (strKey.Length == 0) { return; } if (p.StringKeyToStd) { string strMapped = ImportUtil.MapNameToStandardField(strKey, p.StringKeyToStdFuzzy); if (!string.IsNullOrEmpty(strMapped)) { strKey = strMapped; } } string strValue = QueryValueSafe(xpBase, (bFirst ? p.StringValueXPath : p.StringValueXPath2)); strValue = ApplyRepl(strValue, (bFirst ? c.StringValueRepl : c.StringValueRepl2)); ImportUtil.AppendToField(c.Entry, strKey, strValue, c.Database); }
private static void ImportGroup(XPathNavigator xpBase, GxiProfile p, GxiContext c) { PwGroup pg = new PwGroup(true, true); c.Group.AddGroup(pg, true); GxiContext cSub = c.ModifyWith(pg); pg.Name = QueryValueSafe(xpBase, p.GroupNameXPath); if (pg.Name.Length == 0) { pg.Name = KPRes.Group; } if (p.GroupsInGroup) { ImportObject(xpBase, p, p.GroupXPath, null, GxiImporter.ImportGroup, cSub); } if (p.EntriesInGroup) { ImportObject(xpBase, p, p.EntryXPath, null, GxiImporter.ImportEntry, cSub); } }
private static void ImportGroup(XPathNavigator xpBase, GxiProfile p, GxiContext c) { PwGroup pg = new PwGroup(true, true); c.Group.AddGroup(pg, true); GxiContext cSub = c.ModifyWith(pg); pg.Name = QueryValueSafe(xpBase, p.GroupNameXPath); if(pg.Name.Length == 0) pg.Name = KPRes.Group; if(p.GroupsInGroup) ImportObject(xpBase, p, p.GroupXPath, null, GxiImporter.ImportGroup, cSub); if(p.EntriesInGroup) ImportObject(xpBase, p, p.EntryXPath, null, GxiImporter.ImportEntry, cSub); }
private static void ImportBinaryKvp(XPathNavigator xpBase, GxiProfile p, GxiContext c) { string strKey = QueryValue(xpBase, p.BinaryKeyXPath, p.BinaryKeyUseName); if(string.IsNullOrEmpty(strKey)) return; strKey = ApplyRepl(strKey, c.BinaryKeyRepl); if(strKey.Length == 0) return; string strValue = QueryValueSafe(xpBase, p.BinaryValueXPath); byte[] pbValue = null; if(p.BinaryValueEncoding == GxiBinaryEncoding.Base64) pbValue = Convert.FromBase64String(strValue); else if(p.BinaryValueEncoding == GxiBinaryEncoding.Hex) pbValue = MemUtil.HexStringToByteArray(strValue); else { Debug.Assert(false); } if(pbValue == null) return; c.Entry.Binaries.Set(strKey, new ProtectedBinary(false, pbValue)); }
private static void ImportStringKvp2(XPathNavigator xpBase, GxiProfile p, GxiContext c) { ImportStringKvpEx(xpBase, p, c, false); }
private static void ImportPriv(PwGroup pgStorage, Stream s, GxiProfile p, PwDatabase pdContext, IStatusLogger sl) { StrEncodingInfo sei = StrUtil.GetEncoding(p.Encoding); StreamReader srRaw; if((sei != null) && (sei.Encoding != null)) srRaw = new StreamReader(s, sei.Encoding, true); else srRaw = new StreamReader(s, true); string strDoc = srRaw.ReadToEnd(); srRaw.Close(); strDoc = Preprocess(strDoc, p); StringReader srDoc = new StringReader(strDoc); XPathDocument xd = new XPathDocument(srDoc); GxiContext c = new GxiContext(p, pdContext, pgStorage, null); XPathNavigator xpDoc = xd.CreateNavigator(); ImportObject(xpDoc, p, p.RootXPath, "/*", GxiImporter.ImportRoot, c); srDoc.Close(); }
// private static string QueryValueSafe(XPathNavigator xpBase, string strXPath, // bool bQueryName) // { // return (QueryValue(xpBase, strXPath, bQueryName) ?? string.Empty); // } private static void ImportObject(XPathNavigator xpBase, GxiProfile p, string strXPath, string strAltXPath, ImportObjectDelegate f, GxiContext c) { if(f == null) { Debug.Assert(false); return; } XPathNodeIterator xi = QueryNodes(xpBase, strXPath, strAltXPath); if(xi == null) return; // No assert foreach(XPathNavigator xp in xi) { f(xp, p, c); } }
private static void ImportEntry(XPathNavigator xpBase, GxiProfile p, GxiContext c) { PwEntry pe = new PwEntry(true, true); PwGroup pg = c.Group; // Not the database root group string strGroupPath = QueryValueSafe(xpBase, p.EntryGroupXPath); string strGroupPath2 = QueryValueSafe(xpBase, p.EntryGroupXPath2); if((strGroupPath.Length > 0) && (strGroupPath2.Length > 0)) { Debug.Assert(p.EntryGroupSep.Length > 0); strGroupPath = strGroupPath + p.EntryGroupSep + strGroupPath2; } if(strGroupPath.Length > 0) { if(p.EntryGroupSep.Length == 0) pg = pg.FindCreateGroup(strGroupPath, true); else pg = pg.FindCreateSubTree(strGroupPath, new string[1]{ p.EntryGroupSep }, true); } pg.AddEntry(pe, true); GxiContext cSub = c.ModifyWith(pe); ImportObject(xpBase, p, p.StringKvpXPath, null, GxiImporter.ImportStringKvp, cSub); ImportObject(xpBase, p, p.StringKvpXPath2, null, GxiImporter.ImportStringKvp2, cSub); ImportObject(xpBase, p, p.BinaryKvpXPath, null, GxiImporter.ImportBinaryKvp, cSub); }
private static string Preprocess(string strDoc, GxiProfile p) { string str = strDoc; if(str == null) { Debug.Assert(false); return string.Empty; } if(p.RemoveInvalidChars) str = StrUtil.SafeXmlString(str); if(p.DecodeHtmlEntities) str = XmlUtil.DecodeHtmlEntities(str); return str; }
private static void ImportStringKvpEx(XPathNavigator xpBase, GxiProfile p, GxiContext c, bool bFirst) { string strKey = QueryValue(xpBase, (bFirst ? p.StringKeyXPath : p.StringKeyXPath2), (bFirst ? p.StringKeyUseName : p.StringKeyUseName2)); if(string.IsNullOrEmpty(strKey)) return; strKey = ApplyRepl(strKey, (bFirst ? c.StringKeyRepl : c.StringKeyRepl2)); if(strKey.Length == 0) return; if(p.StringKeyToStd) { string strMapped = ImportUtil.MapNameToStandardField(strKey, p.StringKeyToStdFuzzy); if(!string.IsNullOrEmpty(strMapped)) strKey = strMapped; } string strValue = QueryValueSafe(xpBase, (bFirst ? p.StringValueXPath : p.StringValueXPath2)); strValue = ApplyRepl(strValue, (bFirst ? c.StringValueRepl : c.StringValueRepl2)); ImportUtil.AppendToField(c.Entry, strKey, strValue, c.Database); }
private static void ImportRoot(XPathNavigator xpBase, GxiProfile p, GxiContext c) { ImportObject(xpBase, p, p.GroupXPath, null, GxiImporter.ImportGroup, c); if(p.EntriesInRoot) ImportObject(xpBase, p, p.EntryXPath, null, GxiImporter.ImportEntry, c); }