Debian Codegraph @

src/dr/xml/Report.java

raw symbols SCIP
1 /*
2 * Report.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.xml;
27
28 import java.io.PrintWriter;
29 import java.util.ArrayList;
30
31 /**
32 * A generates a text report from the elements within it.
33 *
34 * @author Andrew Rambaut
35 * @author Alexei Drummond
36 * @author Marc A. Suchard
37 * @version $Id: Report.java,v 1.15 2005/05/24 20:26:01 rambaut Exp $
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 * @param title the title of the report.
76 */
77 public void setTitle(String title) {
78 this.title = title;
79 }
80
81 /**
82 * @return the report's title.
83 */
84 public String getTitle() {
85 return title;
86 }
87
88 /**
89 * @param object the object to be added
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 // AbstractXMLObjectParser implementation
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 }