OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 180 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
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.statements;
15
 
16
import org.openconcerto.odtemplate.TemplateException;
17
import org.openconcerto.odtemplate.engine.DataModel;
18
import org.openconcerto.odtemplate.engine.Processor;
19
 
20
import java.util.List;
21
 
22
import org.jdom.Content;
23
import org.jdom.Element;
24
import org.jdom.Namespace;
25
 
26
/**
27
 * A statement that will be executed to modify the parsed xml.
28
 *
29
 * @author Sylvain
30
 */
31
public abstract class Statement {
32
 
33
    private final String name;
34
 
35
    public Statement(final String name) {
36
        super();
37
        this.name = name;
38
    }
39
 
40
    /**
41
     * The name of this statement. {@link #prepare(Element)} must create an Element with that name.
42
     *
43
     * @return the name of this statement.
44
     */
45
    public final String getName() {
46
        return this.name;
47
    }
48
 
49
    /**
50
     * Find representations of this statement.
51
     *
52
     * @param elem the element to test.
53
     * @return <code>true</code> if elem should prepared by this statement.
54
     */
55
    public abstract boolean matches(Element elem);
56
 
57
    /**
58
     * Creates an element named {@link #getName()}.
59
     *
60
     * @param elem an element that returned <code>true</code> with {@link #matches(Element)}.
61
     * @throws TemplateException
62
     */
63
    public abstract void prepare(Element elem) throws TemplateException;
64
 
65
    /**
66
     * Should produce valid OpenDocument XML.
67
     *
68
     * @param processor the processor.
69
     * @param elem an element that has been prepared.
70
     * @param model the data.
71
     * @throws TemplateException
72
     */
73
    public abstract void execute(Processor<?> processor, Element elem, DataModel model) throws TemplateException;
74
 
180 ilm 75
    public void destroy() {
76
    }
77
 
17 ilm 78
    @Override
79
    public String toString() {
80
        return this.getClass().getSimpleName() + " " + this.getName();
81
    }
82
 
83
    // *** static
84
 
85
    public static final Namespace stmtNS = Namespace.getNamespace("jod", "http://www.jopendocument.org");
86
 
87
    protected static Element getElement(String name) {
88
        return new Element(name, stmtNS);
89
    }
90
 
91
    protected static Element getAncestorByName(Element element, String name) {
92
        while (true) {
93
            final Element parent = element.getParentElement();
94
            if (parent == null)
95
                return null;
96
            if (parent.getName().equals(name))
97
                return parent;
98
            element = parent;
99
        }
100
    }
101
 
102
    /**
103
     * Replace elem by its content. Ie elem will be detached from the tree and its content will be
104
     * inserted at this former location.
105
     *
106
     * @param elem the element to remove.
107
     */
108
    @SuppressWarnings("unchecked")
109
    protected static void pullUp(Element elem) {
110
        final List<Content> parentContent = elem.getParentElement().getContent();
111
        final int index = parentContent.indexOf(elem);
112
        elem.detach();
113
        parentContent.addAll(index, elem.removeContent());
114
    }
115
}