OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 73 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 73 Rev 83
Line 12... Line 12...
12
 */
12
 */
13
 
13
 
14
 package org.openconcerto.openoffice.text;
14
 package org.openconcerto.openoffice.text;
15
 
15
 
16
import org.openconcerto.openoffice.ODDocument;
16
import org.openconcerto.openoffice.ODDocument;
17
import org.openconcerto.openoffice.Style;
17
import org.openconcerto.openoffice.ODPackage;
18
import org.openconcerto.openoffice.XMLVersion;
18
import org.openconcerto.openoffice.XMLFormatVersion;
19
 
19
 
20
import java.util.HashSet;
-
 
21
import java.util.Set;
-
 
22
 
-
 
23
import org.jdom.Attribute;
-
 
24
import org.jdom.Element;
20
import org.jdom.Element;
25
import org.jdom.JDOMException;
-
 
26
import org.jdom.xpath.XPath;
-
 
27
 
21
 
28
/**
22
/**
29
 * A text paragraph, the basic unit of text. See §4.1 of the OpenDocument specification.
23
 * A text paragraph, the basic unit of text. See §4.1 of the OpenDocument specification.
30
 */
24
 */
31
public class Paragraph extends TextNode<ParagraphStyle> {
25
public class Paragraph extends TextNode<ParagraphStyle> {
32
 
26
 
33
    static Element createEmpty(XMLVersion ns) {
27
    public static final TextNodeDesc<?> NODE_DESC = new TextNodeDesc<Paragraph>(Paragraph.class) {
34
        return new Element("p", ns.getTEXT());
-
 
35
    }
-
 
36
 
28
 
-
 
29
        @Override
37
    static private XPath textStylesPath = null;
30
        public Element createProto(XMLFormatVersion vers) {
-
 
31
            return new Element("p", vers.getXMLVersion().getTEXT());
-
 
32
        }
38
 
33
 
39
    static private Set<String> getTextStyles(final Element elem) {
-
 
40
        final Set<String> res = new HashSet<String>();
-
 
41
        synchronized (Paragraph.class) {
-
 
42
            if (textStylesPath == null)
-
 
43
                try {
34
        @Override
44
                    textStylesPath = XPath.newInstance(".//text:span/@text:style-name");
-
 
45
                } catch (JDOMException e) {
35
        public Paragraph wrapNode(ODDocument doc, Element e) {
46
                    // shouldn't happen since it's a constant
-
 
47
                    throw new IllegalStateException(e);
36
            return new Paragraph(e, doc);
48
                }
37
        }
-
 
38
 
49
            try {
39
        @Override
50
                for (final Object o : textStylesPath.selectNodes(elem))
40
        protected Paragraph wrapNode(XMLFormatVersion vers, Element elem) {
51
                    res.add(((Attribute) o).getValue());
-
 
52
            } catch (JDOMException e) {
41
            return new Paragraph(elem, vers);
53
                throw new IllegalArgumentException("could'nt evaluate with " + elem, e);
-
 
54
            }
42
        }
-
 
43
    };
-
 
44
 
-
 
45
    static public Element createEmpty(XMLFormatVersion ns) {
-
 
46
        return NODE_DESC.createEmptyElement(ns);
55
        }
47
    }
-
 
48
 
-
 
49
    // not public since, local element cannot be checked against vers
-
 
50
    protected Paragraph(Element local, final XMLFormatVersion vers) {
56
        return res;
51
        super(local, ParagraphStyle.class, vers);
57
    }
52
    }
58
 
53
 
59
    Paragraph(Element elem, TextDocument parent) {
54
    public Paragraph(Element elem, ODDocument parent) {
60
        super(elem, ParagraphStyle.class, parent);
55
        super(elem, ParagraphStyle.class, parent);
61
    }
56
    }
62
 
57
 
63
    public Paragraph(Element elem) {
58
    public Paragraph(Element elem, ODPackage pkg) {
64
        this(elem, null);
59
        this(elem, pkg.getFormatVersion());
-
 
60
        if (pkg.getXMLFile(elem.getDocument()) == null)
-
 
61
            throw new IllegalArgumentException("Element not in package");
65
    }
62
    }
66
 
63
 
67
    public Paragraph(XMLVersion ns) {
64
    public Paragraph(XMLFormatVersion ns) {
68
        this(createEmpty(ns));
65
        this(createEmpty(ns), ns);
69
    }
66
    }
70
 
67
 
71
    public Paragraph() {
68
    public Paragraph() {
72
        this(XMLVersion.getDefault());
69
        this(XMLFormatVersion.getDefault());
73
    }
70
    }
74
 
71
 
75
    public Paragraph(String text) {
72
    public Paragraph(String text) {
76
        this();
73
        this();
77
        addContent(text);
74
        addContent(text);
Line 90... Line 87...
90
            return null;
87
            return null;
91
        else
88
        else
92
            return getStyle(condName);
89
            return getStyle(condName);
93
    }
90
    }
94
 
91
 
95
    public final void addContent(String text) {
-
 
96
        this.getElement().addContent(text);
-
 
97
    }
-
 
98
 
-
 
99
    public final void addTab() {
-
 
100
        this.getElement().addContent(new Element("tab", getElement().getNamespace()));
-
 
101
    }
-
 
102
 
-
 
103
    public final void addStyledContent(String text, String styleName) {
-
 
104
        if (styleName.equals(getStyleName())) {
-
 
105
            this.addContent(text);
-
 
106
        } else {
-
 
107
            final Element span = new Element("span", getElement().getNamespace());
-
 
108
            span.setAttribute("style-name", styleName, getElement().getNamespace());
-
 
109
            span.addContent(text);
-
 
110
            getElement().addContent(span);
-
 
111
        }
-
 
112
    }
-
 
113
 
-
 
114
    private final Set<String> getUsedTextStyles() {
-
 
115
        return getTextStyles(getElement());
-
 
116
    }
-
 
117
 
-
 
118
    @Override
-
 
119
    protected void checkDocument(ODDocument doc) {
-
 
120
        if (this.getStyleName() != null && getStyle(doc.getPackage(), doc.getContentDocument()) == null)
-
 
121
            throw new IllegalArgumentException("unknown style " + getStyleName() + " in " + doc);
-
 
122
        for (final String styleName : this.getUsedTextStyles()) {
-
 
123
            if (doc.getPackage().getStyle(Style.getStyleDesc(TextStyle.class, doc.getVersion()), styleName) == null) {
-
 
124
                throw new IllegalArgumentException(this + " is using a text:span with an undefined style : " + styleName);
-
 
125
            }
-
 
126
        }
-
 
127
    }
-
 
128
 
-
 
129
    @Override
92
    @Override
130
    public String toString() {
93
    public String toString() {
131
        return this.getClass().getSimpleName() + " " + this.getElement().getText();
94
        return this.getClass().getSimpleName() + " " + this.getElement().getText();
132
    }
95
    }
133
}
96
}