EA-23238 - SIOOBE: TemplateResource.getMethodBody

This commit is contained in:
Dmitry Jemerov
2010-11-09 17:27:44 +03:00
parent aa6f787a8d
commit 9d0e24d7f3
@@ -27,246 +27,250 @@ import java.io.Serializable;
* the text is stored.
*/
public class TemplateResource implements Serializable {
private final boolean isDefault;
private String fileName = "";
private String template = "";
private final boolean isDefault;
private String fileName = "";
private String template = "";
/**
* Constructor.
*
* @param fileName a template filename
* @param template the template velocity body content
*/
public TemplateResource(String fileName, String template) {
this(fileName, template, false);
/**
* Constructor.
*
* @param fileName a template filename
* @param template the template velocity body content
*/
public TemplateResource(String fileName, String template) {
this(fileName, template, false);
}
public TemplateResource(String fileName, String template, boolean aDefault) {
isDefault = aDefault;
this.fileName = fileName;
this.template = template;
}
/**
* Bean constructor
*/
public TemplateResource() {
isDefault = false;
}
public String getTemplate() {
return template;
}
public void setTemplate(String template) {
this.template = template;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public boolean isDefault() {
return isDefault;
}
/**
* Get's the javadoc, if any.
*
* @return the javadoc, null if no javadoc.
*/
@Nullable
public String getJavaDoc() {
int i = template.indexOf("*/");
if (i == -1) {
return null;
}
public TemplateResource(String fileName, String template, boolean aDefault) {
isDefault = aDefault;
this.fileName = fileName;
this.template = template;
return template.substring(0, i + 2);
}
/**
* Get's the method body.
*
* @return the method body.
*/
public String getMethodBody() {
return getMethodBody(template);
}
@Nullable
private static String getMethodBody(String template) {
String signature = getMethodSignature(template);
String s = StringUtil.after(template, signature);
if (s == null) {
return null;
}
/**
* Bean constructor
*/
public TemplateResource() {
isDefault = false;
// skip the starting and ending { }
final String trimmed = s.trim();
return trimmed.substring(1, s.length() - 1);
}
/**
* Gets the method signature
* <p/>
* <code>public String toString()</code>
*/
public String getMethodSignature() {
return getMethodSignature(template);
}
private static String getMethodSignature(String template) {
String s = StringUtil.after(template, "*/").trim();
StringBuffer signature = new StringBuffer();
String[] lines = s.split("\n");
for (String line : lines) {
line = line.trim();
if (line.startsWith("@")) {
continue;
}
signature.append(line);
if (line.indexOf("{") > -1) {
break;
}
}
public String getTemplate() {
return template;
// remove last {
String result = signature.toString();
return result.substring(0, result.lastIndexOf("{"));
}
/**
* Get's the method that this template is for (toString)
*/
public String getTargetMethodName() {
String s = getMethodSignature();
s = StringUtil.before(s, "(");
int i = s.lastIndexOf(" ");
return s.substring(i).trim();
}
/**
* Validates this template to see if its valid for plugin v3.10 or higher.
*
* @return true if valid, false if not
*/
public boolean isValidTemplate() {
return isValidTemplate(template);
}
/**
* Validates the provided template.
*
* @param template the template to validate.
* @return true if valid, false if not.
*/
public static boolean isValidTemplate(String template) {
template = template.trim();
if (template.indexOf("{") == -1) {
return false;
}
public void setTemplate(String template) {
this.template = template;
// ending } must be the last character
String s = template.trim();
if (s.lastIndexOf("}") != s.length() - 1) {
return false;
}
public String getFileName() {
return fileName;
if (getMethodSignature(template) == null) {
return false;
}
public void setFileName(String fileName) {
this.fileName = fileName;
if (getMethodBody(template) == null) {
return false;
}
public boolean isDefault() {
return isDefault;
return true;
}
/**
* Does the template use annotations?
*
* @return true if so, false if not.
*/
public boolean hasAnnotations() {
return getAnnotations() != null;
}
/**
* Get's the annotations
*
* @return the annotation, null if does not have.
*/
public String[] getAnnotations() {
String signature = getMethodSignature();
String javadoc = getJavaDoc();
String annotations;
if (javadoc != null) {
annotations = StringUtil.middle(template, javadoc, signature);
}
else {
annotations = StringUtil.before(template, signature);
}
/**
* Get's the javadoc, if any.
*
* @return the javadoc, null if no javadoc.
*/
@Nullable
public String getJavaDoc() {
int i = template.indexOf("*/");
if (i == -1)
return null;
return template.substring(0, i + 2);
if (StringUtil.isEmpty(annotations)) {
return null;
}
/**
* Get's the method body.
*
* @return the method body.
*/
public String getMethodBody() {
return getMethodBody(template);
if (annotations.indexOf("@") == -1) {
return null;
}
@Nullable
private static String getMethodBody(String template) {
String signature = getMethodSignature(template);
String s = StringUtil.after(template, signature);
if (s == null)
return null;
// skip the starting and ending { }
return s.trim().substring(1, s.length() - 1);
// remove first and last \n
annotations = annotations.trim();
if (annotations.startsWith("\n")) {
annotations = annotations.substring(1);
}
if (annotations.endsWith("\n")) {
annotations = annotations.substring(0, annotations.length() - 1);
}
/**
* Gets the method signature
* <p/>
* <code>public String toString()</code>
*/
public String getMethodSignature() {
return getMethodSignature(template);
}
return annotations.split("\n");
}
private static String getMethodSignature(String template) {
String s = StringUtil.after(template, "*/").trim();
/**
* Important to return filename only as it is the displayname in the UI.
*
* @return filename for UI.
*/
public String toString() {
return fileName != null ? fileName : template;
}
StringBuffer signature = new StringBuffer();
public String getName() {
return fileName;
}
String[] lines = s.split("\n");
for (String line : lines) {
line = line.trim();
if (line.startsWith("@")) {
continue;
}
signature.append(line);
if (line.indexOf("{") > -1) {
break;
}
}
public void copyFrom(TemplateResource templateResource) {
fileName = templateResource.getFileName();
template = templateResource.getTemplate();
}
// remove last {
String result = signature.toString();
return result.substring(0, result.lastIndexOf("{"));
}
public void setName(String name) {
fileName = name;
}
/**
* Get's the method that this template is for (toString)
*/
public String getTargetMethodName() {
String s = getMethodSignature();
s = StringUtil.before(s, "(");
int i = s.lastIndexOf(" ");
return s.substring(i).trim();
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof TemplateResource)) return false;
/**
* Validates this template to see if its valid for plugin v3.10 or higher.
*
* @return true if valid, false if not
*/
public boolean isValidTemplate() {
return isValidTemplate(template);
}
TemplateResource that = (TemplateResource)o;
/**
* Validates the provided template.
*
* @param template the template to validate.
* @return true if valid, false if not.
*/
public static boolean isValidTemplate(String template) {
template = template.trim();
return fileName.equals(that.fileName) && template.equals(that.template);
}
if (template.indexOf("{") == -1) {
return false;
}
// ending } must be the last character
String s = template.trim();
if (s.lastIndexOf("}") != s.length() - 1) {
return false;
}
if (getMethodSignature(template) == null) {
return false;
}
if (getMethodBody(template) == null) {
return false;
}
return true;
}
/**
* Does the template use annotations?
*
* @return true if so, false if not.
*/
public boolean hasAnnotations() {
return getAnnotations() != null;
}
/**
* Get's the annotations
*
* @return the annotation, null if does not have.
*/
public String[] getAnnotations() {
String signature = getMethodSignature();
String javadoc = getJavaDoc();
String annotations;
if (javadoc != null) {
annotations = StringUtil.middle(template, javadoc, signature);
} else {
annotations = StringUtil.before(template, signature);
}
if (StringUtil.isEmpty(annotations)) {
return null;
}
if (annotations.indexOf("@") == -1) {
return null;
}
// remove first and last \n
annotations = annotations.trim();
if (annotations.startsWith("\n")) {
annotations = annotations.substring(1);
}
if (annotations.endsWith("\n")) {
annotations = annotations.substring(0, annotations.length()-1);
}
return annotations.split("\n");
}
/**
* Important to return filename only as it is the displayname in the UI.
* @return filename for UI.
*/
public String toString() {
return fileName != null ? fileName : template;
}
public String getName() {
return fileName;
}
public void copyFrom(TemplateResource templateResource) {
fileName = templateResource.getFileName();
template = templateResource.getTemplate();
}
public void setName(String name) {
fileName = name;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof TemplateResource)) return false;
TemplateResource that = (TemplateResource) o;
return fileName.equals(that.fileName) && template.equals(that.template);
}
@Override
public int hashCode() {
return 31 * fileName.hashCode() + template.hashCode();
}
@Override
public int hashCode() {
return 31 * fileName.hashCode() + template.hashCode();
}
}