Example #1
0
        private static void LoadGlobalSettings(Dictionary <string, Formatter> languageTransforms)
        {
            string settingsFile = PlixDirectory + PlixGlobalSettingsFile;

            if (File.Exists(settingsFile))
            {
                PlixSettingsNameTable names = PlixSettingsSchema.Names;
                using (FileStream plixSettingsStream = new FileStream(settingsFile, FileMode.Open, FileAccess.Read))
                {
                    using (XmlTextReader settingsReader = new XmlTextReader(new StreamReader(plixSettingsStream), names))
                    {
                        using (XmlReader reader = XmlReader.Create(settingsReader, PlixSettingsSchema.ReaderSettings))
                        {
                            if (XmlNodeType.Element == reader.MoveToContent())
                            {
                                while (reader.Read())
                                {
                                    XmlNodeType nodeType1 = reader.NodeType;
                                    if (nodeType1 == XmlNodeType.Element)
                                    {
                                        Debug.Assert(XmlUtility.TestElementName(reader.LocalName, names.FormattersElement));                                         // Only value allowed by the validating reader
                                        if (reader.IsEmptyElement)
                                        {
                                            break;
                                        }
                                        string formattersDir = PlixDirectory + FormattersDirectory;
                                        while (reader.Read())
                                        {
                                            XmlNodeType nodeType2 = reader.NodeType;
                                            if (nodeType2 == XmlNodeType.Element)
                                            {
                                                Debug.Assert(XmlUtility.TestElementName(reader.LocalName, names.FormatterElement));                                                 // Only value allowed by the validating reader
                                                languageTransforms[reader.GetAttribute(PlixSettingsSchema.FileExtensionAttribute)] = new Formatter(formattersDir + reader.GetAttribute(PlixSettingsSchema.TransformAttribute));
                                                XmlUtility.PassEndElement(reader);
                                            }
                                            else if (nodeType2 == XmlNodeType.EndElement)
                                            {
                                                break;
                                            }
                                        }
                                    }
                                    else if (nodeType1 == XmlNodeType.EndElement)
                                    {
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
Example #2
0
        /// <summary>
        /// Load the project settings for a specific transform file and return it
        /// </summary>
        /// <param name="sourceFile">The full path to the data file</param>
        /// <param name="baseDirectory">The path for the base directory (with a trailing \)</param>
        /// <param name="arguments">Argument list to pass to the returned transform</param>
        /// <param name="project">The context project</param>
        /// <param name="automationObjectName">The name of an automation object supported by the live document. Used
        /// to get a live version of the file stream.</param>
        /// <returns>The transform file name. Existence will have been verified.</returns>
        private string LoadProjectSettings(string sourceFile, string baseDirectory, XsltArgumentList arguments, EnvDTE.Project project, out string automationObjectName)
        {
                        #if VerifyUIThread
            Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread();
                        #endif

            Debug.Assert(arguments != null);             // Allocate before call
            string transformFile = null;
            automationObjectName = null;
            string plixProjectSettingsFile = baseDirectory + PlixProjectSettingsFile;
            if (File.Exists(plixProjectSettingsFile))
            {
                // Use the text from the live document if possible
                string          liveText    = null;
                EnvDTE.Document settingsDoc = null;
                try
                {
                    settingsDoc = project.DTE.Documents.Item(plixProjectSettingsFile);
                }
                catch (ArgumentException)
                {
                    // swallow
                }
                catch (InvalidCastException)
                {
                    // swallow
                }
                if (settingsDoc != null)
                {
                    EnvDTE.TextDocument textDoc = settingsDoc.Object("TextDocument") as EnvDTE.TextDocument;
                    if (textDoc != null)
                    {
                        liveText = textDoc.StartPoint.CreateEditPoint().GetText(textDoc.EndPoint);
                    }
                }
                string sourceFileIdentifier = sourceFile.Substring(baseDirectory.Length);
                PlixLoaderNameTable names   = PlixLoaderSchema.Names;
                using (FileStream plixSettingsStream = (liveText == null) ? new FileStream(plixProjectSettingsFile, FileMode.Open, FileAccess.Read) : null)
                {
                    using (XmlTextReader settingsReader = new XmlTextReader((liveText == null) ? new StreamReader(plixSettingsStream) as TextReader : new StringReader(liveText), names))
                    {
                        using (XmlReader reader = XmlReader.Create(settingsReader, PlixLoaderSchema.ReaderSettings))
                        {
                            References references = null;
                            bool       finished   = false;
                            while (!finished && reader.Read())
                            {
                                if (reader.NodeType == XmlNodeType.Element)
                                {
                                    if (!reader.IsEmptyElement)
                                    {
                                        while (reader.Read())
                                        {
                                            XmlNodeType nodeType = reader.NodeType;
                                            if (nodeType == XmlNodeType.Element)
                                            {
                                                Debug.Assert(XmlUtility.TestElementName(reader.LocalName, names.SourceFileElement));                                                 // Only value allowed by the validating loader
                                                string testFileName = reader.GetAttribute(names.FileAttribute);
                                                if (0 == string.Compare(testFileName, sourceFileIdentifier, true, CultureInfo.CurrentCulture))
                                                {
                                                    finished = true;                                                     // Stop looking
                                                    string attrValue = reader.GetAttribute(names.TransformFileAttribute);
                                                    if (attrValue != null && attrValue.Length != 0)
                                                    {
                                                        transformFile = baseDirectory + attrValue;
                                                    }
                                                    attrValue = reader.GetAttribute(names.LiveDocumentObjectAttribute);
                                                    if (attrValue != null && attrValue.Length != 0)
                                                    {
                                                        automationObjectName = attrValue;
                                                    }
                                                    if (!reader.IsEmptyElement)
                                                    {
                                                        while (reader.Read())
                                                        {
                                                            nodeType = reader.NodeType;
                                                            if (nodeType == XmlNodeType.Element)
                                                            {
                                                                string localName = reader.LocalName;
                                                                if (XmlUtility.TestElementName(localName, names.TransformParameterElement))
                                                                {
                                                                    // Add an argument for the transform
                                                                    arguments.AddParam(reader.GetAttribute(names.NameAttribute), "", reader.GetAttribute(names.ValueAttribute));
                                                                }
                                                                else if (XmlUtility.TestElementName(localName, names.ExtensionClassElement))
                                                                {
                                                                    // Load an extension class and associate it with an extension namespace
                                                                    // used by the transform
                                                                    arguments.AddExtensionObject(reader.GetAttribute(names.XslNamespaceAttribute), Type.GetType(reader.GetAttribute(names.ClassNameAttribute), true, false).GetConstructor(Type.EmptyTypes).Invoke(new object[0]));
                                                                }
                                                                else if (XmlUtility.TestElementName(localName, names.ProjectReferenceElement))
                                                                {
                                                                    // The generated code requires project references, add them
                                                                    if (null == references)
                                                                    {
                                                                        references = ((VSProject)project.Object).References;
                                                                    }
                                                                    if (references.Item(reader.GetAttribute(names.NamespaceAttribute)) == null)
                                                                    {
                                                                        references.Add(reader.GetAttribute(names.AssemblyAttribute));
                                                                    }
                                                                }
                                                                else
                                                                {
                                                                    Debug.Assert(false);                                                                     // Not allowed by schema definition
                                                                }
                                                                XmlUtility.PassEndElement(reader);
                                                            }
                                                            else if (nodeType == XmlNodeType.EndElement)
                                                            {
                                                                break;
                                                            }
                                                        }
                                                    }
                                                    break;
                                                }
                                                XmlUtility.PassEndElement(reader);
                                            }
                                            else if (nodeType == XmlNodeType.EndElement)
                                            {
                                                break;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            bool verifiedExistence = false;
            if (transformFile == null)
            {
                string fileBase = sourceFile.Substring(0, sourceFile.LastIndexOf('.'));
                transformFile = fileBase + ".xslt";
                if (File.Exists(transformFile))
                {
                    verifiedExistence = true;
                }
                else
                {
                    transformFile = fileBase + ".xsl";
                }
            }
            if (!verifiedExistence && !File.Exists(transformFile))
            {
                transformFile = null;
            }
            return(transformFile);
        }