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