<commit_before>package br.usp.ime.ccsl.choreos.middleware.proxy.codegenerator;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import javax.wsdl.Definition;
import javax.wsdl.WSDLException;
import javax.wsdl.factory.WSDLFactory;
import javax.wsdl.xml.WSDLReader;

import org.apache.commons.io.FileUtils;
import org.apache.cxf.tools.wsdlto.WSDLToJava;

import com.ibm.wsdl.BindingImpl;
import com.sun.tools.javac.Main;

public class CodeGeneratorHelper {
    public static final String SRC_GENERATED_CLIENT_JAVA = "src/generated/client/java";
    public static final String SRC_GENERATED_SERVER_JAVA = "src/generated/server/java";
    public static final String TARGET_GENERATED_SERVER_JAVA_CODE = "target/classes/generated";

    public static final String CLASSPATH = "target/classes";

    public static final boolean SERVER = true;
    public static final boolean CLIENT = false;

    private List<String> implementation;

    @SuppressWarnings("unchecked")
    private void createImplementationClass(URL wsdlInterfaceDescriptor) {

	implementation = new ArrayList<String>();

	String packageName = getPackage(wsdlInterfaceDescriptor);

	implementation.add("package " + packageName.substring(0, packageName.length() - 1) + ";");
	implementation.add("");
	implementation.add("public class " + getPortName(wsdlInterfaceDescriptor) + "Impl implements "
		+ getPortName(wsdlInterfaceDescriptor) + "{");
	implementation.add("");

	String className = packageName + getPortName(wsdlInterfaceDescriptor);
	Class implementedInterface = getInterfaceClass(className);

	for (int i = 0; i < implementedInterface.getMethods().length; i++) {
	    generateMethod(implementedInterface.getMethods()[i]);
	}

	implementation.add("}");

	String fileName = getDestinationFolder(CodeGeneratorHelper.SRC_GENERATED_SERVER_JAVA, wsdlInterfaceDescriptor)
		+ getPortName(wsdlInterfaceDescriptor) + "Impl.java";
	File output = new File(fileName);

	writeLines(output, implementation);
    }

    private String getPackage(URL wsdlInterfaceDescriptor) {
	return getDestinationFolder("", wsdlInterfaceDescriptor).replace('/', '.').substring(1);
    }

    private void generateMethod(Method method) {
	String line = "public " + method.getReturnType().getSimpleName() + " " + method.getName() + "(";

	for (int i = 0; i < method.getParameterTypes().length; i++) {
	    line = line + method.getParameterTypes()[i].getSimpleName() + " param" + i;
	    line = (i == method.getParameterTypes().length) ? line + ", " : line;
	}
	line = line + "){";
	implementation.add(line);

	line = "Exception e = new Exception(\"There is no web service active for this role\");";
	implementation.add(line);
	implementation.add("return null;");
	implementation.add("}");

    }

    @SuppressWarnings("unchecked")
    private Class getInterfaceClass(String portName) {

	Class clazz = null;
	try {
	    clazz = Class.forName(portName);
	} catch (ClassNotFoundException e) {
	    e.printStackTrace();
	}
	if (clazz == null)
	    System.out.println("Class " + portName + " was not found.");
	return clazz;
    }

    public String generateJavaCode(URL wsdlInterfaceDescriptor, boolean server) {
	String[] parameters = null;
	if (server) {
	    parameters = new String[] { "-server", "-d", SRC_GENERATED_SERVER_JAVA,
		    wsdlInterfaceDescriptor.toExternalForm() };
	    WSDLToJava.main(parameters);
	    createImplementationClass(wsdlInterfaceDescriptor);
	    return getDestinationFolder(SRC_GENERATED_SERVER_JAVA, wsdlInterfaceDescriptor);
	} else {
	    parameters = new String[] { "-client", "-d", SRC_GENERATED_CLIENT_JAVA,
		    wsdlInterfaceDescriptor.toExternalForm() };
	    WSDLToJava.main(parameters);
	    return getDestinationFolder(SRC_GENERATED_CLIENT_JAVA, wsdlInterfaceDescriptor);
	}

    }

    private void writeLines(File fileHandler, List<String> fileLinesCopy) {
	try {
	    FileUtils.writeLines(fileHandler, fileLinesCopy);
	} catch (IOException e) {
	    System.out.println("Problem writing output to file");
	    e.printStackTrace();
	}
    }

    public String getNamespace(URL wsdlInterfaceDescriptor) {
	Definition def = null;
	try {
	    WSDLFactory factory = WSDLFactory.newInstance();
	    WSDLReader reader = factory.newWSDLReader();
	    reader.setFeature("javax.wsdl.verbose", false);
	    reader.setFeature("javax.wsdl.importDocuments", true);
	    def = reader.readWSDL(null, wsdlInterfaceDescriptor.toExternalForm());
	} catch (WSDLException e) {
	    e.printStackTrace();
	}

	return def.getTargetNamespace();

    }

    @SuppressWarnings("unchecked")
    public String getPortName(URL wsdlInterfaceDescriptor) {
	Definition def = null;
	try {
	    WSDLFactory factory = WSDLFactory.newInstance();
	    WSDLReader reader = factory.newWSDLReader();
	    reader.setFeature("javax.wsdl.verbose", false);
	    reader.setFeature("javax.wsdl.importDocuments", true);
	    def = reader.readWSDL(null, wsdlInterfaceDescriptor.toExternalForm());
	} catch (WSDLException e) {
	    e.printStackTrace();
	}

	Collection bindingList = def.getBindings().values();
	for (Iterator bindingIterator = bindingList.iterator(); bindingIterator.hasNext();) {
	    BindingImpl bind = (BindingImpl) bindingIterator.next();
	    if (!bind.getPortType().isUndefined())
		return bind.getPortType().getQName().getLocalPart();
	}

	return "";

    }

    public String getDestinationFolder(String destinationPrefix, URL wsdlInterfaceDescriptor) {
	String namespace = getNamespace(wsdlInterfaceDescriptor);
	String destinationFolder = destinationPrefix;

	if (namespace.matches("http://.*")) {
	    String[] pieces = namespace.split("//")[1].split("\\.");
	    String invertedPieces = "";

	    for (int i = pieces.length - 1; i >= 0; i--) {
		invertedPieces = invertedPieces + "/" + pieces[i].replace("/", "");
	    }

	    return destinationFolder.concat(invertedPieces + "/");
	} else
	    return namespace;
<commit_msg>Remove supress warnings 'unchecked' using '<?>'<commit_after>package br.usp.ime.ccsl.choreos.middleware.proxy.codegenerator;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import javax.wsdl.Definition;
import javax.wsdl.WSDLException;
import javax.wsdl.factory.WSDLFactory;
import javax.wsdl.xml.WSDLReader;

import org.apache.commons.io.FileUtils;
import org.apache.cxf.tools.wsdlto.WSDLToJava;

import com.ibm.wsdl.BindingImpl;
import com.sun.tools.javac.Main;

public class CodeGeneratorHelper {
    public static final String SRC_GENERATED_CLIENT_JAVA = "src/generated/client/java";
    public static final String SRC_GENERATED_SERVER_JAVA = "src/generated/server/java";
    public static final String TARGET_GENERATED_SERVER_JAVA_CODE = "target/classes/generated";

    public static final String CLASSPATH = "target/classes";

    public static final boolean SERVER = true;
    public static final boolean CLIENT = false;

    private List<String> implementation;

    private void createImplementationClass(URL wsdlInterfaceDescriptor) {
	implementation = new ArrayList<String>();

	String packageName = getPackage(wsdlInterfaceDescriptor);

	implementation.add("package " + packageName.substring(0, packageName.length() - 1) + ";");
	implementation.add("");
	implementation.add("public class " + getPortName(wsdlInterfaceDescriptor) + "Impl implements "
		+ getPortName(wsdlInterfaceDescriptor) + "{");
	implementation.add("");

	String className = packageName + getPortName(wsdlInterfaceDescriptor);
	Class<?> implementedInterface = getInterfaceClass(className);

	for (int i = 0; i < implementedInterface.getMethods().length; i++) {
	    generateMethod(implementedInterface.getMethods()[i]);
	}

	implementation.add("}");

	String fileName = getDestinationFolder(CodeGeneratorHelper.SRC_GENERATED_SERVER_JAVA, wsdlInterfaceDescriptor)
		+ getPortName(wsdlInterfaceDescriptor) + "Impl.java";
	File output = new File(fileName);

	writeLines(output, implementation);
    }

    private String getPackage(URL wsdlInterfaceDescriptor) {
	return getDestinationFolder("", wsdlInterfaceDescriptor).replace('/', '.').substring(1);
    }

    private void generateMethod(Method method) {
	String line = "public " + method.getReturnType().getSimpleName() + " " + method.getName() + "(";

	for (int i = 0; i < method.getParameterTypes().length; i++) {
	    line = line + method.getParameterTypes()[i].getSimpleName() + " param" + i;
	    line = (i == method.getParameterTypes().length) ? line + ", " : line;
	}
	line = line + "){";
	implementation.add(line);

	line = "Exception e = new Exception(\"There is no web service active for this role\");";
	implementation.add(line);
	implementation.add("return null;");
	implementation.add("}");

    }

    private Class<?> getInterfaceClass(String portName) {
	Class<?> clazz = null;
	try {
	    clazz = Class.forName(portName);
	} catch (ClassNotFoundException e) {
	    e.printStackTrace();
	}
	if (clazz == null)
	    System.out.println("Class " + portName + " was not found.");
	return clazz;
    }

    public String generateJavaCode(URL wsdlInterfaceDescriptor, boolean server) {
	String[] parameters = null;
	if (server) {
	    parameters = new String[] { "-server", "-d", SRC_GENERATED_SERVER_JAVA,
		    wsdlInterfaceDescriptor.toExternalForm() };
	    WSDLToJava.main(parameters);
	    createImplementationClass(wsdlInterfaceDescriptor);
	    return getDestinationFolder(SRC_GENERATED_SERVER_JAVA, wsdlInterfaceDescriptor);
	} else {
	    parameters = new String[] { "-client", "-d", SRC_GENERATED_CLIENT_JAVA,
		    wsdlInterfaceDescriptor.toExternalForm() };
	    WSDLToJava.main(parameters);
	    return getDestinationFolder(SRC_GENERATED_CLIENT_JAVA, wsdlInterfaceDescriptor);
	}

    }

    private void writeLines(File fileHandler, List<String> fileLinesCopy) {
	try {
	    FileUtils.writeLines(fileHandler, fileLinesCopy);
	} catch (IOException e) {
	    System.out.println("Problem writing output to file");
	    e.printStackTrace();
	}
    }

    public String getNamespace(URL wsdlInterfaceDescriptor) {
	Definition def = null;
	try {
	    WSDLFactory factory = WSDLFactory.newInstance();
	    WSDLReader reader = factory.newWSDLReader();
	    reader.setFeature("javax.wsdl.verbose", false);
	    reader.setFeature("javax.wsdl.importDocuments", true);
	    def = reader.readWSDL(null, wsdlInterfaceDescriptor.toExternalForm());
	} catch (WSDLException e) {
	    e.printStackTrace();
	}

	return def.getTargetNamespace();
    }

    public String getPortName(URL wsdlInterfaceDescriptor) {
	Definition def = null;
	try {
	    WSDLFactory factory = WSDLFactory.newInstance();
	    WSDLReader reader = factory.newWSDLReader();
	    reader.setFeature("javax.wsdl.verbose", false);
	    reader.setFeature("javax.wsdl.importDocuments", true);
	    def = reader.readWSDL(null, wsdlInterfaceDescriptor.toExternalForm());
	} catch (WSDLException e) {
	    e.printStackTrace();
	}

	Collection<?> bindingList = def.getBindings().values();
	for (Iterator<?> bindingIterator = bindingList.iterator(); bindingIterator.hasNext();) {
	    BindingImpl bind = (BindingImpl) bindingIterator.next();
	    if (!bind.getPortType().isUndefined())
		return bind.getPortType().getQName().getLocalPart();
	}

	return "";
    }

    public String getDestinationFolder(String destinationPrefix, URL wsdlInterfaceDescriptor) {
	String namespace = getNamespace(wsdlInterfaceDescriptor);
	String destinationFolder = destinationPrefix;

	if (namespace.matches("http://.*")) {
	    String[] pieces = namespace.split("//")[1].split("\\.");
	    String invertedPieces = "";

	    for (int i = pieces.length - 1; i >= 0; i--) {
		invertedPieces = invertedPieces + "/" + pieces[i].replace("/", "");
	    }

	    return destinationFolder.concat(invertedPieces + "/");
	} else
	    return namespace;

{'fraction_non_alphanumeric': 0.12713834911144328, 'fraction_numerical': 0.0013286829430327188, 'mean_word_length': 4.656646312822922, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5716781', 'n_tokens_mistral': 3756, 'n_tokens_neox': 3494, 'n_words': 730}