public XElement GetFileUnitForXmlSnippet(string xmlSnippet, string fileName) { var xml = string.Format(FileTemplate, xmlSnippet, KsuAdapter.GetLanguage(SourceLanguage), fileName); var fileUnit = XElement.Parse(xml, LoadOptions.PreserveWhitespace); return(fileUnit); }
/// <summary> /// Runs <see cref="ExecutablePath"/> with the specified arguments. <paramref name="standardInput"/> is passed in to the process's standard input stream /// </summary> /// <param name="arguments">configuration arguments</param> /// <param name="standardInput">contents of standard input</param> /// <returns>contents of standard output</returns> private string Run(Collection <string> arguments, string standardInput) { string argumentText = KsuAdapter.MakeArgumentString(arguments); var output = KsuAdapter.RunExecutable(this.ExecutablePath, argumentText, standardInput); return(output); }
protected override string ComputeMergeId() { if (IsGlobal) { return("NG"); } else if (IsAnonymous) { return(base.ComputeMergeId()); } else { return(String.Format("{0}:N:{1}", KsuAdapter.GetLanguage(ProgrammingLanguage), this.Name)); } }
/// <summary> /// Generate SrcML from a given string of source code. /// </summary> /// <param name="source">A string containing the source code to parse.</param> /// <param name="language">The source language to use (C,C++,Java,AspectJ). /// If the source languageFilter is either not in this list or is null, the default source language (C++) will be used.</param> /// <returns>XML representing the source.</returns> public string GenerateSrcMLFromString(string source, Language language) { Collection <string> arguments = DefaultNamespaceArguments; arguments.Add("--no-xml-declaration"); arguments.Add(String.Format(CultureInfo.InvariantCulture, "--language={0}", KsuAdapter.GetLanguage(language))); var argumentString = KsuAdapter.MakeArgumentString(arguments); try { var xml = KsuAdapter.RunExecutable(this.src2srcml_exe, argumentString, source); return(xml); } catch (SrcMLRuntimeException e) { throw new SrcMLException("src2srcml encountered an error", e); } }
//public override void RemoveFile(string fileName) { // int definitionLocationCount = RemoveLocations(fileName); // RemoveFile(fileName, 0 == definitionLocationCount); //} protected override string ComputeMergeId() { if (Language.Java == ProgrammingLanguage || Language.CSharp == ProgrammingLanguage && !IsPartial) { return(base.ComputeMergeId()); } char typeSpecifier; switch (Kind) { case TypeKind.Class: typeSpecifier = 'C'; break; case TypeKind.Enumeration: typeSpecifier = 'E'; break; case TypeKind.Interface: typeSpecifier = 'I'; break; case TypeKind.Struct: typeSpecifier = 'S'; break; case TypeKind.Union: typeSpecifier = 'U'; break; default: typeSpecifier = 'T'; break; } string id = String.Format("{0}:T{1}:{2}", KsuAdapter.GetLanguage(ProgrammingLanguage), typeSpecifier, this.Name); return(id); }
private void generateSrcMLDoc(string path, string xmlFileName, Language language) { var arguments = DefaultNamespaceArguments; if (language > Language.Any) { arguments.Add(String.Format(CultureInfo.InvariantCulture, "--language={0}", KsuAdapter.GetLanguage(language))); } arguments.Add(String.Format(CultureInfo.InvariantCulture, "\"{0}\"", path)); arguments.Add(String.Format(CultureInfo.InvariantCulture, "--output=\"{0}\"", xmlFileName)); var argumentString = KsuAdapter.MakeArgumentString(arguments); try { KsuAdapter.RunExecutable(this.src2srcml_exe, argumentString); } catch (SrcMLRuntimeException e) { throw new SrcMLException("src2srcml.exe encountered an error", e); } }
private void generateSrcMLDoc(string rootDirectory, string xmlFileName, IEnumerable <string> fileNames) { var arguments = DefaultNamespaceArguments; var tempFileListing = Path.GetTempFileName(); using (StreamWriter writer = new StreamWriter(tempFileListing)) { foreach (var fileName in fileNames) { writer.WriteLine(fileName); } } arguments.Add(String.Format(CultureInfo.InvariantCulture, "--files-from=\"{0}\"", tempFileListing)); arguments.Add(String.Format(CultureInfo.InvariantCulture, "--directory=\"{0}\"", rootDirectory)); arguments.Add(String.Format(CultureInfo.InvariantCulture, "--output=\"{0}\"", xmlFileName)); if (ExtensionMapping.NonDefaultValueCount > 0) { arguments.Add(String.Format(CultureInfo.InvariantCulture, "--register-ext {0}", KsuAdapter.ConvertMappingToString(ExtensionMapping))); } var argumentString = KsuAdapter.MakeArgumentString(arguments); try { KsuAdapter.RunExecutable(this.src2srcml_exe, argumentString); } catch (SrcMLRuntimeException e) { throw new SrcMLException("src2srcml.exe encountered an error", e); } finally { File.Delete(tempFileListing); } }
protected override string ComputeMergeId() { if (!PrefixIsResolved || Language.Java == ProgrammingLanguage || Language.CSharp == ProgrammingLanguage && !IsPartial) { return(base.ComputeMergeId()); } char methodType = 'M'; if (IsConstructor) { methodType = 'C'; } else if (IsDestructor) { methodType = 'D'; } var parameterTypes = from parameter in Parameters select parameter.VariableType; string id = String.Format("{0}:M{1}:{2}:{3}", KsuAdapter.GetLanguage(ProgrammingLanguage), methodType, this.Name, String.Join(",", parameterTypes)); return(id); }
/// <summary> /// Converts <paramref name="language"/> to <c>--language=LANGUAGE</c> /// </summary> /// <param name="language">The language to use</param> /// <returns>the language command line parameter</returns> private static string MakeLanguageArgument(Language language) { return(language == Language.Any ? String.Empty : String.Format("--language={0}", KsuAdapter.GetLanguage(language))); }
/// <summary> /// Runs <see cref="ExecutablePath"/> with the specified arguments /// </summary> /// <param name="arguments">the arguments</param> private void Run(Collection <string> arguments) { string argumentText = KsuAdapter.MakeArgumentString(arguments); KsuAdapter.RunExecutable(ExecutablePath, argumentText); }
/// <summary> /// Generate SrcML from a given string of source code. /// </summary> /// <param name="source">A string containing the source code to parse.</param> /// <param name="language">The language to parse the string as. Language.Any is not valid.</param> /// <returns>XML representing the source.</returns> public string GenerateSrcMLFromString(string source, Language language) { if (language == Language.Any) { throw new SrcMLException("Any is not a valid language. Pick an actual language in the enumeration"); } Collection <string> arguments = new Collection <string>(this.NamespaceArguments); arguments.Add("--no-xml-declaration"); arguments.Add(String.Format(CultureInfo.InvariantCulture, "--language={0}", KsuAdapter.GetLanguage(language))); var xml = Run(arguments, source); return(xml); }
/// <summary> /// Generates a SrcML document from a collection of source files using the specified language. /// </summary> /// <param name="sourceFileNames">The source files to generate SrcML from.</param> /// <param name="xmlFileName">The file name to write the resulting XML to.</param> /// <param name="language">The language to parse the source files as.</param> /// <returns>A SrcMLFile for <paramref name="xmlFileName"/>.</returns> public SrcMLFile GenerateSrcMLFromFiles(IEnumerable <string> sourceFileNames, string xmlFileName, Language language) { var additionalArguments = new Collection <string>(); if (language > Language.Any) { additionalArguments.Add(String.Format(CultureInfo.InvariantCulture, "--language={0}", KsuAdapter.GetLanguage(language))); } Run(xmlFileName, additionalArguments, new Collection <string>(sourceFileNames.ToList())); return(new SrcMLFile(xmlFileName)); }
private void sourceBox_TextChanged(object sender, TextChangedEventArgs e) { if (0 == sourceBox.Text.Length) { srcmlBox.Text = ""; } else { if (binDirIsValid) { try { _xml = XmlGenerator.GenerateSrcMLFromString(sourceBox.Text, this._language); languageLabel.Content = String.Format("(Code parsed as {0})", KsuAdapter.GetLanguage(this._language)); var doc = XDocument.Parse(_xml, LoadOptions.PreserveWhitespace); this.xmlTree.DataContext = doc; int startIndex = _xml.IndexOf('<', 1); int endIndex = _xml.LastIndexOf('<'); srcmlBox.Text = _xml.Substring(startIndex, endIndex - startIndex); } catch (Win32Exception) { PrintErrorInSrcmlBox("The SrcML directory does not contain a valid copy of src2srcml.exe.\n\nSelect a valid directory from File->Select SrcML Directory..."); this.binDirIsValid = false; } } } }
/// <summary> /// Writes XML attributes from this object to the XML writer /// </summary> /// <param name="writer">The XML writer</param> protected virtual void WriteXmlAttributes(XmlWriter writer) { writer.WriteAttributeString(LanguageXmlName, KsuAdapter.GetLanguage(ProgrammingLanguage)); }
/// <summary> /// Generate a SrcML document from the difference between <see cref="nameOfOriginalFile"/> and <see cref="modifiedFileName"/> with the specified language. /// </summary> /// <param name="nameOfOriginalFile">The 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> /// <param name="language">The language to parse the source file as.</param> /// <returns>A SrcMLFile for <paramref name="xmlFileName"/>.</returns> public SrcMLFile GenerateSrcDiffFromFile(string nameOfOriginalFile, string nameOfModifiedFile, string xmlFileName, Language language) { // Console.WriteLine("xmlFileName = [" + xmlFileName + "]"); if (!File.Exists(nameOfOriginalFile)) { throw new FileNotFoundException(String.Format(CultureInfo.CurrentCulture, "{0} does not exist.", nameOfOriginalFile)); } if (!File.Exists(nameOfModifiedFile)) { throw new FileNotFoundException(String.Format(CultureInfo.CurrentCulture, "{0} does not exist.", nameOfModifiedFile)); } var arguments = new Collection <string>(); if (language > Language.Any) { arguments.Add(String.Format(CultureInfo.InvariantCulture, "--language={0}", KsuAdapter.GetLanguage(language))); } arguments.Add(String.Format("\"{0}\" \"{1}\"", nameOfOriginalFile, nameOfModifiedFile)); Run(xmlFileName, arguments); return(new SrcMLFile(xmlFileName)); }
/// <summary> /// Converts <paramref name="extensionMap"/> to <c>--register-ext EXTENSIONMAP</c> /// </summary> /// <param name="extensionMap">the extension map to use</param> /// <returns>The extension map command line parameter</returns> private static string MakeExtensionMapArgument(Dictionary <string, Language> extensionMap) { return(extensionMap.Count > 0 ? String.Format("--register-ext {0}", KsuAdapter.ConvertMappingToString(extensionMap)) : String.Empty); }
/// <summary> /// Generates a SrcML document from the given path and place it in the XML file. /// The output can be controlled by using <paramref name="filesToExclude"/>, and <paramref name="languageFilter"/> /// </summary> /// <param name="directoryPath">the directory path</param> /// <param name="xmlFileName">the path of the xml file</param> /// <param name="filesToExclude">A collection of files to exclude from <paramref name="xmlFileName"/></param> /// <param name="languageFilter">the language to filter on</param> /// <returns>A SrcMLFile that points at <paramref name="xmlFileName"/></returns> public SrcMLFile GenerateSrcMLFromDirectory(string directoryPath, string xmlFileName, IEnumerable <string> filesToExclude, Language languageFilter) { if (!Directory.Exists(directoryPath)) { throw new DirectoryNotFoundException(String.Format(CultureInfo.CurrentCulture, "{0} does not exist.", directoryPath)); } Collection <string> additionalArguments = new Collection <string>(); DirectoryInfo dir = new DirectoryInfo(directoryPath); var fileObjectsToExclude = from f in filesToExclude select new FileInfo(f); var files = (from filePath in dir.GetFiles("*", SearchOption.AllDirectories) where ExtensionMapping.ContainsKey(filePath.Extension) select filePath).Except(fileObjectsToExclude, new FileInfoComparer()); IEnumerable <string> reducedFileList; if (Language.Any == languageFilter) { reducedFileList = from f in files select f.FullName; } else { additionalArguments.Add(String.Format(CultureInfo.InvariantCulture, "--language={0}", KsuAdapter.GetLanguage(languageFilter))); reducedFileList = from f in files where languageFilter == ExtensionMapping[f.Extension] select f.FullName; } Run(xmlFileName, additionalArguments, new Collection <string>(reducedFileList.ToList())); return(new SrcMLFile(xmlFileName)); }
/// <summary> /// Generate a SrcML document from a single source file with the specified language. /// </summary> /// <param name="sourceFileName">The path to the source file to convert.</param> /// <param name="xmlFileName">The file name to write the resulting XML to.</param> /// <param name="language">The language to parse the source file as.</param> /// <returns>A SrcMLFile for <paramref name="xmlFileName"/>.</returns> public SrcMLFile GenerateSrcMLFromFile(string sourceFileName, string xmlFileName, Language language) { var arguments = new Collection <string>(); if (language > Language.Any) { arguments.Add(String.Format(CultureInfo.InvariantCulture, "--language={0}", KsuAdapter.GetLanguage(language))); } arguments.Add(String.Format("\"{0}\"", sourceFileName)); //Console.WriteLine("sourceFileName = [" + sourceFileName + "]"); //Console.WriteLine("xmlFileName = [" + xmlFileName + "]"); Run(xmlFileName, arguments); return(new SrcMLFile(xmlFileName)); }
public static void Src2SrcML([Required(Description = "Source file or directory of source code to convert")] string source, [Optional(null, Description = @"The file to write SrcML to. By default, this is <Current Directory>\<Directory>-<Date>.xml For instance, if you run .\srcml.exe src2srcml c:\source\python, the resulting output file will be located at .\python-YYYMMDDHHMM.xml")] string outputFileName, [Optional("Any", Description = @"Language to use. Only files for this language will be included. The options are * Any (the default: indicates that all recognized languages should be included) * C * C++ * Java * AspectJ")] string language, [Optional("", Description = @"Mapping of file extensions to languages. This is formatted like this: ""/languageMapping:ext1=LANG;ext2=LANG;ext3=LANG"" For example, to map foo.h and foo.cxx to C++, we would use ""/languageMapping:h=C++;cxx=C++"" All of the languages that are valid for the /language option are valid except for ""Any"". ")] string languageMapping, [Optional(null, Description = @"Folder with SrcML binaries. If this is not given, the following directories are checked: 1. %SRCMLBINDIR% 2. c:\Program Files (x86)\SrcML\bin 3. c:\Program Files\SrcML\bin (only checked if c:\Program Files (x86) does not exist) 4. The current directory")] string binaryFolder) { if (null == outputFileName) { var name = source.Split(new char[] { Path.DirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries).Last(); outputFileName = String.Format("{0}-{1}.xml", name, DateTime.Now.ToString("yyyyMMddHHmmss")); } SrcMLGenerator generator = null; if (null != binaryFolder) { try { generator = GetGeneratorWithDirectory(binaryFolder); } catch (IOException e) { Console.Error.WriteLine("Invalid binary directory: {0}", e.Message); Environment.Exit(-1); } } else { //try //{ // check all of the usual suspects generator = new SrcMLGenerator(); //} //catch(IOException) //{ // generator = null; //} } Language lang = SrcMLElement.GetLanguageFromString(language); if (lang > Language.Any) { Console.WriteLine("Using {0} language", language); } if (String.Empty != languageMapping) { foreach (var pair in ParseLanguageMap(languageMapping)) { Console.WriteLine("Mapping {0} files to {1} language", pair.Extension, KsuAdapter.GetLanguage(pair.Language)); string ext = pair.Extension.StartsWith(".") ? pair.Extension : "." + pair.Extension; generator.ExtensionMapping.Add(ext, pair.Language); } } SrcMLFile doc; if (Directory.Exists(source)) { doc = generator.GenerateSrcMLFileFromDirectory(source, outputFileName, lang); Console.WriteLine("Created {0}, a srcML archive, from {1} files located at {2}", doc.FileName, doc.FileUnits.Count(), source); } else if (File.Exists(source)) { generator.GenerateSrcMLFromFile(source, outputFileName, lang); Console.WriteLine("Converted {0} to a srcML document at {1}", source, outputFileName); } else { Console.Error.WriteLine("the input folder or directory ({0}) does not exist!", source); Environment.Exit(-2); } }
/// <summary> /// Runs this executable and places the output in the specified output file. /// This executable is run with the following string <c>[this.ExecutablePath] --register-ext [ExtensionMapping] --output=[outputfile] [addititionlArguments]</c> /// </summary> /// <param name="outputFile">The output file.</param> /// <param name="additionalArguments">The additional arguments.</param> public void Run(string outputFile, Collection <string> additionalArguments) { if (null == additionalArguments) { throw new ArgumentNullException("additionalArguments"); } var arguments = new Collection <string>(); foreach (var argument in this.NamespaceArguments) { arguments.Add(argument); } if (ExtensionMapping.NonDefaultValueCount > 0) { arguments.Add(String.Format(CultureInfo.InvariantCulture, "--register-ext {0}", KsuAdapter.ConvertMappingToString(ExtensionMapping))); } arguments.Add(String.Format(CultureInfo.InvariantCulture, "--output=\"{0}\"", outputFile)); foreach (var arg in additionalArguments) { arguments.Add(arg); } try { base.Run(arguments); } catch (SrcMLRuntimeException e) { throw new SrcMLException(String.Format(CultureInfo.CurrentCulture, "{0} encountered an error: {1}", this.ExecutablePath, e.Message), e); } }