/// <summary> /// Creates a new wrapper generator. /// </summary> /// <param name="name"></param> public XsdWrapperGenerator(string name) { if (name==null) throw new ArgumentNullException("name"); this.conformer = new NameConformer(); this.ns = new NamespaceDeclaration(name,this.conformer); this.ns.Imports.Add("System.Xml"); this.ns.Imports.Add("System.Xml.Serialization"); this.ns.Imports.Add("System.IO"); }
internal ClassDeclaration(string name, NamespaceDeclaration ns) : base(name,ns.Conformer) { this.ns = ns; }
public ClassDeclaration AddClass(NamespaceDeclaration ns) { ClassDeclaration col = ns.AddClass(this.CollectionName); // set base class as CollectionBase col.Parent = new TypeTypeDeclaration(typeof(CollectionBase)); // default constructor col.AddConstructor(); // add indexer if (this.ItemGet || this.ItemSet) { IndexerDeclaration index = col.AddIndexer( this.Type ); ParameterDeclaration pindex = index.Signature.Parameters.Add(typeof(int),"index",false); // get body if (this.ItemGet) { index.Get.Return( (Expr.This.Prop("List").Item(Expr.Arg(pindex)).Cast(this.Type) ) ); } // set body if (this.ItemSet) { index.Set.Add( Stm.Assign( Expr.This.Prop("List").Item(Expr.Arg(pindex)), Expr.Value ) ); } } string pname = ns.Conformer.ToCamel(this.Type.Name); // add method if (this.Add) { MethodDeclaration add = col.AddMethod("Add"); ParameterDeclaration para = add.Signature.Parameters.Add(this.Type,pname,true); add.Body.Add( Expr.This.Prop("List").Method("Add").Invoke(para) ); } if (this.AddRange) { MethodDeclaration add = col.AddMethod("AddRange"); ParameterDeclaration para = add.Signature.Parameters.Add(col,pname,true); ForEachStatement fe = Stm.ForEach( this.Type, "item", Expr.Arg(para), false ); fe.Body.Add( Expr.This.Prop("List").Method("Add").Invoke( fe.Local) ); add.Body.Add(fe); } // contains method if (this.Contains) { MethodDeclaration contains = col.AddMethod("Contains"); contains.Signature.ReturnType = new TypeTypeDeclaration(typeof(bool)); ParameterDeclaration para = contains.Signature.Parameters.Add(this.Type,pname,true); contains.Body.Return( Expr.This.Prop("List").Method("Contains").Invoke(para) ); } // remove method if (this.Remove) { MethodDeclaration remove = col.AddMethod("Remove"); ParameterDeclaration para = remove.Signature.Parameters.Add(this.Type,pname,true); remove.Doc.Summary.AddText("Removes the first occurrence of a specific ParameterDeclaration from this ParameterDeclarationCollection."); remove.Body.Add( Expr.This.Prop("List").Method("Remove").Invoke(para) ); } // insert if (this.Insert) { MethodDeclaration insert = col.AddMethod("Insert"); ParameterDeclaration index = insert.Signature.Parameters.Add(typeof(int),"index",true); ParameterDeclaration para = insert.Signature.Parameters.Add(this.Type,pname,true); insert.Body.Add( Expr.This.Prop("List").Method("Insert").Invoke(index,para) ); } // indexof if (this.IndexOf) { MethodDeclaration indexof = col.AddMethod("IndexOf"); ParameterDeclaration para = indexof.Signature.Parameters.Add(this.Type,pname,true); indexof.Signature.ReturnType = new TypeTypeDeclaration(typeof(int)); indexof.Body.Return( Expr.This.Prop("List").Method("IndexOf").Invoke(para) ); } if (this.Enumerator) { // create subclass ClassDeclaration en = col.AddClass("Enumerator"); // add wrapped field FieldDeclaration wrapped = en.AddField( typeof(IEnumerator),"wrapped" ); // add IEnumerator en.Interfaces.Add(typeof(IEnumerator)); // add constructor ConstructorDeclaration cs = en.AddConstructor(); ParameterDeclaration collection = cs.Signature.Parameters.Add(col,"collection",true); cs.Body.Add( Stm.Assign( Expr.This.Field(wrapped), Expr.Arg(collection).Cast(typeof(CollectionBase)).Method("GetEnumerator").Invoke() ) ); // add current PropertyDeclaration current = en.AddProperty(this.Type,"Current"); current.Get.Return( (Expr.This.Field(wrapped).Prop("Current")).Cast(this.Type) ); // add explicit interface implementation PropertyDeclaration currentEn = en.AddProperty(typeof(Object),"Current"); currentEn.Get.Return(Expr.This.Prop(current)); currentEn.PrivateImplementationType = wrapped.Type; // add reset MethodDeclaration reset = en.AddMethod("Reset"); reset.ImplementationTypes.Add( wrapped.Type ); reset.Body.Add( Expr.This.Field(wrapped).Method("Reset").Invoke()); // add movenext MethodDeclaration movenext = en.AddMethod("MoveNext"); movenext.ImplementationTypes.Add( wrapped.Type ); movenext.Signature.ReturnType = new TypeTypeDeclaration(typeof(bool)); movenext.Body.Return( Expr.This.Field(wrapped).Method("MoveNext").Invoke()); // add get enuemrator MethodDeclaration geten = col.AddMethod("GetEnumerator"); geten.Attributes |= MemberAttributes.New; geten.ImplementationTypes.Add( new TypeTypeDeclaration(typeof(IEnumerable)) ); geten.Signature.ReturnType = en; geten.Body.Return(Expr.New(en,Expr.This)); } return col; }
public void GenerateCode( string outputPath, NamespaceDeclaration ns ) { if (ns==null) throw new ArgumentNullException("ns"); ICodeGenerator gen = provider.CreateGenerator(); foreach(DictionaryEntry de in ns.ToCodeDom()) { FileName key = (FileName)de.Key; CodeNamespace nms = (CodeNamespace)de.Value; // creating directory string path; if (createFolders) path = PathFromNamespace(outputPath,key.Namespace); else path = outputPath; string filePath = Path.Combine(path,key.Name + "." + this.Provider.FileExtension); using(StreamWriter writer = new StreamWriter(filePath)) { // add copyright nms.Comments.Add(new CodeCommentStatement(copyright)); // Create a TextWriter to a StreamWriter to an output file. IndentedTextWriter tw = new IndentedTextWriter(writer,this.tab); // Generate source code using the code generator. gen.GenerateCodeFromNamespace(nms, tw, options); // log this.OnFileCreated(new StringEventArgs(filePath)); } } }
public NamespaceDeclaration AddNamespace(string name) { if (name==null) throw new ArgumentNullException("name"); if (this.namespaces.Contains(name)) throw new ApplicationException("namespace already created"); NamespaceDeclaration ns = new NamespaceDeclaration( this.Conformer.ToCapitalized(name), this.Conformer ); ns.parent = this; this.namespaces.Add(name,ns); return ns; }
public static void Refly() { // creating the Refly.Demo namespace NamespaceDeclaration demo = new NamespaceDeclaration("Refly.Demo"); // create the user class ClassDeclaration user = demo.AddClass("User"); // add name field FieldDeclaration name = user.AddField(typeof(string), "name"); // add constructor ConstructorDeclaration cstr = user.AddConstructor(); // add name parameter ParameterDeclaration pname = cstr.Signature.Parameters.Add(typeof(string), "name",true); // this.name = name; cstr.Body.AddAssign( Expr.This.Field(name), Expr.Arg(pname) ); // add property user.AddProperty(name, true, false, false); }
public ClassDeclaration AddClass(NamespaceDeclaration ns) { ClassDeclaration col = ns.AddClass(this.DictionaryName); // set base class as CollectionBase col.Parent = new TypeTypeDeclaration(typeof(DictionaryBase)); // default constructor col.AddConstructor(); // add indexer if (this.ItemGet || this.ItemSet) { IndexerDeclaration index = col.AddIndexer( this.ValueType ); ParameterDeclaration pindex = index.Signature.Parameters.Add(KeyType,"key",false); // get body if (this.ItemGet) { index.Get.Return( (Expr.This.Prop("Dictionary").Item(Expr.Arg(pindex)).Cast(this.ValueType) ) ); } // set body if (this.ItemSet) { index.Set.Add( Stm.Assign( Expr.This.Prop("Dictionary").Item(Expr.Arg(pindex)), Expr.Value ) ); } } // add method if (this.Add) { MethodDeclaration add = col.AddMethod("Add"); ParameterDeclaration pKey = add.Signature.Parameters.Add(this.KeyType,"key",true); ParameterDeclaration pValue = add.Signature.Parameters.Add(this.ValueType,"value",true); add.Body.Add( Expr.This.Prop("Dictionary").Method("Add").Invoke(pKey,pValue) ); } // contains method if (this.Contains) { MethodDeclaration contains = col.AddMethod("Contains"); contains.Signature.ReturnType = new TypeTypeDeclaration(typeof(bool)); ParameterDeclaration pKey = contains.Signature.Parameters.Add(this.KeyType,"key",true); contains.Body.Return( Expr.This.Prop("Dictionary").Method("Contains").Invoke(pKey) ); } // remove method if (this.Remove) { MethodDeclaration remove = col.AddMethod("Remove"); ParameterDeclaration pKey = remove.Signature.Parameters.Add(this.KeyType,"key",true); remove.Body.Add( Expr.This.Prop("Dictionary").Method("Remove").Invoke(pKey) ); } return col; }
public virtual void Prepare() { // check paramters if(this.Namespace==null || this.Namespace.Length==0) throw new ArgumentException("Namespace is empty"); this.ns=new NamespaceDeclaration(this.Namespace); foreach(Import import in this.imports) this.ns.Imports.Add(import.ToString()); }