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;
|
156 |
ilm |
26 |
import java.util.Collections;
|
|
|
27 |
import java.util.HashMap;
|
73 |
ilm |
28 |
import java.util.Locale;
|
156 |
ilm |
29 |
import java.util.Map;
|
73 |
ilm |
30 |
import java.util.Map.Entry;
|
156 |
ilm |
31 |
import java.util.Set;
|
|
|
32 |
import java.util.logging.Level;
|
73 |
ilm |
33 |
import java.util.regex.Pattern;
|
|
|
34 |
|
132 |
ilm |
35 |
import org.jdom2.Document;
|
|
|
36 |
import org.jdom2.Element;
|
|
|
37 |
import org.jdom2.JDOMException;
|
|
|
38 |
import org.jdom2.input.SAXBuilder;
|
|
|
39 |
|
73 |
ilm |
40 |
import net.jcip.annotations.ThreadSafe;
|
|
|
41 |
|
|
|
42 |
/**
|
|
|
43 |
* Parses XML to create phrases.
|
|
|
44 |
*
|
|
|
45 |
* <pre>
|
|
|
46 |
* <element refid="elementCode">
|
|
|
47 |
* <name base="elemName" nounClass="masculine">
|
|
|
48 |
* <variant refids="singular,plural" value="elemNameBothSingularAndPlural" />
|
|
|
49 |
* <variant idPattern="(singular|plural)" value="elemNameBothSingularAndPlural" />
|
|
|
50 |
* </name>
|
|
|
51 |
* </element>
|
|
|
52 |
* </pre>
|
|
|
53 |
*
|
|
|
54 |
* @author Sylvain
|
|
|
55 |
*
|
|
|
56 |
*/
|
|
|
57 |
@ThreadSafe
|
156 |
ilm |
58 |
public class SQLElementNamesFromXML {
|
73 |
ilm |
59 |
|
|
|
60 |
static public final Pattern SPLIT_PATTERN = Pattern.compile("\\s*,\\s*");
|
156 |
ilm |
61 |
static private final Set<Object> SHORT_VARIANTS = Collections.singleton(Grammar.PLURAL);
|
73 |
ilm |
62 |
|
156 |
ilm |
63 |
private static final String ELEMENT_ELEM_NAME = "element";
|
|
|
64 |
private static final String NAME_ID_ATTR = "refid";
|
|
|
65 |
private static final String VARIANT_ATTR = "variant";
|
|
|
66 |
private static final String VARIANT_REFIDS_ATTR = "refids";
|
|
|
67 |
private static final String VARIANT_VALUE_ATTR = "value";
|
|
|
68 |
private static final String NAME_PLURAL_ATTR = "namePlural";
|
|
|
69 |
|
|
|
70 |
static private final String getNounClassAttrName(final boolean shortForm) {
|
|
|
71 |
return shortForm ? "nameClass" : "nounClass";
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
// <element nameClass="masculine" name="contact fournisseur">
|
|
|
75 |
// <name nounClass="masculine" base="droit utilisateur">
|
|
|
76 |
static final private void setNounClassAndBase(final Element elem, final Phrase phrase, final boolean shortForm) {
|
|
|
77 |
elem.setAttribute(getNounClassAttrName(shortForm), phrase.getNounClass().getName());
|
|
|
78 |
elem.setAttribute(shortForm ? "name" : "base", phrase.getBase());
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
private final Locale locale;
|
|
|
82 |
|
73 |
ilm |
83 |
public SQLElementNamesFromXML(Locale locale) {
|
156 |
ilm |
84 |
this.locale = locale;
|
73 |
ilm |
85 |
}
|
|
|
86 |
|
156 |
ilm |
87 |
public final Locale getLocale() {
|
|
|
88 |
return this.locale;
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
public final Map<String, Phrase> load(final InputStream ins) throws JDOMException, IOException {
|
73 |
ilm |
92 |
final Document doc = new SAXBuilder().build(ins);
|
156 |
ilm |
93 |
return load(doc.getRootElement());
|
73 |
ilm |
94 |
}
|
|
|
95 |
|
156 |
ilm |
96 |
public final Map<String, Phrase> load(final Element root) throws IOException {
|
|
|
97 |
final Grammar gr = Grammar.getInstance(getLocale());
|
|
|
98 |
final Map<String, Phrase> res = new HashMap<>();
|
|
|
99 |
for (final Element elem : root.getChildren(ELEMENT_ELEM_NAME)) {
|
|
|
100 |
final Entry<String, Phrase> e = this.createPhrase(gr, elem);
|
|
|
101 |
if (e != null)
|
|
|
102 |
res.put(e.getKey(), e.getValue());
|
|
|
103 |
}
|
|
|
104 |
return res;
|
|
|
105 |
}
|
|
|
106 |
|
73 |
ilm |
107 |
public final Entry<String, Phrase> createPhrase(final Element elem) throws IOException {
|
|
|
108 |
return this.createPhrase(Grammar.getInstance(getLocale()), elem);
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
private Entry<String, Phrase> createPhrase(final Grammar gr, final Element elem) throws IOException {
|
156 |
ilm |
112 |
final String refid = elem.getAttributeValue(NAME_ID_ATTR);
|
73 |
ilm |
113 |
if (refid == null)
|
|
|
114 |
throw new IOException("No refid attribute");
|
|
|
115 |
|
|
|
116 |
final Element nameElem = elem.getChild("name");
|
|
|
117 |
final boolean hasChild = nameElem != null;
|
|
|
118 |
final String nameAttr = elem.getAttributeValue("name");
|
|
|
119 |
if (!hasChild && nameAttr == null) {
|
156 |
ilm |
120 |
// perhaps elem is just used to change item names
|
|
|
121 |
Log.get().log(Level.FINER, "No name for code : {0}", refid);
|
73 |
ilm |
122 |
return null;
|
|
|
123 |
}
|
|
|
124 |
if (hasChild && nameAttr != null) {
|
|
|
125 |
Log.get().warning("Ignoring attribute : " + nameAttr);
|
|
|
126 |
}
|
|
|
127 |
|
|
|
128 |
final String base = hasChild ? nameElem.getAttributeValue("base") : nameAttr;
|
|
|
129 |
if (base == null)
|
|
|
130 |
throw new IOException("No base for the name of " + refid);
|
156 |
ilm |
131 |
final String nounClassName = (hasChild ? nameElem : elem).getAttributeValue(getNounClassAttrName(!hasChild));
|
73 |
ilm |
132 |
final NounClass nounClass = nounClassName == null ? null : gr.getNounClass(nounClassName);
|
|
|
133 |
|
|
|
134 |
final Phrase res = new Phrase(gr, base, nounClass);
|
|
|
135 |
if (!hasChild) {
|
|
|
136 |
// most languages have at most 2 grammatical number
|
156 |
ilm |
137 |
final String plural = elem.getAttributeValue(NAME_PLURAL_ATTR);
|
73 |
ilm |
138 |
if (plural != null)
|
|
|
139 |
res.putVariant(Grammar.PLURAL, plural);
|
|
|
140 |
} else {
|
156 |
ilm |
141 |
for (final Element variantElem : nameElem.getChildren(VARIANT_ATTR)) {
|
|
|
142 |
final String value = variantElem.getAttributeValue(VARIANT_VALUE_ATTR);
|
73 |
ilm |
143 |
if (value == null) {
|
|
|
144 |
warning(refid, variantElem, "No value");
|
|
|
145 |
continue;
|
|
|
146 |
}
|
156 |
ilm |
147 |
final String variantIDs = variantElem.getAttributeValue(VARIANT_REFIDS_ATTR);
|
73 |
ilm |
148 |
final String variantPattern = variantElem.getAttributeValue("idPattern");
|
|
|
149 |
if (variantIDs == null && variantPattern == null) {
|
|
|
150 |
warning(refid, variantElem, "No ID");
|
|
|
151 |
} else if (variantIDs != null) {
|
|
|
152 |
if (variantPattern != null) {
|
|
|
153 |
warning(refid, variantElem, "Ignorig pattern " + variantPattern);
|
|
|
154 |
}
|
|
|
155 |
for (final String variantID : SPLIT_PATTERN.split(variantIDs)) {
|
|
|
156 |
final VariantKey variantKey = gr.getVariantKey(variantID);
|
|
|
157 |
if (variantKey == null) {
|
|
|
158 |
warning(refid, variantElem, "Ignorig " + variantID);
|
|
|
159 |
} else {
|
|
|
160 |
res.putVariant(variantKey, value);
|
|
|
161 |
}
|
|
|
162 |
}
|
|
|
163 |
} else {
|
|
|
164 |
assert variantIDs == null && variantPattern != null;
|
|
|
165 |
final Pattern p = Pattern.compile(variantPattern);
|
|
|
166 |
for (final VariantKey vk : gr.getVariantKeys()) {
|
|
|
167 |
if (p.matcher(vk.getID()).matches()) {
|
|
|
168 |
res.putVariant(vk, value);
|
|
|
169 |
}
|
|
|
170 |
}
|
|
|
171 |
}
|
|
|
172 |
}
|
|
|
173 |
}
|
|
|
174 |
return new SimpleEntry<String, Phrase>(refid, res);
|
|
|
175 |
}
|
|
|
176 |
|
|
|
177 |
private void warning(final String refid, final Element variantElem, final String msg) {
|
132 |
ilm |
178 |
Log.get().warning(msg + " for variant of " + refid + " : " + JDOM2Utils.output(variantElem));
|
73 |
ilm |
179 |
}
|
156 |
ilm |
180 |
|
|
|
181 |
public final Element createElement(final String refID, final Phrase phrase) {
|
|
|
182 |
final Element elem = new Element(ELEMENT_ELEM_NAME);
|
|
|
183 |
elem.setAttribute(NAME_ID_ATTR, refID);
|
|
|
184 |
final Set<VariantKey> explicitVariants = phrase.getExplicitVariants();
|
|
|
185 |
final boolean shortForm = SHORT_VARIANTS.containsAll(explicitVariants);
|
|
|
186 |
final Element nameElem;
|
|
|
187 |
if (shortForm) {
|
|
|
188 |
nameElem = elem;
|
|
|
189 |
} else {
|
|
|
190 |
nameElem = new Element("name");
|
|
|
191 |
elem.addContent(nameElem);
|
|
|
192 |
}
|
|
|
193 |
setNounClassAndBase(nameElem, phrase, shortForm);
|
|
|
194 |
if (!shortForm) {
|
|
|
195 |
// <variant refids="plural" value="droits utilisateurs"
|
|
|
196 |
for (final VariantKey explicitVariant : explicitVariants) {
|
|
|
197 |
final Element variantElem = new Element(VARIANT_ATTR);
|
|
|
198 |
variantElem.setAttribute(VARIANT_REFIDS_ATTR, explicitVariant.getID());
|
|
|
199 |
variantElem.setAttribute(VARIANT_VALUE_ATTR, phrase.getVariant(explicitVariant));
|
|
|
200 |
nameElem.addContent(variantElem);
|
|
|
201 |
}
|
|
|
202 |
} else if (explicitVariants.contains(Grammar.PLURAL)) {
|
|
|
203 |
elem.setAttribute(NAME_PLURAL_ATTR, phrase.getVariant(Grammar.PLURAL));
|
|
|
204 |
}
|
|
|
205 |
return elem;
|
|
|
206 |
}
|
|
|
207 |
|
73 |
ilm |
208 |
}
|