blob: bc6e9d22341ac11f74941cb41fa95f66f43edcea [file] [log] [blame]
/*
* Copyright (c) 2005, Innoopract Informationssysteme GmbH.
* 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
*
* Contributors:
* Innoopract - initial API and implementation
*/
package bug2html;
import java.util.Iterator;
import java.util.Set;
/**
* Converts a Set of Bugs to an XML-Representation.
* @see #generateXML(Set)
* @author Elias
*/
class XMLGenerator {
private boolean withEmptyMilestones;
private static final String EMPTY_MILESTONE = "---";
XMLGenerator() {
// utility class;
}
/**
* Enables inclusion of bugs with empty milestones in output.
*/
void setEmptyMilestones(final boolean isEnabled) {
withEmptyMilestones = isEnabled;
}
private StringBuffer bugToXML(final Bug bug) {
if (bug == null) {
throw new IllegalArgumentException();
}
StringBuffer result = new StringBuffer();
// item
result.append("<item priority=\"" + priorityToString(bug) + "\"");
String status = statusToString(bug);
if (status != null) {
result.append(" status=\"" + status + "\"");
}
if (bug.isHelpWanted()) {
result.append(" helpWanted=\"true\"");
}
result.append(">\n");
// description
result.append(" <description>" + "[" + toHTML(bug.getComponent()) + "] "
+ toHTML(bug.getShortDescr()) + "</description>\n");
// complete description
if (bug.getLongDescr() != Description.EMPTY_DESCRIPTION) {
result.append(" <detail>" + toHTML(bug.getLongDescr().toString())
+ "</detail>\n");
}
// bugzilla link
result.append(" <bugzilla link=\"" + idToURI(bug) + "\"/>\n");
// depends on other bugs
Set dependencies = bug.getDependencies();
if (!dependencies.isEmpty()) {
Iterator iter = dependencies.iterator();
while (iter.hasNext()) {
int bugId = ((Integer) iter.next()).intValue();
result.append(" <step><description href=\"" + idToURI(bugId) + "\">");
result.append("depends on " + bugId);
result.append("</description></step>\n");
}
}
// close item
result.append("</item>\n");
return result;
}
/**
* Converts a Set of Bugs to an XML-Representation.
* The XML-Representation conforms to milestonePlan.xsd.
* @return the XML-Representation in a StringBuffer
*/
StringBuffer generateXML(final Set bugSet) {
StringBuffer result = new StringBuffer();
// header
result.append("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n");
result.append("<plan xmlns:xsi="
+ "\"http://www.w3.org/2001/XMLSchema-instance\"\n");
result.append(" xsi:noNamespaceSchemaLocation=\"../../../../"
+ "development/milestone_plans/milestonePlan.xsd\">\n");
result.append("<component name=\"Overview of\" subproject=\"any\">\n");
result.append("<milestone name=\"Milestones\">\n");
result.append("<title>All Milestones</title>\n");
// main section
String lastMilestone = null;
Iterator iter = bugSet.iterator();
while (iter.hasNext()) {
Bug bug = (Bug) iter.next();
// still in same milestone?
if (lastMilestone == null || !lastMilestone.equals(bug.getMilestone())) {
if (lastMilestone != null) {
result.append("</category>\n");
}
result.append("<category name=\"" + toHTML(bug.getMilestone())
+ "\">\n");
lastMilestone = bug.getMilestone();
}
// append bug to milestone
if (withEmptyMilestones) {
result.append(bugToXML(bug));
} else if (!(EMPTY_MILESTONE.equals(bug.getMilestone()))) {
result.append(bugToXML(bug));
}
}
result.append("</category>\n");
// footer
result.append("</milestone>\n");
result.append("</component>\n");
result.append("</plan>\n");
return result;
}
private String idToURI(final Bug bug) {
return idToURI(bug.getId());
}
private String idToURI(final int id) {
return BugFetcher.BUGZILLA_BUGLIST_QUERY + "id=" + id;
}
private String match(final String[][] map, final String match) {
for (int i = 0; i < map.length; i++) {
if (map[i][0].equals(match)) {
return map[i][1];
}
}
throw new IllegalArgumentException(match);
}
private String priorityToString(final Bug bug) {
String[][] map = {
{
"P1", "high"
}, {
"P2", "medium"
}, {
"P3", "low"
}, {
"P4", "low"
}, {
"P5", "low"
}
};
return match(map, bug.getPriority());
}
private String statusToString(final Bug bug) {
String[][] map = {
{
"UNCONFIRMED", "investigate"
}, {
"NEW", "new"
}, {
"ASSIGNED", "in-progress"
}, {
"REOPENED", null
}, {
"RESOLVED", "done"
}, {
"VERIFIED", "done"
}, {
"CLOSED", "done"
}
};
return match(map, bug.getStatus());
}
private String toHTML(final String input) {
if (input == null) {
throw new IllegalArgumentException();
}
StringBuffer result = new StringBuffer();
for (int i = 0, n = input.length(); i < n; i++) {
char c = input.charAt(i);
switch (c) {
case '<':
result.append("&lt;");
break;
case '>':
result.append("&gt;");
break;
case '&':
result.append("&amp;");
break;
case '"':
result.append("&quot;");
break;
default:
result.append(c);
}
}
return result.toString();
}
}