Exemplo n.º 1
0
        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);
            }
        }
Exemplo n.º 2
0
            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);
            }
Exemplo n.º 3
0
			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);
			}
Exemplo n.º 4
0
        /// <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();
        }
Exemplo n.º 5
0
        // 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);
            }
        }
Exemplo n.º 6
0
        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);
        }
Exemplo n.º 7
0
        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);
        }
Exemplo n.º 8
0
        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));
        }
Exemplo n.º 9
0
        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);
        }
Exemplo n.º 10
0
        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);
            }
        }
Exemplo n.º 11
0
        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);
        }
Exemplo n.º 12
0
        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);
            }
        }
Exemplo n.º 13
0
        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);
        }
Exemplo n.º 14
0
        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));
        }
Exemplo n.º 15
0
 private static void ImportStringKvp2(XPathNavigator xpBase, GxiProfile p,
                                      GxiContext c)
 {
     ImportStringKvpEx(xpBase, p, c, false);
 }
Exemplo n.º 16
0
        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();
        }
Exemplo n.º 17
0
        // 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); }
        }
Exemplo n.º 18
0
        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);
        }
Exemplo n.º 19
0
        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;
        }
Exemplo n.º 20
0
        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);
        }
Exemplo n.º 21
0
 private static void ImportStringKvp2(XPathNavigator xpBase, GxiProfile p,
     GxiContext c)
 {
     ImportStringKvpEx(xpBase, p, c, false);
 }
Exemplo n.º 22
0
        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);
        }