JsonServerGenerator::JsonServerGenerator(std::string ifile, Info & info) { Interface i = parseXml(ifile); std::string stub = generateStub(i, info); { std::ofstream of(info.getServerDst("Json").c_str()); of << stub << std::endl; } }
InterfaceGenerator::InterfaceGenerator(std::string ifile, Info & info) { Interface i = parseXml(ifile); std::string stub = generateStub(i, info); { std::ofstream of(info.getInterfaceDst().c_str()); of << stub << std::endl; } std::string cppstub = generateCppStub(i, info); { std::ofstream of(info.getInterfaceSourceDst().c_str()); of << cppstub << std::endl; } }
void JavaCodeGenerator::generate(TalkyUnit* unit, string path) { fs::path dir(path); //package if (unit->currentPackage != "") { string packagePath = unit->currentPackage; // std::replace(packagePath.begin(), packagePath.end(), ".", "/"); boost::replace_all(packagePath, ".", "/"); dir += "/" + packagePath; cout << "final path:" << dir << endl; boost::filesystem::create_directories(dir); } if (!fs::exists(dir)) { cout << "java generator output dir does not exist:" << dir << endl; exit(-1); } dir = fs::canonical(dir); path = dir.native(); cout << "generating java source to:" << path << endl; map<string, Definition*>& definitions = unit->definitions; for ( map<string, Definition*>::iterator it = definitions.begin(); it != definitions.end(); ++it ) { Definition* definition = it->second; string name = definition->name; DefinitionType type = definition->getType(); if (type == DFT_ENUM) { ofstream ofs(path + "/" + name + ".java"); if (unit->currentPackage != "") { ofs << "package " << unit->currentPackage << ";" << endl; } ofs << "public class " << name << "{" << endl; Enum& theEnum = *dynamic_cast<Enum*>(definition); for (int i = 0; i < theEnum.members.size(); i++) { ofs << "public final static int " << theEnum.members[i] << "=" << i << ";" << endl; } ofs << "public final static int MAX_VALUE=" << theEnum.members.size() << ";" << endl; ofs << "public static String toString(byte value){" << endl; for (int i = 0; i < theEnum.members.size(); i++) { ofs << "if(value == " << theEnum.members[i] << "){" << endl; ofs << "return \"" << theEnum.members[i] << "\";" << endl; ofs << "}" << endl; } ofs << "return null;" << endl; ofs << "}" << endl; ofs << "public static int toValue(String name){" << endl; for (int i = 0; i < theEnum.members.size(); i++) { ofs << "if(name.equals(\"" << theEnum.members[i] << "\")){" << endl; ofs << "return " << theEnum.members[i] << ";" << endl; ofs << "}" << endl; } ofs << "return -1;" << endl; ofs << "}" << endl; ofs << "}" << endl; ofs.close(); } else if (type == DFT_STRUCTURE) { ofstream ofs(path + "/" + name + ".java"); if (unit->currentPackage != "") { ofs << "package " << unit->currentPackage << ";" << endl; } generateImports(ofs); ofs << "public class " << name << "{" << endl; Structure& theStructure = *dynamic_cast<Structure*>(definition); std::vector<TypeDeclaration*>& fields = theStructure.members; for (int i = 0; i < fields.size(); i++) { TypeDeclaration& theField = *fields[i]; string dataTypeName = getTypeName(theField); ofs << "public " << dataTypeName << " " << theField.name << ";" << endl; } //serializer ofs << "public void serialize(DataOutputStream dos) throws Exception{" << endl; //mark fields that need serialization int fieldMarkSize = (fields.size() + 7) / 8; ofs << "FieldMark fm = new FieldMark(" << fieldMarkSize << ");" << endl; for (int i = 0; i < fields.size(); i++) { TypeDeclaration& theField = *fields[i]; if (theField.declarationType == DLT_ARRAY || theField.declarationType == DLT_BYTE_ARRAY) { //fm.mark(name == null || name.length == 0 ? false : true); ofs << "fm.mark(" << theField.name << " != null && " << theField.name << ".length > 0);" << endl; } else if (theField.declarationType == DLT_USER) { ofs << "fm.mark(true);" << endl; } else if (theField.declarationType == DLT_PRIMITIVE) { if (theField.dataType == DT_INT64 || theField.dataType == DT_UINT64 || theField.dataType == DT_DOUBLE || theField.dataType == DT_FLOAT || theField.dataType == DT_INT32 || theField.dataType == DT_UINT32 || theField.dataType == DT_INT16 || theField.dataType == DT_UINT16 || theField.dataType == DT_INT8) { ofs << "fm.mark(" << theField.name << " != 0);" << endl; } else if (theField.dataType == DT_BOOL) { ofs << "fm.mark(true);" << endl; } else if (theField.dataType == DT_STRING) { ofs << "fm.mark(" << theField.name << " != null && " << theField.name << ".length() > 0);" << endl; } } } // serialize the field mark ofs << "dos.write(fm.getData());" << endl; //actual serilization code for (int i = 0; i < fields.size(); i++) { TypeDeclaration& theField = *fields[i]; ofs << "if(fm.isMarked(" + __ITOA(i) + ")){" << endl; serializeField(ofs, theField); ofs << "}" << endl; } // ofs << "}" << endl; ofs << "}" << endl; //deserializer ofs << "public void deserialize(DataInputStream dis) throws Exception{" << endl; ofs << "FieldMark fm = new FieldMark(" << fieldMarkSize << ");" << endl; ofs << "dis.read(fm.getData());" << endl; for (int i = 0; i < fields.size(); i++) { TypeDeclaration& theField = *fields[i]; ofs << "if(fm.readMark()){" << endl; deserializeField(ofs, theField); ofs << "}" << endl; } ofs << "}" << endl; ofs << "}" << endl; ofs.close(); } else if (type == DFT_INTERFACE) { Interface& theInterface = *dynamic_cast<Interface*>(definition); generateProxy(path + "/" + name + "Proxy" + ".java", unit, theInterface); generateStub(path + "/" + name + "Stub" + ".java", unit, theInterface); generateDispatcher(path + "/" + name + "Dispatcher" + ".java", unit, theInterface); } } }