refactored the C# code generation scripts to make them a little easier to work with

git-svn-id: https://svn.apache.org/repos/asf/incubator/activemq/trunk@379507 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
James Strachan 2006-02-21 15:58:36 +00:00
parent e733f71b48
commit 66dd4364ea
4 changed files with 166 additions and 48 deletions

View File

@ -0,0 +1,34 @@
/**
*
* Copyright 2005-2006 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.activemq.openwire.tool;
import java.io.File;
/**
*
* @version $Revision$
*/
public abstract class OpenWireCSharpClassesScript extends OpenWireClassesScript {
public Object run() {
filePostFix = ".cs";
destDir = new File("../openwire-dotnet/src/OpenWire.Client/Commands");
return super.run();
}
}

View File

@ -0,0 +1,119 @@
/**
*
* Copyright 2005-2006 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.activemq.openwire.tool;
import mx4j.tools.adaptor.http.GetAttributeCommandProcessor;
import org.codehaus.jam.JClass;
import org.codehaus.jam.JamClassIterator;
import java.io.File;
import java.io.*;
import java.util.*;
/**
*
* @version $Revision$
*/
public abstract class OpenWireClassesScript extends OpenWireScript {
protected Set manuallyMaintainedClasses = new HashSet();
protected File destDir = new File("target/generated/classes");
protected File destFile;
protected String filePostFix = "";
protected JClass jclass;
protected JClass superclass;
protected String simpleName;
protected String className;
protected String baseClass;
protected StringBuffer buffer;
public OpenWireClassesScript() {
String[] names = { "ActiveMQDestination", "ActiveMQTempDestination", "ActiveMQQueue", "ActiveMQTopic", "ActiveMQTempQueue", "ActiveMQTempTopic",
"BaseCommand", "ActiveMQMessage", "ActiveMQTextMessage", "ActiveMQMapMessage", "ActiveMQBytesMessage", "ActiveMQStreamMessage",
"ActiveMQStreamMessage", "DataStructureSupport" };
for (int i = 0; i < names.length; i++) {
manuallyMaintainedClasses.add(names[i]);
}
}
public Object run() {
destDir.mkdirs();
buffer = new StringBuffer();
JamClassIterator iter = getClasses();
while (iter.hasNext()) {
jclass = iter.nextClass();
if (isValidClass(jclass)) {
processClass(jclass);
}
}
return null;
}
protected boolean isValidClass(JClass jclass) {
if (jclass.getAnnotation("openwire:marshaller") == null) {
return false;
}
return manuallyMaintainedClasses.contains(jclass.getSimpleName());
}
protected void processClass(JClass jclass) {
simpleName = jclass.getSimpleName();
superclass = jclass.getSuperclass();
System.out.println("Processing class: " + simpleName);
className = getClassName(jclass);
destFile = new File(destDir, className + filePostFix);
baseClass = getBaseClassName(jclass);
PrintWriter out = null;
try {
out = new PrintWriter(new FileWriter(destFile));
}
catch (Exception e) {
throw new RuntimeException(e);
}
finally {
if (out != null) {
out.close();
}
}
}
protected abstract void generateFile(PrintWriter out);
protected String getBaseClassName(JClass jclass) {
String answer = "AbstractCommand";
if (superclass != null) {
String name = superclass.getSimpleName();
if (name != null && !name.equals("Object")) {
answer = name;
}
}
return answer;
}
protected String getClassName(JClass jclass) {
return "AbstractCommand";
}
}

View File

@ -23,6 +23,8 @@ import org.codehaus.jam.JClass;
import org.codehaus.jam.JField;
import org.codehaus.jam.JMethod;
import org.codehaus.jam.JProperty;
import org.codehaus.jam.JamClassIterator;
import org.codehaus.jam.JamService;
/**
* @version $Revision$
@ -72,6 +74,14 @@ public abstract class OpenWireScript extends GramSupport {
return false; //j.getSuperclass()!=null && isMarshallAware(j.getSuperclass());
}
public JamService getJam() {
return (JamService) getBinding().getVariable("jam");
}
public JamClassIterator getClasses() {
return getJam().getClasses();
}
/**
* Converts the Java type to a C# type name
*/

View File

@ -14,59 +14,16 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import org.apache.activemq.openwire.tool.OpenWireScript
import org.apache.activemq.openwire.tool.OpenWireCSharpClassesScript
/**
* Generates the C# marshalling code for the Open Wire Format
*
* @version $Revision$
*/
class GenerateCSharpClasses extends OpenWireScript {
class GenerateCSharpClasses extends OpenWireCSharpClassesScript {
Object run() {
def destDir = new File("../openwire-dotnet/src/OpenWire.Client/Commands")
destDir.mkdirs()
def messageClasses = classes.findAll {
it.getAnnotation("openwire:marshaller")!=null
}
def manuallyMaintainedClasses = ['ActiveMQDestination', 'ActiveMQTempDestination', 'ActiveMQQueue', 'ActiveMQTopic', 'ActiveMQTempQueue', 'ActiveMQTempTopic', 'BaseCommand',
'ActiveMQMessage', 'ActiveMQTextMessage', 'ActiveMQMapMessage', 'ActiveMQBytesMessage', 'ActiveMQStreamMessage', 'ActiveMQStreamMessage']
println "Generating Java marshalling code to directory ${destDir}"
def buffer = new StringBuffer()
int counter = 0
Map map = [:]
for (jclass in messageClasses) {
if (manuallyMaintainedClasses.contains(jclass.simpleName)) continue
println "Processing $jclass.simpleName"
def properties = jclass.declaredProperties.findAll { isValidProperty(it) }
def file = new File(destDir, jclass.simpleName + ".cs")
String baseClass = jclass.superclass.simpleName
if (baseClass == "Object") {
baseClass = "AbstractCommand"
}
/*
String baseClass = "AbstractCommand"
if (jclass.superclass?.simpleName == "ActiveMQMessage") {
baseClass = "ActiveMQMessage"
}
*/
buffer << """
${jclass.simpleName}.class
"""
file.withWriter { out |
void generateFile(PrintWriter out) {
out << """//
// Marshalling code for Open Wire Format for ${jclass.simpleName}
//
@ -134,6 +91,4 @@ namespace OpenWire.Client.Commands
}
"""
}
}
}
}