001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.activemq.openwire.tool;
018    
019    import java.io.File;
020    import java.io.PrintWriter;
021    
022    /**
023     * 
024     */
025    public class CppMarshallingHeadersGenerator extends JavaMarshallingGenerator {
026    
027        protected String targetDir = "./src";
028    
029        public Object run() {
030            filePostFix = getFilePostFix();
031            if (destDir == null) {
032                destDir = new File(targetDir + "/marshal");
033            }
034            return super.run();
035        }
036    
037        protected String getFilePostFix() {
038            return ".hpp";
039        }
040    
041        protected void generateLicence(PrintWriter out) {
042            out.println("/**");
043            out.println(" * Licensed to the Apache Software Foundation (ASF) under one or more");
044            out.println(" * contributor license agreements.  See the NOTICE file distributed with");
045            out.println(" * this work for additional information regarding copyright ownership.");
046            out.println(" * The ASF licenses this file to You under the Apache License, Version 2.0");
047            out.println(" * (the \"License\"); you may not use this file except in compliance with");
048            out.println(" * the License.  You may obtain a copy of the License at");
049            out.println(" *");
050            out.println(" *      http://www.apache.org/licenses/LICENSE-2.0");
051            out.println(" *");
052            out.println(" * Unless required by applicable law or agreed to in writing, software");
053            out.println(" * distributed under the License is distributed on an \"AS IS\" BASIS,");
054            out.println(" * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.");
055            out.println(" * See the License for the specific language governing permissions and");
056            out.println(" * limitations under the License.");
057            out.println(" */");
058        }
059    
060        protected void generateFile(PrintWriter out) throws Exception {
061            generateLicence(out);
062    
063            out.println("#ifndef " + className + "_hpp_");
064            out.println("#define " + className + "_hpp_");
065            out.println("");
066            out.println("#include <string>");
067            out.println("");
068            out.println("#include \"command/IDataStructure.hpp\"");
069            out.println("");
070            out.println("/* we could cut this down  - for now include all possible headers */");
071            out.println("#include \"command/BrokerId.hpp\"");
072            out.println("#include \"command/ConnectionId.hpp\"");
073            out.println("#include \"command/ConsumerId.hpp\"");
074            out.println("#include \"command/ProducerId.hpp\"");
075            out.println("#include \"command/SessionId.hpp\"");
076            out.println("");
077            out.println("#include \"io/BinaryReader.hpp\"");
078            out.println("#include \"io/BinaryWriter.hpp\"");
079            out.println("");
080            out.println("#include \"command/" + baseClass + ".hpp\"");
081            out.println("#include \"util/ifr/p.hpp\"");
082            out.println("");
083            out.println("#include \"protocol/ProtocolFormat.hpp\"");
084            out.println("");
085            out.println("namespace apache");
086            out.println("{");
087            out.println("  namespace activemq");
088            out.println("  {");
089            out.println("    namespace client");
090            out.println("    {");
091            out.println("      namespace marshal");
092            out.println("      {");
093            out.println("        using namespace ifr ;");
094            out.println("        using namespace apache::activemq::client::command;");
095            out.println("        using namespace apache::activemq::client::io;");
096            out.println("        using namespace apache::activemq::client::protocol;");
097            out.println("");
098            out.println("/*");
099            out.println(" *");
100            out.println(" */");
101            out.println("class " + className + " : public " + baseClass + "");
102            out.println("{");
103            out.println("public:");
104            out.println("    " + className + "() ;");
105            out.println("    virtual ~" + className + "() ;");
106            out.println("");
107            out.println("    virtual IDataStructure* createCommand() ;");
108            out.println("    virtual char getDataStructureType() ;");
109            out.println("    ");
110            out.println("    virtual void unmarshal(ProtocolFormat& wireFormat, Object o, BinaryReader& dataIn, BooleanStream& bs) ;");
111            out.println("    virtual int marshal1(ProtocolFormat& wireFormat, Object& o, BooleanStream& bs) ;");
112            out.println("    virtual void marshal2(ProtocolFormat& wireFormat, Object& o, BinaryWriter& dataOut, BooleanStream& bs) ;");
113            out.println("} ;");
114            out.println("");
115            out.println("/* namespace */");
116            out.println("     }");
117            out.println("    }");
118            out.println("  }");
119            out.println("}");
120            out.println("#endif /*" + className + "_hpp_*/");
121        }
122    
123        public void generateFactory(PrintWriter out) {
124            generateLicence(out);
125            out.println("");
126            out.println("// Marshalling code for Open Wire Format ");
127            out.println("//");
128            out.println("//");
129            out.println("// NOTE!: This file is autogenerated - do not modify!");
130            out.println("//        if you need to make a change, please see the Groovy scripts in the");
131            out.println("//        activemq-openwire module");
132            out.println("//");
133            out.println("");
134            out.println("#ifndef MarshallerFactory_hpp_");
135            out.println("#define MarshallerFactory_hpp_");
136            out.println("");
137            out.println("");
138            out.println("namespace apache");
139            out.println("{");
140            out.println("  namespace activemq");
141            out.println("  {");
142            out.println("    namespace client");
143            out.println("    {");
144            out.println("      namespace marshal");
145            out.println("      {");
146            out.println("        using namespace ifr ;");
147            out.println("        using namespace std ;");
148            out.println("        using namespace apache::activemq::client;");
149            out.println("        using namespace apache::activemq::client::command;");
150            out.println("        using namespace apache::activemq::client::io;");
151            out.println("");
152            out.println("/*");
153            out.println(" * ");
154            out.println(" */");
155            out.println("class MarshallerFactory");
156            out.println("{");
157            out.println("public:");
158            out.println("    MarshallerFactory() ;");
159            out.println("    virtual ~MarshallerFactory() ;");
160            out.println("");
161            out.println("    virtual void configure(ProtocolFormat& format) ;");
162            out.println("} ;");
163            out.println("");
164            out.println("/* namespace */");
165            out.println("      }");
166            out.println("    }");
167            out.println("  }");
168            out.println("}");
169            out.println("");
170            out.println("#endif /*MarshallerFactory_hpp_*/");
171            out.println("");
172        }
173    
174        public String getTargetDir() {
175            return targetDir;
176        }
177    
178        public void setTargetDir(String targetDir) {
179            this.targetDir = targetDir;
180        }
181    }