Example #1
0
        // Resolves conrefs in this file
        private void ResolveConRefs(DitaElement parentElement, DitaCollection collection)
        {
            if (parentElement != null)
            {
                bool updated = false;

                // Does this element have a conref?
                string conref = parentElement.AttributeValueOrDefault("conref", String.Empty);
                if (!string.IsNullOrEmpty(conref))
                {
                    // We expect the conref to be in the form of filename.xml#fileid/elementid
                    Regex           conRefRegex           = new Regex("^(.*)#(.*)/(.*)$", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);
                    MatchCollection conRefMatchCollection = conRefRegex.Matches(conref);
                    if (conRefMatchCollection?.Count > 0 && (conRefMatchCollection[0].Groups.Count == 4))
                    {
                        // Extract the components of the conref
                        string refFileName  = conRefMatchCollection[0].Groups[1].Value;
                        string refFileId    = conRefMatchCollection[0].Groups[2].Value;
                        string refElementId = conRefMatchCollection[0].Groups[3].Value;

                        if (Path.GetFileNameWithoutExtension(refFileName) != refFileId)
                        {
                            Trace.TraceWarning($"conref file name '{refFileName}' is not equal to file id '{refFileId}'.");
                        }

                        // Try to find the file that this conref refers to
                        DitaFile refFile = collection.GetFileByName(refFileName);
                        if (refFile != null)
                        {
                            // Does the references element exist in this file
                            if (refFile.ElementsById.ContainsKey(refElementId))
                            {
                                DitaElement refElement = refFile.ElementsById[refElementId];
                                // Copy the refernce element
                                parentElement.Copy(refElement);
                                updated = true;
                            }
                            else
                            {
                                Trace.TraceWarning($"Element '{refElementId}' not found in file '{refFileName}'.");
                            }
                        }
                        else
                        {
                            Trace.TraceWarning($"Can't find file '{refFileName}' referenced in file '{FileName}'.");
                        }
                    }
                    else
                    {
                        Trace.TraceWarning($"conref {conref} not in expected format.");
                    }
                }

                // Update child references
                if (!updated && parentElement.Children != null)
                {
                    foreach (DitaElement childElement in parentElement.Children)
                    {
                        ResolveConRefs(childElement, collection);
                    }
                }
            }
        }