org.redmine.ta.internal.RedmineXMLGenerator Maven / Gradle / Ivy
Show all versions of redmine-java-api Show documentation
package org.redmine.ta.internal;
import org.redmine.ta.beans.*;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* Can't use Castor here because this "post" format differs from "get" one. see
* http://www.redmine.org/issues/6128#note-2 for details.
*
* CANNOT use JAXB or Castor XML libraries because they do not work under
* Android OS.
*
* Also CANNOT use Simple XML because of this:
* http://sourceforge.net/mailarchive/message.php?msg_id=27079427
*
* @author Alexey Skorokhodov
*/
public class RedmineXMLGenerator {
private static final String REDMINE_START_DATE_FORMAT = "yyyy-MM-dd";
private static final SimpleDateFormat sdf = new SimpleDateFormat(
REDMINE_START_DATE_FORMAT);
private final static String XML_PREFIX = "\n";
public static String toXML(String projectKey, Issue issue) {
StringBuilder b = new StringBuilder(XML_PREFIX + "");
// projectKey is required for "new issue" request, but not for
// "update issue" one.
appendIfNotNull(b, "project_id", projectKey);
appendIfNotNull(b, "parent_issue_id", issue.getParentId());
appendIfNotNull(b, "subject", issue.getSubject());
if (issue.getTracker() != null) {
appendIfNotNull(b, "tracker_id", issue.getTracker().getId());
}
appendIfNotNull(b, "notes", issue.getNotes());
appendIfNotNull(b, "status_id", issue.getStatusId());
//have to make such ugly solution
//cause dy default redmine the start date field of new issue is set as today
//redmine promises to include this patch into next major version
//http://www.redmine.org/issues/2277
if (issue.getStartDate() == null) {
appendNull(b, "start_date");
}
else {
appendIfNotNull(b, "start_date", issue.getStartDate());
}
appendIfNotNull(b, "due_date", issue.getDueDate());
if (issue.getEstimatedHours() != null) {
appendIfNotNull(b, "estimated_hours", issue.getEstimatedHours());
}
appendIfNotNull(b, "description", issue.getDescription());
User ass = issue.getAssignee();
if (ass != null) {
appendIfNotNull(b, "assigned_to_id", ass.getId());
}
appendIfNotNull(b, "done_ratio", issue.getDoneRatio());
if (!issue.getCustomFields().isEmpty()) {
b.append("");
for (CustomField field : issue.getCustomFields()) {
b.append("");
b.append("" + field.getValue() +" ");
b.append(" ");
}
b.append(" ");
}
b.append(" ");
return b.toString();
}
public static String toXML(Object o) {
// Redmine objects don't have some common base class
if (o instanceof TimeEntry) {
return toXML((TimeEntry) o);
}
if (o instanceof Project) {
return toXML((Project) o);
}
if (o instanceof User) {
return toXML((User) o);
}
if (o instanceof IssueRelation) {
return toXML((IssueRelation) o);
}
throw new RuntimeException("Object type is not supported.");
}
public static String toXML(TimeEntry timeEntry) {
StringBuilder b = new StringBuilder(XML_PREFIX + "");
appendIfNotNull(b, "id", timeEntry.getId());
appendIfNotNull(b, "issue_id", timeEntry.getIssueId());
appendIfNotNull(b, "project_id", timeEntry.getProjectId());
appendIfNotNull(b, "user_id", timeEntry.getUserId());
appendIfNotNull(b, "activity_id", timeEntry.getActivityId());
appendIfNotNull(b, "hours", timeEntry.getHours());
appendIfNotNull(b, "comments", timeEntry.getComment());
appendIfNotNull(b, "spent_on", timeEntry.getSpentOn());
b.append(" ");
return b.toString();
}
public static String toXML(Project o) {
StringBuilder b = new StringBuilder(XML_PREFIX + "");
appendIfNotNull(b, "id", o.getId());
appendIfNotNull(b, "name", o.getName());
appendIfNotNull(b, "identifier", o.getIdentifier());
appendIfNotNull(b, "description", o.getDescription());
appendIfNotNull(b, "homepage", o.getHomepage());
appendIfNotNull(b, "parent_id", o.getParentId());
b.append(" ");
return b.toString();
}
public static String toXML(User o) {
StringBuilder b = new StringBuilder(XML_PREFIX + "");
appendIfNotNull(b, "id", o.getId());
appendIfNotNull(b, "login", o.getLogin());
appendIfNotNull(b, "password", o.getPassword());
appendIfNotNull(b, "firstname", o.getFirstName());
appendIfNotNull(b, "lastname", o.getLastName());
appendIfNotNull(b, "mail", o.getMail());
b.append(" ");
return b.toString();
}
public static String toXML(IssueRelation o) {
StringBuilder b = new StringBuilder(XML_PREFIX + "");
appendIfNotNull(b, "issue_to_id", o.getIssueToId());
appendIfNotNull(b, "relation_type", o.getType());
b.append(" ");
return b.toString();
}
/**
* append, if the value is not NULL
*/
private static final void appendIfNotNull(StringBuilder b, String tag, Object value) {
if (value != null) {
b.append("<" + tag + ">");
if (value instanceof Date) {
// always use Short Date Format for now!
b.append(sdf.format(value));
} else if (value instanceof String) {
b.append(encodeXML((String) value));
} else {
b.append(value);
}
b.append("" + tag + ">");
}
}
/**
* append NULL value
*/
private static final void appendNull(StringBuilder b, String tag) {
b.append("<" + tag + ">null" + tag + ">");
}
private static String encodeXML(String value) {
return value.replace("&", "&").replace("'", "'")
.replace("\"", """).replace("<", "<")
.replace(">", ">");
}
}