private void OutputSwitch(CodeFormatter cppFile, List<string> sourceValues, List<string> resultValues, string defaultValue, string sourcePrefix, string resultPrefix)
        {
            cppFile.WriteLine("switch (source)");
            cppFile.WriteLine("{");

            List<string> alreadyDone = new List<string>();

            for (int i = 0; i < sourceValues.Count; i++)
            {
                if (alreadyDone.Contains(sourceValues[i]))
                    continue;

                alreadyDone.Add(sourceValues[i]);

                cppFile.FormatLine("case {0}{1}:", sourcePrefix, sourceValues[i]);
                cppFile.IncreaseIndent();
                cppFile.FormatLine("return {0}{1};", resultPrefix, resultValues[i]);
                cppFile.DecreaseIndent();
            }

            cppFile.WriteLine("default:");
            cppFile.IncreaseIndent();
            cppFile.FormatLine("return {0}{1};", resultPrefix, defaultValue);
            cppFile.DecreaseIndent();


            cppFile.WriteLine("}");
        }
        internal void EmitForeignToFCS(CodeFormatter cppFile, MemberSpec foreignSourceObject, MemberSpec fcsDestObject, string fcsMemberName)
        {
            cppFile.FormatLine("if ({1}{2}{0})", boolMemberName, foreignSourceObject.Name, foreignSourceObject.Type.GetDereferencingOperator() );
            cppFile.WriteLine("{");
            cppFile.FormatLine("{0}{1}{2} = {3}::{4};", fcsDestObject.Name, fcsDestObject.Type.GetDereferencingOperator(), fcsMemberName, FCSType.Name, enumValue);
            cppFile.WriteLine("}");

        }
        public override void EmitFCSToForeign(CodeFormatter cppFile, MemberSpec foreignDestObject, MemberSpec fcsSourceObject)
        {
            foreach (EnumToBoolMapping etbm in mappings)
            {
                etbm.EmitSetToFalse(cppFile, foreignDestObject);
            }
            cppFile.FormatLine("switch ({1}{2}{0})", FCSSpec.Name, fcsSourceObject.Name, fcsSourceObject.Type.GetDereferencingOperator());
            cppFile.WriteLine("{");
            foreach (EnumToBoolMapping etbm in mappings)
            {
                etbm.EmitFCSToForeign(cppFile, foreignDestObject);

            }
            cppFile.WriteLine("}");
        }
 private void AddForeignToFCSConvert(CodeFormatter cppFile)
 {
     MemberSpec dest = new MemberSpec(FCSType, "destination");
     MemberSpec source = new MemberSpec(ForeignType, "source");
     BeginMethod(cppFile, dest.Type, "Convert", source);
     OutputSwitch(cppFile, ForeignValues, FCSValues, FCSDefault, ForeignTypePrefix(), FCSType.Name+"::");
     cppFile.WriteLine("}");
 }
 protected void BeginMethod(CodeFormatter cppFile, TypeSpec returnType, string methodName, params object[] parameters)
 {
     cppFile.WriteLine("");
     StringBuilder bld = new StringBuilder();
     bld.AppendFormat("{0} {1}::{2}(", returnType.ToString(), GetClassName(), methodName);
     for (int i = 0; i < parameters.Length; i++)
     {
         MemberSpec ms = parameters[i] as MemberSpec;
         TypeSpec type = ms.Type;
         string name = ms.Name;
         if (name == null || type == null)
         {
             throw new ArgumentException("Wrong arguments to BeginMethod");
         }
         if (i != 0)
             bld.Append(", ");
         if (name == "source" && type.Modifier == TypeSpec.Modifiers.pointer)
             bld.Append("const ");
         bld.AppendFormat("{0} {1}", type.ToString(), name);
     }
     bld.Append(")");
     cppFile.WriteLine(bld.ToString());
     cppFile.WriteLine("{");
 }
        private void EmitHandleEponymousNode(CodeFormatter cf)
        {
            cf.WriteLine("private void HandleEponymousNode(System.Xml.XmlReader reader)");
            cf.WriteLine("{");

            if (StartIsMarkerOnly)
            {
                cf.WriteLine("if (reader.IsStartElement())");
                cf.WriteLine("{");
                if (HasType)
                {
                    cf.FormatLine("AddCompletedObject(new {0}(true));", TypeName);
                    cf.FormatLine("m_dataObject = new {0}(false);", TypeName);
                }

                EmitFieldInitializers(cf);
                cf.WriteLine("}");
            }

            if (HandleParentNodeContent)
            {
                cf.WriteLine("m_dataObject.Contents = new TextContent(reader.ReadString());");
            }

            if (listAttrMappings.Count > 0 ||
                MustEmitStartNodeCall)
            {
                cf.WriteLine("if (reader.IsStartElement())");
                cf.WriteLine("{");
                cf.WriteLine("if (!m_bSeenOurStartNode)");
                cf.WriteLine("{");
                cf.WriteLine("HandleStartNode(reader);");
                cf.WriteLine("m_bSeenOurStartNode = true;");
                cf.WriteLine("}");
                cf.WriteLine("else");
                cf.WriteLine("{");
                if (mappings.Count > 0 || listGroupsToHandle.Count > 0 || listContentNodes.Count > 0 || listNodesToHandle.Count > 0 ||// simple nodes may not have any 
                    MustEmitProcess) //used where the handling is currently too complex to auto-generate
                {
                    cf.WriteLine("HandleNamedNode(reader);");
                }
                cf.WriteLine("return;");
                cf.WriteLine("}");
                cf.WriteLine("}");
            }


            if (ForceNotifyContext)
            {
                cf.WriteLine("if (reader.IsStartElement())");
                cf.WriteLine("{");
                AddNotifyContextOfNodeStart(cf);
            }
            else
            {
                cf.WriteLine("if (reader.IsStartElement() && !reader.IsEmptyElement)");
                cf.WriteLine("{");

                if (NotifyContext)
                    AddNotifyContextOfNodeStart(cf);
            }
            cf.WriteLine("}");
            cf.WriteLine("else");
            cf.WriteLine("{");

            if (!string.IsNullOrEmpty(PostProcessCode) || MustEmitPostProcess)
            {
                cf.WriteLine("PostProcess(reader);");
            }

            if (NotifyContext ||  ForceNotifyContext)
                AddNotifyContextOfNodeEnd(cf);
         
            if (ForceAddDataObject)
            {
                if (HasStopParentElementDispatchFlag)
                {
                    cf.WriteLine("if (m_dataObject != null && m_dispatchThis)");
                }
                else
                {
                    cf.WriteLine("if (m_dataObject != null)");
                }
                cf.IncreaseIndent();
                cf.WriteLine("AddCompletedObject(m_dataObject);");
                cf.DecreaseIndent();
            }

            if (!StartIsMarkerOnly && IsHasContentType)
            {
                if (HasType)
                {
                    if (HasStopParentElementDispatchFlag)
                    {
                        cf.WriteLine("if (m_dispatchThis)");
                        if (InitializeFieldOnEnd)
                        {
                            cf.WriteLine("{");
                            cf.IncreaseIndent();
                            cf.FormatLine("{0} endObject = new {0}(false);", TypeName);
                            foreach (KeyValuePair<string, string> pair in fieldValueInitializers)
                            {
                                cf.FormatLine("endObject.{0} = {1};", pair.Key, pair.Value);
                            }

                            cf.WriteLine("AddCompletedObject(endObject);");
                            cf.DecreaseIndent();
                            cf.WriteLine("}");
                        }
                        else
                        {
                            cf.IncreaseIndent();
                            cf.FormatLine("AddCompletedObject(new {0}(false));", TypeName);
                            cf.DecreaseIndent();
                        }
                    }
                    else
                    {
                        if (InitializeFieldOnEnd)
                        {
                            cf.WriteLine("if ( m_dataObject != null)");
                            cf.WriteLine("{");
                            cf.WriteLine("AddCompletedObject(m_dataObject);");
                            cf.WriteLine("}");
                            cf.WriteLine("else");
                            cf.WriteLine("{");
                            cf.FormatLine("{0} endObject = new {0}(false);", TypeName);
                            foreach (KeyValuePair<string, string> pair in fieldValueInitializers)
                            {
                                cf.FormatLine("endObject.{0} = {1};", pair.Key, pair.Value);
                            }

                            cf.WriteLine("AddCompletedObject(endObject);");
                            cf.WriteLine("}");
                        }
                        else
                        {
                            cf.FormatLine("AddCompletedObject(new {0}(false));", TypeName);
                        }
                    }
                }
            }
            else
            {
                if (HasType)
                {
                    if(HasStopParentElementDispatchFlag)
                    {
                        cf.WriteLine("if (m_dispatchThis)");
                        cf.IncreaseIndent();
                        cf.WriteLine("AddCompletedObject(m_dataObject);");
                        cf.DecreaseIndent();
                    }
                    else
                    {
                       cf.WriteLine("AddCompletedObject(m_dataObject);");
                    }
                }
            }

            cf.WriteLine("IsFinished = true;");
            if (HasType)
                cf.WriteLine("m_dataObject = null;");

            cf.WriteLine("}");

            cf.WriteLine("}");

        }
        private void HandleSingleNode(CodeFormatter cf, string item, bool bSuppressParentEmit)
        {
            if (!GenerationContext.CanHandle(item))
            {
                cf.FormatLine("// case \"{0}\": Unhandled", item);
            }
            else
            {
                cf.BeginCase("\"" + item + "\"");
                cf.FormatLine("SubConsumer = new {0}_Handler(Context);", item);
                if (!bSuppressParentEmit && !StartIsMarkerOnly)
                {
                    if (HasType)
                    {
                        cf.WriteLine("AddCompletedObject(m_dataObject);");
                        cf.WriteLine("m_dataObject = null;");
                    }
                }
                cf.EndCase();

                AddHandledSubNode(item);
            }
        }
        private void EmitContentNodeHandlers(CodeFormatter cf)
        {
            foreach (string nodeName in listContentNodes)
            {
                if (string.IsNullOrEmpty(nodeName))
                    continue;

                if (GenerationContext.CanHandle(nodeName))
                {
                    throw new Exception("Unexpected : we have a handler for a content type node (" + nodeName + ")");
                }
                else
                {
                    cf.BeginCase("\"" + nodeName + "\"");
                    if (HasType)
                        cf.WriteLine("AddCompletedObject(m_dataObject);");
                    cf.WriteLine("AddCompletedObject(new TextContent(reader.ReadString()));");
                    cf.EndCase();
                    AddHandledSubNode(nodeName);
                }

            }
        }
        private void EmitStartNodeHandler(CodeFormatter cf)
        {
            List<string> handledAttrs = new List<string>();
            if (listAttrMappings.Count > 0)
            {

                cf.WriteLine("protected override void HandleStartNode(System.Xml.XmlReader reader)");
                cf.WriteLine("{");

                foreach (AttrToFieldMapping mapping in listAttrMappings)
                {
                    mapping.EmitHandler(cf);
                    handledAttrs.Add(mapping.AttrName);
                }

                cf.WriteLine("}");
            }

            if (!NodeHasNoAttributes)
            {
                string[] attrs = GenerationContext.ResolveAttributes(this.NodeNameSpace + ":" + this.NodeName);

                foreach (string expectedAttr in attrs)
                {
                    if (!handledAttrs.Contains(expectedAttr))
                        cf.FormatLine("// {0} : unhandled attribute", expectedAttr);


                }
            }
        }
        //getting to point where compiler doesn't like the size of the case statement
        //so wrap it in a separate handler - seems to satisfy the compiler so far
        private void EmitHandleNamedNode(CodeFormatter cf)
        {
            cf.WriteLine("private void HandleNamedNode(System.Xml.XmlReader reader)");

            cf.WriteLine("{");
            cf.WriteLine("switch (reader.LocalName)");
            cf.WriteLine("{");

            EmitCaseHandlers(cf);
            EmitHandleGroupsCases(cf);
            EmitContentNodeHandlers(cf);
            EmitHandleNotes(cf);

            cf.WriteLine("}");
            cf.WriteLine("}");
        }
        private void EmitHandleNode(CodeFormatter cf)
        {
            cf.WriteLine("public override void HandleNode(System.Xml.XmlReader reader)");
            cf.WriteLine("{");

            cf.FormatLine("if(reader.LocalName == \"{0}\")", NodeName);
            cf.WriteLine("{");
            cf.WriteLine("HandleEponymousNode(reader);");
            cf.WriteLine("return;");
            cf.WriteLine("}");

            if (mappings.Count > 0 || listGroupsToHandle.Count > 0 || listContentNodes.Count > 0 || listNodesToHandle.Count>0 ||// simple nodes may not have any 
                MustEmitProcess  ) //used where the handling is currently too complex to auto-generate
            {
                cf.WriteLine("if(string.IsNullOrEmpty(reader.LocalName) || !reader.IsStartElement())");
                cf.WriteLine("{");
                cf.WriteLine("return;");
                cf.WriteLine("}");
                cf.WriteLine("");
                cf.WriteLine("HandleNamedNode(reader);");
            }

            cf.WriteLine("}");
            cf.WriteLine("");



            if (mappings.Count > 0 || listGroupsToHandle.Count > 0 || listContentNodes.Count > 0 ||listNodesToHandle.Count>0) // simple nodes may not have any 
            {
                EmitHandleNamedNode(cf);
            }

            if(!NodeHasNoAttributes)
                EmitNotHandledWarnings(cf);
        }
        private void EmitConstructors(CodeFormatter cf)
        {
            cf.FormatLine("public {0}(ParsingContext context) : base(context)", GetClassName());
            cf.WriteLine("{");
            if (!StartIsMarkerOnly)
            {
                if (HasType)
                {
                    cf.FormatLine("m_dataObject = new {0}({1});", TypeName, IsHasContentType ? "true" : "");
                }

                if( this.HasStopParentElementDispatchFlag)
                {
                    cf.WriteLine("m_dispatchThis = true;");
                }
                EmitFieldInitializers(cf);
            }
            cf.WriteLine("}");
            cf.WriteLine();

            if (!StartIsMarkerOnly && HasType)
            {
                cf.FormatLine("public {0}({1} dataObject, ParsingContext context) : base(context)", GetClassName(), TypeName);
                cf.WriteLine("{");

                cf.WriteLine("m_dataObject = dataObject;");
                if( this.HasStopParentElementDispatchFlag)
                {
                    cf.WriteLine("m_dispatchThis = true;");
                }
                EmitFieldInitializers(cf);


                cf.WriteLine("}");
                cf.WriteLine();
            }

        }
 private void AddNotifyContextOfNodeEnd(CodeFormatter cf)
 {
     cf.WriteLine("if(Context != null)");
     cf.WriteLine("{");
     cf.FormatLine("Context.NotifyEndOfNodeNamed(\"{0}\");", NodeName);
     cf.WriteLine("}");
 }
Exemple #14
0
        private void ConstructResultWithParams(CodeFormatter cppFile, TypeSpec resultType)
        {
            StringBuilder bld = new StringBuilder();
            switch (resultType.Modifier)
            {
                case TypeSpec.Modifiers.value:
                    bld.AppendFormat("{0} result", resultType.ToString());
                    break;
                case TypeSpec.Modifiers.managedReference:
                    if (FCSTypeIsHasContentType)
                    {
                        throw new Exception("case not handled");
                    }
                    else
                    {
                        bld.AppendFormat("{0} result = gcnew {1}", resultType.ToString(), resultType.Name);
                    }
                    break;
                case TypeSpec.Modifiers.pointer:
                    throw new NotImplementedException();
                case TypeSpec.Modifiers.reference:
                    throw new NotImplementedException();
            }

            bld.Append("(");
            foreach (MemberMapping mm in this.MemberMappings)
            {
                bld.AppendFormat("source{0}{1}, ", ForeignType.GetDereferencingOperator(), mm.ForeignSpec.Name);
            }

            bld.Remove(bld.Length - 2, 2);
            bld.Append(");");
            cppFile.WriteLine(bld.ToString());

        }
Exemple #15
0
        private void AddFCSToForeignConstruct(CodeFormatter cppFile)
        {
            MemberSpec source = new MemberSpec(FCSType, "source");
            MemberSpec context = new MemberSpec(new TypeSpec("RTFFileContext", TypeSpec.TypeSpecCategories.ForeignType) { Modifier = TypeSpec.Modifiers.pointer }, "pContext");
            BeginMethod(cppFile, ForeignType, "Construct", source, context);
            if (ForeignType.Modifier == TypeSpec.Modifiers.pointer && FCSType.Modifier == TypeSpec.Modifiers.managedReference)
            {
                cppFile.WriteLine("if (source == nullptr)");
                cppFile.IncreaseIndent();
                cppFile.WriteLine("return NULL;");
                cppFile.DecreaseIndent();
            }

            ConstructResult(cppFile, ForeignType);
            cppFile.FormatLine("Populate(result, source);");
            cppFile.WriteLine("return result;");
            cppFile.WriteLine("}");
        }
Exemple #16
0
        private void AddForeignToFCSPopulate(CodeFormatter cppFile)
        {
            MemberSpec dest = new MemberSpec(FCSType, "destination");
            MemberSpec source = new MemberSpec(ForeignType, "source");
            BeginMethod(cppFile, TypeSpec.Void, "Populate", dest, source);

            if (ForeignType.Modifier == TypeSpec.Modifiers.pointer)
            {
                cppFile.WriteLine("if (source == NULL)");
                cppFile.IncreaseIndent();
                cppFile.WriteLine("return;");
                cppFile.DecreaseIndent();
            }

            foreach (MemberMapping mm in MemberMappings)
            {
                mm.EmitForeignToFCS(cppFile, dest, source);

            }
            cppFile.WriteLine("}");
        }
Exemple #17
0
        private void AddFCSToForeignPopulate(CodeFormatter cppFile)
        {
            MemberSpec dest = new MemberSpec(ForeignType, "destination");
            MemberSpec source = new MemberSpec(FCSType, "source");
            BeginMethod(cppFile, TypeSpec.Void, "Populate", dest, source);
            foreach (MemberMapping mm in MemberMappings)
            {
                mm.EmitFCSToForeign(cppFile, dest, source);

            }
            cppFile.WriteLine("}");
        }
Exemple #18
0
        private void AddForeignToFCSConstruct(CodeFormatter cppFile)
        {
            MemberSpec source = new MemberSpec(ForeignType, "source");
            BeginMethod(cppFile, FCSType, "Construct", source);
            if (ForeignType.Modifier == TypeSpec.Modifiers.pointer && FCSType.Modifier == TypeSpec.Modifiers.managedReference)
            {
                cppFile.WriteLine("if (source == NULL)");
                cppFile.IncreaseIndent();
                cppFile.WriteLine("return nullptr;");
                cppFile.DecreaseIndent();
            }
            if (UseManagedCtor)
            {
                ConstructResultWithParams(cppFile, FCSType);

            }
            else
            {
                ConstructResult(cppFile, FCSType);
                cppFile.FormatLine("Populate(result, source);");
            }
            cppFile.WriteLine("return result;");
            cppFile.WriteLine("}");
        }
        private void EmitMemberVariables(CodeFormatter cf)
        {
            cf.WriteLine("");
            if (HasType)
            {
                cf.FormatLine("{0} m_dataObject;", TypeName);
            }

            if(HasStopParentElementDispatchFlag)
            {
                cf.WriteLine("bool m_dispatchThis;");

            }

            if (listAttrMappings.Count > 0 ||
                MustEmitStartNodeCall)
            {
                cf.WriteLine("bool m_bSeenOurStartNode;");
            }
        }
        private void EmitPostProcess(CodeFormatter cf)
        {
            if (string.IsNullOrEmpty(PostProcessCode))
                return;

            cf.WriteLine("private void PostProcess(System.Xml.XmlReader reader)");
            cf.WriteLine("{");
            cf.WriteLine("if (reader == null) return;");  // more to stop compiler complaining

            foreach (string s in PostProcessCode.Split('\r'))
            {
                cf.WriteLine(s);
            }
            cf.WriteLine("}");
        }
        private void EmitOnSubConsumerComplete(CodeFormatter cf)
        {
            cf.WriteLine("public override void OnSubConsumerComplete(object subConsumerOutput)");
            cf.WriteLine("{");

            foreach (IFieldMapping fm in mappings)
            {
                fm.EmitCompleteHandler(cf);

            }

            cf.WriteLine("}");
        }
 protected static void EndHeaderFile(CodeFormatter headerFile)
 {
     headerFile.WriteLine("};");
     headerFile.EndNamespace();
 }