OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 73 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
73 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.sql.element;
15
 
16
import org.openconcerto.sql.Log;
17
import org.openconcerto.utils.i18n.Grammar;
18
import org.openconcerto.utils.i18n.NounClass;
19
import org.openconcerto.utils.i18n.Phrase;
20
import org.openconcerto.utils.i18n.VariantKey;
132 ilm 21
import org.openconcerto.xml.JDOM2Utils;
73 ilm 22
 
23
import java.io.IOException;
24
import java.io.InputStream;
25
import java.util.AbstractMap.SimpleEntry;
26
import java.util.Locale;
27
import java.util.Map.Entry;
28
import java.util.regex.Pattern;
29
 
132 ilm 30
import org.jdom2.Document;
31
import org.jdom2.Element;
32
import org.jdom2.JDOMException;
33
import org.jdom2.input.SAXBuilder;
34
 
73 ilm 35
import net.jcip.annotations.ThreadSafe;
36
 
37
/**
38
 * Parses XML to create phrases.
39
 *
40
 * <pre>
41
 *     &lt;element refid="elementCode">
42
 *         &lt;name base="elemName" nounClass="masculine">
43
 *             &lt;variant refids="singular,plural" value="elemNameBothSingularAndPlural" />
44
 *             &lt;variant idPattern="(singular|plural)" value="elemNameBothSingularAndPlural" />
45
 *         &lt;/name>
46
 *     &lt;/element>
47
 * </pre>
48
 *
49
 * @author Sylvain
50
 *
51
 */
52
@ThreadSafe
53
public class SQLElementNamesFromXML extends SQLElementNamesMap.ByCode {
54
 
55
    static public final Pattern SPLIT_PATTERN = Pattern.compile("\\s*,\\s*");
56
 
57
    public SQLElementNamesFromXML(Locale locale) {
58
        super(locale);
59
    }
60
 
61
    public final void load(final InputStream ins) throws JDOMException, IOException {
62
        final Grammar gr = Grammar.getInstance(getLocale());
63
        final Document doc = new SAXBuilder().build(ins);
132 ilm 64
        for (final Element elem : doc.getRootElement().getChildren("element"))
73 ilm 65
            this.load(gr, elem);
66
    }
67
 
68
    public final Entry<String, Phrase> createPhrase(final Element elem) throws IOException {
69
        return this.createPhrase(Grammar.getInstance(getLocale()), elem);
70
    }
71
 
72
    private Entry<String, Phrase> createPhrase(final Grammar gr, final Element elem) throws IOException {
73
        final String refid = elem.getAttributeValue("refid");
74
        if (refid == null)
75
            throw new IOException("No refid attribute");
76
 
77
        final Element nameElem = elem.getChild("name");
78
        final boolean hasChild = nameElem != null;
79
        final String nameAttr = elem.getAttributeValue("name");
80
        if (!hasChild && nameAttr == null) {
81
            Log.get().warning("No name for code : " + refid);
82
            return null;
83
        }
84
        if (hasChild && nameAttr != null) {
85
            Log.get().warning("Ignoring attribute : " + nameAttr);
86
        }
87
 
88
        final String base = hasChild ? nameElem.getAttributeValue("base") : nameAttr;
89
        if (base == null)
90
            throw new IOException("No base for the name of " + refid);
91
        final String nounClassName = hasChild ? nameElem.getAttributeValue("nounClass") : elem.getAttributeValue("nameClass");
92
        final NounClass nounClass = nounClassName == null ? null : gr.getNounClass(nounClassName);
93
 
94
        final Phrase res = new Phrase(gr, base, nounClass);
95
        if (!hasChild) {
96
            // most languages have at most 2 grammatical number
97
            final String plural = elem.getAttributeValue("namePlural");
98
            if (plural != null)
99
                res.putVariant(Grammar.PLURAL, plural);
100
        } else {
132 ilm 101
            for (final Element variantElem : nameElem.getChildren("variant")) {
73 ilm 102
                final String value = variantElem.getAttributeValue("value");
103
                if (value == null) {
104
                    warning(refid, variantElem, "No value");
105
                    continue;
106
                }
107
                final String variantIDs = variantElem.getAttributeValue("refids");
108
                final String variantPattern = variantElem.getAttributeValue("idPattern");
109
                if (variantIDs == null && variantPattern == null) {
110
                    warning(refid, variantElem, "No ID");
111
                } else if (variantIDs != null) {
112
                    if (variantPattern != null) {
113
                        warning(refid, variantElem, "Ignorig pattern " + variantPattern);
114
                    }
115
                    for (final String variantID : SPLIT_PATTERN.split(variantIDs)) {
116
                        final VariantKey variantKey = gr.getVariantKey(variantID);
117
                        if (variantKey == null) {
118
                            warning(refid, variantElem, "Ignorig " + variantID);
119
                        } else {
120
                            res.putVariant(variantKey, value);
121
                        }
122
                    }
123
                } else {
124
                    assert variantIDs == null && variantPattern != null;
125
                    final Pattern p = Pattern.compile(variantPattern);
126
                    for (final VariantKey vk : gr.getVariantKeys()) {
127
                        if (p.matcher(vk.getID()).matches()) {
128
                            res.putVariant(vk, value);
129
                        }
130
                    }
131
                }
132
            }
133
        }
134
        return new SimpleEntry<String, Phrase>(refid, res);
135
    }
136
 
137
    private void load(final Grammar gr, final Element elem) throws IOException {
138
        final Entry<String, Phrase> entry = this.createPhrase(gr, elem);
139
        if (entry != null)
140
            this.put(entry.getKey(), entry.getValue());
141
    }
142
 
143
    private void warning(final String refid, final Element variantElem, final String msg) {
132 ilm 144
        Log.get().warning(msg + " for variant of " + refid + " : " + JDOM2Utils.output(variantElem));
73 ilm 145
    }
146
}