void ProcessAttributes(int iChannel, XmlNode iTargetNode, ProcessInclude iInclude) { foreach (XmlAttribute lAttr in iTargetNode.Attributes) { lAttr.Value = lAttr.Value.Replace("%C%", iChannel.ToString()); lAttr.Value = ReplaceKoTemplate(lAttr.Value, iChannel, iInclude); // lAttr.Value = lAttr.Value.Replace("%N%", mChannelCount.ToString()); } }
static private int VerbCheck(CheckOptions opts) { string lFileName = Path.ChangeExtension(opts.XmlFileName, "xml"); Console.WriteLine("Reading and resolving xml file {0}", lFileName); ProcessInclude lResult = ProcessInclude.Factory(opts.XmlFileName, "", ""); lResult.LoadAdvanced(lFileName); return(ProcessSanityChecks(lResult.GetDocument()) ? 0 : 1); }
static private int VerbCreate(CreateOptions opts) { WriteVersion(); string lHeaderFileName = Path.ChangeExtension(opts.XmlFileName, "h"); if (opts.HeaderFileName != "") { lHeaderFileName = opts.HeaderFileName; } Console.WriteLine("Processing xml file {0}", opts.XmlFileName); ProcessInclude lResult = ProcessInclude.Factory(opts.XmlFileName, lHeaderFileName, opts.Prefix); lResult.Expand(); // We restore the original namespace in File lResult.SetNamespace(); XmlDocument lXml = lResult.GetDocument(); bool lSuccess = ProcessSanityChecks(lXml); string lTempXmlFileName = Path.GetTempFileName(); File.Delete(lTempXmlFileName); if (opts.Debug) { lTempXmlFileName = opts.XmlFileName; } lTempXmlFileName = Path.ChangeExtension(lTempXmlFileName, "debug.xml"); if (opts.Debug) { Console.WriteLine("Writing debug file to {0}", lTempXmlFileName); } lXml.Save(lTempXmlFileName); Console.WriteLine("Writing header file to {0}", lHeaderFileName); File.WriteAllText(lHeaderFileName, lResult.HeaderGenerated); string lOutputFileName = Path.ChangeExtension(opts.OutputFile, "knxprod"); if (opts.OutputFile == "") { lOutputFileName = Path.ChangeExtension(opts.XmlFileName, "knxprod"); } if (lSuccess) { string lEtsPath = FindEtsPath(lTempXmlFileName); ExportKnxprod(lEtsPath, lTempXmlFileName, lOutputFileName); } else { Console.WriteLine("--> Skipping creation of {0} due to check errors! <--", lOutputFileName); } if (!opts.Debug) { File.Delete(lTempXmlFileName); } return(0); }
void ProcessParameter(int iChannel, XmlNode iTargetNode, ProcessInclude iInclude) { //calculate new offset XmlNode lMemory = iTargetNode.SelectSingleNode("Memory"); if (lMemory != null) { XmlNode lAttr = lMemory.Attributes.GetNamedItem("Offset"); int lOffset = int.Parse(lAttr.Value); lOffset += iInclude.ParameterBlockOffset + (iChannel - 1) * iInclude.ParameterBlockSize; lAttr.Value = lOffset.ToString(); } }
void ProcessUnion(int iChannel, XmlNode iTargetNode, ProcessInclude iInclude) { //calculate new offset ProcessParameter(iChannel, iTargetNode, iInclude); XmlNodeList lChildren = iTargetNode.ChildNodes; foreach (XmlNode lChild in lChildren) { if (lChild.Name == "Parameter") { ProcessAttributes(iChannel, lChild, iInclude); } } }
public static ProcessInclude Factory(string iXmlFileName, string iHeaderFileName, string iHeaderPrefixName) { ProcessInclude lInclude = null; if (gIncludes.ContainsKey(iXmlFileName)) { lInclude = gIncludes[iXmlFileName]; } else { lInclude = new ProcessInclude(iXmlFileName, iHeaderFileName, iHeaderPrefixName); gIncludes.Add(iXmlFileName, lInclude); } return(lInclude); }
void ProcessChannel(int iChannel, XmlNode iTargetNode, ProcessInclude iInclude) { //attributes of the node if (iTargetNode.Attributes != null) { ProcessAttributes(iChannel, iTargetNode, iInclude); } //Print individual children of the node, gets only direct children of the node XmlNodeList lChildren = iTargetNode.ChildNodes; foreach (XmlNode lChild in lChildren) { ProcessChannel(iChannel, lChild, iInclude); } }
public void ExportHeader(string iHeaderFileName, string iHeaderPrefixName, ProcessInclude iInclude, XmlNodeList iChildren = null) { // iInclude.ParseHeaderFile(iHeaderFileName); if (mParameterTypesNode == null) { // before we start with template processing, we calculate all Parameter relevant info mParameterTypesNode = mDocument.SelectSingleNode("//ParameterTypes"); } if (mParameterTypesNode != null) { // the main document contains necessary ParameterTypes definitions // there are new parameters in include, we have to calculate a new parameter offset XmlNodeList lParameterNodes = mDocument.SelectNodes("//Parameters/Parameter|//Parameters/Union"); if (lParameterNodes != null) { mParameterBlockSize = CalcParamSize(lParameterNodes, mParameterTypesNode); } if (iChildren != null) { // ... and we do parameter processing, so we calculate ParamBlockSize for this include int lBlockSize = iInclude.CalcParamSize(iChildren, mParameterTypesNode); if (lBlockSize > 0) { iInclude.ParameterBlockSize = lBlockSize; // we calculate also ParamOffset iInclude.ParameterBlockOffset = mParameterBlockSize; } } } // Header file generation is only possible before we resolve includes // First we serialize local parameters of this instance ExportHeaderParameterStart(mHeaderGenerated, mParameterTypesNode, iHeaderPrefixName); // followed by template parameters of the include if (iInclude != this) { iInclude.ExportHeaderParameterBlock(mHeaderGenerated, mParameterTypesNode, iHeaderPrefixName); } ExportHeaderKoStart(mHeaderGenerated, iHeaderPrefixName); if (iInclude != this) { iInclude.ExportHeaderKoBlock(mHeaderGenerated, iHeaderPrefixName); } }
void ProcessTemplate(int iChannel, XmlNode iTargetNode, ProcessInclude iInclude) { ProcessAttributes(iChannel, iTargetNode, iInclude); if (iTargetNode.Name == "Parameter") { ProcessParameter(iChannel, iTargetNode, iInclude); } else if (iTargetNode.Name == "Union") { ProcessUnion(iChannel, iTargetNode, iInclude); } else if (iTargetNode.Name == "Channel" || iTargetNode.Name == "ParameterBlock" || iTargetNode.Name == "choose") { ProcessChannel(iChannel, iTargetNode, iInclude); } }
string ReplaceKoTemplate(string iValue, int iChannel, ProcessInclude iInclude) { string lResult = iValue; Match lMatch = Regex.Match(iValue, @"%K(\d{1,3})%"); int lBlockSize = 0; int lOffset = 0; if (iInclude != null) { lBlockSize = iInclude.mKoBlockSize; lOffset = iInclude.KoOffset; } // MatchCollection lMatches = Regex.Matches(iValue, @"%K(\d{1,3})%"); if (lMatch.Captures.Count > 0) { int lShift = int.Parse(lMatch.Groups[1].Value); lResult = iValue.Replace(lMatch.Value, ((iChannel - 1) * lBlockSize + lOffset + lShift).ToString()); } return(lResult); }
/// <summary> /// Resolves Includes inside xml document /// </summary> /// <param name="iCurrentDir">Directory to use for relative href expressions</param> public void ResolveIncludes(string iCurrentDir) { nsmgr = new XmlNamespaceManager(mDocument.NameTable); nsmgr.AddNamespace("mc", cOwnNamespace); // process define node XmlNodeList lDefineNodes = mDocument.SelectNodes("//mc:define", nsmgr); if (lDefineNodes != null && lDefineNodes.Count > 0) { foreach (XmlNode lDefineNode in lDefineNodes) { DefineContent.Factory(lDefineNode); } } //find all XIncludes in a copy of the document XmlNodeList lIncludeNodes = mDocument.SelectNodes("//mc:include", nsmgr); // get all <include> nodes foreach (XmlNode lIncludeNode in lIncludeNodes) // try { //Load document... string lIncludeName = lIncludeNode.Attributes.GetNamedItemValueOrEmpty("href"); string lHeaderPrefixName = lIncludeNode.Attributes.GetNamedItemValueOrEmpty("prefix"); DefineContent lDefine = DefineContent.GetDefineContent(lHeaderPrefixName); bool lIsTemplate = (lIncludeNode.Attributes.GetNamedItemValueOrEmpty("type") == "template"); bool lIsParameter = (lIncludeNode.Attributes.GetNamedItemValueOrEmpty("type") == "parameter"); ProcessInclude lInclude = ProcessInclude.Factory(lIncludeName, lDefine.header, lHeaderPrefixName); lHeaderPrefixName = lInclude.mHeaderPrefixName; lDefine = DefineContent.GetDefineContent(lHeaderPrefixName.Trim('_')); string lTargetPath = Path.Combine(iCurrentDir, lIncludeName); lInclude.LoadAdvanced(lTargetPath); //...find include in real document... XmlNode lParent = lIncludeNode.ParentNode; string lXPath = lIncludeNode.Attributes.GetNamedItemValueOrEmpty("xpath"); XmlNodeList lChildren = lInclude.SelectNodes(lXPath); string lHeaderFileName = Path.Combine(iCurrentDir, lDefine.header); // if (lIsParameter) { // ExportHeader(lHeaderFileName, lHeaderPrefixName, lInclude, lChildren); // } if (lIsTemplate) { if (lChildren.Count > 0 && "Parameter | Union | ComObject".Contains(lChildren[0].LocalName)) { // at this point we are including a template file // ChannelCount and KoOffset are taken from correct prefix lInclude.ChannelCount = lDefine.NumChannels; lInclude.KoOffset = lDefine.KoOffset; ExportHeader(lHeaderFileName, lHeaderPrefixName, lInclude, lChildren); } } // here we do template processing and repeat the template as many times as // the Channels parameter in header file for (int lChannel = 1; lChannel <= lInclude.ChannelCount; lChannel++) { foreach (XmlNode lChild in lChildren) { //necessary for move between XmlDocument contexts XmlNode lImportNode = lParent.OwnerDocument.ImportNode(lChild, true); // for any Parameter node we do offset recalculation // if there is no prefixname, we do no template replacement if (lHeaderPrefixName != "") { ProcessTemplate(lChannel, lImportNode, lInclude); } lParent.InsertBefore(lImportNode, lIncludeNode); } } lParent.RemoveChild(lIncludeNode); if (lInclude.ChannelCount > 1) { ReplaceDocumentStrings("%N%", lInclude.ChannelCount.ToString()); } // if (lHeaderPrefixName != "") ProcessIncludeFinish(lChildren); //if this fails, something is wrong } if (lDefineNodes != null && lDefineNodes.Count > 0) { foreach (XmlNode lDefineNode in lDefineNodes) { lDefineNode.ParentNode.RemoveChild(lDefineNode); } } // catch { } }