17 |
ilm |
1 |
/*
|
|
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
|
|
3 |
*
|
185 |
ilm |
4 |
* Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
|
17 |
ilm |
5 |
*
|
|
|
6 |
* The contents of this file are subject to the terms of the GNU General Public License Version 3
|
|
|
7 |
* only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
|
|
|
8 |
* copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
|
|
|
9 |
* language governing permissions and limitations under the License.
|
|
|
10 |
*
|
|
|
11 |
* When distributing the software, include this License Header Notice in each file.
|
|
|
12 |
*/
|
|
|
13 |
|
|
|
14 |
package org.openconcerto.odtemplate.engine;
|
|
|
15 |
|
|
|
16 |
import org.openconcerto.odtemplate.TemplateException;
|
|
|
17 |
import org.openconcerto.odtemplate.statements.ForEach;
|
|
|
18 |
import org.openconcerto.odtemplate.statements.If;
|
|
|
19 |
import org.openconcerto.odtemplate.statements.Include;
|
|
|
20 |
import org.openconcerto.odtemplate.statements.SetStmt;
|
|
|
21 |
import org.openconcerto.odtemplate.statements.Statement;
|
|
|
22 |
import org.openconcerto.xml.JDOMUtils;
|
|
|
23 |
|
|
|
24 |
import java.util.HashMap;
|
|
|
25 |
import java.util.HashSet;
|
|
|
26 |
import java.util.Iterator;
|
|
|
27 |
import java.util.LinkedHashMap;
|
|
|
28 |
import java.util.Map;
|
|
|
29 |
import java.util.Set;
|
|
|
30 |
|
|
|
31 |
import org.jdom.Element;
|
|
|
32 |
import org.jdom.filter.ElementFilter;
|
|
|
33 |
|
|
|
34 |
/**
|
|
|
35 |
* Parse material, which can then be executed with {@link #execute(DataModel)} .
|
|
|
36 |
*
|
|
|
37 |
* @param <E> type of the material this parser works on.
|
|
|
38 |
* @author Sylvain
|
|
|
39 |
*/
|
|
|
40 |
public class Parsed<E> {
|
|
|
41 |
|
|
|
42 |
static private Set<Statement> createDefaultStatements() {
|
|
|
43 |
final Set<Statement> res = new HashSet<Statement>();
|
|
|
44 |
res.add(new ForEach());
|
|
|
45 |
res.add(new If());
|
|
|
46 |
res.add(new SetStmt());
|
|
|
47 |
res.add(new Include());
|
|
|
48 |
return res;
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
private final Material<E> src;
|
|
|
52 |
private final Map<String, Statement> statements;
|
|
|
53 |
|
|
|
54 |
public Parsed(Material<E> src) throws TemplateException {
|
|
|
55 |
this(src, createDefaultStatements());
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
/**
|
|
|
59 |
* Parse <code>src</code> using the same settings as <code>p</code>.
|
|
|
60 |
*
|
|
|
61 |
* @param src the material to parse.
|
|
|
62 |
* @param p settings.
|
|
|
63 |
* @throws TemplateException if an error occurs.
|
|
|
64 |
*/
|
|
|
65 |
public Parsed(Material<E> src, Parsed<?> p) throws TemplateException {
|
|
|
66 |
this(src, new HashSet<Statement>(p.statements.values()));
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
public Parsed(Material<E> src, Set<Statement> statements) throws TemplateException {
|
|
|
70 |
this.src = src;
|
|
|
71 |
|
|
|
72 |
this.statements = new HashMap<String, Statement>(statements.size());
|
|
|
73 |
for (final Statement stmt : statements) {
|
|
|
74 |
this.statements.put(stmt.getName(), stmt);
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
this.prepare(statements);
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
private void prepare(final Set<Statement> statementsDef) throws TemplateException {
|
|
|
81 |
if (this.src.hasRoot()) {
|
|
|
82 |
final Map<Element, Statement> statements = new LinkedHashMap<Element, Statement>();
|
|
|
83 |
final Iterator<Element> iter = this.src.getRoot().getDescendants(new ElementFilter());
|
|
|
84 |
while (iter.hasNext()) {
|
|
|
85 |
final Element e = iter.next();
|
|
|
86 |
for (final Statement stmt : statementsDef) {
|
|
|
87 |
if (stmt.matches(e)) {
|
|
|
88 |
statements.put(e, stmt);
|
|
|
89 |
break;
|
|
|
90 |
}
|
|
|
91 |
}
|
|
|
92 |
}
|
|
|
93 |
for (final Element e : statements.keySet()) {
|
|
|
94 |
try {
|
|
|
95 |
statements.get(e).prepare(e);
|
|
|
96 |
} catch (Exception exn) {
|
|
|
97 |
throw new TemplateException("Couldn't prepare " + JDOMUtils.output(e), exn);
|
|
|
98 |
}
|
|
|
99 |
}
|
|
|
100 |
}
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
public Material<E> getSource() {
|
|
|
104 |
return this.src;
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
public Statement getStatement(String name) {
|
|
|
108 |
return this.statements.get(name);
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
public E execute(final DataModel data) throws TemplateException {
|
|
|
112 |
if (!this.getSource().hasRoot()) {
|
|
|
113 |
return this.getSource().getWhole();
|
|
|
114 |
}
|
|
|
115 |
return new Processor<E>(this, data).process().getWhole();
|
|
|
116 |
}
|
180 |
ilm |
117 |
|
|
|
118 |
public void destroy() {
|
|
|
119 |
for (final Statement st : this.statements.values())
|
|
|
120 |
st.destroy();
|
|
|
121 |
}
|
17 |
ilm |
122 |
}
|