Пример #1
0
        public void TestCreateSrcMLArchiveFtF() {
            using (Archive srcmlArchive = new Archive(), srcmlArchive2 = new Archive()) {
                using (Unit srcmlUnit = new Unit()) {
                    srcmlUnit.SetUnitFilename(Path.Combine(TestInputPath, "input.cpp"));
                    srcmlArchive.SetArchiveLanguage(LibSrcMLRunner.SrcMLLanguages.SRCML_LANGUAGE_CXX);
                    srcmlArchive.AddUnit(srcmlUnit);
                    srcmlArchive.SetOutputFile("output");
                    srcmlArchive.ArchivePack();

                    srcmlUnit.SetUnitFilename(Path.Combine(TestInputPath, "input2.cpp"));
                    srcmlArchive2.SetArchiveLanguage(LibSrcMLRunner.SrcMLLanguages.SRCML_LANGUAGE_CXX);
                    srcmlArchive2.AddUnit(srcmlUnit);
                    srcmlArchive2.SetOutputFile("output");

                    srcmlArchive2.ArchivePack();

                    IntPtr structPtr = srcmlArchive.GetPtrToStruct();
                    IntPtr structPtr2 = srcmlArchive2.GetPtrToStruct();

                    List<IntPtr> structArrayPtr = new List<IntPtr>();
                    structArrayPtr.Add(structPtr);
                    structArrayPtr.Add(structPtr2);

                    try {
                        Assert.True(LibSrcMLRunner.SrcmlCreateArchiveFtF(structArrayPtr.ToArray(), structArrayPtr.Count()) == IntPtr.Zero);
                    }
                    catch (SrcMLException e) {
                        Console.WriteLine(e.Message);
                    }
                }
                {
                    Assert.True(File.Exists("output0.cpp.xml"));
                    SrcMLFile srcFile = new SrcMLFile("output0.cpp.xml");
                    Assert.IsNotNull(srcFile);

                    var files = srcFile.FileUnits.ToList();
                    Assert.AreEqual(1, files.Count());

                    string file = Path.Combine(TestInputPath, "input.cpp");
                    var f1 = (from ele in files
                              where ele.Attribute("filename").Value == file
                              select ele);
                    Assert.AreEqual(file, f1.FirstOrDefault().Attribute("filename").Value);
                }
                {
                    Assert.True(File.Exists("output1.cpp.xml"));
                    SrcMLFile srcFile = new SrcMLFile("output1.cpp.xml");
                    Assert.IsNotNull(srcFile);

                    var files = srcFile.FileUnits.ToList();
                    Assert.AreEqual(1, files.Count());

                    string file1 = Path.Combine(TestInputPath, "input2.cpp");
                    var f2 = (from ele in files
                              where ele.Attribute("filename").Value == file1
                              select ele);
                    Assert.AreEqual(file1, f2.FirstOrDefault().Attribute("filename").Value);
                }
            }
        }
Пример #2
0
        public SrcMLFile Merge(SrcMLFile other, string outputFileName)
        {
            //if (null == other)
            //    throw new ArgumentNullException("other");

            using (XmlWriter xw = XmlWriter.Create(outputFileName, new XmlWriterSettings()
            {
                Indent = false
            })) {
                xw.WriteStartElement(SRC.Unit.LocalName, SRC.Unit.NamespaceName);
                WriteXmlnsAttributes(xw);

                foreach (var unit in this.FileUnits)
                {
                    unit.WriteTo(xw);
                }

                if (other != null)
                {
                    foreach (var unit in other.FileUnits)
                    {
                        unit.WriteTo(xw);
                    }
                }

                xw.WriteEndElement();
            }
            return(new SrcMLFile(outputFileName));
        }
Пример #3
0
        public void BasicTest()
        {
            if(!File.Exists(Helper.NppXmlPath)) {
                Assert.Ignore(String.Format("SrcML for Notepad++ is not available at {0}", Helper.NppXmlPath));
            }
            var document = new SrcMLFile(Helper.NppXmlPath);

            var newUses = from unit in document.FileUnits
                          from use in QueryForNew(unit)
                          select use;

            SyntaticCategoryDataModel model = new SyntaticCategoryDataModel();
            
            foreach (var element in newUses)
            {
                var occurrence = new SyntaticOccurance(model, element);
            }

            //Console.WriteLine("{0} uses of the \"new\" operator in {1} categories", newUses.Count(), model.SyntaticCategories.Keys.Count);
            
            foreach (var category in model.SyntaticCategories.Keys)
            {
                
                var xpath = model.SyntaticCategories[category].First().CategoryAsXPath;//.Substring(1);

                var results = from use in newUses
                              let occurrence = new SyntaticOccurance(model, use)
                              where occurrence.CategoryAsXPath == xpath
                              select use;

                //Console.WriteLine("{0,3} uses of the new operator in {1}", results.Count(), xpath);
                Assert.AreEqual(model.SyntaticCategories[category].Count, results.Count(), category);
            }
        }
Пример #4
0
        /// <summary>
        /// Run the query against the given document.
        /// </summary>
        /// <param name="document">The document to query.</param>
        /// <param name="transform">The transform containing the Query.</param>
        /// <returns>The list of matching nodes.</returns>
        public static IEnumerable<XElement> RunQuery(SrcMLFile document, ITransform transform)
        {
            if (null == document)
                throw new ArgumentNullException("document");

            return document.QueryEachUnit(transform);
        }
Пример #5
0
        /// <summary>
        /// Instantiates a new SrcMLFile with the characteristics of another SrcMLFile.
        /// </summary>
        /// <param name="other">The SrcMLFile to copy.</param>
        public SrcMLFile(SrcMLFile other)
            : base(other)
        {
            if (null == other)
                throw new ArgumentNullException("other");

            this._rootDirectory = other._rootDirectory;
        }
Пример #6
0
        /// <summary>
        /// Run the query against the given document.
        /// </summary>
        /// <param name="document">The document to query.</param>
        /// <param name="transform">The transform containing the Query.</param>
        /// <returns>The list of matching nodes.</returns>
        public static IEnumerable <XElement> RunQuery(SrcMLFile document, ITransform transform)
        {
            if (null == document)
            {
                throw new ArgumentNullException("document");
            }

            return(document.QueryEachUnit(transform));
        }
Пример #7
0
        /// <summary>
        /// Instantiates a new SrcMLFile with the characteristics of another SrcMLFile.
        /// </summary>
        /// <param name="other">The SrcMLFile to copy.</param>
        public SrcMLFile(SrcMLFile other)
        {
            if (null == other)
            {
                throw new ArgumentNullException("other");
            }

            this._rootDirectory = other._rootDirectory;
        }
Пример #8
0
        public void SingleFileTest()
        {
            File.WriteAllText("test.xml", String.Format(@"<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes""?>
<unit xmlns=""{0}"" xmlns:cpp=""{1}"" xmlns:op=""{2}"" xmlns:type=""{3}"" languageFilter=""C++"" dir="""" filename=""bar.c"">
</unit>", SRC.NS, CPP.NS, SRC.NS, TYPE.NS));
            var doc = new SrcMLFile("test.xml");

            Assert.IsNotNull(doc);
            Assert.AreEqual(1, doc.FileUnits.Count(), "A non-compound file should have only a single  file unit");
        }
Пример #9
0
        public void SingleFileTest()
        {
            File.WriteAllText("test.xml", @"<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes""?>
<unit xmlns=""http://www.sdml.info/srcML/src"" xmlns:cpp=""http://www.sdml.info/srcML/cpp"" xmlns:op=""http://www.sdml.info/srcML/operator"" xmlns:type=""http://www.sdml.info/srcML/modifier"" languageFilter=""C++"" dir="""" filename=""bar.c"">
</unit>");
            var doc = new SrcMLFile("test.xml");

            Assert.IsNotNull(doc);
            Assert.AreEqual(1, doc.FileUnits.Count(), "A non-compound file should have only a single  file unit");
        }
Пример #10
0
        /// <summary>
        /// Added by JZ on 12/4/2012
        /// Generate both a SrcML XElement and document from a single source file. The language will be inferred from the extension.
        /// </summary>
        /// <param name="sourceFileName">path to the source file to convert.</param>
        /// <param name="xmlFileName">The File name to write the resulting XML to.</param>
        /// <returns>An XElement representing the source.</returns>
        public XElement GenerateSrcMLAndXElementFromFile(string sourceFileName, string xmlFileName)
        {
            SrcMLFile srcMLFile = GenerateSrcMLFromFile(sourceFileName, xmlFileName);

            //string srcml = srcMLFile.GetXMLString();
            return(srcMLFile.FileUnits.FirstOrDefault());
            //if (srcml != String.Empty)
            //{
            //    return XElement.Parse(srcml, LoadOptions.PreserveWhitespace);
            //}
            //else
            //{
            //    return null;
            //}
        }
Пример #11
0
        public SrcMLFile Merge(SrcMLFile other, string outputFileName) {
            //if (null == other)
            //    throw new ArgumentNullException("other");

            using(XmlWriter xw = XmlWriter.Create(outputFileName, new XmlWriterSettings() { Indent = false })) {
                xw.WriteStartElement(SRC.Unit.LocalName, SRC.Unit.NamespaceName);
                WriteXmlnsAttributes(xw);

                foreach(var unit in this.FileUnits) {
                    unit.WriteTo(xw);
                }

                if(other != null) {
                    foreach(var unit in other.FileUnits) {
                        unit.WriteTo(xw);
                    }
                }

                xw.WriteEndElement();
            }
            return new SrcMLFile(outputFileName);
        }
Пример #12
0
        public void CompoundFileTest()
        {
            File.WriteAllText("test.xml", @"<?xml version=""1.0"" encoding=""utf-8""?><unit xmlns=""http://www.sdml.info/srcML/src"">
<unit languageFilter=""C"" dir="""" filename=""foo.c""></unit>
<unit languageFilter=""C"" dir="""" filename=""bar.c""></unit>
</unit>");

            var doc = new SrcMLFile("test.xml");

            Assert.IsNotNull(doc);
            Assert.AreEqual(2, doc.FileUnits.Count(), "This compound file should have 2 units.");
        }
Пример #13
0
        /// <summary>
        /// Generate both a SrcML string and document from the difference between <see cref="nameOfOriginalFile"/> and <see cref="modifiedFileName"/>. The language will be inferred from the extension.
        /// </summary>
        /// <param name="nameOfOriginalFile">path to the source file to convert.</param>
        /// <param name="nameOfModifiedFile">The File name to write the resulting XML to.</param>
        /// <param name="xmlFileName">The File name to write the resulting XML to.</param>
        /// <returns>An XML string representing the source.</returns>
        public string GenerateSrDiffAndStringFromFile(string nameOfOriginalFile, string nameOfModifiedFile, string outputXmlFileName)
        {
            SrcMLFile srcMLFile = GenerateSrcDiffFromFile(nameOfOriginalFile, nameOfModifiedFile, outputXmlFileName);

            return(srcMLFile.GetXMLString());
        }
Пример #14
0
        public override void Execute()
        {
            Console.WriteLine("Using srcML file {0}", this.File);

            UnigramSwumBuilder builder = new UnigramSwumBuilder();
            if (CountFile != null)
            {
                Console.WriteLine("Initializing SamuraiIdSplitter using word count file {0}", this.CountFile);
                builder.Splitter = new SamuraiIdSplitter(CountFile);
            }
            Console.WriteLine("SwumBuilder initialized");

            if (this.SamplePercent <= 0) { this.SamplePercent = 100; }
            Random rand = new Random();

            SrcMLFile testFile = new SrcMLFile(this.File);
            int methodCount = 0;
            var functionTypes = new XName[] { SRC.Function, SRC.Constructor, SRC.Destructor };
            foreach (XElement file in testFile.FileUnits)
            {
                Console.WriteLine("File {0}:", file.Attribute("filename").Value);

                //print all the function names
                foreach (var func in (from func in file.Descendants()
                                      where functionTypes.Any(c => c == func.Name) && !func.Ancestors(SRC.Declaration).Any() && (rand.Next(100) < this.SamplePercent)
                                      select func))
                {
                    string funcName = SrcMLElement.GetNameForMethod(func).Value;
                    Console.WriteLine("<{0}> {1}", func.Name.LocalName, GetMethodSignature(func));

                    MethodDeclarationNode mdn = new MethodDeclarationNode(funcName, ContextBuilder.BuildMethodContext(func));
                    builder.ApplyRules(mdn);
                    Console.WriteLine(mdn.ToString() + Environment.NewLine);
                    methodCount++;
                }
            }
            Console.WriteLine("{0} functions analyzed", methodCount);
            
        }
Пример #15
0
 public void TestStrangeEncodings([Values(@"badPathTest\BadPath™\badPathTest.c", @"srcmltest\fooBody.c")] string sourceFileName)
 {
     var xmlFileName = Path.Combine("srcml_xml", Path.GetFileName(Path.ChangeExtension(sourceFileName, ".xml")));
     generator.GenerateSrcMLFromFile(sourceFileName, xmlFileName, Language.C);
     Console.WriteLine("FILE: {0}", sourceFileName);
     var doc = new SrcMLFile(xmlFileName);
     Assert.IsNotNull(doc);
 }
Пример #16
0
 public void TestGenerateSrcMLFromFile_NonDefaultExtension() {
     generator.GenerateSrcMLFromFile(@"srcmltest\CSHARP.cs", @"srcml_xml\CSHARP.xml");
     var doc = new SrcMLFile(@"srcml_xml\CSHARP.xml");
     Assert.IsNotNull(doc);
     Assert.AreEqual(1, doc.FileUnits.Count());
 }
Пример #17
0
        public void EmptyOutputFileTest() {
            File.WriteAllText("srcml_xml\\emptyFile.xml", "");
            Assert.IsTrue(File.Exists("srcml_xml\\emptyFile.xml"));

            generator.GenerateSrcMLFromFile("srcmltest\\foo.c", "srcml_xml\\emptyFile.xml");
            var doc = new SrcMLFile("srcml_xml\\emptyFile.xml");
            Assert.IsNotNull(doc);
            Assert.AreEqual(1, doc.FileUnits.Count());
        }
Пример #18
0
        public void WhitespaceInCompoundDocumentTest()
        {
            File.WriteAllText("test.xml", String.Format(@"<?xml version=""1.0"" encoding=""utf-8""?><unit xmlns=""{0}"">
<unit languageFilter=""C"" filename=""c:\Test\foo.c"">
<comment type=""line"">//line1</comment>
<comment type=""line"">//line2</comment>
</unit>
<unit languageFilter=""C"" filename=""z:\Test\bar.c""></unit>
</unit>", SRC.NS));

            var doc = new SrcMLFile("test.xml");

            var firstUnit = doc.FileUnits.First();

            var firstComment = firstUnit.Elements(SRC.Comment).First();
            var lastComment = firstUnit.Elements(SRC.Comment).Last();
            var nextNode = firstComment.NextNode;

            Assert.AreNotEqual(lastComment, nextNode);
        }
Пример #19
0
        public void TestGenerateSrcMLFromFiles() {
            LibSrcMLRunner run = new LibSrcMLRunner();
            List<string> fileList = new List<string>() { Path.Combine(TestInputPath, "input.cpp"), Path.Combine(TestInputPath, "input2.cpp") };
            try {
                run.GenerateSrcMLFromFiles(fileList, "output", Language.CPlusPlus, new List<UInt32>() { LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_MODIFIER }, new Dictionary<string, Language>() { });

                Assert.True(File.Exists("output0.cpp.xml"));
                SrcMLFile srcFile = new SrcMLFile("output0.cpp.xml");
                Assert.IsNotNull(srcFile);

                var files = srcFile.FileUnits.ToList();
                Assert.AreEqual(2, files.Count());

                string file = Path.Combine(TestInputPath, "input.cpp");
                var f1 = (from ele in files
                          where ele.Attribute("filename").Value == file
                          select ele);
                Assert.AreEqual(file, f1.FirstOrDefault().Attribute("filename").Value);

                string file2 = Path.Combine(TestInputPath, "input2.cpp");
                var f2 = (from ele in files
                          where ele.Attribute("filename").Value == file2
                          select ele);
                Assert.AreEqual(file2, f2.FirstOrDefault().Attribute("filename").Value);
            }
            catch (SrcMLException e) {
                throw e;
            }
        }
Пример #20
0
        /// <summary>
        /// Added by JZ on 12/3/2012
        /// Generate both a SrcML string and document from a single source file. The language will be inferred from the extension.
        /// </summary>
        /// <param name="sourceFileName">path to the source file to convert.</param>
        /// <param name="xmlFileName">The File name to write the resulting XML to.</param>
        /// <returns>An XML string representing the source.</returns>
        public string GenerateSrcMLAndStringFromFile(string sourceFileName, string xmlFileName)
        {
            SrcMLFile srcMLFile = GenerateSrcMLFromFile(sourceFileName, xmlFileName);

            return(srcMLFile.GetXMLString());
        }
Пример #21
0
        public void MultipleFilesTest_Language() {
            generator.GenerateSrcMLFromFiles(new string[] { "srcmltest\\foo.c", "srcmltest\\bar.c" }, "srcml_xml\\multiplefile.xml", Language.CPlusPlus);
            var doc = new SrcMLFile("srcml_xml\\multiplefile.xml");

            Assert.IsNotNull(doc);
            var files = doc.FileUnits.ToList();
            Assert.AreEqual(2, files.Count());
            Assert.AreEqual("srcmltest\\foo.c", files[0].Attribute("filename").Value);
            Assert.AreEqual("C++", files[0].Attribute("language").Value);
            Assert.AreEqual("srcmltest\\bar.c", files[1].Attribute("filename").Value);
            Assert.AreEqual("C++", files[1].Attribute("language").Value);
        }
Пример #22
0
        public void TestApplyXsltToSrcMLFile() {
            LibSrcMLRunner run = new LibSrcMLRunner();
            Assert.IsTrue(File.Exists("function_def.xml"));
            Assert.IsTrue(File.Exists("Test.xsl"));
            run.ApplyXsltToSrcMLFile("function_def.xml", "Test.xsl", "o.cpp.xml");
           
            SrcMLFile srcFile = new SrcMLFile("o.cpp.xml.xslout");
            Assert.IsNotNull(srcFile);

            var files = srcFile.FileUnits.ToList();
            Assert.AreEqual(1, files.Count());

            XmlReader read = srcFile.GetXDocument().CreateReader();
            XmlNamespaceManager namespaceManager = new XmlNamespaceManager(read.NameTable);
            namespaceManager.AddNamespace("src", "http://www.srcML.org/srcML/src");

            var persist = srcFile.GetXDocument().XPathSelectElement("//src:test",namespaceManager);
            Assert.IsNotNull(persist);
            Assert.AreEqual(persist.Value, "TestPassed");
            
        }
Пример #23
0
        public void InvalidLanguageTest() {
            generator.GenerateSrcMLFromFile("srcmltest\\foo.c", "srcml_xml\\invalidlanguage_java.xml", Language.Java);
            var doc = new SrcMLFile("srcml_xml\\invalidlanguage_java.xml");
            Assert.IsNotNull(doc);

            doc = null;
            generator.GenerateSrcMLFromFile("srcmltest\\foo.c", "srcml_xml\\invalidlanguage_cpp.xml", Language.CPlusPlus);
            doc = new SrcMLFile("srcml_xml\\invalidlanguage_cpp.xml");
            Assert.IsNotNull(doc);

            doc = null;
            generator.GenerateSrcMLFromFile("srcmltest\\foo.c", "srcml_xml\\invalidlanguage_c.xml", Language.C);
            doc = new SrcMLFile("srcml_xml\\invalidlanguage_c.xml");

            Assert.IsNotNull(doc);
        }
Пример #24
0
        public void TestCreateSrcMLArchiveMtF() {
            using (Archive srcmlArchive = new Archive()) {
                List<String> bufferList = new List<String>();
                List<String> fileList = new List<String>();

                String str = "int main(){int c; c = 0; ++c;}";
                String str2 = "int foo(){int c; c = 0; ++c;}";

                fileList.Add("input.cpp");
                fileList.Add("input2.cpp");

                bufferList.Add(str);
                bufferList.Add(str2);

                var buffandfile = bufferList.Zip(fileList, (b, f) => new { buf = b, file = f });
                foreach (var pair in buffandfile) {
                    using (Unit srcmlUnit = new Unit()) {
                        srcmlUnit.SetUnitBuffer(pair.buf);
                        srcmlUnit.SetUnitFilename(pair.file);

                        srcmlArchive.SetArchiveLanguage(LibSrcMLRunner.SrcMLLanguages.SRCML_LANGUAGE_CXX);
                        srcmlArchive.AddUnit(srcmlUnit);
                    }
                }
                srcmlArchive.SetOutputFile("output");
                srcmlArchive.ArchivePack();

                IntPtr structPtr = srcmlArchive.GetPtrToStruct();

                List<IntPtr> structArrayPtr = new List<IntPtr>();
                structArrayPtr.Add(structPtr);

                Assert.True(LibSrcMLRunner.SrcmlCreateArchiveMtF(structArrayPtr.ToArray(), structArrayPtr.Count()) == IntPtr.Zero);
                Assert.True(File.Exists("output0.cpp.xml"));

                SrcMLFile srcFile = new SrcMLFile("output0.cpp.xml");
                Assert.IsNotNull(srcFile);

                var files = srcFile.FileUnits.ToList();
                Assert.AreEqual(2, files.Count());


                string file = "input.cpp";
                var f1 = (from ele in files
                          where ele.Attribute("filename").Value == file
                          select ele);
                Assert.AreEqual("input.cpp", f1.FirstOrDefault().Attribute("filename").Value);

                string file2 = "input2.cpp";
                var f2 = (from ele in files
                          where ele.Attribute("filename").Value == file2
                          select ele);
                Assert.AreEqual(file2, f2.FirstOrDefault().Attribute("filename").Value);
            }
        }
Пример #25
0
        public void MultipleFilesTest_Language() {
            generator.GenerateSrcMLFromFiles(new string[] { "srcmltest\\foo.c", "srcmltest\\bar.c" }, "srcml_xml\\multiplefile.xml", Language.CPlusPlus);
            var doc = new SrcMLFile("srcml_xml\\multiplefile.xml");

            Assert.IsNotNull(doc);
            var files = doc.FileUnits.ToList();
            Assert.AreEqual(2, files.Count());

            string file = "srcmltest\\foo.c";
            var f1 = (from ele in files
                      where ele.Attribute("filename").Value == file
                      select ele);
            Assert.AreEqual("srcmltest\\foo.c", f1.FirstOrDefault().Attribute("filename").Value);
            Assert.AreEqual("C++", f1.FirstOrDefault().Attribute("language").Value);

            string file2 = "srcmltest\\bar.c";
            var f2 = (from ele in files
                      where ele.Attribute("filename").Value == file2
                      select ele);
            Assert.AreEqual("srcmltest\\bar.c", f2.FirstOrDefault().Attribute("filename").Value);
            Assert.AreEqual("C++", f2.FirstOrDefault().Attribute("language").Value);
        }
Пример #26
0
        /// <summary>
        /// Takes the given <paramref name="fileName"/> in <paramref name="doc"/> and returns the original source code.
        /// <para>Instead of this, use <see cref="Extensions.ToSource(XElement,int)"/>.</para>
        /// <seealso cref="ExtractSourceFile(SrcMLFile, int)"/>
        /// </summary>
        /// <param name="doc">The SrcML document to query.</param>
        /// <param name="fileName">The file name to search for.</param>
        /// <returns>A string with the original source code.</returns>
        public string ExtractSourceFile(SrcMLFile doc, string fileName)
        {
            if (null == doc)
                throw new ArgumentNullException("doc");

            return this.ExtractSourceFile(doc, doc.IndexOfUnit(fileName));
        }
Пример #27
0
 public void InputWithSpacesTest() {
     generator.GenerateSrcMLFromFile("srcmltest\\File with spaces.cpp", "srcml_xml\\input_with_spaces.xml");
     var doc = new SrcMLFile("srcml_xml\\input_with_spaces.xml");
     Assert.IsNotNull(doc);
     Assert.AreEqual(1, doc.FileUnits.Count());
 }
Пример #28
0
        public override void Execute()
        {
            Console.WriteLine("Using file {0}", this.File);

            SrcMLFile testFile = new SrcMLFile(this.File);
            HashSet<string> sameCaseWords = new HashSet<string>();
            //ConservativeIdSplitter splitter = new ConservativeIdSplitter();

            var functionTypes = new List<XName>() { SRC.Function, SRC.Constructor, SRC.Destructor };
            foreach (XElement file in testFile.FileUnits)
            {
                Console.WriteLine("File {0}:", file.Attribute("filename").Value);

                //print all the function names
                //var funcs = from func in file.Descendants()
                //            where functionTypes.Any(c => c == func.Name)
                //            select func;
                foreach (var func in (from func in file.Descendants()
                                      where functionTypes.Any(c => c == func.Name)
                                      select func))
                {
                    string funcName = SrcMLElement.GetNameForMethod(func).Value;
                    if (IsSameCase(funcName))
                    {
                        sameCaseWords.Add(funcName);
                    }
                    if (PrintAll) { Console.WriteLine("<{0}> {1}", func.Name.LocalName, funcName); }
                }

                //print all the parameter names
                if (this.Parameters)
                {
                    foreach (var param in file.Descendants(SRC.Parameter))
                    {
                        if (param.Element(SRC.Declaration) != null)
                        {
                            var paramName = param.Element(SRC.Declaration).Element(SRC.Name);
                            if (paramName != null)
                            {
                                if (IsSameCase(paramName.Value))
                                {
                                    sameCaseWords.Add(paramName.Value);
                                }
                                if (PrintAll) { Console.WriteLine("<{0}> {1}", param.Name.LocalName, paramName.Value); }
                            }
                        }
                    }
                }

                //print all the variable names
                if (this.Variables)
                {
                    foreach (var declStmt in file.Descendants(SRC.DeclarationStatement))
                    {
                        foreach (var decl in declStmt.Elements(SRC.Declaration))
                        {
                            var variableName = decl.Element(SRC.Name);
                            if (variableName != null)
                            {
                                //if a qualified name, grab the last name
                                var childNames = variableName.Elements(SRC.Name);
                                if (childNames.Count() > 1)
                                {
                                    variableName = childNames.Last();
                                }

                                if (IsSameCase(variableName.Value))
                                {
                                    sameCaseWords.Add(variableName.Value);
                                }
                                if (PrintAll) { Console.WriteLine("<{0}> {1}", declStmt.Name.LocalName, variableName.Value); }
                            }
                        }
                    }
                }
            }

            Console.WriteLine("=== sameCaseWords: ===");
            foreach (string word in sameCaseWords)
            {
                Console.WriteLine(word);
            }
        }
Пример #29
0
 public void DifferentLanguageTest() {
     generator.GenerateSrcMLFromFile("srcmltest\\CSHARP.cs", "srcml_xml\\differentlanguage_java.xml", Language.Java);
     var doc = new SrcMLFile("srcml_xml\\differentlanguage_java.xml");
     Assert.IsNotNull(doc);
 }
Пример #30
0
        public void WhitespaceInSingleDocumentTest()
        {
            File.WriteAllText("test.xml", @"<?xml version=""1.0"" encoding=""utf-8""?>
<unit  xmlns=""http://www.sdml.info/srcML/src"" languageFilter=""C"" dir=""c:\Test"" filename=""foo.c""><comment type=""line"">//line1</comment>
<comment type=""line"">//line2</comment>
</unit>");

            var doc = new SrcMLFile("test.xml");

            var firstUnit = doc.FileUnits.First();

            var firstComment = firstUnit.Elements(SRC.Comment).First();
            var lastComment = firstUnit.Elements(SRC.Comment).Last();
            var nextNode = firstComment.NextNode;

            Assert.AreNotEqual(lastComment, nextNode);
        }
Пример #31
0
        public override void Execute() {
            if(Pause) {
                Console.WriteLine("Ready to begin (press Enter)");
                Console.ReadLine();
            }

            Console.WriteLine("Using srcML file {0}", this.File);

            var builder = new UnigramSwumBuilder();
            if(!string.IsNullOrWhiteSpace(CountFile)) {
                Console.WriteLine("Initializing SamuraiIdSplitter using word count file {0}", this.CountFile);
                builder.Splitter = new SamuraiIdSplitter(CountFile);
            }
            Console.WriteLine("SwumBuilder initialized");

            int methodCount = 0, fieldCount = 0;

            {
                SrcMLFile testFile = new SrcMLFile(this.File);
                var functionTypes = new XName[] {SRC.Function, SRC.Constructor, SRC.Destructor};
                foreach(XElement file in testFile.FileUnits) {
                    string fileName = file.Attribute("filename").Value;
                    Console.WriteLine("File {0}:", fileName);

                    //compute SWUM on each function
                    foreach(var func in (from func in file.Descendants()
                                         where functionTypes.Contains(func.Name) && !func.Ancestors(SRC.Declaration).Any()
                                         select func)) {
                        var nameElement = SrcMLElement.GetNameForMethod(func);
                        if(nameElement != null) {
                            string funcName = nameElement.Value;
                            string funcSignature = SrcMLElement.GetMethodSignature(func);
                            if(PrintSwum) {
                                Console.WriteLine("<{0}> {1}", func.Name.LocalName, funcSignature);
                            }

                            MethodDeclarationNode mdn = new MethodDeclarationNode(funcName, ContextBuilder.BuildMethodContext(func));
                            builder.ApplyRules(mdn);
                            methodSwum[string.Format("{0}:{1}", fileName, funcSignature)] = mdn;
                            if(PrintSwum) {
                                Console.WriteLine(mdn.ToString() + Environment.NewLine);
                            }

                            methodCount++;
                        }
                    }

                    //compute SWUM on each field
                    foreach(var fieldDecl in (from declStmt in file.Descendants(SRC.DeclarationStatement)
                                          where !declStmt.Ancestors().Any(n => functionTypes.Contains(n.Name))
                                          select declStmt.Element(SRC.Declaration))) {

                        int declPos = 1;
                        foreach(var nameElement in fieldDecl.Elements(SRC.Name)) {

                            string fieldName = nameElement.Elements(SRC.Name).Any() ? nameElement.Elements(SRC.Name).Last().Value : nameElement.Value;
                            if(PrintSwum) {
                                Console.WriteLine("Field: {0}, Name: {1}", fieldDecl.Value, fieldName);
                            }

                            FieldDeclarationNode fdn = new FieldDeclarationNode(fieldName, ContextBuilder.BuildFieldContext(fieldDecl));
                            builder.ApplyRules(fdn);
                            fieldSwum[string.Format("{0}:{1}:{2}", fileName, fieldDecl.Value, declPos)] = fdn;
                            if(PrintSwum) {
                                Console.WriteLine(fdn.ToString() + Environment.NewLine);
                            }

                            fieldCount++;
                            declPos++;
                        }
                    }
                }
                
            } 

            GC.Collect();

            Console.WriteLine("{0} functions analyzed", methodCount);
            Console.WriteLine("{0} functions in dictionary", methodSwum.Count);
            Console.WriteLine("{0} fields analyzed", fieldCount);
            Console.WriteLine("{0} fields in dictionary", fieldSwum.Count);

            if(Pause) {
                Console.WriteLine("Finished building SWUM (press Enter)");
                Console.ReadLine();
            }
        }
Пример #32
0
        public void EmptyElementTest()
        {
            File.WriteAllText("test.xml", @"<?xml version=""1.0"" encoding=""utf-8""?>
<unit  xmlns=""http://www.sdml.info/srcML/src"">
<unit languageFilter=""C"" dir=""c:\Test"" filename=""beforeBlank.c""><expr_stmt /></unit>
<unit languageFilter=""C"" dir=""c:\Test"" filename=""foo.c"" />
<unit languageFilter=""C"" dir=""c:\Test"" filename=""bar.c""><comment type=""line"">//line1</comment>
<comment type=""line"">//line2</comment>
</unit>
</unit>");
            var doc = new SrcMLFile("test.xml");
            Assert.AreEqual(3, doc.FileUnits.Count());
        }
Пример #33
0
        public void GenerateSrcMLFromFiles(IEnumerable<string> sourceFileNames, string xmlFileName) {
            var filesByLanguage = new Dictionary<Language, List<string>>();
            //determine which runner should process each source file
            foreach(var sourceFile in sourceFileNames) {
                var ext = Path.GetExtension(sourceFile);
                if(ext != null && extensionMapping.ContainsKey(ext)) {
                    Language lang = extensionMapping[ext];
                    if(nonDefaultExecutables.ContainsKey(lang)) {
                        //this file should be parsed by a non-default runner
                        if(!filesByLanguage.ContainsKey(lang)) {
                            filesByLanguage[lang] = new List<string>() { sourceFile };
                        } else {
                            filesByLanguage[lang].Add(sourceFile);
                        }
                    } else {
                        //should be parsed by the default runner
                        if(!filesByLanguage.ContainsKey(Language.Any)) {
                            filesByLanguage[Language.Any] = new List<string>() { sourceFile };
                        } else {
                            filesByLanguage[Language.Any].Add(sourceFile);
                        }
                    }
                }
            }

            //convert files to srcml
            SrcMLFile tempArchive = null;
            foreach(var kvp in filesByLanguage) {
                var tempOutputFile = Path.GetTempFileName();
                SrcMLFile tempResult;
                if(kvp.Key == Language.Any) {
                    var mapForRunner = CreateExtensionMappingForRunner(defaultExecutable);
                    defaultExecutable.GenerateSrcMLFromFiles(kvp.Value, tempOutputFile, Language.Any, new Collection<string>(defaultArguments.ToList()), mapForRunner);
                } else {
                    var runner = nonDefaultExecutables[kvp.Key];
                    var mapForRunner = CreateExtensionMappingForRunner(runner);
                    runner.GenerateSrcMLFromFiles(kvp.Value, tempOutputFile, kvp.Key, new Collection<string>(nonDefaultArguments[kvp.Key].ToList()), mapForRunner);
                }
                tempResult = new SrcMLFile(tempOutputFile);
                var oldArchiveFile = tempArchive != null ? tempArchive.FileName : null;
                tempArchive = tempResult.Merge(tempArchive, Path.GetTempFileName());
                File.Delete(tempOutputFile);
                if(oldArchiveFile != null) { File.Delete(oldArchiveFile); }
            }

            if(tempArchive != null) {
                if(File.Exists(xmlFileName)) {
                    File.Delete(xmlFileName);
                }
                File.Move(tempArchive.FileName, xmlFileName);
            }
        }
Пример #34
0
        /// <summary>
        /// Generate both a SrcML XElement and document from the difference between <see cref="nameOfOriginalFile"/> and <see cref="modifiedFileName"/>. The language will be inferred from the extension.
        /// </summary>
        /// <param name="nameOfOriginalFile">path to the source file to convert.</param>
        /// <param name="modifiedFileName">The File name to write the resulting XML to.</param>
        /// <param name="outputXmlFileName">The File name to write the resulting XML to.</param>
        /// <returns>An XElement representing the source.</returns>
        public XElement GenerateSrcDiffAndXElementFromFile(string nameOfOriginalFile, string modifiedFileName, string outputXmlFileName)
        {
            SrcMLFile srcMLFile = GenerateSrcDiffFromFile(nameOfOriginalFile, modifiedFileName, outputXmlFileName);

            return(srcMLFile.FileUnits.FirstOrDefault());
        }
Пример #35
0
        public string ExtractSourceFile(SrcMLFile doc, int unitIndex)
        {
            if (null == doc)
                throw new ArgumentNullException("doc");
            if (unitIndex == int.MaxValue)
                throw new ArgumentOutOfRangeException("unitIndex", "Unit index must be less than Int32.MaxValue");
            
            string output, outfile = Path.GetTempFileName();
            string arguments = String.Format(CultureInfo.InvariantCulture, "-U {0} \"{1}\" \"{2}\"", unitIndex + 1, doc.FileName, outfile);

            try
            {
                KsuAdapter.RunExecutable(this.srcml2src_exe, arguments);
            }
            catch (SrcMLRuntimeException e)
            {
                throw new SrcMLException("srcml2src encountered an error", e);
            }

            using (StreamReader reader = new StreamReader(outfile))
            {
                output = reader.ReadToEnd();
            }

            File.Delete(outfile);

            return output;
        }
Пример #36
0
        public void GenerateSrcMLFromFiles(IEnumerable <string> sourceFileNames, string xmlFileName)
        {
            var filesByLanguage = new Dictionary <Language, List <string> >();

            //determine which runner should process each source file
            foreach (var sourceFile in sourceFileNames)
            {
                var ext = Path.GetExtension(sourceFile);
                if (ext != null && extensionMapping.ContainsKey(ext))
                {
                    Language lang = extensionMapping[ext];
                    if (nonDefaultExecutables.ContainsKey(lang))
                    {
                        //this file should be parsed by a non-default runner
                        if (!filesByLanguage.ContainsKey(lang))
                        {
                            filesByLanguage[lang] = new List <string>()
                            {
                                sourceFile
                            };
                        }
                        else
                        {
                            filesByLanguage[lang].Add(sourceFile);
                        }
                    }
                    else
                    {
                        //should be parsed by the default runner
                        if (!filesByLanguage.ContainsKey(Language.Any))
                        {
                            filesByLanguage[Language.Any] = new List <string>()
                            {
                                sourceFile
                            };
                        }
                        else
                        {
                            filesByLanguage[Language.Any].Add(sourceFile);
                        }
                    }
                }
            }

            //convert files to srcml
            SrcMLFile tempArchive = null;

            foreach (var kvp in filesByLanguage)
            {
                var       tempOutputFile = Path.GetTempFileName();
                SrcMLFile tempResult;
                if (kvp.Key == Language.Any)
                {
                    var mapForRunner = CreateExtensionMappingForRunner(defaultExecutable);
                    defaultExecutable.GenerateSrcMLFromFiles(kvp.Value, tempOutputFile, Language.Any, new Collection <string>(defaultArguments.ToList()), mapForRunner);
                }
                else
                {
                    var runner       = nonDefaultExecutables[kvp.Key];
                    var mapForRunner = CreateExtensionMappingForRunner(runner);
                    runner.GenerateSrcMLFromFiles(kvp.Value, tempOutputFile, kvp.Key, new Collection <string>(nonDefaultArguments[kvp.Key].ToList()), mapForRunner);
                }
                tempResult = new SrcMLFile(tempOutputFile);
                var oldArchiveFile = tempArchive != null ? tempArchive.FileName : null;
                tempArchive = tempResult.Merge(tempArchive, Path.GetTempFileName());
                File.Delete(tempOutputFile);
                if (oldArchiveFile != null)
                {
                    File.Delete(oldArchiveFile);
                }
            }

            if (tempArchive != null)
            {
                if (File.Exists(xmlFileName))
                {
                    File.Delete(xmlFileName);
                }
                File.Move(tempArchive.FileName, xmlFileName);
            }
        }
Пример #37
0
        public void DisjunctMergedFileTest()
        {
            File.WriteAllText("test.xml", @"<?xml version=""1.0"" encoding=""utf-8""?><unit xmlns=""http://www.sdml.info/srcML/src"">
<unit languageFilter=""C"" filename=""c:\Test\foo.c""></unit>
<unit languageFilter=""C"" filename=""z:\Release\bar.c""></unit>
</unit>");

            var doc = new SrcMLFile("test.xml");

            Assert.IsNotNull(doc);
            Assert.IsNull(doc.ProjectDirectory, String.Format("The ProjectDirectory property should be null when there is no common path. ({0})", doc.ProjectDirectory));
        }
Пример #38
0
        public override void Execute()
        {
            SrcMLFile testFile = new SrcMLFile(this.File);
            XElement firstFile = testFile.FileUnits.First();

            //get all the functions
            var containers = new System.Collections.ObjectModel.Collection<XName>() { SRC.Function, SRC.Constructor, SRC.Destructor };
            var funcs = from func in firstFile.Descendants()
                        where containers.Any(c => c == func.Name)
                        select func;

            Dictionary<XElement, string> functionComments = new Dictionary<XElement, string>();


            //grab the comment block for each function
            foreach (var func in funcs)
            {
                StringBuilder functionComment = new StringBuilder();

                var prevElements = func.ElementsBeforeSelf().Reverse();
                foreach (var element in prevElements)
                {
                    if (element.Name == SRC.Comment)
                    {
                        //add comment to beginning of comment block
                        functionComment.Insert(0, element.Value + System.Environment.NewLine);
                    }
                    else
                    {
                        //found something besides a comment
                        break;
                    }
                }

                functionComments[func] = functionComment.ToString();
            }

            AbbreviationExpander ae = new AbbreviationExpander();
            ae.Expand(Word, functionComments.First().Key, functionComments.First().Value, "");

            //string patternRegex;
            //string shortForm = Word;
            //StringBuilder sb = new StringBuilder(@"\b");
            //for (int i = 0; i < shortForm.Length - 1; i++)
            //{
            //    sb.AppendFormat(@"{0}\w+\s+", shortForm[i]);
            //}
            //sb.AppendFormat(@"{0}\w+\b", shortForm[shortForm.Length - 1]);
            //patternRegex = sb.ToString();

            //Console.WriteLine("Acronym regex: {0}", patternRegex);

            //string methodComment = functionComments.First().Value;
            //Match m = Regex.Match(methodComment, string.Format("({0})", patternRegex), RegexOptions.IgnoreCase);
            //if (m.Success)
            //{
            //    Console.WriteLine("Found match: {0}", m.Groups[1].Value);
            //}
        }