public bool Load(string fn, bool indirectLoadSave = false)
        {
            this.fn = fn;
            if (this.openPackage != null)
            {
                this.openPackage.Close();
            }
            this.openPackage = null;

            if (fn.ToLower().EndsWith(".xml"))
            {
                // load only XML
                try
                {
                    var reader = new StreamReader(fn);
                    // TODO: use aasenv serialzers here!
                    this.aasenv = AdminShellSerializationHelper.DeserializeXmlFromStreamWithCompat(reader.BaseStream);
                    if (this.aasenv == null)
                    {
                        throw (new Exception("Type error for XML file!"));
                    }
                    reader.Close();
                }
                catch (Exception ex)
                {
                    throw (new Exception(string.Format("While reading AAS {0} at {1} gave: {2}", fn, AdminShellUtil.ShortLocation(ex), ex.Message)));
                }
                return(true);
            }

            if (fn.ToLower().EndsWith(".json"))
            {
                // load only JSON
                try
                {
                    using (StreamReader file = System.IO.File.OpenText(fn))
                    {
                        // TODO: use aasenv serialzers here!
                        JsonSerializer serializer = new JsonSerializer();
                        serializer.Converters.Add(new AdminShellConverters.JsonAasxConverter("modelType", "name"));
                        this.aasenv = (AdminShell.AdministrationShellEnv)serializer.Deserialize(file, typeof(AdminShell.AdministrationShellEnv));
                    }
                }
                catch (Exception ex)
                {
                    throw (new Exception(string.Format("While reading AAS {0} at {1} gave: {2}", fn, AdminShellUtil.ShortLocation(ex), ex.Message)));
                }
                return(true);
            }

            if (fn.ToLower().EndsWith(".aasx"))
            {
                var fnToLoad = fn;
                this.tempFn = null;
                if (indirectLoadSave)
                {
                    try
                    {
                        this.tempFn = System.IO.Path.GetTempFileName().Replace(".tmp", ".aasx");
                        System.IO.File.Copy(fn, this.tempFn);
                        fnToLoad = this.tempFn;
                    }
                    catch (Exception ex)
                    {
                        throw (new Exception(string.Format("While copying AASX {0} for indirect load at {1} gave: {2}", fn, AdminShellUtil.ShortLocation(ex), ex.Message)));
                    }
                }

                string nsuri = "";

                // for (int loop = 0; loop < 2; loop++)
                for (int loop = 1; loop < 2; loop++)
                {
                    // load package AASX
                    try
                    {
                        Package package = null;
                        switch (loop)
                        {
                        case 0:
                            package = Package.Open(fnToLoad, FileMode.Open);
                            break;

                        case 1:
                            package = Package.Open(fnToLoad, FileMode.Open, FileAccess.Read, FileShare.Read);
                            break;
                        }

                        // get the origin from the package
                        PackagePart originPart = null;
                        var         xs         = package.GetRelationshipsByType("http://www.admin-shell.io/aasx/relationships/aasx-origin");
                        foreach (var x in xs)
                        {
                            if (x.SourceUri.ToString() == "/")
                            {
                                originPart = package.GetPart(x.TargetUri);
                                break;
                            }
                        }
                        if (originPart == null)
                        {
                            throw (new Exception("Unable to find AASX origin. Aborting!"));
                        }

                        // get the specs from the package
                        PackagePart specPart = null;
                        xs = originPart.GetRelationshipsByType("http://www.admin-shell.io/aasx/relationships/aas-spec");
                        foreach (var x in xs)
                        {
                            specPart = package.GetPart(x.TargetUri);
                            break;
                        }
                        if (specPart == null)
                        {
                            throw (new Exception("Unable to find AASX spec(s). Aborting!"));
                        }

                        // open spec part to read
                        try
                        {
                            if (specPart.Uri.ToString().ToLower().Trim().EndsWith("json"))
                            {
                                using (var s = specPart.GetStream(FileMode.Open))
                                {
                                    using (StreamReader file = new StreamReader(s))
                                    {
                                        JsonSerializer serializer = new JsonSerializer();
                                        serializer.Converters.Add(new AdminShellConverters.JsonAasxConverter("modelType", "name"));
                                        this.aasenv = (AdminShell.AdministrationShellEnv)serializer.Deserialize(file, typeof(AdminShell.AdministrationShellEnv));
                                        file.Close();
                                    }
                                    s.Close();
                                }
                                loop = 2;
                            }
                            else
                            {
                                // using (var s = specPart.GetStream(FileMode.Open))
                                var s = specPart.GetStream(FileMode.Open);

                                {
                                    // own catch loop to be more specific

                                    if (loop == 0)
                                    {
                                        nsuri = AdminShellSerializationHelper.TryReadXmlFirstElementNamespaceURI(s);
                                        s.Close();
                                        package.Close();
                                        continue;
                                    }
                                    // neu
                                    nsuri = AdminShellSerializationHelper.TryReadXmlFirstElementNamespaceURI(s);
                                    s.Close();
                                    s = specPart.GetStream(FileMode.Open, FileAccess.Read);

                                    // read V1.0?
                                    if (nsuri != null && nsuri.Trim() == "http://www.admin-shell.io/aas/1/0")
                                    {
#if UseAasxCompatibilityModels
                                        XmlSerializer serializer = new XmlSerializer(typeof(AasxCompatibilityModels.AdminShellV10.AdministrationShellEnv), "http://www.admin-shell.io/aas/1/0");
                                        var           v10        = serializer.Deserialize(s) as AasxCompatibilityModels.AdminShellV10.AdministrationShellEnv;
                                        this.aasenv = new AdminShellV20.AdministrationShellEnv(v10);
#else
                                        throw (new Exception("Cannot handle AAS file format http://www.admin-shell.io/aas/1/0 !"));
#endif
                                    }

                                    // read V2.0?
                                    if (nsuri != null && nsuri.Trim() == "http://www.admin-shell.io/aas/2/0")
                                    {
                                        XmlSerializer serializer = new XmlSerializer(typeof(AdminShell.AdministrationShellEnv), "http://www.admin-shell.io/aas/2/0");
                                        this.aasenv = serializer.Deserialize(s) as AdminShell.AdministrationShellEnv;
                                    }



                                    this.openPackage = package;
                                    if (this.aasenv == null)
                                    {
                                        throw (new Exception("Type error for XML file!"));
                                    }
                                    s.Close();
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            throw (new Exception(string.Format("While reading AAS {0} spec at {1} gave: {2}", fn, AdminShellUtil.ShortLocation(ex), ex.Message)));
                        }
                        // Package must not close to read e.g. thumbnails
                        //// package.Close();
                    }
                    catch (Exception ex)
                    {
                        throw (new Exception(string.Format("While reading AASX {0} at {1} gave: {2}", fn, AdminShellUtil.ShortLocation(ex), ex.Message)));
                    }
                }
                return(true);
            }

            // Don't know to handle
            throw (new Exception(string.Format($"Not able to handle {fn}.")));
        }
Example #2
0
        public bool Load(string fn, bool indirectLoadSave = false)
        {
            this.fn = fn;
            if (this.openPackage != null)
            {
                this.openPackage.Close();
            }
            this.openPackage = null;

            if (fn.ToLower().EndsWith(".xml"))
            {
                // load only XML
                try
                {
                    var reader = new StreamReader(fn);
                    this.aasenv = AdminShellSerializationHelper.DeserializeXmlFromStreamWithCompat(reader.BaseStream);
                    if (this.aasenv == null)
                    {
                        throw (new Exception("Type error for XML file!"));
                    }
                    reader.Close();
                }
                catch (Exception ex)
                {
                    throw (new Exception(
                               string.Format("While reading AAS {0} at {1} gave: {2}",
                                             fn, AdminShellUtil.ShortLocation(ex), ex.Message)));
                }
                return(true);
            }

            if (fn.ToLower().EndsWith(".json"))
            {
                // load only JSON
                try
                {
                    using (StreamReader file = System.IO.File.OpenText(fn))
                    {
                        // TODO (Michael Hoffmeister, 2020-08-01): use a unified function to create a serializer
                        JsonSerializer serializer = new JsonSerializer();
                        serializer.Converters.Add(new AdminShellConverters.JsonAasxConverter("modelType", "name"));
                        this.aasenv = (AdminShell.AdministrationShellEnv)serializer.Deserialize(
                            file, typeof(AdminShell.AdministrationShellEnv));
                    }
                }
                catch (Exception ex)
                {
                    throw (new Exception(
                               string.Format("While reading AAS {0} at {1} gave: {2}",
                                             fn, AdminShellUtil.ShortLocation(ex), ex.Message)));
                }
                return(true);
            }

            if (fn.ToLower().EndsWith(".aasx"))
            {
                var fnToLoad = fn;
                this.tempFn = null;
                if (indirectLoadSave)
                {
                    try
                    {
                        this.tempFn = System.IO.Path.GetTempFileName().Replace(".tmp", ".aasx");
                        System.IO.File.Copy(fn, this.tempFn);
                        fnToLoad = this.tempFn;
                    }
                    catch (Exception ex)
                    {
                        throw (new Exception(
                                   string.Format("While copying AASX {0} for indirect load at {1} gave: {2}",
                                                 fn, AdminShellUtil.ShortLocation(ex), ex.Message)));
                    }
                }

                // load package AASX
                try
                {
                    var package = Package.Open(fnToLoad, FileMode.Open);

                    // get the origin from the package
                    PackagePart originPart = null;
                    var         xs         = package.GetRelationshipsByType(
                        "http://www.admin-shell.io/aasx/relationships/aasx-origin");
                    foreach (var x in xs)
                    {
                        if (x.SourceUri.ToString() == "/")
                        {
                            originPart = package.GetPart(x.TargetUri);
                            break;
                        }
                    }
                    if (originPart == null)
                    {
                        throw (new Exception("Unable to find AASX origin. Aborting!"));
                    }

                    // get the specs from the package
                    PackagePart specPart = null;
                    xs = originPart.GetRelationshipsByType("http://www.admin-shell.io/aasx/relationships/aas-spec");
                    foreach (var x in xs)
                    {
                        specPart = package.GetPart(x.TargetUri);
                        break;
                    }
                    if (specPart == null)
                    {
                        throw (new Exception("Unable to find AASX spec(s). Aborting!"));
                    }

                    // open spec part to read
                    try
                    {
                        if (specPart.Uri.ToString().ToLower().Trim().EndsWith("json"))
                        {
                            using (var s = specPart.GetStream(FileMode.Open))
                            {
                                using (StreamReader file = new StreamReader(s))
                                {
                                    JsonSerializer serializer = new JsonSerializer();
                                    serializer.Converters.Add(
                                        new AdminShellConverters.JsonAasxConverter("modelType", "name"));
                                    this.aasenv = (AdminShell.AdministrationShellEnv)serializer.Deserialize(
                                        file, typeof(AdminShell.AdministrationShellEnv));
                                }
                            }
                        }
                        else
                        {
                            using (var s = specPart.GetStream(FileMode.Open))
                            {
                                // own catch loop to be more specific
                                this.aasenv      = AdminShellSerializationHelper.DeserializeXmlFromStreamWithCompat(s);
                                this.openPackage = package;
                                if (this.aasenv == null)
                                {
                                    throw (new Exception("Type error for XML file!"));
                                }
                                s.Close();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw (new Exception(
                                   string.Format("While reading AAS {0} spec at {1} gave: {2}",
                                                 fn, AdminShellUtil.ShortLocation(ex), ex.Message)));
                    }
                }
                catch (Exception ex)
                {
                    throw (new Exception(
                               string.Format("While reading AASX {0} at {1} gave: {2}",
                                             fn, AdminShellUtil.ShortLocation(ex), ex.Message)));
                }
                return(true);
            }

            // Don't know to handle
            throw (new Exception(string.Format($"Not able to handle {fn}.")));
        }