diff options
author | Matthew Newhook <matthew@zeroc.com> | 2002-01-25 16:56:52 +0000 |
---|---|---|
committer | Matthew Newhook <matthew@zeroc.com> | 2002-01-25 16:56:52 +0000 |
commit | ccb36bbe1463c2b4760f793ec5611b24caa6c09f (patch) | |
tree | 0fb6ecc7d0ba90c6b82606b46b3621f5fbcd238a /cpp/src | |
parent | Changed Gen.cpp to fix a minor bug that prevented compiling in VC++. Added (diff) | |
download | ice-ccb36bbe1463c2b4760f793ec5611b24caa6c09f.tar.bz2 ice-ccb36bbe1463c2b4760f793ec5611b24caa6c09f.tar.xz ice-ccb36bbe1463c2b4760f793ec5611b24caa6c09f.zip |
Use visitor pattern. Add ability to generate WSDL definitions for all types
in a file.
Diffstat (limited to 'cpp/src')
-rw-r--r-- | cpp/src/slice2wsdl/Gen.cpp | 175 | ||||
-rw-r--r-- | cpp/src/slice2wsdl/Gen.h | 30 | ||||
-rw-r--r-- | cpp/src/slice2wsdl/Main.cpp | 38 |
3 files changed, 76 insertions, 167 deletions
diff --git a/cpp/src/slice2wsdl/Gen.cpp b/cpp/src/slice2wsdl/Gen.cpp index 29f6f879c53..57aeb726592 100644 --- a/cpp/src/slice2wsdl/Gen.cpp +++ b/cpp/src/slice2wsdl/Gen.cpp @@ -12,6 +12,7 @@ #include <Slice/CPlusPlusUtil.h> // TODO: ??? #include <Gen.h> +#include <Slice/OutputUtil.h> using namespace std; using namespace Slice; @@ -19,12 +20,12 @@ using namespace Slice; static const string internalId = "_internal."; Slice::Gen::Gen(const string& name, const string& base, const string& include, - const vector<string>& includePaths, const string& dir, - const ClassDefPtr& classDef) : + const vector<string>& includePaths, const string& dir) : + _name(name), _base(base), _include(include), _includePaths(includePaths), - _classDef(classDef) + _dir(dir) { _orgName = "http://www.noorg.org"; // TODO: argument! for (vector<string>::iterator p = _includePaths.begin(); p != _includePaths.end(); ++p) @@ -40,85 +41,79 @@ Slice::Gen::Gen(const string& name, const string& base, const string& include, { _base.erase(0, pos + 1); } +} + +Slice::Gen::~Gen() +{ +} + +bool +Slice::Gen::visitClassDefStart(const ClassDefPtr& p) +{ + Output O; //string fileO = _base + ".wsdl"; - string fileO = containedToId(classDef) + classDef->name() + ".wsdl"; - if (!dir.empty()) + string fileO = containedToId(p) + p->name() + ".wsdl"; + if (!_dir.empty()) { - fileO = dir + '/' + fileO; + fileO = _dir + '/' + fileO; } O.open(fileO.c_str()); if (!O) { - cerr << name << ": can't open `" << fileO << "' for writing: " << strerror(errno) << endl; - return; + cerr << _name << ": can't open `" << fileO << "' for writing: " << strerror(errno) << endl; + return false; } - printHeader(); + printHeader(O); O << "\n<!-- Generated from file `" << changeInclude(_base, _includePaths) << ".ice' -->\n"; -} -Slice::Gen::~Gen() -{ -} - -bool -Slice::Gen::operator!() const -{ - return !O; -} - -void -Slice::Gen::generate(const UnitPtr& unit) -{ - unit->mergeModules(); - - string scopeId = containedToId(_classDef); + string scopeId = containedToId(p); // // TODO: It would be better if start() aligned the attributes // correctly. // ostringstream os; - os << "wsdl:definitions name=\"" << scopeId << _classDef->name() << "\"" + os << "wsdl:definitions name=\"" << scopeId << p->name() << "\"" << "\n xmlns:wsdl=\"http://schemas.xmlsoap.org/wsdl/\"" << "\n xmlns:xsd1=\"" << _orgName << "/schemas\"" << "\n xmlns:tns=\"" << _orgName << "/definitions\"" << "\n targetNamespace=\"" << _orgName << "/definitions\""; - start(os.str()); + start(O, os.str()); // TODO: schemaLocation? O << sp << nl << "<wsdl:import namespace=\"" << _orgName << "/schemas\" location=\"" << _base << ".xsd\"/>"; - OperationList ops = _classDef->allOperations(); + OperationList ops = p->allOperations(); for (OperationList::const_iterator q = ops.begin(); q != ops.end(); ++q) { - emitMessage(*q); + emitMessage(O, *q); } O << sp; os.str(""); os << "wsdl:portType name=\"" << scopeId << "PortType\""; - start(os.str()); + start(O, os.str()); for (OperationList::const_iterator r = ops.begin(); r != ops.end(); ++r) { - emitOperation(*r); + emitOperation(O, *r); } - end(); // PortType + end(O); // PortType - end(); // definitions + end(O); // definitions - O << nl; + return false; } void -Slice::Gen::emitMessage(const OperationPtr& p) +Slice::Gen::emitMessage(Output& O, const OperationPtr& p) { O << sp; @@ -126,38 +121,38 @@ Slice::Gen::emitMessage(const OperationPtr& p) ostringstream os; os << "wsdl:message name=\"input." << p->name() << "\""; - start(os.str()); + start(O, os.str()); O << nl << "<wsdl:part name=\"body\" element=\"xsd1:" << scopeId << "request." << p->name() << "\"/>"; - end(); // message + end(O); // message os.str(""); os << "wsdl:message name=\"output." << p->name() << "\""; - start(os.str()); + start(O, os.str()); O << nl << "<wsdl:part name=\"body\" element=\"xsd1:" << scopeId << "request." << p->name() << "\"/>"; - end(); // message + end(O); // message } void -Slice::Gen::emitOperation(const OperationPtr& p) +Slice::Gen::emitOperation(Output& O, const OperationPtr& p) { string scopeId = containedToId(p); ostringstream os; os << "wsdl:operation name=\"" << p->name() << "\""; - start(os.str()); + start(O, os.str()); O << nl << "<wsdl:input message=\"tns:input." << p->name() << "\">"; O << nl << "<wsdl:output message=\"tns:output." << p->name() << "\">"; - end(); // operation + end(O); // operation } void -Slice::Gen::printHeader() +Slice::Gen::printHeader(Output& O) { static const char* header = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" @@ -181,7 +176,7 @@ Slice::Gen::printHeader() void -Slice::Gen::start(const std::string& element) +Slice::Gen::start(Output& O, const std::string& element) { O << nl << '<' << element << '>'; O.inc(); @@ -198,7 +193,7 @@ Slice::Gen::start(const std::string& element) } void -Slice::Gen::end() +Slice::Gen::end(Output& O) { string element = _elementStack.top(); _elementStack.pop(); @@ -237,91 +232,3 @@ Slice::Gen::containedToId(const ContainedPtr& contained) return id; } - -string -Slice::Gen::toString(const SyntaxTreeBasePtr& p) -{ - string tag; - string linkend; - string s; - - static const char* builtinTable[] = - { - "xs:byte", - "xs:boolean", - "xs;short", - "xs:int", - "xs:long", - "xs:float", - "xs:double", - "xs:string", - "ice:_internal.reference", /* Object */ - "ice:_internal.proxyType", /* Object* */ - "???" /* LocalObject */ - }; - - BuiltinPtr builtin = BuiltinPtr::dynamicCast(p); - if (builtin) - { - s = builtinTable[builtin->kind()]; - //tag = "type"; - } - - ProxyPtr proxy = ProxyPtr::dynamicCast(p); - if (proxy) - { - s = "ice:_internal.proxyType"; - } - - ClassDeclPtr cl = ClassDeclPtr::dynamicCast(p); - if (cl) - { - string scopeId = containedToId(cl); - //s = "tns:" + internalId + scopeId + cl->name() + "Type"; - s = "ice:_internal.reference"; - } - - ExceptionPtr ex = ExceptionPtr::dynamicCast(p); - if (ex) - { - string scopeId = containedToId(ex); - s = "tns:" + internalId + scopeId + ex->name() + "Type"; - } - - StructPtr st = StructPtr::dynamicCast(p); - if (st) - { - string scopeId = containedToId(st); - s = "tns:" + internalId + scopeId + st->name() + "Type"; - } - - EnumeratorPtr en = EnumeratorPtr::dynamicCast(p); - if (en) - { - string scopeId = containedToId(en); - s = "tns:" + internalId + scopeId + en->name() + "Type"; - } - - SequencePtr sq = SequencePtr::dynamicCast(p); - if (sq) - { - string scopeId = containedToId(sq); - s = "tns:" + internalId + scopeId + sq->name() + "Type"; - } - - DictionaryPtr di = DictionaryPtr::dynamicCast(p); - if (di) - { - string scopeId = containedToId(di); - s = "tns:" + internalId + scopeId + di->name() + "Type"; - } - - EnumPtr em = EnumPtr::dynamicCast(p); - if (em) - { - string scopeId = containedToId(em); - s = "tns:" + internalId + scopeId + em->name() + "Type"; - } - - return s; -} diff --git a/cpp/src/slice2wsdl/Gen.h b/cpp/src/slice2wsdl/Gen.h index 507f9e86ba1..77dd00681ec 100644 --- a/cpp/src/slice2wsdl/Gen.h +++ b/cpp/src/slice2wsdl/Gen.h @@ -12,13 +12,14 @@ #define GEN_H #include <Slice/Parser.h> -#include <Slice/OutputUtil.h> #include <stack> namespace Slice { -class Gen : public ::IceUtil::noncopyable +class Output; + +class Gen : public ::IceUtil::noncopyable, public ParserVisitor { public: @@ -26,33 +27,28 @@ public: const std::string&, const std::string&, const std::vector<std::string>&, - const std::string&, - const ClassDefPtr&); + const std::string&); virtual ~Gen(); - bool operator!() const; // Returns true if there was a constructor error - - void generate(const UnitPtr&); - - void emitMessage(const OperationPtr&); - void emitOperation(const OperationPtr&); + virtual bool visitClassDefStart(const ClassDefPtr&); private: - void printHeader(); - void start(const std::string&); - void end(); + void emitMessage(Output&, const OperationPtr&); + void emitOperation(Output&, const OperationPtr&); - std::string containedToId(const ContainedPtr&); - std::string toString(const SyntaxTreeBasePtr&); + void printHeader(Output&); + void start(Output&, const std::string&); + void end(Output&); - Output O; + std::string containedToId(const ContainedPtr&); + std::string _name; std::string _base; std::string _include; std::string _orgName; std::vector<std::string> _includePaths; - ClassDefPtr _classDef; + std::string _dir; std::stack<std::string> _elementStack; }; diff --git a/cpp/src/slice2wsdl/Main.cpp b/cpp/src/slice2wsdl/Main.cpp index 2efe434b590..e1d77cb24f6 100644 --- a/cpp/src/slice2wsdl/Main.cpp +++ b/cpp/src/slice2wsdl/Main.cpp @@ -17,7 +17,7 @@ using namespace Slice; void usage(const char* n) { - cerr << "Usage: " << n << " [options] slice-file type-id\n"; + cerr << "Usage: " << n << " [options] slice-file [type-id ...]\n"; cerr << "Options:\n" "-h, --help Show this message.\n" @@ -133,14 +133,13 @@ main(int argc, char* argv[]) } } - if (argc != 3) + if (argc < 2) { usage(argv[0]); return EXIT_FAILURE; } string sourceFile = argv[1]; - string typeId = argv[2]; int status = EXIT_SUCCESS; @@ -196,23 +195,30 @@ main(int argc, char* argv[]) } else { - ContainedList contained = unit->findContents(typeId); - if (contained.empty() || contained.front()->containedType() != Contained::ContainedTypeClass) + Gen gen(argv[0], base, include, includePaths, output); + + if (argc > 2) { - cerr << argv[0] << ": invalid type: " << typeId << endl; - status = EXIT_FAILURE; + for (idx = 2 ; idx < argc; ++idx) + { + ClassDeclPtr classDecl; + TypeList classTypes = unit->lookupType(argv[idx], false); + if (!classTypes.empty()) + { + classDecl = ClassDeclPtr::dynamicCast(classTypes.front()); + } + if (!classDecl) + { + cerr << argv[0] << ": invalid type: " << argv[idx] << endl; + status = EXIT_FAILURE; + break; + } + gen.visitClassDefStart(classDecl->definition()); + } } else { - ClassDefPtr p = ClassDefPtr::dynamicCast(contained.front()); - assert(p); - Gen gen(argv[0], base, include, includePaths, output, p); - if (!gen) - { - unit->destroy(); - return EXIT_FAILURE; - } - gen.generate(unit); + unit->visit(&gen); } } |