/** * Creates a new formatter instance with the default configuration. */ public Formatter() { this(Convention.DEFAULT, Symbols.getDefault(), Constants.DEFAULT_INDENTATION); }
/** * Sets the opening and closing brackets to the given pairs. * Each string shall contain exactly two code points (usually two characters). * The first code point is taken as the opening bracket, and the second code point as the closing bracket. * * <div class="note"><b>Example:</b> * The following code will instruct the WKT formatter to use the (…) pair of brackets at formatting time, * but still accept the more common […] pair of brackets at parsing time: * * {@preformat java * symbols.setPairedBrackets("()", "[]"); * }</div> * * @param preferred the preferred pair of opening and closing quotes, used at formatting time. * @param alternatives alternative pairs of opening and closing quotes accepted at parsing time. */ public void setPairedBrackets(final String preferred, final String... alternatives) { checkWritePermission(); brackets = toCodePoints(preferred, alternatives); }
/** * Returns an immutable copy of this set of symbols, or {@code this} if this instance is already immutable. */ final Symbols immutable() { if (isImmutable) { return this; } final Symbols clone = clone(); clone.isImmutable = true; return clone; }
/** * Sets the characters used for opening and closing a sequence of values. * * @param openSequence the character for opening a sequence of values, as a Unicode code point. * @param closeSequence the character for closing a sequence of values, as a Unicode code point. */ public void setSequenceBrackets(final int openSequence, final int closeSequence) { checkWritePermission(); ensureValidQuoteOrBracket("openSequence", openSequence); ensureValidQuoteOrBracket("closeSequence", closeSequence); this.openSequence = openSequence; this.closeSequence = closeSequence; }
final Symbols s = Symbols.getDefault(); for (final Object wkt : definitions.values()) { if (s.containsAxis((String) wkt)) { containsAxis = true; break;
/** * Tests {@link Symbols} serialization. */ @Test public void testSerialization() { assertSame(Symbols.SQUARE_BRACKETS, assertSerializedEquals(Symbols.SQUARE_BRACKETS)); assertSame(Symbols.CURLY_BRACKETS, assertSerializedEquals(Symbols.CURLY_BRACKETS)); /* * Test with a new instance using a closing quote symbol different than the opening one. * This is necessary in order to ensure that the symbol is recomputed correctly. */ final Symbols symbols = new Symbols(Symbols.CURLY_BRACKETS); assertEquals("quote", "\"", symbols.getQuote()); symbols.setPairedQuotes("“”", "\"\""); assertEquals("quote", "”", symbols.getQuote()); final Symbols c = assertSerializedEquals(symbols); assertNotSame("Expected a new instance.", symbols, c); assertEquals("quote", "”", c.getQuote()); // Verify the recomputed value. } }
final Symbols symbols = new Symbols(Symbols.SQUARE_BRACKETS); symbols.setPairedQuotes("“”"); parser = format = new WKTFormat(null, null); format.setSymbols(symbols);
/** * Double or delete any closing quote character that may appear at or after the given index, * then append the closing quote character. The action taken for the quote character depends * on the WKT version: * * <ul> * <li>For WKT 2, double the quote as specified in the standard.</li> * <li>For WKT 1, conservatively delete the quote because the standard does not said what to do.</li> * </ul> */ private void closeQuote(int fromIndex) { final String quote = symbols.getQuote(); while ((fromIndex = buffer.indexOf(quote, fromIndex)) >= 0) { final int n = quote.length(); if (convention.majorVersion() == 1) { buffer.delete(fromIndex, fromIndex + n); } else { buffer.insert(fromIndex += n, quote); fromIndex += n; } } buffer.append(quote); }
/** * Sets the locale of decimal format symbols or other symbols. * Note that any non-English locale is likely to produce WKT that do not conform to ISO 19162. * Such WKT can be used for human reading, but not for data export. * * @param locale the new symbols locale. */ public void setLocale(final Locale locale) { checkWritePermission(); ensureNonNull("locale", locale); this.locale = locale; }
/** * Asserts that the call to {@link Symbols#containsAxis(CharSequence)} produce the given result. * This method expects an array using the {@code “…”} quotation marks, which will be replaced by * the standard {@code '"'} quotation mark after we tested the given string. */ private static void assertContainsAxis(final String message, final boolean expected, final String wkt) { assertEquals(message, expected, Symbols.getDefault().containsAxis(wkt)); final StringBuilder buffer = new StringBuilder(wkt); StringBuilders.replace(buffer, '“', '"'); StringBuilders.replace(buffer, '”', '"'); assertFalse(wkt.contentEquals(buffer)); assertEquals(message, expected, Symbols.getDefault().containsAxis(buffer)); }
/** * Sets the characters used for opening and closing a sequence of values. * * @param openSequence the character for opening a sequence of values, as a Unicode code point. * @param closeSequence the character for closing a sequence of values, as a Unicode code point. */ public void setSequenceBrackets(final int openSequence, final int closeSequence) { checkWritePermission(); ensureValidQuoteOrBracket("openSequence", openSequence); ensureValidQuoteOrBracket("closeSequence", closeSequence); this.openSequence = openSequence; this.closeSequence = closeSequence; }
/** * Double or delete any closing quote character that may appear at or after the given index, * then append the closing quote character. The action taken for the quote character depends * on the WKT version: * * <ul> * <li>For WKT 2, double the quote as specified in the standard.</li> * <li>For WKT 1, conservatively delete the quote because the standard does not said what to do.</li> * </ul> */ private void closeQuote(int fromIndex) { final String quote = symbols.getQuote(); while ((fromIndex = buffer.indexOf(quote, fromIndex)) >= 0) { final int n = quote.length(); if (convention.majorVersion() == 1) { buffer.delete(fromIndex, fromIndex + n); } else { buffer.insert(fromIndex += n, quote); fromIndex += n; } } buffer.append(quote); }
/** * Sets the locale of decimal format symbols or other symbols. * Note that any non-English locale is likely to produce WKT that do not conform to ISO 19162. * Such WKT can be used for human reading, but not for data export. * * @param locale the new symbols locale. */ public void setLocale(final Locale locale) { checkWritePermission(); ensureNonNull("locale", locale); this.locale = locale; }
/** * Creates a new formatter instance with the default configuration. */ public Formatter() { this(Convention.DEFAULT, Symbols.getDefault(), Constants.DEFAULT_INDENTATION); }
/** * Sets the opening and closing brackets to the given pairs. * Each string shall contain exactly two code points (usually two characters). * The first code point is taken as the opening bracket, and the second code point as the closing bracket. * * <div class="note"><b>Example:</b> * The following code will instruct the WKT formatter to use the (…) pair of brackets at formatting time, * but still accept the more common […] pair of brackets at parsing time: * * {@preformat java * symbols.setPairedBrackets("()", "[]"); * }</div> * * @param preferred the preferred pair of opening and closing quotes, used at formatting time. * @param alternatives alternative pairs of opening and closing quotes accepted at parsing time. */ public void setPairedBrackets(final String preferred, final String... alternatives) { checkWritePermission(); brackets = toCodePoints(preferred, alternatives); }
/** * Sets the string to use as a separator in a list of values. * The given string will be used "as-is" at formatting time, * but leading and trailing spaces will be ignored at parsing time. * * @param separator the new string to use as a separator in a list of values. */ public void setSeparator(final String separator) { checkWritePermission(); final String s = CharSequences.trimWhitespaces(separator.trim()); ensureNonEmpty("separator", s); this.separator = separator; trimmedSeparator = s; }
/** * Returns an immutable copy of this set of symbols, or {@code this} if this instance is already immutable. */ final Symbols immutable() { if (isImmutable) { return this; } final Symbols clone = clone(); clone.isImmutable = true; return clone; }
/** * Creates a format for the given locale and timezone. The given locale will be used for * {@link InternationalString} localization; this is <strong>not</strong> the locale for number format. * * @param locale the locale for the new {@code Format}, or {@code null} for {@code Locale.ROOT}. * @param timezone the timezone, or {@code null} for UTC. */ public WKTFormat(final Locale locale, final TimeZone timezone) { super(locale, timezone); convention = Convention.DEFAULT; symbols = Symbols.getDefault(); keywordCase = KeywordCase.DEFAULT; keywordStyle = KeywordStyle.DEFAULT; indentation = Constants.DEFAULT_INDENTATION; }
/** * Sets the opening and closing quotes to the given pairs. * Each string shall contain exactly two code points (usually two characters). * The first code point is taken as the opening quote, and the second code point as the closing quote. * * <div class="note"><b>Example:</b> * The following code will instruct the WKT formatter to use the prettier “…” quotation marks at formatting time * (especially useful for {@code String} constants in Java code), but still accept the standard "…" quotation marks * at parsing time: * * {@preformat java * symbols.setPairedQuotes("“”", "\"\""); * }</div> * * @param preferred the preferred pair of opening and closing quotes, used at formatting time. * @param alternatives alternative pairs of opening and closing quotes accepted at parsing time. */ public void setPairedQuotes(final String preferred, final String... alternatives) { checkWritePermission(); quotes = toCodePoints(preferred, alternatives); quote = preferred.substring(Character.charCount(quotes[0])).trim(); }
/** * Sets the string to use as a separator in a list of values. * The given string will be used "as-is" at formatting time, * but leading and trailing spaces will be ignored at parsing time. * * @param separator the new string to use as a separator in a list of values. */ public void setSeparator(final String separator) { checkWritePermission(); final String s = CharSequences.trimWhitespaces(separator.trim()); ensureNonEmpty("separator", s); this.separator = separator; trimmedSeparator = s; }