OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 17 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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