com.zsmartsystems.zigbee.dongle.ember.autocode.ClassGenerator Maven / Gradle / Ivy
/**
* Copyright (c) 2016-2024 by the respective copyright holders.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package com.zsmartsystems.zigbee.dongle.ember.autocode;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import com.zsmartsystems.zigbee.dongle.ember.autocode.xml.Parameter;
/**
*
* @author Chris Jackson
*
*/
public abstract class ClassGenerator {
protected int lineLen = 80;
protected String sourceRootPath = "../com.zsmartsystems.zigbee.dongle.ember/src/main/java/";
protected List importList = new ArrayList();
protected String stringToConstant(String value) {
value = value.replaceAll("\\(.*?\\) ?", "");
value = value.trim();
value = value.replace("+", "_Plus");
value = value.replace(" ", "_");
value = value.replace("-", "_");
value = value.replace(".", "_");
value = value.replace("/", "_");
value = value.replaceAll("_+", "_");
return value.toUpperCase();
}
private String toProperCase(String str) {
return str.substring(0, 1).toUpperCase() + str.substring(1).toLowerCase();
}
private String toCamelCase(String value) {
value = value.replaceAll("\\(.*?\\) ?", "");
value = value.replace("+", "_Plus");
value = value.replace(" ", "_");
value = value.replace("-", "_");
value = value.replace(".", "_");
value = value.replace("/", "_");
value = value.replaceAll("_+", "_");
String[] parts = value.split("_");
String camelCaseString = "";
for (String part : parts) {
camelCaseString = camelCaseString + toProperCase(part);
}
return camelCaseString;
}
private String splitCamelCase(String str) {
String output = "";
for (String w : str.split("(?") && line.contains("")) {
year = line.substring(line.indexOf("") + 14, line.indexOf(" "));
break;
}
line = br.readLine();
}
br.close();
br = new BufferedReader(new FileReader("../src/etc/header.txt"));
line = br.readLine();
out.println("/**");
while (line != null) {
out.println(" * " + line.replaceFirst("\\$\\{year\\}", year));
line = br.readLine();
}
out.println(" */");
br.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
protected String getDataType(String dataType) {
switch (dataType) {
case "BYTE":
return "int";
case "CONST":
return "String";
case "STRUCT_BYTE":
break;
case "VARIANT":
return "List";
default:
return "ERROR";
}
return null;
}
protected void outputWithLinebreak(PrintStream out, String indent, String line) {
String[] words = line.split(" ");
if (words.length == 0) {
return;
}
out.print(indent + " *");
int len = 2;
for (String word : words) {
if (word.equalsIgnoreCase("note:")) {
if (len > 2) {
out.println();
}
out.println(indent + " * ");
out.print(indent + " * Note:");
continue;
}
if (len + word.length() > lineLen) {
out.println();
out.print(indent + " *");
len = 2;
}
out.print(" ");
out.print(word);
len += word.length();
}
// loop or skipping loop doesn't end line so we do
out.println();
}
protected String formatParameterString(Parameter parameter) {
String modifier = "";
if (parameter.data_type.contains("[")) {
modifier = "[cnt]";
}
if (parameter.displayType != null) {
switch (parameter.displayType.toLowerCase()) {
case "hex":
String size = "";
if (parameter.displayLength != 0) {
size = "0" + parameter.displayLength;
}
return "String.format(\"%" + size + "X\", " + parameter.name + modifier + ")";
case "string":
return parameter.name + modifier;
default:
break;
}
}
return parameter.name + modifier;
}
}