| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
package dr.xml; |
| 27 |
|
| 28 |
import java.io.PrintWriter; |
| 29 |
import java.util.ArrayList; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public class Report { |
| 40 |
|
| 41 |
public static final String REPORT = "report"; |
| 42 |
public static final String FILENAME = "fileName"; |
| 43 |
|
| 44 |
protected String title = ""; |
| 45 |
protected ArrayList<Object> objects = new ArrayList<Object>(); |
| 46 |
private PrintWriter writer; |
| 47 |
|
| 48 |
public void createReport() { |
| 49 |
|
| 50 |
if (!title.equalsIgnoreCase("")) { |
| 51 |
writer.println(getTitle()); |
| 52 |
writer.println(); |
| 53 |
} |
| 54 |
|
| 55 |
for (Object object : objects) { |
| 56 |
final String item; |
| 57 |
|
| 58 |
if (object instanceof Reportable) { |
| 59 |
item = ((Reportable) object).getReport(); |
| 60 |
} else { |
| 61 |
item = object.toString(); |
| 62 |
} |
| 63 |
writer.print(item.trim()); |
| 64 |
writer.print("\n"); |
| 65 |
} |
| 66 |
writer.println(); |
| 67 |
writer.flush(); |
| 68 |
} |
| 69 |
|
| 70 |
public void setOutput(PrintWriter writer) { |
| 71 |
this.writer = writer; |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
public void setTitle(String title) { |
| 78 |
this.title = title; |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
public String getTitle() { |
| 85 |
return title; |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
public void add(Object object) { |
| 92 |
objects.add(object); |
| 93 |
} |
| 94 |
|
| 95 |
public static XMLObjectParser PARSER = new AbstractXMLObjectParser() { |
| 96 |
|
| 97 |
public String getParserName() { |
| 98 |
return REPORT; |
| 99 |
} |
| 100 |
|
| 101 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 102 |
|
| 103 |
Report report; |
| 104 |
|
| 105 |
if (xo.hasAttribute("type")) { |
| 106 |
if (xo.getAttribute("type").equals("TEXT")) |
| 107 |
report = new Report(); |
| 108 |
else if (xo.getAttribute("type").equals("XHTML")) |
| 109 |
report = new XHTMLReport(); |
| 110 |
else |
| 111 |
throw new XMLParseException("unknown document type, " + xo.getAttribute("type") + ", for report"); |
| 112 |
} else |
| 113 |
report = new Report(); |
| 114 |
|
| 115 |
report.setTitle(xo.getAttribute("title", "")); |
| 116 |
|
| 117 |
for (int i = 0; i < xo.getChildCount(); i++) { |
| 118 |
Object child = xo.getChild(i); |
| 119 |
report.add(child); |
| 120 |
} |
| 121 |
|
| 122 |
report.setOutput(XMLParser.getFilePrintWriter(xo, getParserName())); |
| 123 |
report.createReport(); |
| 124 |
|
| 125 |
return report; |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
public String getParserDescription() { |
| 133 |
return "Generates a report using the given text and elements"; |
| 134 |
} |
| 135 |
|
| 136 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 137 |
return rules; |
| 138 |
} |
| 139 |
|
| 140 |
private final XMLSyntaxRule[] rules = { |
| 141 |
new StringAttributeRule("type", "The format of the report", new String[]{"TEXT", "XHTML"}, true), |
| 142 |
new StringAttributeRule("title", "The title of the report", "Report", true), |
| 143 |
new ElementRule(Object.class, "An arbitrary mixture of text and elements to report", 1, Integer.MAX_VALUE), |
| 144 |
AttributeRule.newStringRule(FILENAME, true), |
| 145 |
}; |
| 146 |
|
| 147 |
public Class getReturnType() { |
| 148 |
return Report.class; |
| 149 |
} |
| 150 |
}; |
| 151 |
|
| 152 |
} |