Debian Codegraph @

src/dr/xml/AbstractXMLObjectParser.java

raw symbols SCIP
1 /*
2 * AbstractXMLObjectParser.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 dr.app.tools.BeastParserDoc;
29 import org.w3c.dom.NamedNodeMap;
30
31 import java.io.PrintWriter;
32 import java.io.StringWriter;
33 import java.util.ArrayList;
34 import java.util.List;
35 import java.util.Map;
36
37 public abstract class AbstractXMLObjectParser implements XMLObjectParser {
38
39 public final Object parseXMLObject(XMLObject xo, String id, Map<String, XMLObject> store, boolean strictXML)
40 throws XMLParseException {
41
42 this.store = store;
43
44 if (hasSyntaxRules()) {
45 final XMLSyntaxRule[] rules = getSyntaxRules();
46 for (XMLSyntaxRule rule : rules) {
47 if (!rule.isSatisfied(xo)) {
48 if (id != null) {
49 throw new XMLParseException("The '<" + getParserName() +
50 ">' element, with id, '" + id +
51 "', is incorrectly constructed.\nThe following was expected:\n" +
52 rule.ruleString(xo));
53 } else {
54 String parentId = null;
55 XMLObject xop = xo;
56 while (parentId == null && xop != null) {
57 xop = xop.getParent();
58 if (xop != null && xop.hasId()) {
59 parentId = xop.getId();
60 }
61 }
62
63 throw new XMLParseException("The '<" + getParserName() +
64 ">' element, nested within an element with id, '" + parentId +
65 "', is incorrectly constructed.\nThe following was expected:\n" +
66 rule.ruleString(xo));
67 }
68 }
69 }
70
71 // Look for undeclared attributes and issue a warning
72 final NamedNodeMap attributes = xo.getAttributes();
73 for (int k = 0; k < attributes.getLength(); ++k) {
74 String name = attributes.item(k).getNodeName();
75 if (name.equals(XMLObject.ID)) continue;
76
77 for (XMLSyntaxRule rule : rules) {
78 if (rule.containsAttribute(name)) {
79 name = null;
80 break;
81 }
82 }
83 if (name != null) {
84 final String msg = "Unhandled attribute (typo?) " + name + " in " + xo;
85 if (strictXML) {
86 throw new XMLParseException(msg);
87 }
88 // System.err.println("WARNING:" + msg);
89 java.util.logging.Logger.getLogger("dr.xml").warning(msg);
90
91 }
92 }
93
94 // try to catch out of place elements, placed either by mistake or from older incompatible files.
95
96 for (int k = 0; k < xo.getChildCount(); ++k) {
97 final Object child = xo.getChild(k);
98 String unexpectedName;
99 if (child instanceof XMLObject) {
100 final XMLObject ch = (XMLObject) child;
101 unexpectedName = !isAllowed(ch.getName()) ? ch.getName() : null;
102 final List<String> unexpected = isUnexpected(ch);
103 if (unexpected != null) {
104 String n = "";
105 for (int j = 0; j < unexpected.size(); j += 2) {
106 n = n + ", " + unexpected.get(j) + " in " + unexpected.get(j + 1);
107 }
108 if (unexpectedName == null) {
109 unexpectedName = n.substring(1, n.length());
110 } else {
111 unexpectedName = unexpectedName + n;
112 }
113 }
114 } else {
115 unexpectedName = child.getClass().getName();
116 for (XMLSyntaxRule rule : rules) {
117 if (rule.isLegalElementClass(child.getClass())) {
118 unexpectedName = null;
119 break;
120 }
121 }
122 }
123 if (unexpectedName != null) {
124
125 String msg = "Unexpected element in " + xo + ": " + unexpectedName;
126 if (strictXML) {
127 throw new XMLParseException(msg);
128 }
129 // System.err.println("WARNING: " + msg);
130 java.util.logging.Logger.getLogger("dr.xml").warning(msg);
131 }
132 }
133 }
134
135 try {
136 return parseXMLObject(xo);
137 } catch (XMLParseException xpe) {
138 XMLParseException e = new XMLParseException("Error parsing '<" + getParserName() +
139 ">' element with id, '" + id + "':\n" +
140 xpe.getMessage());
141 e.setStackTrace(xpe.getStackTrace());
142 throw e;
143 }
144 }
145
146 public String[] getParserNames() {
147 return new String[]{getParserName()};
148 }
149
150 public final void throwUnrecognizedElement(XMLObject xo) throws XMLParseException {
151 throw new XMLParseException("Unrecognized element '<" + xo.getName() + ">' in element '<" + getParserName() + ">'");
152 }
153
154 public abstract Object parseXMLObject(XMLObject xo) throws XMLParseException;
155
156 /**
157 * @return an array of syntax rules required by this element.
158 * Order is not important.
159 */
160 public abstract XMLSyntaxRule[] getSyntaxRules();
161
162 /**
163 * Allowed if any of the rules allows that element
164 *
165 * @param elementName String
166 * @return boolean isAllowed
167 */
168 public final boolean isAllowed(String elementName) {
169 final XMLSyntaxRule[] rules = getSyntaxRules();
170 if (rules != null && rules.length > 0) {
171 for (XMLSyntaxRule rule : rules) {
172 if (rule.isLegalElementName(elementName)) {
173 return true;
174 }
175 }
176 }
177
178 return false;
179 }
180
181 public final List<String> isUnexpected(XMLObject element) {
182 List<String> un = null;
183 final XMLSyntaxRule[] rules = getSyntaxRules();
184 if (rules != null && rules.length > 0) {
185 for (XMLSyntaxRule rule : rules) {
186 if (rule.isLegalElementName(element.getName())) {
187 for (int nc = 0; nc < element.getChildCount(); ++nc) {
188 final Object child = element.getChild(nc);
189 if (child instanceof XMLObject) {
190 final String name = ((XMLObject) child).getName();
191 if (!rule.isLegalSubelementName(name)) {
192 if (un == null) {
193 un = new ArrayList<String>();
194 }
195 un.add(name);
196 un.add(element.getName());
197 }
198 }
199 }
200 }
201 }
202 }
203
204 return un;
205 }
206
207 public abstract String getParserDescription();
208
209 public abstract Class getReturnType();
210
211 public final boolean hasExample() {
212 return getExample() != null;
213 }
214
215 public String getExample() {
216 return null;
217 }
218
219 public final Map<String, XMLObject> getStore() {
220 return store;
221 }
222
223 /**
224 * @return a description of this parser as a string.
225 */
226 public final String toHTML(XMLDocumentationHandler handler) {
227 StringBuffer buffer = new StringBuffer();
228 buffer.append("<div id=\"").append(getParserName()).append("\" class=\"element\">\n");
229 buffer.append(" <div class=\"elementheader\">\n");
230 buffer.append(" <span class=\"elementname\"><a href=\"").append(BeastParserDoc.INDEX_HTML)
231 .append("#").append(getParserName()).append("\"> <h3>&lt;").append(getParserName())
232 .append("&gt;</h3></a></span>\n");
233 buffer.append(" <div class=\"description\"><b>Description:</b><br>\n");
234 buffer.append(" ").append(getParserDescription()).append("\n");
235 buffer.append(" </div>\n");
236 buffer.append(" </div>\n");
237 if (hasSyntaxRules()) {
238 XMLSyntaxRule[] rules = getSyntaxRules();
239 buffer.append(" <div class=\"rules\"><b>Rule:</b>\n");
240 for (XMLSyntaxRule rule : rules) {
241 buffer.append(rule.htmlRuleString(handler));
242 }
243 buffer.append(" </div>\n");
244 }
245 if (hasExample()) {
246 buffer.append("<div class=\"example\"><b>Example:</b>");
247 StringWriter sw = new StringWriter();
248 PrintWriter pw = new PrintWriter(sw);
249 handler.outputHTMLExampleXML(pw, this);
250 pw.flush();
251 pw.close();
252 buffer.append(sw.toString());
253 buffer.append("</div>\n");
254 }
255 buffer.append("</div>\n");
256 return buffer.toString();
257 }
258
259 /**
260 * @return a description of this parser as a string.
261 */
262 public final String toWiki(XMLDocumentationHandler handler) {
263 StringBuffer buffer = new StringBuffer();
264 buffer.append("===<code>&lt;").append(getParserName()).append("&gt;</code> element===\n\n");
265 buffer.append(getParserDescription()).append("\n\n");
266
267 if (hasSyntaxRules()) {
268 XMLSyntaxRule[] rules = getSyntaxRules();
269 List<XMLSyntaxRule> attributes = new ArrayList<XMLSyntaxRule>();
270 List<XMLSyntaxRule> contents = new ArrayList<XMLSyntaxRule>();
271 for (XMLSyntaxRule rule : rules) {
272 if (rule instanceof AttributeRule) {
273 attributes.add(rule);
274 } else {
275 contents.add(rule);
276 }
277 }
278
279 if (attributes.size() > 0) {
280 buffer.append("\nThe element takes following attributes:\n");
281 for (XMLSyntaxRule rule : attributes) {
282 buffer.append(rule.wikiRuleString(handler, "*"));
283 }
284 buffer.append("\n");
285 }
286
287 if (contents.size() > 0) {
288 buffer.append("\nThe element has the following contents:\n");
289 for (XMLSyntaxRule rule : contents) {
290 buffer.append(rule.wikiRuleString(handler, "*"));
291 }
292 buffer.append("\n");
293 }
294 }
295 buffer.append("\nExample:\n");
296 StringWriter sw = new StringWriter();
297 PrintWriter pw = new PrintWriter(sw);
298 handler.outputHTMLExampleXML(pw, this);
299 pw.flush();
300 pw.close();
301 buffer.append(sw.toString());
302 buffer.append("\n");
303 return buffer.toString();
304 }
305
306 /**
307 * @return a description of this parser as a string.
308 */
309 public final String toMarkdown(XMLDocumentationHandler handler) {
310 StringBuffer buffer = new StringBuffer();
311 buffer.append("## <code>&lt;").append(getParserName()).append("&gt;</code> element\n\n");
312 buffer.append(getParserDescription()).append("\n\n");
313
314 if (hasSyntaxRules()) {
315 XMLSyntaxRule[] rules = getSyntaxRules();
316 List<XMLSyntaxRule> attributes = new ArrayList<XMLSyntaxRule>();
317 List<XMLSyntaxRule> contents = new ArrayList<XMLSyntaxRule>();
318 for (XMLSyntaxRule rule : rules) {
319 if (rule instanceof AttributeRule) {
320 attributes.add(rule);
321 } else {
322 contents.add(rule);
323 }
324 }
325
326 if (attributes.size() > 0) {
327 buffer.append("The element takes following attributes:\n\n");
328 for (XMLSyntaxRule rule : attributes) {
329 buffer.append(rule.markdownRuleString(handler, ""));
330 }
331 }
332
333 if (contents.size() > 0) {
334 buffer.append("The element has the following contents:\n\n");
335 for (XMLSyntaxRule rule : contents) {
336 buffer.append(rule.markdownRuleString(handler, ""));
337 }
338 }
339 }
340 buffer.append("Example:\n\n");
341 StringWriter sw = new StringWriter();
342 PrintWriter pw = new PrintWriter(sw);
343 handler.outputMarkdownExampleXML(pw, this);
344 pw.flush();
345 pw.close();
346 buffer.append(sw.toString());
347 buffer.append("\n");
348 return buffer.toString();
349 }
350
351 /**
352 * @return a description of this parser as a string.
353 */
354 public String toString() {
355 StringBuffer buffer = new StringBuffer();
356 buffer.append("\nELEMENT ").append(getParserName()).append("\n");
357
358 if (hasSyntaxRules()) {
359 XMLSyntaxRule[] rules = getSyntaxRules();
360 for (XMLSyntaxRule rule : rules) {
361 buffer.append(" ").append(rule.ruleString()).append("\n");
362 }
363 }
364 return buffer.toString();
365 }
366
367 //************************************************************************
368 // private methods
369 //************************************************************************
370
371 public final boolean hasSyntaxRules() {
372 XMLSyntaxRule[] rules = getSyntaxRules();
373 return (rules != null && rules.length > 0);
374 }
375
376 private Map<String, XMLObject> store = null;
377 }