Skip to content

Commit 004e30e

Browse files
authored
Merge pull request #4360 from evolvedbinary/feature/fn-default-language#0
Implement fn:default-language#0
2 parents 62b3866 + 53bf909 commit 004e30e

File tree

7 files changed

+128
-13
lines changed

7 files changed

+128
-13
lines changed

exist-core/src/main/java/org/exist/interpreter/Context.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,13 @@ public interface Context {
131131

132132
Source getSource();
133133

134+
/**
135+
* Get the default language.
136+
*
137+
* @return the default language
138+
*/
139+
String getDefaultLanguage();
140+
134141
/**
135142
* Declare a user-defined static prefix/namespace mapping.
136143
*

exist-core/src/main/java/org/exist/xquery/XQueryContext.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,11 @@ public class XQueryContext implements BinaryValueManager, Context {
291291
*/
292292
private String defaultCollation = Collations.UNICODE_CODEPOINT_COLLATION_URI;
293293

294+
/**
295+
* The default language
296+
*/
297+
private static final String DefaultLanguage = Locale.getDefault().getLanguage();
298+
294299
/**
295300
* Default Collator. Will be null for the default unicode codepoint collation.
296301
*/
@@ -3317,6 +3322,11 @@ public void setSource(final Source source) {
33173322
this.source = source;
33183323
}
33193324

3325+
@Override
3326+
public String getDefaultLanguage() {
3327+
return DefaultLanguage;
3328+
}
3329+
33203330
/**
33213331
* NOTE: the {@link #unsubscribe()} method can be called
33223332
* from {@link org.exist.storage.NotificationService#unsubscribe(UpdateListener)}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* eXist-db Open Source Native XML Database
3+
* Copyright (C) 2001 The eXist-db Authors
4+
*
5+
6+
* http://www.exist-db.org
7+
*
8+
* This library is free software; you can redistribute it and/or
9+
* modify it under the terms of the GNU Lesser General Public
10+
* License as published by the Free Software Foundation; either
11+
* version 2.1 of the License, or (at your option) any later version.
12+
*
13+
* This library is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16+
* Lesser General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU Lesser General Public
19+
* License along with this library; if not, write to the Free Software
20+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21+
*/
22+
package org.exist.xquery.functions.fn;
23+
24+
import org.apache.logging.log4j.LogManager;
25+
import org.apache.logging.log4j.Logger;
26+
import org.exist.dom.QName;
27+
import org.exist.xquery.*;
28+
import org.exist.xquery.value.Sequence;
29+
import org.exist.xquery.value.StringValue;
30+
import org.exist.xquery.value.Type;
31+
32+
public class FnDefaultLanguage extends BasicFunction {
33+
34+
public static final FunctionSignature FS_DEFAULT_LANGUAGE = FunctionDSL.functionSignature(
35+
new QName("default-language", Function.BUILTIN_FUNCTION_NS),
36+
"Returns the xs:language that is " +
37+
"the value of the default language property from the dynamic context " +
38+
"during the evaluation of a query or transformation in which " +
39+
"fn:default-language() is executed.",
40+
FunctionDSL.returns(Type.LANGUAGE, Cardinality.EXACTLY_ONE, "the default language within query execution time span"));
41+
42+
public FnDefaultLanguage(final XQueryContext context) {
43+
super(context, FS_DEFAULT_LANGUAGE);
44+
}
45+
46+
@Override
47+
public Sequence eval(final Sequence[] args, final Sequence contextSequence) throws XPathException {
48+
49+
return new StringValue(context.getDefaultLanguage());
50+
}
51+
52+
}

exist-core/src/main/java/org/exist/xquery/functions/fn/FnFormatDates.java

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@
3535

3636
public class FnFormatDates extends BasicFunction {
3737

38-
private final static String DEFAULT_LANGUAGE = Locale.getDefault().getLanguage();
39-
40-
private static FunctionParameterSequenceType DATETIME =
38+
private static FunctionParameterSequenceType DATETIME =
4139
new FunctionParameterSequenceType(
4240
"value", Type.DATE_TIME, Cardinality.ZERO_OR_ONE, "The datetime");
4341

@@ -152,13 +150,13 @@ public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathExce
152150

153151
final AbstractDateTimeValue value = (AbstractDateTimeValue) args[0].itemAt(0);
154152
final String picture = args[1].getStringValue();
155-
final Optional<String> language;
153+
final String language;
156154
final Optional<String> place;
157155
if (getArgumentCount() == 5) {
158156
if (args[2].hasOne()) {
159-
language = Optional.of(args[2].getStringValue());
157+
language = args[2].getStringValue();
160158
} else {
161-
language = Optional.empty();
159+
language = context.getDefaultLanguage();
162160
}
163161

164162
if(args[4].hasOne()) {
@@ -167,14 +165,14 @@ public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathExce
167165
place = Optional.empty();
168166
}
169167
} else {
170-
language = Optional.empty();
168+
language = context.getDefaultLanguage();
171169
place = Optional.empty();
172170
}
173171

174172
return new StringValue(formatDate(picture, value, language, place));
175173
}
176174

177-
private String formatDate(String pic, AbstractDateTimeValue dt, final Optional<String> language,
175+
private String formatDate(String pic, AbstractDateTimeValue dt, final String language,
178176
final Optional<String> place) throws XPathException {
179177

180178
final boolean tzHMZNPictureHint = pic.equals("[H00]:[M00] [ZN]");
@@ -213,7 +211,7 @@ private String formatDate(String pic, AbstractDateTimeValue dt, final Optional<S
213211
return sb.toString();
214212
}
215213

216-
private void formatComponent(String component, AbstractDateTimeValue dt, final Optional<String> language,
214+
private void formatComponent(String component, AbstractDateTimeValue dt, final String language,
217215
final Optional<String> place, final boolean tzHMZNPictureHint, final StringBuilder sb)
218216
throws XPathException {
219217
final Matcher matcher = componentPattern.matcher(component);
@@ -395,8 +393,8 @@ private void formatComponent(String component, AbstractDateTimeValue dt, final O
395393
}
396394

397395
private String formatTimeZone(final String timezonePicture, final int hour, final int minute,
398-
final TimeZone timeZone, final Optional<String> language, final Optional<String> place) {
399-
final Locale locale = new Locale(language.orElse(DEFAULT_LANGUAGE));
396+
final TimeZone timeZone, final String language, final Optional<String> place) {
397+
final Locale locale = new Locale(language);
400398

401399
final String format;
402400
switch(timezonePicture) {
@@ -487,9 +485,9 @@ private String getDefaultFormat(char specifier) {
487485
}
488486
}
489487

490-
private void formatNumber(char specifier, String picture, String width, int num, final Optional<String> language,
488+
private void formatNumber(char specifier, String picture, String width, int num, final String language,
491489
StringBuilder sb) throws XPathException {
492-
final NumberFormatter formatter = NumberFormatter.getInstance(language.orElse(DEFAULT_LANGUAGE));
490+
final NumberFormatter formatter = NumberFormatter.getInstance(language);
493491
if ("N".equals(picture) || "n".equals(picture) || "Nn".equals(picture)) {
494492
String name;
495493
switch (specifier) {

exist-core/src/main/java/org/exist/xquery/functions/fn/FnModule.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public class FnModule extends AbstractInternalModule {
6767
new FunctionDef(FunDeepEqual.signatures[0], FunDeepEqual.class),
6868
new FunctionDef(FunDeepEqual.signatures[1], FunDeepEqual.class),
6969
new FunctionDef(FunDefaultCollation.signature, FunDefaultCollation.class),
70+
new FunctionDef(FnDefaultLanguage.FS_DEFAULT_LANGUAGE, FnDefaultLanguage.class),
7071
new FunctionDef(FunDistinctValues.signatures[0], FunDistinctValues.class),
7172
new FunctionDef(FunDistinctValues.signatures[1], FunDistinctValues.class),
7273
new FunctionDef(FunDoc.signature, FunDoc.class),

exist-core/src/test/java/org/exist/xquery/XQueryFunctionsTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,4 +1024,13 @@ public void base64BinaryCast() throws XMLDBException, URISyntaxException {
10241024
ResourceSet resultRetreive = existEmbeddedServer.executeQuery(queryRetreive);
10251025
assertEquals("retreive, Expect single result", 1, resultRetreive.getSize());
10261026
}
1027+
1028+
@Test
1029+
public void defaultLanguage() throws XMLDBException {
1030+
1031+
final ResourceSet result = existEmbeddedServer.executeQuery("default-language()");
1032+
assertEquals(1, result.getSize());
1033+
final String defaultLanguage = (String) result.getResource(0).getContent();
1034+
assertEquals(Locale.getDefault().getLanguage(), defaultLanguage);
1035+
}
10271036
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
(:
2+
: eXist-db Open Source Native XML Database
3+
: Copyright (C) 2001 The eXist-db Authors
4+
:
5+
6+
: http://www.exist-db.org
7+
:
8+
: This library is free software; you can redistribute it and/or
9+
: modify it under the terms of the GNU Lesser General Public
10+
: License as published by the Free Software Foundation; either
11+
: version 2.1 of the License, or (at your option) any later version.
12+
:
13+
: This library is distributed in the hope that it will be useful,
14+
: but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16+
: Lesser General Public License for more details.
17+
:
18+
: You should have received a copy of the GNU Lesser General Public
19+
: License along with this library; if not, write to the Free Software
20+
: Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21+
:)
22+
xquery version "3.1";
23+
24+
module namespace test-sort="http://exist-db.org/xquery/test/function-language";
25+
26+
declare namespace test="http://exist-db.org/xquery/xqsuite";
27+
28+
(:
29+
: Test that the answer we get is a member of the list of known language codes
30+
: The list is acquired from from java.util.Locale.getISOLanguages()
31+
:)
32+
declare
33+
%test:assertExists
34+
function test-sort:default-language() {
35+
let $language := fn:default-language()
36+
let $all := ("aa","ab","ae","af","ak","am","an","ar","as","av","ay","az","ba","be","bg","bh","bi","bm","bn","bo","br","bs","ca","ce","ch","co","cr","cs","cu","cv","cy","da","de","dv","dz","ee","el","en","eo","es","et","eu","fa","ff","fi","fj","fo","fr","fy","ga","gd","gl","gn","gu","gv","ha","he","hi","ho","hr","ht","hu","hy","hz","ia","id","ie","ig","ii","ik","in","io","is","it","iu","iw","ja","ji","jv","ka","kg","ki","kj","kk","kl","km","kn","ko","kr","ks","ku","kv","kw","ky","la","lb","lg","li","ln","lo","lt","lu","lv","mg","mh","mi","mk","ml","mn","mo","mr","ms","mt","my","na","nb","nd","ne","ng","nl","nn","no","nr","nv","ny","oc","oj","om","or","os","pa","pi","pl","ps","pt","qu","rm","rn","ro","ru","rw","sa","sc","sd","se","sg","si","sk","sl","sm","sn","so","sq","sr","ss","st","su","sv","sw","ta","te","tg","th","ti","tk","tl","tn","to","tr","ts","tt","tw","ty","ug","uk","ur","uz","ve","vi","vo","wa","wo","xh","yi","yo","za","zh","zu")
37+
return index-of($all, $language)
38+
};

0 commit comments

Comments
 (0)