Exemplo n.º 1
0
    /// <summary>
    /// Load all wiring documents in a *.phon file.
    /// </summary>
    /// <param name="doc">The XML document in a phon format.</param>
    /// <param name="wdActive">The active found document.</param>
    /// <returns>True if the load was successful, else false.</returns>
    public bool LoadDocument(System.Xml.XmlDocument doc, bool clearFirst)
    {
        if (clearFirst == true)
        {
            this.Clear();
        }

        System.Xml.XmlElement root = doc.DocumentElement;

        // We have a seperate copy of what's loaded because if we're appending
        // (if clearFirst is false) we could have other stuff in there. We need
        // a list of only what was created from this load.
        List <WiringDocument> loaded = new List <WiringDocument>();
        //
        Dictionary <string, WiringDocument> loadedByID =
            new Dictionary <string, WiringDocument>();

        foreach (System.Xml.XmlElement ele in root)
        {
            if (ele.Name == "message")
            {
                this.documentMessages.Add(ele.InnerText);
            }
            else if (ele.Name == WiringDocument.xmlName)
            {
                string guid = string.Empty;
                System.Xml.XmlAttribute attrGUID = ele.Attributes["id"];
                if (attrGUID != null)
                {
                    guid = attrGUID.Value;
                }

                WiringDocument wd = new WiringDocument(false, guid);
                if (wd.LoadXML(ele) == false)
                {
                    continue;
                }

                loaded.Add(wd);
                loadedByID.Add(wd.guid, wd);

                this.AddDocument(wd);
            }
        }

        foreach (WiringDocument wd in loaded)
        {
            Dictionary <string, LLDNBase> dirLocal = new Dictionary <string, LLDNBase>();
            foreach (LLDNBase gb in wd.Generators)
            {
                dirLocal.Add(gb.GUID, gb);
            }

            foreach (LLDNBase gb in wd.Generators)
            {
                gb.PostLoadXML(dirLocal, loadedByID);
            }
        }

        System.Xml.XmlAttribute attrActive = root.Attributes["active"];
        if (attrActive != null)
        {
            string activeGUID = attrActive.Value;
            foreach (WiringDocument wd in loaded)
            {
                if (wd.guid == activeGUID)
                {
                    this.activeDocument = wd;
                    break;
                }
            }
        }

        this.EnsureActive();

        return(true);
    }