Skip to content

Commit e3c5c8a

Browse files
alanpaxtonadamretter
authored andcommitted
[refactor] Address issues identified by CI, Codacy, Sonarcloud, and code reviews
1 parent a844e70 commit e3c5c8a

File tree

11 files changed

+472
-419
lines changed

11 files changed

+472
-419
lines changed

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3327,14 +3327,6 @@ public String getDefaultLanguage() {
33273327
return DefaultLanguage;
33283328
}
33293329

3330-
/**
3331-
* TODO (AP) this is a temporary implementation which will conflict and be sorted at merge
3332-
* @return the default language for the locale
3333-
*/
3334-
public String getDefaultLanguage() {
3335-
return Locale.getDefault().getLanguage();
3336-
}
3337-
33383330
/**
33393331
* NOTE: the {@link #unsubscribe()} method can be called
33403332
* from {@link org.exist.storage.NotificationService#unsubscribe(UpdateListener)}

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,18 @@
2222

2323
package org.exist.xquery.functions.fn;
2424

25-
import org.exist.xquery.*;
25+
import org.exist.xquery.BasicFunction;
26+
import org.exist.xquery.FunctionSignature;
27+
import org.exist.xquery.XPathException;
28+
import org.exist.xquery.XQueryContext;
2629
import org.exist.xquery.functions.integer.IntegerPicture;
2730
import org.exist.xquery.value.*;
2831

2932
import java.math.BigInteger;
3033
import java.util.ArrayList;
31-
import java.util.IllformedLocaleException;
3234
import java.util.List;
33-
import java.util.Locale;
3435

3536
import static org.exist.xquery.FunctionDSL.*;
36-
import static org.exist.xquery.FunctionDSL.arity;
3737
import static org.exist.xquery.functions.fn.FnModule.functionSignatures;
3838

3939
/**
@@ -77,7 +77,7 @@ public Sequence eval(final Sequence[] args, final Sequence contextSequence)
7777
// If $value is an empty sequence, the function returns a zero-length string
7878
// https://www.w3.org/TR/xpath-functions-31/#func-format-integer
7979
if (args[0].isEmpty()) {
80-
return AtomicValue.EMPTY_SEQUENCE;
80+
return Sequence.EMPTY_SEQUENCE;
8181
}
8282

8383
// If the value of $value is negative, the rules below are applied to the absolute value of $value,
@@ -86,6 +86,9 @@ public Sequence eval(final Sequence[] args, final Sequence contextSequence)
8686
final BigInteger bigInteger = integerValue.toJavaObject(BigInteger.class);
8787

8888
final IntegerPicture picture = IntegerPicture.fromString(args[1].getStringValue());
89+
90+
// Build a list of languages to try
91+
// the called picture will use the first one with a valid locale
8992
final List<String> languages = new ArrayList<>(2);
9093
if (args.length == 3 && !args[2].isEmpty()) {
9194
languages.add(args[2].getStringValue());

exist-core/src/main/java/org/exist/xquery/functions/integer/DigitsIntegerPicture.java

Lines changed: 147 additions & 74 deletions
Large diffs are not rendered by default.

exist-core/src/main/java/org/exist/xquery/functions/integer/FormatModifier.java

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
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+
123
package org.exist.xquery.functions.integer;
224

325
import org.exist.xquery.ErrorCodes;
@@ -6,23 +28,23 @@
628
import java.util.regex.Matcher;
729
import java.util.regex.Pattern;
830

31+
/**
32+
* Represent format modifier part of the formatting picture for integer formatting
33+
* {@see https://www.w3.org/TR/xpath-functions-31/#formatting-integers}
34+
*/
935
class FormatModifier {
1036

11-
enum Numbering {Cardinal, Ordinal}
12-
13-
;
14-
15-
enum LetterSequence {Alphabetic, Traditional}
37+
enum Numbering {CARDINAL, ORDINAL}
1638

17-
;
39+
enum LetterSequence {ALPHABETIC, TRADITIONAL}
1840

1941
final String modifier;
2042
final boolean isEmpty;
21-
Numbering numbering = Numbering.Cardinal;
43+
Numbering numbering = Numbering.CARDINAL;
2244
String variation;
23-
LetterSequence letterSequence = LetterSequence.Alphabetic;
45+
LetterSequence letterSequence = LetterSequence.ALPHABETIC;
2446

25-
final static Pattern modifierPattern = Pattern.compile("^(?:([co])(\\((.+)\\))?)?([at])?$");
47+
static final Pattern modifierPattern = Pattern.compile("^(?:([co])(\\((.+)\\))?)?([at])?$");
2648

2749
FormatModifier(final String modifier) throws XPathException {
2850
this.modifier = modifier;
@@ -33,28 +55,28 @@ enum LetterSequence {Alphabetic, Traditional}
3355
}
3456

3557
private void parseModifier() throws XPathException {
36-
Matcher m = modifierPattern.matcher(modifier);
58+
final Matcher m = modifierPattern.matcher(modifier);
3759
if (!m.matches()) {
3860
throw new XPathException(ErrorCodes.FODF1310, "Modifier " + modifier + " is not a valid pattern modifier");
3961
}
40-
String n = m.group(1);
62+
final String n = m.group(1);
4163
if (n != null) {
42-
if (n.equals("c")) numbering = Numbering.Cardinal;
43-
if (n.equals("o")) numbering = Numbering.Ordinal;
64+
if (n.equals("c")) numbering = Numbering.CARDINAL;
65+
if (n.equals("o")) numbering = Numbering.ORDINAL;
4466
}
45-
String v = m.group(3);
67+
final String v = m.group(3);
4668
if (v != null) {
4769
variation = v;
4870
}
49-
String l = m.group(4);
71+
final String l = m.group(4);
5072
if (l != null) {
51-
if (l.equals("a")) letterSequence = LetterSequence.Alphabetic;
52-
if (l.equals("t")) letterSequence = LetterSequence.Traditional;
73+
if (l.equals("a")) letterSequence = LetterSequence.ALPHABETIC;
74+
if (l.equals("t")) letterSequence = LetterSequence.TRADITIONAL;
5375
}
5476
}
5577

5678
public String toString() {
57-
StringBuilder sb = new StringBuilder();
79+
final StringBuilder sb = new StringBuilder();
5880
sb.append("numbering=").append(numbering).append("::");
5981
sb.append("variation=").append(variation).append("::");
6082
sb.append("lettersequence=").append(letterSequence).append("::");

exist-core/src/main/java/org/exist/xquery/functions/integer/IntegerPicture.java

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -32,28 +32,18 @@
3232
import java.util.regex.Pattern;
3333

3434
/**
35-
* Format numbers according to the rules
36-
* {@see https://www.w3.org/TR/xpath-functions-31/#formatting-integers}
35+
* Format numbers according to the rules for
36+
* <a href="https://www.w3.org/TR/xpath-functions-31/#formatting-integers">format-integer</a>
3737
*/
3838
public abstract class IntegerPicture {
3939

40-
static IntegerPicture DEFAULT;
41-
42-
static {
43-
try {
44-
DEFAULT = new DigitsIntegerPicture("1", new FormatModifier(""));
45-
} catch (final XPathException e) {
46-
e.printStackTrace();
47-
}
48-
}
49-
50-
final static BigInteger TEN = BigInteger.valueOf(10L);
40+
static final BigInteger TEN = BigInteger.valueOf(10L);
5141

5242
//This contains \\v (vertical whitespace characters) so anything with vertical white space isn't a pattern
5343
//It also disallows 0 instances of the pattern
5444
//When decimal digit pattern doesn't match, we end up falling into a standard default.
55-
final static Pattern decimalDigitPattern = Pattern.compile("^((\\p{Nd}|#|[^\\p{N}\\p{L}\\v])+)$", Pattern.UNICODE_CHARACTER_CLASS);
56-
final static Pattern invalidDigitPattern = Pattern.compile("(\\p{Nd})");
45+
static final Pattern decimalDigitPattern = Pattern.compile("^((\\p{Nd}|#|[^\\p{N}\\p{L}\\v])+)$", Pattern.UNICODE_CHARACTER_CLASS);
46+
static final Pattern invalidDigitPattern = Pattern.compile("(\\p{Nd})");
5747

5848
/**
5949
* The value of $picture consists of a primary format token,
@@ -81,19 +71,19 @@ public static IntegerPicture fromString(final String pictureFormat) throws XPath
8171
}
8272

8373
// type 1 matcher (some digits)
84-
final Matcher decimalDigitMatcher = decimalDigitPattern.matcher(primaryFormatToken);
74+
final Matcher decimalDigitMatcher = IntegerPicture.decimalDigitPattern.matcher(primaryFormatToken);
8575
if (decimalDigitMatcher.matches()) {
8676
return new DigitsIntegerPicture(primaryFormatToken, formatModifier);
8777
}
8878

8979
// incorrect type 1 matcher (and not anything else)
90-
final Matcher invalidDigitMatcher = invalidDigitPattern.matcher(primaryFormatToken);
80+
final Matcher invalidDigitMatcher = IntegerPicture.invalidDigitPattern.matcher(primaryFormatToken);
9181
if (invalidDigitMatcher.find()) {
9282
throw new XPathException(ErrorCodes.FODF1310, "Invalid primary format token is not a valid decimal digital pattern: " + primaryFormatToken);
9383
}
9484

9585
// specifically defined format token rules 2-8
96-
// {@see https://www.w3.org/TR/xpath-functions-31/#formatting-integers}
86+
// <a href="https://www.w3.org/TR/xpath-functions-31/#formatting-integers"/>
9787
switch (primaryFormatToken) {
9888
case "A":
9989
return new SequenceIntegerPicture('A');
@@ -104,21 +94,21 @@ public static IntegerPicture fromString(final String pictureFormat) throws XPath
10494
case "I":
10595
return new RomanIntegerPicture(true/*isUpper*/);
10696
case "W":
107-
return new WordPicture(WordPicture.CaseAndCaps.Upper, formatModifier);
97+
return new WordPicture(WordPicture.CaseAndCaps.UPPER, formatModifier);
10898
case "w":
109-
return new WordPicture(WordPicture.CaseAndCaps.Lower, formatModifier);
99+
return new WordPicture(WordPicture.CaseAndCaps.LOWER, formatModifier);
110100
case "Ww":
111-
return new WordPicture(WordPicture.CaseAndCaps.Capitalized, formatModifier);
101+
return new WordPicture(WordPicture.CaseAndCaps.CAPITALIZED, formatModifier);
112102
default:
113103
break;
114104
}
115105

116106
// Rule 9 - sequences
117-
// {@see https://www.w3.org/TR/xpath-functions-31/#formatting-integers}
118-
final List<Integer> codePoints = CodePoints(primaryFormatToken);
107+
// <a href="https://www.w3.org/TR/xpath-functions-31/#formatting-integers"/>
108+
final List<Integer> codePoints = IntegerPicture.codePoints(primaryFormatToken);
119109
final Optional<IntegerPicture> numberingPicture = NumberingPicture.fromIndexCodePoint(codePoints.get(0), formatModifier);
120110

121-
return numberingPicture.orElse(defaultPictureWithModifier(formatModifier));
111+
return numberingPicture.orElse(IntegerPicture.defaultPictureWithModifier(formatModifier));
122112
}
123113

124114
static IntegerPicture defaultPictureWithModifier(final FormatModifier formatModifier) throws XPathException {
@@ -131,10 +121,11 @@ static IntegerPicture defaultPictureWithModifier(final FormatModifier formatModi
131121
* @param bigInteger the integer to format
132122
* @param locale of the language to use in formatting
133123
* @return a string containing the formatted integer
124+
* @throws XPathException if the locale is ill-formed
134125
*/
135-
abstract protected String formatInteger(BigInteger bigInteger, Locale locale) throws XPathException;
126+
protected abstract String formatInteger(BigInteger bigInteger, Locale locale) throws XPathException;
136127

137-
private Locale getLocaleFromLanguages(final List<String> languages) throws XPathException {
128+
private static Locale getLocaleFromLanguages(final List<String> languages) throws XPathException {
138129

139130
IllformedLocaleException languageILE = null;
140131
for (final String language : languages) {
@@ -146,12 +137,13 @@ private Locale getLocaleFromLanguages(final List<String> languages) throws XPath
146137
languageILE = ile;
147138
}
148139
}
140+
assert languageILE != null;
149141
throw new XPathException(ErrorCodes.FODF1310, languageILE.getMessage());
150142
}
151143

152144
public final String formatInteger(final BigInteger bigInteger, final List<String> languages) throws XPathException {
153145

154-
final Locale locale = getLocaleFromLanguages(languages);
146+
final Locale locale = IntegerPicture.getLocaleFromLanguages(languages);
155147
return formatInteger(bigInteger, locale);
156148
}
157149

@@ -161,17 +153,18 @@ public final String formatInteger(final BigInteger bigInteger, final List<String
161153
* @param s the input string
162154
* @return a list of the codepoints forming the string
163155
*/
164-
protected static List<Integer> CodePoints(final String s) {
156+
protected static List<Integer> codePoints(final String s) {
165157
final List<Integer> codePointList = new ArrayList<>(s.length());
166-
for (int i = 0; i < s.length(); ) {
158+
int i = 0;
159+
while (i < s.length()) {
167160
final int codePoint = Character.codePointAt(s, i);
168161
i += Character.charCount(codePoint);
169162
codePointList.add(codePoint);
170163
}
171164
return codePointList;
172165
}
173166

174-
protected static String FromCodePoint(final int codePoint) {
167+
protected static String fromCodePoint(final int codePoint) {
175168
final StringBuilder sb = new StringBuilder();
176169
for (final char c : Character.toChars(codePoint)) {
177170
sb.append(c);
@@ -187,6 +180,4 @@ protected static String ordinalSuffix(final int value, final Locale locale) {
187180
for (; sb.length() > 0 && Character.isAlphabetic(sb.charAt(i)); i++) ;
188181
return sb.delete(i, sb.length()).reverse().toString();
189182
}
190-
191-
192183
}

0 commit comments

Comments
 (0)