OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 17 | Rev 65 | Go to most recent revision | 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
 *
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
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.openoffice.text;
15
 
25 ilm 16
import org.openconcerto.openoffice.ODDocument;
17 ilm 17
import org.openconcerto.openoffice.XMLVersion;
18
 
19
import java.util.HashSet;
20
import java.util.Set;
21
 
22
import org.jdom.Attribute;
23
import org.jdom.Element;
24
import org.jdom.JDOMException;
25
import org.jdom.xpath.XPath;
26
 
27
/**
28
 * A text paragraph, the basic unit of text. See §4.1 of the OpenDocument specification.
29
 */
30
public class Paragraph extends TextNode<ParagraphStyle> {
31
 
32
    static Element createEmpty(XMLVersion ns) {
33
        return new Element("p", ns.getTEXT());
34
    }
35
 
36
    static private XPath textStylesPath = null;
37
 
38
    static private Set<String> getTextStyles(final Element elem) {
39
        final Set<String> res = new HashSet<String>();
40
        synchronized (Paragraph.class) {
41
            if (textStylesPath == null)
42
                try {
43
                    textStylesPath = XPath.newInstance(".//text:span/@text:style-name");
44
                } catch (JDOMException e) {
45
                    // shouldn't happen since it's a constant
46
                    throw new IllegalStateException(e);
47
                }
48
            try {
49
                for (final Object o : textStylesPath.selectNodes(elem))
50
                    res.add(((Attribute) o).getValue());
51
            } catch (JDOMException e) {
52
                throw new IllegalArgumentException("could'nt evaluate with " + elem, e);
53
            }
54
        }
55
        return res;
56
    }
57
 
25 ilm 58
    Paragraph(Element elem, TextDocument parent) {
59
        super(elem, ParagraphStyle.class, parent);
60
    }
61
 
17 ilm 62
    public Paragraph(Element elem) {
25 ilm 63
        this(elem, null);
17 ilm 64
    }
65
 
66
    public Paragraph(XMLVersion ns) {
67
        this(createEmpty(ns));
68
    }
69
 
70
    public Paragraph() {
71
        this(XMLVersion.getDefault());
72
    }
73
 
74
    public Paragraph(String text) {
75
        this();
76
        addContent(text);
77
    }
78
 
25 ilm 79
    // MAYBE add updateStyle() which evaluates the conditions in style:map of the conditional style
80
    // to update style-name
81
    /**
82
     * A style containing conditions and maps to other styles.
83
     *
84
     * @return the conditional style or <code>null</code> if none or if this isn't in a document.
85
     */
86
    public final ParagraphStyle getConditionalStyle() {
87
        final String condName = this.getElement().getAttributeValue("cond-style-name", this.getElement().getNamespace());
88
        if (condName == null)
89
            return null;
90
        else
91
            return getStyle(condName);
92
    }
93
 
17 ilm 94
    public final void setStyle(String styleName) {
95
        getElement().setAttribute("style-name", styleName, getElement().getNamespace());
96
    }
97
 
98
    public final void addContent(String text) {
99
        this.getElement().addContent(text);
100
    }
101
 
102
    public final void addTab() {
103
        this.getElement().addContent(new Element("tab", getElement().getNamespace()));
104
    }
105
 
106
    public final void addStyledContent(String text, String styleName) {
107
        if (styleName.equals(getStyleName())) {
108
            this.addContent(text);
109
        } else {
110
            final Element span = new Element("span", getElement().getNamespace());
111
            span.setAttribute("style-name", styleName, getElement().getNamespace());
112
            span.addContent(text);
113
            getElement().addContent(span);
114
        }
115
    }
116
 
117
    private final Set<String> getUsedTextStyles() {
118
        return getTextStyles(getElement());
119
    }
120
 
25 ilm 121
    @Override
122
    protected void checkDocument(ODDocument doc) {
123
        if (this.getStyleName() != null && getStyle(doc.getPackage(), doc.getContentDocument()) == null)
17 ilm 124
            throw new IllegalArgumentException("unknown style " + getStyleName() + " in " + doc);
125
        for (final String styleName : this.getUsedTextStyles()) {
25 ilm 126
            if (doc.getPackage().getStyle(TextStyle.DESC, styleName) == null) {
17 ilm 127
                throw new IllegalArgumentException(this + " is using a text:span with an undefined style : " + styleName);
128
            }
129
        }
130
    }
131
 
132
    @Override
133
    public String toString() {
134
        return this.getClass().getSimpleName() + " " + this.getElement().getText();
135
    }
136
}