public static async Task<MetaMetadataRepository> ReadDirectoryRecursively(MetaMetadataRepository mainRepo, String path, SimplTypesScope mmdTScope, SimplTypesScope metadataTScope)
        {
            Stack<string> stack = new Stack<string>();
            stack.Push(path);
            while(stack.Count > 0)
            {
                string dir = stack.Pop();
                Debug.WriteLine("Looking in : " + dir);
                //String[] files = Directory.GetFiles(dir, "*.xml");
                string[] files = await FundamentalPlatformSpecifics.Get().GetFilesFromDirectory(dir, ".xml");
                foreach (string file in files)
                {
                    MetaMetadataRepository thatRepo = await ReadRepository(file, mmdTScope, metadataTScope, mainRepo);
                    // if (thatRepo != null)
                    //    mainRepo.IntegrateRepository(thatRepo);

                    //string[] directories = Directory.GetDirectories(dir);
                    //                                           string[] directories = await FundamentalPlatformSpecifics.Get().GetDirectoriesFromDirectory(dir);
                    //                                           foreach (
                    //                                                    string innerDir in directories.Where(
                    //                                                                                          innerDir =>
                    //                                                                                            !innerDir.Contains(".svn")))
                    //                                                                                    stack.Push(innerDir);
                    //                                                                            });
                }
                
            }

            return mainRepo;
        }
        public virtual async Task <Document> GetOrConstructDocument(ParsedUri location)
        {
            var doc = await GetDocument(location);

            if (doc == null && MetaMetadataRepository != null)
            {
                doc = MetaMetadataRepository.ConstructDocument(location, false);
                if (doc != null)
                {
                    GlobalDocumentCollection.AddDocument(doc, location);
                }
            }

            return(doc);
        }
Пример #3
0
        private MetaMetadataCompositeField GetMetaMetadata()
        {
            MetaMetadataCompositeField mm = _metaMetadata;

            //mm = await MetaMetadataTranslationScope.Get().Deserialize()
            if (_repository == null)
            {
                _repository = MetaMetadataRepositoryInit.GetRepository();
            }

            if (mm == null && _repository != null)
            {
                if (this.MetaMetadataName != null)  // get from saved composition
                {
                    mm = _repository.GetMMByName(this.MetaMetadataName.Value);
                }

                if (mm == null)
                {
                    ParsedUri location = Location == null ? null : Location.Value;
                    if (location != null)
                    {
                        mm = IsImage ? _repository.GetImageMM(location) : _repository.GetDocumentMM(location);

                        // TODO -- also try to resolve by mime type ???
                    }
                    if (mm == null)
                    {
                        mm = _repository.GetByClass(this.GetType());
                    }
                    if (mm == null && MetadataClassDescriptor != null)
                    {
                        mm = _repository.GetMMByName(MetadataClassDescriptor.TagName);
                    }
                }

                if (mm != null)
                {
                    MetaMetadata = mm;
                }
            }
            return(mm);
        }
        public void ValidateRepo(MetaMetadataRepository repo)
        {
            Console.WriteLine(repo.RepositoryByName.Count);
            List<string> mmdNames = new List<string>(repo.RepositoryByName.Keys);
            mmdNames.Sort();
            foreach (string mmdName in mmdNames)
            {
                Console.WriteLine(mmdName);
            }
            Console.WriteLine("--------------------");

            Assert.IsTrue(repo.RepositoryByName.Count > 200);
            Assert.IsNotNull(repo.GetMMByName("metadata"));
            Assert.IsNotNull(repo.GetMMByName("document"));
            Assert.IsNotNull(repo.GetMMByName("amazon_product"));
            Assert.IsNotNull(repo.GetMMByName("google_search"));
            Assert.IsNotNull(repo.GetMMByName("acm_portal"));
            Assert.IsNotNull(repo.GetMMByName("scholarly_article"));
        }
        public void ValidateRepo(MetaMetadataRepository repo)
        {
            Console.WriteLine(repo.RepositoryByName.Count);
            List <string> mmdNames = new List <string>(repo.RepositoryByName.Keys);

            mmdNames.Sort();
            foreach (string mmdName in mmdNames)
            {
                Console.WriteLine(mmdName);
            }
            Console.WriteLine("--------------------");

            Assert.IsTrue(repo.RepositoryByName.Count > 200);
            Assert.IsNotNull(repo.GetMMByName("metadata"));
            Assert.IsNotNull(repo.GetMMByName("document"));
            Assert.IsNotNull(repo.GetMMByName("amazon_product"));
            Assert.IsNotNull(repo.GetMMByName("google_search"));
            Assert.IsNotNull(repo.GetMMByName("acm_portal"));
            Assert.IsNotNull(repo.GetMMByName("scholarly_article"));
        }
        private void WebView_LoadCompleted(object sender, EventArgs e)
        {
            //if (Source == null || BLANK_PAGE.Equals(Source))// || loadingComplete)
            //   return;
            WebView webView = sender as WebView;

            if (webView == null)
            {
                return;
            }
            webView.Stop(); // Stopping further requests.
            Console.WriteLine("Finished loading in " + DateTime.Now.Subtract(timeStart).TotalMilliseconds + "ms. Executing javascript. -- ");
            Console.WriteLine("======================================= Time To Load : " + DateTime.Now.Subtract(timeStart).TotalMilliseconds);
            parseStart = DateTime.Now;
            MetaMetadataRepository repository = SemanticsSessionScope.MetaMetadataRepository;
            MetaMetadata           mmd        = repository.GetDocumentMM(_puri);

            Console.WriteLine("Got MMD: " + mmd.Name);
            String jsonMMD = WebBrowserPool.GetJsonMMD(mmd);

            //Console.WriteLine("json:\n" + jsonMMD + "\n");
            //jsonMMD = jsonMMD.Replace("\\", "\\\\");

            webView.JSConsoleMessageAdded += new JSConsoleMessageAddedEventHandler(JSConsoleEvent);

            webView.ExecuteJavascript(jsonMMD);
            webView.ExecuteJavascript(WebBrowserPool.MmdDomHelperJsString);
            Console.WriteLine("Done js code execution, calling function. --" + DateTime.Now + " : " + DateTime.Now.Millisecond);
            //TODO: Currently executes asynchronously. Can we make this asynchronous?

            webView.CreateObject("CallBack");
            webView.SetObjectCallback("CallBack", "MetadataExtracted", OnMetadataExtracted);

            //webView.ExecuteJavascript("CallBack.MetadataExtracted('some value');");
            //Return happens through the task, on completion of the method via the callback
            webView.ExecuteJavascript("extractMetadataWithCallback(mmd);");

            _requestTimedOut.Start();
        }
Пример #7
0
        public void Connect()
        {
            DocumentClosureConnectionHelper documentClosureConnectionHelper = new DocumentClosureConnectionHelper(SemanticsSessionScope, Document, this);

            Debug.WriteLine("Connect running from thread: " + Task.CurrentId);
            MetaMetadataCompositeField metaMetadata = Document.MetaMetadata;

            // then try to create a connection using the PURL
            string    userAgentString = metaMetadata.UserAgentString;
            ParsedUri originalPURL    = Document.Location.Value;

            PURLConnection = new PURLConnection(originalPURL);
            if (originalPURL.IsFile)
            {
                // TODO handle local files here!
                var file = PURLConnection.File;
                if (SemanticsPlatformSpecifics.Get().FileIsADictionary(file))
                {
                    // TODO FileDirectoryParser
                    // DocumentParser = DocumentParser.GetDocumentParser(FILE_DIRECTORY_PARSER);
                }
                else
                {
                    PURLConnection.FileConnect();
                    // we already have the correct meta-metadata, having used suffix to construct, or having gotten it from a restore.
                }
            }
            else
            {
                PURLConnection.NetworkConnect(documentClosureConnectionHelper, userAgentString); // HERE!
                if (PURLConnection.Good)
                {
                    Document document = this.Document; // may have changed during redirect processing
                    metaMetadata = document.MetaMetadata;

                    // check for a parser that was discovered while processing a re-direct

                    // if we made PURL connection but could not find parser using container
                    if ((PURLConnection != null) && !originalPURL.IsFile)
                    {
                        string cacheValue = PURLConnection.Response.Headers == null
                                                ? null
                                                : PURLConnection.Response.Headers["X-Cache"];
                        bool cacheHit = cacheValue != null && cacheValue.Contains("HIT");
                        if (metaMetadata.IsGenericMetadata)
                        {
                            // see if we can find more specifc meta-metadata using mimeType
                            MetaMetadataRepository repository = SemanticsSessionScope.MetaMetadataRepository;
                            string       mimeType             = PURLConnection.MimeType;
                            MetaMetadata mimeMmd = mimeType == null ? null : repository.GetMMByMime(mimeType);
                            if (mimeMmd != null && !mimeMmd.Equals(metaMetadata))
                            {
                                // new meta-metadata!
                                if (!mimeMmd.MetadataClass.GetTypeInfo().IsAssignableFrom(document.GetType().GetTypeInfo()))
                                //if (!mimeMmd.MetadataClass.IsAssignableFrom(document.GetType()))
                                {
                                    // more specifc so we need new metadata!
                                    document = (Document)(mimeMmd).ConstructMetadata();
                                    // set temporary on stack
                                    ChangeDocument(document);
                                }
                                metaMetadata = mimeMmd;
                            }
                        }
                    }
                }
            }

            if (DocumentParser == null)
            {
                DocumentParser = DocumentParser.GetDocumentParser(metaMetadata.Parser);
            }
            if (DocumentParser != null)
            {
                DocumentParser.FillValues(SemanticsSessionScope, PURLConnection, metaMetadata, this);
            }
            else
            {
                Debug.WriteLine("WARNING: no parser found: " + metaMetadata);
            }
//            else if (!DocumentParser.isRegisteredNoParser(PURLConnection.getPurl()))
//            {
//                warning("No DocumentParser found: " + metaMetadata);
//            }
        }
        public void TestArticlesInheritanceRelationships()
        {
            MetaMetadataRepositoryLoader loader             = new MetaMetadataRepositoryLoader();
            SimplTypesScope        metadataTranslationScope = RepositoryMetadataTranslationScope.Get();
            SimplTypesScope        mmdTScope  = MetaMetadataTranslationScope.Get();
            MetaMetadataRepository repository = MetaMetadataRepositoryLoader.ReadDirectoryRecursively(
                "../../../UnitTests/Data/TestRepository",
                mmdTScope,
                metadataTranslationScope);

            repository.TraverseAndInheritMetaMetadata();

            MetaMetadata metadata = repository.GetMMByName("metadata");

            Assert.IsNull(metadata.InheritedMmd);
            Assert.IsTrue(metadata.MmdScope == null || metadata.MmdScope.Count == 0);
            // meta_metadata_name
            MetaMetadataScalarField metadata__meta_metadata_name = (MetaMetadataScalarField)metadata.GetChildMetaMetadata()["meta_metadata_name"];

            Assert.IsNull(metadata__meta_metadata_name.InheritedField);
            Assert.AreSame(metadata, metadata__meta_metadata_name.DeclaringMmd);
            Assert.AreEqual(/*typeof(MetadataStringScalarType).Name*/ "MetadataString", metadata__meta_metadata_name.ScalarTypeP.SimplName);
            // mixins
            MetaMetadataCollectionField metadata__mixins = (MetaMetadataCollectionField)metadata.GetChildMetaMetadata()["mixins"];

            Assert.IsNull(metadata__mixins.InheritedField);
            Assert.AreSame(metadata, metadata__mixins.DeclaringMmd);
            Assert.AreSame(metadata, metadata__mixins.InheritedMmd);

            MetaMetadata document = repository.GetMMByName("document");

            Assert.AreSame(metadata, document.InheritedMmd);
            Assert.IsTrue(document.MmdScope == null || document.MmdScope.Count == 0);
            Assert.AreEqual(metadata__meta_metadata_name, document.GetChildMetaMetadata()["meta_metadata_name"]);
            Assert.AreEqual(metadata__mixins, document.GetChildMetaMetadata()["mixins"]);
            // location
            MetaMetadataScalarField document__location = (MetaMetadataScalarField)document.GetChildMetaMetadata()["location"];

            Assert.IsNull(document__location.InheritedField);
            Assert.AreSame(document, document__location.DeclaringMmd);
            // additional_locations
            MetaMetadataCollectionField document__additional_locations = (MetaMetadataCollectionField)document.GetChildMetaMetadata()["additional_locations"];

            Assert.IsNull(document__additional_locations.InheritedField);
            Assert.AreSame(document, document__additional_locations.DeclaringMmd);
            Assert.IsNull(document__additional_locations.InheritedMmd);

            MetaMetadata article = repository.GetMMByName("article");
            MetaMetadata author  = article.MmdScope["author"];

            Assert.AreSame(metadata, author.InheritedMmd);
            Assert.AreEqual(metadata__meta_metadata_name, author.GetChildMetaMetadata()["meta_metadata_name"]);
            Assert.AreEqual(metadata__mixins, author.GetChildMetaMetadata()["mixins"]);
            // name
            MetaMetadataScalarField author__name = (MetaMetadataScalarField)author.GetChildMetaMetadata()["name"];

            Assert.IsNull(author__name.InheritedField);
            Assert.AreSame(author, author__name.DeclaringMmd);
            // affiliation
            MetaMetadataScalarField author__affiliation = (MetaMetadataScalarField)author.GetChildMetaMetadata()["affiliation"];

            //Assert.IsNull(author__affiliation.InheritedField);
            Assert.AreSame(author, author__affiliation.DeclaringMmd);

            MetaMetadata source = article.MmdScope["source"];

            Assert.AreSame(document, source.InheritedMmd);
            Assert.AreEqual(metadata__meta_metadata_name, source.GetChildMetaMetadata()["meta_metadata_name"]);
            Assert.AreEqual(metadata__mixins, source.GetChildMetaMetadata()["mixins"]);
            Assert.AreEqual(document__additional_locations, source.GetChildMetaMetadata()["additional_locations"]);
            // archive_name
            MetaMetadataScalarField source__archive_name = (MetaMetadataScalarField)source.GetChildMetaMetadata()["archive_name"];

            Assert.IsNull(source__archive_name.InheritedField);
            Assert.AreSame(source, source__archive_name.DeclaringMmd);
            // location
            MetaMetadataScalarField source__location = (MetaMetadataScalarField)source.GetChildMetaMetadata()["location"];

            Assert.AreSame(document__location, source__location.InheritedField);
            Assert.IsFalse(document__location.Hide);
            Assert.IsTrue(source__location.Hide);
            // year_of_publication
            MetaMetadataScalarField source__year_of_publication = (MetaMetadataScalarField)source.GetChildMetaMetadata()["year_of_publication"];

            Assert.IsNull(source__year_of_publication.InheritedField);
            Assert.AreSame(source, source__year_of_publication.DeclaringMmd);
            // isbn
            MetaMetadataScalarField source__isbn = (MetaMetadataScalarField)source.GetChildMetaMetadata()["isbn"];

            Assert.IsNull(source__isbn.InheritedField);
            Assert.AreSame(source, source__isbn.DeclaringMmd);

            Assert.AreSame(document, article.InheritedMmd);
            Assert.IsTrue(article.MmdScope.Count == 2);
            Assert.AreEqual(metadata__meta_metadata_name, article.GetChildMetaMetadata()["meta_metadata_name"]);
            Assert.AreEqual(metadata__mixins, article.GetChildMetaMetadata()["mixins"]);
            Assert.AreEqual(document__location, article.GetChildMetaMetadata()["location"]);
            Assert.AreEqual(document__additional_locations, article.GetChildMetaMetadata()["additional_locations"]);
            // title
            MetaMetadataScalarField article__title = (MetaMetadataScalarField)article.GetChildMetaMetadata()["title"];

            Assert.IsNull(article__title.InheritedField);
            Assert.AreSame(article, article__title.DeclaringMmd);
            // authors
            MetaMetadataCollectionField article__authors = (MetaMetadataCollectionField)article.GetChildMetaMetadata()["authors"];

            Assert.IsNull(article__authors.InheritedField);
            Assert.AreSame(article, article__authors.DeclaringMmd);
            Assert.AreSame(author, article__authors.InheritedMmd);
            // source
            MetaMetadataCompositeField article__source = (MetaMetadataCompositeField)article.GetChildMetaMetadata()["source"];

            Assert.IsNull(article__source.InheritedField);
            Assert.AreSame(article, article__source.DeclaringMmd);
            Assert.AreSame(source, article__source.InheritedMmd);
            // pages
            MetaMetadataScalarField article__pages = (MetaMetadataScalarField)article.GetChildMetaMetadata()["pages"];

            Assert.IsNull(article__pages.InheritedField);
            Assert.AreSame(article, article__pages.DeclaringMmd);

            MetaMetadata paper = repository.GetMMByName("paper");
            MetaMetadata tag   = paper.MmdScope["tag"];

            Assert.AreSame(metadata, tag.InheritedMmd);
            Assert.AreEqual(metadata__meta_metadata_name, tag.GetChildMetaMetadata()["meta_metadata_name"]);
            Assert.AreEqual(metadata__mixins, tag.GetChildMetaMetadata()["mixins"]);
            // tag_name
            MetaMetadataScalarField tag__tag_name = (MetaMetadataScalarField)tag.GetChildMetaMetadata()["tag_name"];

            Assert.IsNull(tag__tag_name.InheritedField);
            Assert.AreSame(tag, tag__tag_name.DeclaringMmd);
            // link
            MetaMetadataScalarField tag__link = (MetaMetadataScalarField)tag.GetChildMetaMetadata()["link"];

            Assert.IsNull(tag__link.InheritedField);
            Assert.AreSame(tag, tag__link.DeclaringMmd);

            Assert.AreSame(article, paper.InheritedMmd);
            Assert.IsTrue(paper.MmdScope.Count == 1);
            Assert.AreEqual(metadata__meta_metadata_name, paper.GetChildMetaMetadata()["meta_metadata_name"]);
            Assert.AreEqual(metadata__mixins, paper.GetChildMetaMetadata()["mixins"]);
            Assert.AreEqual(document__location, paper.GetChildMetaMetadata()["location"]);
            Assert.AreEqual(document__additional_locations, paper.GetChildMetaMetadata()["additional_locations"]);
            Assert.AreEqual(article__title, paper.GetChildMetaMetadata()["title"]);
            // Assert.AreSame(article__authors, paper.GetChildMetaMetadata()["authors"]);
            Assert.AreEqual(article__source, paper.GetChildMetaMetadata()["source"]);
            Assert.AreEqual(article__pages, paper.GetChildMetaMetadata()["pages"]);
            // authors: TODO
            // abstract_field
            MetaMetadataScalarField paper__abstract_field = (MetaMetadataScalarField)paper.GetChildMetaMetadata()["abstract_field"];

            Assert.IsNull(paper__abstract_field.InheritedField);
            Assert.AreSame(paper, paper__abstract_field.DeclaringMmd);
            // references
            MetaMetadataCollectionField paper__references = (MetaMetadataCollectionField)paper.GetChildMetaMetadata()["references"];

            Assert.IsNull(paper__references.InheritedField);
            Assert.AreSame(paper, paper__references.DeclaringMmd);
            Assert.AreSame(paper, paper__references.InheritedMmd);
            // citations
            MetaMetadataCollectionField paper__citations = (MetaMetadataCollectionField)paper.GetChildMetaMetadata()["citations"];

            Assert.IsNull(paper__citations.InheritedField);
            Assert.AreSame(paper, paper__citations.DeclaringMmd);
            Assert.AreSame(paper, paper__citations.InheritedMmd);
            // keywords
            MetaMetadataCollectionField paper__keywords = (MetaMetadataCollectionField)paper.GetChildMetaMetadata()["keywords"];

            Assert.IsNull(paper__keywords.InheritedField);
            Assert.AreSame(paper, paper__keywords.DeclaringMmd);
            Assert.AreEqual(/*typeof(MetadataStringScalarType).Name*/ "MetadataString", paper__keywords.ChildScalarType.SimplName);

            MetaMetadata acm_paper = repository.GetMMByName("acm_paper");

            Assert.AreSame(paper, acm_paper.InheritedMmd);
            Assert.IsTrue(acm_paper.MmdScope == null || acm_paper.MmdScope.Count == 0);
            Assert.AreEqual(metadata__meta_metadata_name, acm_paper.GetChildMetaMetadata()["meta_metadata_name"]);
            Assert.AreEqual(metadata__mixins, acm_paper.GetChildMetaMetadata()["mixins"]);
            Assert.AreEqual(document__location, acm_paper.GetChildMetaMetadata()["location"]);
            Assert.AreEqual(document__additional_locations, acm_paper.GetChildMetaMetadata()["additional_locations"]);
            Assert.AreEqual(article__source, acm_paper.GetChildMetaMetadata()["source"]);
            Assert.AreEqual(article__pages, acm_paper.GetChildMetaMetadata()["pages"]);
            Assert.AreEqual(paper__abstract_field, acm_paper.GetChildMetaMetadata()["abstract_field"]);
            Assert.AreEqual(paper__references, acm_paper.GetChildMetaMetadata()["references"]);
            Assert.AreEqual(paper__citations, acm_paper.GetChildMetaMetadata()["citations"]);
            Assert.AreEqual(paper__keywords, acm_paper.GetChildMetaMetadata()["keywords"]);
            // title
            MetaMetadataScalarField acm_paper__title = (MetaMetadataScalarField)acm_paper.GetChildMetaMetadata()["title"];

            Assert.AreEqual(article__title, acm_paper__title.InheritedField);
            Assert.AreSame(article, acm_paper__title.DeclaringMmd);
            // authors
            MetaMetadataCollectionField acm_paper__authors = (MetaMetadataCollectionField)acm_paper.GetChildMetaMetadata()["authors"];

            // Assert.AreSame(article__authors, acm_paper__authors.InheritedField); // should
            // inherit from paper__authors
            Assert.AreSame(article, acm_paper__authors.DeclaringMmd);
            Assert.AreSame(author, acm_paper__authors.InheritedMmd);
            // authors.name
            MetaMetadataScalarField acm_paper__authors__name = (MetaMetadataScalarField)acm_paper__authors.GetChildMetaMetadata()["name"];

            //Assert.AreEqual(author__name, acm_paper__authors__name.InheritedField);
            Assert.AreSame(author, acm_paper__authors__name.DeclaringMmd);
            Assert.AreEqual("location", acm_paper__authors__name.NavigatesTo);
            // authors.affiliation
            MetaMetadataScalarField acm_paper__authors__affiliation = (MetaMetadataScalarField)acm_paper__authors.GetChildMetaMetadata()["affiliation"];

            Assert.AreEqual("./affiliation", acm_paper__authors__affiliation.Xpath);
        }
 public InheritanceHandler(MetaMetadata rootMmd)
 {
     this.rootMmd = rootMmd;
     this.repository = rootMmd.Repository;
 }
Пример #10
0
        private MetaMetadataCompositeField GetMetaMetadata()
        {
            MetaMetadataCompositeField mm = _metaMetadata;

            //mm = await MetaMetadataTranslationScope.Get().Deserialize()
            if (_repository == null)
                _repository = MetaMetadataRepositoryInit.GetRepository();

            if (mm == null && _repository != null)
            {
                if ( this.MetaMetadataName != null) // get from saved composition
                    mm = _repository.GetMMByName(this.MetaMetadataName.Value);

                if (mm == null)
                {
                    ParsedUri location = Location == null ? null : Location.Value;
                    if (location != null)
                    {
                        mm = IsImage ? _repository.GetImageMM(location) : _repository.GetDocumentMM(location);

                        // TODO -- also try to resolve by mime type ???
                    }
                    if (mm == null)
                        mm = _repository.GetByClass(this.GetType());
                    if (mm == null && MetadataClassDescriptor != null)
                    {
                        mm = _repository.GetMMByName(MetadataClassDescriptor.TagName);
                    }
                }

                if (mm != null)
                    MetaMetadata = mm;
            }
            return mm;
        }
 private void InheritFromTopLevelMetaMetadata(MetaMetadata inheritedMmd, MetaMetadataRepository repository, InheritanceHandler inheritanceHandler)
 {
     InheritNonFieldElements(inheritedMmd, inheritanceHandler);
     InheritFrom(repository, inheritedMmd, inheritanceHandler);
 }
        private bool InheritFromSuperField(MetaMetadataRepository repository, InheritanceHandler inheritanceHandler)
        {
            bool inhertedIsInheriting = false;
            MetaMetadataCompositeField inheritedField = (MetaMetadataCompositeField)SuperField;
            if (inheritedField != null)
            {
                inheritedField.Repository = repository;
                if (inheritedField.InheritInProcess)
                {
                    inhertedIsInheriting = true;

                    // if inheriting from the root mmd, we need to clone and keep the environment right now.
                    InheritanceHandler inheritanceHandlerToUse = inheritanceHandler.clone();
                    inheritanceHandler.Pop(this);

                    //inheritedField.InheritFinished += (sender, e) => InheritFrom(repository, inheritedField);
                    this.AddInheritanceFinishHandler(inheritedField, InheritFieldFinished, inheritanceHandlerToUse);
                }
                else
                {
                    inheritedField.InheritMetaMetadata(inheritanceHandler);
                    InheritFromCompositeField(inheritedField, repository, inheritanceHandler);
                }
            }
            return inhertedIsInheriting;
        }
 private void InheritFromCompositeField(MetaMetadataCompositeField inheritedField, MetaMetadataRepository repository, InheritanceHandler inheritanceHandler)
 {
     InheritFrom(repository, inheritedField, inheritanceHandler);
 }
        protected virtual void InheritFrom(MetaMetadataRepository repository,
            MetaMetadataCompositeField inheritedStructure,
            InheritanceHandler inheritanceHandler)
        {
            // init nested fields inside this
            var subfields = Kids.Values;
            foreach (MetaMetadataField f in subfields)
                if (f is MetaMetadataNestedField)
                {
                    f.Repository = (repository);
                    MetaMetadataNestedField nested = (MetaMetadataNestedField) f;
                    if (nested.PackageName == null)
                        nested.PackageName = PackageName;
                    nested.Scope = Scope;
                }

            // inherit fields with attributes from inheritedStructure
            // if inheritedStructure == null, this must be the root meta-metadata
            if (inheritedStructure != null)
            {
                var inheritedStructSubfields = inheritedStructure.Kids.Values;
                foreach (MetaMetadataField field in inheritedStructSubfields)
                {
                    if (field is MetaMetadataNestedField)
                    {
                        ((MetaMetadataNestedField)field).InheritMetaMetadata(inheritanceHandler);
                    }
                    string fieldName = field.Name;
                    MetaMetadataField fieldLocal;
                    kids.TryGetValue(fieldName, out fieldLocal);

                    if (fieldLocal == null && inheritanceHandler.IsUsingGenerics(field))
                    {
                        // if the super field is using generics, we will need to re-evaluate generic type vars
                        fieldLocal = (MetaMetadataField) Activator.CreateInstance(field.GetType());

                        //Prepare Child Field For Inheritance
                        fieldLocal.Repository = (repository);
                        if (fieldLocal is MetaMetadataNestedField)
                        {
                            MetaMetadataNestedField nested = (MetaMetadataNestedField)fieldLocal;
                            if (nested.PackageName == null)
                                nested.PackageName = PackageName;
                            nested.Scope = Scope;
                        }
                    }
                    if (fieldLocal != null)
                    {
                        Debug.WriteLine("inheriting field: " + fieldLocal + " <= " + field);
                        if (field.GetType() != fieldLocal.GetType())
                            Debug.WriteLine("local field " + fieldLocal + " hides field " + fieldLocal +
                                            " with the same name in super mmd type!");
                        // debug("inheriting field " + fieldLocal + " from " + field);
                        if (field != fieldLocal)
                            fieldLocal.SuperField = field;
                        fieldLocal.DeclaringMmd = field.DeclaringMmd;
                        fieldLocal.InheritAttributes(field);
                        if (fieldLocal is MetaMetadataNestedField)
                            ((MetaMetadataNestedField)fieldLocal).PackageName =
                                ((MetaMetadataNestedField)field).PackageName;
                    }
                }
            }

            // recursively call inheritMetaMetadata() on nested fields
            foreach (MetaMetadataField f in subfields)
            {
                // a new field is defined inside this mmd
                if (f.DeclaringMmd == this && f.SuperField == null)
                    SetNewMetadataClass(true);

                // recursively call this method on nested fields
                f.Repository = repository;
                if (f is MetaMetadataNestedField)
                {
                    MetaMetadataNestedField f1 = (MetaMetadataNestedField) f;
                    f1.InheritMetaMetadata(inheritanceHandler);
                    if (f1.IsNewMetadataClass())
                        SetNewMetadataClass(true);

                    MetaMetadataNestedField f0 = (MetaMetadataNestedField) f.SuperField;
                    if (f0 != null && f0.GetTypeName() != f1.GetTypeName())
                    {
                        // inherited field w changing base type (polymorphic case)
                        f1.InheritMetaMetadata(inheritanceHandler);
                        MetaMetadata mmd0 = f0.TypeMmd;
                        MetaMetadata mmd1 = f1.TypeMmd;
                        if (mmd1.IsDerivedFrom(mmd0))
                            SetNewMetadataClass(true);
                        else
                            throw new MetaMetadataException("incompatible types: " + mmd0 + " => " + mmd1);
                    }
                }
            }

            // clone fields only declared in inheritedStructure.
            // must clone them after recursively calling inheritMetaMetadata(), so that their nested
            // structures (which may be inherited too) can be cloned.
            if (inheritedStructure != null)
            {
                var inheritedStructSubfields = inheritedStructure.Kids.Values;
                foreach (MetaMetadataField field in inheritedStructSubfields)
                {
                    string fieldName = field.Name;
                    MetaMetadataField fieldLocal;
                    kids.TryGetValue(fieldName, out fieldLocal);

                    if (fieldLocal == null)
                    {
                        //					MetaMetadataField clonedField = (MetaMetadataField) field.clone();
                        //					clonedField.setParent(this);
                        //					this.getChildMetaMetadata().put(fieldName, clonedField);
                        kids.Put(fieldName, field);
                    }
                }
            }
        }
        protected virtual MetaMetadata FindOrGenerateInheritedMetaMetadata(MetaMetadataRepository repository, InheritanceHandler inheritanceHandler)
        {
            MetaMetadata inheritedMmd = this.TypeMmd;
            if (inheritedMmd == null)
            {
                MmdScope mmdScope = this.Scope;
                String inheritedMmdName = Type ?? Name;

                if (ExtendsAttribute != null)
                {
                    // determine new type name
                    if (inheritedMmdName == null)
                        throw new MetaMetadataException("attribute 'name' must be specified: " + this);
                    if (inheritanceHandler.ResolveMmdName(inheritedMmdName) != null)
                        // currently we don't encourage re-using existing name. however, in the future, when package names are available, we can change this.
                        throw new MetaMetadataException("meta-metadata '" + inheritedMmdName + "' already exists! please use another name to prevent name collision. hint: use 'tag' to change the tag if needed.");

                    // determine from which meta-metadata to inherit
                    inheritedMmd = inheritanceHandler.ResolveMmdName(ExtendsAttribute);
                    if (ExtendsAttribute == null || inheritedMmd == null)
                        throw new MetaMetadataException("super type not specified or recognized: " + this + ", super type name: " + ExtendsAttribute);

                    // generate inline mmds and put it into current scope
                    MetaMetadata generatedMmd = this.GenerateMetaMetadata(inheritedMmdName, inheritedMmd);
                    mmdScope.Put(inheritedMmdName, generatedMmd);
                    mmdScope.Put(generatedMmd.Name, generatedMmd);

                    // recursively do inheritance on generated mmd
                    generatedMmd.InheritMetaMetadata(null); // this will set generateClassDescriptor to true if necessary

                    MakeThisFieldUseMmd(inheritedMmdName, generatedMmd);
                    return generatedMmd;
                }
                else
                {
                    // use type / extends
                    if (inheritedMmdName == null)
                        throw new MetaMetadataException("no type / extends defined for " + this
                            + " (note that due to a limitation explicit child_scalar_type is needed for scalar collection fields, even if it has been declared in super field).");

                    NameType[] nameType = new NameType[1];
                    inheritedMmd = inheritanceHandler.ResolveMmdName(inheritedMmdName, nameType);

                    if (inheritedMmd == null)
                        throw new MetaMetadataException("meta-metadata not found: " + inheritedMmdName + " (if you want to define new types inline, you need to specify extends/child_extends).");

                    if (!inheritedMmdName.Equals(inheritedMmd.Name) && nameType[0] == NameType.MMD)
                    {
                        // could be inline mmd
                        this.MakeThisFieldUseMmd(inheritedMmdName, inheritedMmd);
                    }

                    // process normal mmd / field
                    Debug.WriteLine("setting " + this + ".inheritedMmd to " + inheritedMmd);
                    TypeMmd = inheritedMmd;
                }
            }
            return inheritedMmd;
        }
        public static async Task<MetaMetadataRepository> ReadRepository(string filename, SimplTypesScope mmdTScope, SimplTypesScope metadataTScope, MetaMetadataRepository mainRepo)
        {
            MetaMetadataRepository repo = null;
            Debug.WriteLine("MetaMetadataRepository Reading:\t\t" + filename);

            try
            {
                repo = await mmdTScope.DeserializeFile(filename, Format.Xml) as MetaMetadataRepository;
                if (repo != null)
                {
                    repo.MetadataTScope = metadataTScope;
                    repo.File = filename;
                    repo.InitializeSuffixAndMimeDicts();

                    if (repo.RepositoryByName == null)
                        return repo;

                    foreach (var repoEntry in repo.RepositoryByName)
                    {
                        MetaMetadata mmd = repoEntry.Value;
                        string mmdName = repoEntry.Key;

                        //mmd.File = new FileInfo(filename);
                        mmd.File = await FundamentalPlatformSpecifics.Get().CreateFile(filename);
                        mmd.Parent = mainRepo;
                        mmd.Repository = mainRepo;

                        string packageName = mmd.PackageName ?? repo.PackageName;
                        if (packageName == null)
                            throw new MetaMetadataException("No Package Name Specified For " + mmd);
                        mmd.PackageName = packageName;

                        MmdScope packageMmdScopes;
                        mainRepo.PackageMmdScopes.TryGetValue(mmd.PackageName, out packageMmdScopes);
                        if(packageMmdScopes == null)
                        {
                            packageMmdScopes = new MmdScope(repo.PackageName);
                            packageMmdScopes.PutAll(mainRepo.RepositoryByName);
                            mainRepo.PackageMmdScopes.Put(packageName, packageMmdScopes);
                        }

                        MetaMetadata existingMmd;
                        switch (mmd.Visibility)
                        {
                            case Visibility.GLOBAL:
                            
                                mainRepo.RepositoryByName.TryGetValue(mmdName, out existingMmd);

                                if (existingMmd != null && existingMmd != mmd)
                                    throw new MetaMetadataException("MMD already exists: " + mmdName + " in " + filename);

                                mainRepo.RepositoryByName.Put(mmdName, mmd);
                                break;
                            case Visibility.PACKAGE:
                                Object mmdObj = null;
                                packageMmdScopes.TryGetValue(mmdName, out mmdObj);
                                existingMmd = (MetaMetadata) mmdObj;

                                if (existingMmd != null && existingMmd != mmd)
                                    throw new MetaMetadataException("MMD already exists: " + mmdName + " in " + filename);

                                packageMmdScopes.Put(mmdName, mmd);
                                break;
                        }
                    }

                    foreach (MetaMetadata metaMetadata in repo.RepositoryByName.Values)
                    {
                        if (metaMetadata.PackageName == null)
                        {
                            Debug.WriteLine("No Package name defined for: " + metaMetadata.Name);
                            continue;
                        }
                        MmdScope packageMmdScope;
                        mainRepo.PackageMmdScopes.TryGetValue(metaMetadata.PackageName, out packageMmdScope);
                        metaMetadata.Scope = packageMmdScope;
                    }

                    mainRepo.IntegrateRepository(repo);
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine("Couldn't translate repository file: " + filename);
                Debug.WriteLine(e);
            }

            return repo;
        }