private static void ProcessDirectory(string ADirName, string ASelectedLocalisation) { List <string> FilesToProcess = new List <string>(); List <string> AbstractFiles = new List <string>(); foreach (string file in System.IO.Directory.GetFiles(ADirName, "*.yaml")) { string baseyaml; if (TYml2Xml.ReadHeader(file, out baseyaml)) { if (!AbstractFiles.Contains(baseyaml)) { AbstractFiles.Add(baseyaml); if (FilesToProcess.Contains(baseyaml)) { FilesToProcess.Remove(baseyaml); } } if (!AbstractFiles.Contains(Path.GetFileName(file)) && !FilesToProcess.Contains(Path.GetFileName(file))) { FilesToProcess.Add(Path.GetFileName(file)); } } } foreach (string file in FilesToProcess) { Console.WriteLine("working on " + file); ProcessFile(ADirName + Path.DirectorySeparatorChar + file, ASelectedLocalisation); } foreach (string subdir in System.IO.Directory.GetDirectories(ADirName)) { ProcessDirectory(subdir, ASelectedLocalisation); } }
/// <summary> /// this loads the contents of the yaml file. /// it supports inheritance, base elements are overwritten /// </summary> /// <param name="AYamlFilename"></param> /// <param name="ASelectedLocalisation"></param> /// <param name="AAlreadyGotLocalisation"></param> /// <param name="depth">0 is the last file that is derived from all base files</param> /// <returns></returns> protected Boolean LoadRecursively(string AYamlFilename, string ASelectedLocalisation, bool AAlreadyGotLocalisation, Int32 depth) { // check if file exists for localisation string localisedFile = null; if ((ASelectedLocalisation != null) && !AAlreadyGotLocalisation) { localisedFile = Path.GetDirectoryName(AYamlFilename) + Path.DirectorySeparatorChar + Path.GetFileNameWithoutExtension(AYamlFilename) + "." + ASelectedLocalisation + ".yaml"; // first check if there is such a file if (!File.Exists(localisedFile)) { localisedFile = null; } } if (localisedFile == null) { localisedFile = AYamlFilename; } string baseyaml; if (!TYml2Xml.ReadHeader(localisedFile, out baseyaml)) { throw new Exception("This is not an OpenPetra Yaml file"); } if ((baseyaml.Length > 0) && baseyaml.EndsWith(".yaml")) { LoadRecursively(System.IO.Path.GetDirectoryName(AYamlFilename) + System.IO.Path.DirectorySeparatorChar + baseyaml, ASelectedLocalisation, localisedFile != AYamlFilename, depth - 1); } if ((depth == 0) && (FCodeStorage.FXmlNodes != null)) { // apply the tag, so that we know which things have been changed by the last yml file TYml2Xml.Tag((XmlNode)FCodeStorage.FXmlNodes[TParseYAMLFormsDefinition.ROOTNODEYML]); } XmlDocument newDoc = null; localisedFile = Path.GetFullPath(localisedFile); if (CachedYamlFiles.ContainsKey(localisedFile)) { newDoc = CachedYamlFiles[localisedFile]; } else { Console.WriteLine("Loading " + localisedFile + "..."); TYml2Xml yml2xml = new TYml2Xml(localisedFile); newDoc = yml2xml.ParseYML2XML(); CachedYamlFiles.Add(localisedFile, newDoc); } TYml2Xml.Merge(ref FCodeStorage.FXmlDocument, newDoc, depth); if (TLogging.DebugLevel > 0) { // for debugging: FCodeStorage.FXmlDocument.Save(localisedFile + ".xml"); } FCodeStorage.FXmlNodes = TYml2Xml.ReferenceNodes(FCodeStorage.FXmlDocument); FCodeStorage.FRootNode = (XmlNode)FCodeStorage.FXmlNodes[TParseYAMLFormsDefinition.ROOTNODEYML]; if (baseyaml.Length == 0) { if (FCodeStorage.FXmlNodes[TYml2Xml.ROOTNODEINTERNAL] == null) { throw new Exception("TParseYAMLFormsDefinition.LoadRecursively: YML Document could not be properly parsed"); } if (TXMLParser.GetAttribute((XmlNode)FCodeStorage.FXmlNodes[TParseYAMLFormsDefinition.ROOTNODEYML], "BaseYaml").Length > 0) { throw new Exception("The BaseYaml attribute must come first!"); } } if (depth == 0) { FCodeStorage.FFilename = AYamlFilename; LoadData(FCodeStorage.FXmlNodes); } return(true); }
/// <summary> /// process the yaml document /// </summary> /// <returns></returns> public Boolean ProcessDocument() { string baseyaml; if (!TYml2Xml.ReadHeader(FYamlFilename, out baseyaml)) { Console.WriteLine("ProcessYAML: cannot recognise type of form"); } else { new TAppSettingsManager(false); //****************** //* parsing ******* //****************** XmlDocument myDoc = TYml2Xml.CreateXmlDocument(); TCodeStorage codeStorage = new TCodeStorage(myDoc, FXmlNodes); TParseYAMLFormsDefinition yamlParser = new TParseYAMLFormsDefinition(ref codeStorage); // should not need to be specific to special forms yamlParser.LoadRecursively(FYamlFilename, FSelectedLocalisation); // for debugging purposes, we can write the xml file that has been parsed from the yaml file // codeStorage.FXmlDocument.Save(FYamlFilename + ".xml"); //**************** //* output ******* //**************** TFormWriter writer = null; // get the appropriate derived class from IFormWriter (e.g. TFrmReportWriter) XmlNode rootNode = (XmlNode)yamlParser.FCodeStorage.FXmlNodes[TParseYAMLFormsDefinition.ROOTNODEYML]; string formType = TYml2Xml.GetAttribute(rootNode, "FormType"); if (formType == "abstract") { Console.WriteLine("Ignore yaml file because it has the formtype abstract: " + FYamlFilename); return(true); } // the Template attribute is also quite important, because it determines which code is written // FormType is mainly important for the difference of the controls of reports and normal screens writer = GetWriter(formType); if (writer == null) { Console.WriteLine("cannot find writer for {0}", formType); return(false); } string templateDir = TAppSettingsManager.GetValue("TemplateDir", true); string template = TYml2Xml.GetAttribute(rootNode, "Template"); if (template.Length > 0) { template = templateDir + Path.DirectorySeparatorChar + template + writer.CodeFileExtension; } string destinationFile = writer.CalculateDestinationFilename(FYamlFilename); string manualCodeFile = writer.CalculateManualCodeFilename(FYamlFilename); // need to know the path to the manual code file in order to call manual functions which would not be called if they do not exist codeStorage.ManualCodeFilename = manualCodeFile; writer.CreateCode(codeStorage, FYamlFilename, template); writer.CreateResourceFile(FYamlFilename, templateDir); writer.CreateDesignerFile(FYamlFilename, rootNode, templateDir); return(writer.WriteFile(destinationFile)); } return(false); }