Java Code Examples for com.helger.commons.string.StringHelper#getExplodedArray()

The following examples show how to use com.helger.commons.string.StringHelper#getExplodedArray() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LocaleCache.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
/**
 * Get the {@link Locale} object matching the given language.
 *
 * @param sLanguage
 *        The language to use. May be <code>null</code> or empty.
 * @param aMissingHandler
 *        An optional handler to be invoked if the provided locale is not yet
 *        contained. May be <code>null</code>.
 * @return <code>null</code> if the passed language string is
 *         <code>null</code> or empty
 * @since 9.3.9
 */
@Nullable
public Locale getLocaleExt (@Nullable final String sLanguage, @Nullable final IMissingLocaleHandler aMissingHandler)
{
  if (sLanguage != null && sLanguage.length () > 2)
  {
    // parse
    final String [] aParts = StringHelper.getExplodedArray (LocaleHelper.LOCALE_SEPARATOR, sLanguage, 3);
    if (aParts.length == 3)
      return getLocale (aParts[0], aParts[1], aParts[2], aMissingHandler);
    if (aParts.length == 2)
      return getLocale (aParts[0], aParts[1], "", aMissingHandler);
    // else fall through
  }
  return getLocale (sLanguage, "", "", aMissingHandler);
}
 
Example 2
Source File: CSVWriterTest.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
/**
 * Test writing to a list.
 *
 * @throws IOException
 *         if the reader fails.
 */
@Test
public void testWriteAll () throws IOException
{
  final ICommonsList <ICommonsList <String>> allElements = new CommonsArrayList <> ();
  allElements.add (StringHelper.getExploded ('#', "Name#Phone#Email"));
  allElements.add (StringHelper.getExploded ('#', "Glen#1234#[email protected]"));
  allElements.add (StringHelper.getExploded ('#', "John#5678#[email protected]"));

  final NonBlockingStringWriter aSW = new NonBlockingStringWriter ();
  try (final CSVWriter aWriter = new CSVWriter (aSW))
  {
    aWriter.writeAll (allElements);
  }

  final String sResult = aSW.getAsString ().trim ();
  final String [] aLines = StringHelper.getExplodedArray ('\n', sResult);

  assertEquals (3, aLines.length);
}
 
Example 3
Source File: CSVWriterTest.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
/**
 * Test writing from a list.
 *
 * @throws IOException
 *         if the reader fails.
 */
@Test
public void testWriteAllObjects () throws IOException
{
  final ICommonsList <ICommonsList <String>> allElements = new CommonsArrayList <> ();
  allElements.add (StringHelper.getExploded ('#', "Name#Phone#Email"));
  allElements.add (StringHelper.getExploded ('#', "Glen#1234#[email protected]"));
  allElements.add (StringHelper.getExploded ('#', "John#5678#[email protected]"));

  final NonBlockingStringWriter aSW = new NonBlockingStringWriter ();
  try (final CSVWriter aWriter = new CSVWriter (aSW))
  {
    aWriter.writeAll (allElements, false);
  }

  final String sResult = aSW.getAsString ();
  final String [] aLines = StringHelper.getExplodedArray ('\n', sResult.trim ());
  assertEquals (3, aLines.length);

  final String [] aValues = StringHelper.getExplodedArray (',', aLines[1]);
  assertEquals (3, aValues.length);
  assertEquals ("1234", aValues[1]);
}
 
Example 4
Source File: Version.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Nonnull
@ReturnsMutableCopy
private static String [] _extSplit (@Nonnull final String s)
{
  final String [] aDotParts = StringHelper.getExplodedArray ('.', s, 2);
  if (aDotParts.length == 2)
  {
    // Dots always take precedence
    return aDotParts;
  }

  if (StringParser.isInt (aDotParts[0]))
  {
    // If it is numeric, use the dot parts anyway (e.g. for "5" or "-1")
    return aDotParts;
  }

  final String [] aDashParts = StringHelper.getExplodedArray ('-', s, 2);
  if (aDashParts.length == 1)
  {
    // Neither dot nor dash present
    return aDotParts;
  }

  // More matches for dash split! (e.g. "0-RC1")
  return aDashParts;
}
 
Example 5
Source File: LocaleCache.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
/**
 * Check if the passed language is in the cache.
 *
 * @param sLanguage
 *        The language to check.
 * @return <code>true</code> if it is in the cache, <code>false</code>
 *         otherwise.
 */
public boolean containsLocale (@Nullable final String sLanguage)
{
  if (sLanguage != null && sLanguage.length () > 2)
  {
    // parse
    final String [] aParts = StringHelper.getExplodedArray (LocaleHelper.LOCALE_SEPARATOR, sLanguage, 3);
    if (aParts.length == 3)
      return containsLocale (aParts[0], aParts[1], aParts[2]);
    if (aParts.length == 2)
      return containsLocale (aParts[0], aParts[1], "");
    // else fall through
  }
  return containsLocale (sLanguage, "", "");
}
 
Example 6
Source File: VersionRange.java    From ph-commons with Apache License 2.0 4 votes vote down vote up
/**
 * Construct a version range object from a string.<br>
 * Examples:<br>
 * <ul>
 * <li>[1.2.3, 4.5.6) -- 1.2.3 &lt;= x &lt; 4.5.6</li>
 * <li>[1.2.3, 4.5.6] -- 1.2.3 &lt;= x &lt;= 4.5.6</li>
 * <li>(1.2.3, 4.5.6) -- 1.2.3 &lt; x &lt; 4.5.6</li>
 * <li>(1.2.3, 4.5.6] -- 1.2.3 &lt; x &lt;= 4.5.6</li>
 * <li>1.2.3 -- 1.2.3 &lt;= x</li>
 * <li>[1.2.3 -- 1.2.3 &lt;= x</li>
 * <li>(1.2.3 -- 1.2.3 &lt; x</li>
 * <li><i>null</i> -- 0.0.0 &lt;= x</li>
 * <li>1, 4 -- 1 &lt;= x &lt;= 4</li>
 * </ul>
 *
 * @param sVersionString
 *        the version range in a string format as depicted above
 * @return The parsed {@link VersionRange} object
 * @throws IllegalArgumentException
 *         if the floor version is &lt; than the ceiling version
 */
@Nonnull
public static VersionRange parse (@Nullable final String sVersionString)
{
  final String s = sVersionString == null ? "" : sVersionString.trim ();
  if (s.length () == 0)
  {
    // empty string == range [0.0, infinity)
    return new VersionRange (Version.DEFAULT_VERSION, true, null, false);
  }

  Version aFloorVersion;
  boolean bIncludeFloor;
  Version aCeilVersion;
  boolean bIncludeCeil;

  int i = 0;
  // parse initial token
  if (s.charAt (i) == '[')
  {
    bIncludeFloor = true;
    i++;
  }
  else
    if (s.charAt (i) == '(')
    {
      bIncludeFloor = false;
      i++;
    }
    else
      bIncludeFloor = true;

  // check last token
  int j = 0;
  if (StringHelper.endsWith (s, ']'))
  {
    bIncludeCeil = true;
    j++;
  }
  else
    if (StringHelper.endsWith (s, ')'))
    {
      bIncludeCeil = false;
      j++;
    }
    else
      bIncludeCeil = false;

  // get length of version stuff
  final int nRestLen = s.length () - i - j;
  if (nRestLen == 0)
  {
    // only delimiter braces present?
    aFloorVersion = Version.DEFAULT_VERSION;
    aCeilVersion = null;
  }
  else
  {
    final String [] parts = StringHelper.getExplodedArray (',', s.substring (i, s.length () - j));
    final String sFloor = parts[0].trim ();
    final String sCeiling = parts.length > 1 ? parts[1].trim () : null;

    // get floor version
    aFloorVersion = Version.parse (sFloor);

    if (StringHelper.hasNoText (sCeiling))
      aCeilVersion = null;
    else
      aCeilVersion = Version.parse (sCeiling);
  }

  // check if floor <= ceil
  if (aCeilVersion != null && aFloorVersion.compareTo (aCeilVersion) > 0)
    throw new IllegalArgumentException ("Floor version may not be greater than the ceiling version!");

  return new VersionRange (aFloorVersion, bIncludeFloor, aCeilVersion, bIncludeCeil);
}
 
Example 7
Source File: MainCreateJAXBBinding22.java    From ph-ubl with Apache License 2.0 4 votes vote down vote up
@Nonnull
private static String _convertToPackage (@Nonnull final String sNamespaceURI)
{
  // Lowercase everything
  String s = sNamespaceURI.toLowerCase (Locale.US);

  String [] aParts;
  final URL aURL = URLHelper.getAsURL (sNamespaceURI);
  if (aURL != null)
  {
    // Host
    String sHost = aURL.getHost ();

    // Kick static prefix: www.helger.com -> helger.com
    sHost = StringHelper.trimStart (sHost, "www.");

    // Reverse domain: helger.com -> com.helger
    final List <String> x = CollectionHelper.getReverseList (StringHelper.getExploded ('.', sHost));

    // Path in regular order:
    final String sPath = StringHelper.trimStart (aURL.getPath (), '/');
    x.addAll (StringHelper.getExploded ('/', sPath));

    // Convert to array
    aParts = ArrayHelper.newArray (x, String.class);
  }
  else
  {
    // Kick known prefixes
    for (final String sPrefix : new String [] { "urn:", "http://" })
      if (s.startsWith (sPrefix))
      {
        s = s.substring (sPrefix.length ());
        break;
      }

    // Replace all illegal characters
    s = StringHelper.replaceAll (s, ':', '.');
    s = StringHelper.replaceAll (s, '-', '_');
    aParts = StringHelper.getExplodedArray ('.', s);
  }

  // Split into pieces and replace all illegal package parts (e.g. only
  // numeric) with valid ones
  for (int i = 0; i < aParts.length; ++i)
    aParts[i] = RegExHelper.getAsIdentifier (aParts[i]);

  return StringHelper.getImploded (".", aParts);
}
 
Example 8
Source File: MainCreateJAXBBinding21.java    From ph-ubl with Apache License 2.0 4 votes vote down vote up
@Nonnull
private static String _convertToPackage (@Nonnull final String sNamespaceURI)
{
  // Lowercase everything
  String s = sNamespaceURI.toLowerCase (Locale.US);

  String [] aParts;
  final URL aURL = URLHelper.getAsURL (sNamespaceURI);
  if (aURL != null)
  {
    // Host
    String sHost = aURL.getHost ();

    // Kick static prefix: www.helger.com -> helger.com
    sHost = StringHelper.trimStart (sHost, "www.");

    // Reverse domain: helger.com -> com.helger
    final List <String> x = CollectionHelper.getReverseList (StringHelper.getExploded ('.', sHost));

    // Path in regular order:
    final String sPath = StringHelper.trimStart (aURL.getPath (), '/');
    x.addAll (StringHelper.getExploded ('/', sPath));

    // Convert to array
    aParts = ArrayHelper.newArray (x, String.class);
  }
  else
  {
    // Kick known prefixes
    for (final String sPrefix : new String [] { "urn:", "http://" })
      if (s.startsWith (sPrefix))
      {
        s = s.substring (sPrefix.length ());
        break;
      }

    // Replace all illegal characters
    s = StringHelper.replaceAll (s, ':', '.');
    s = StringHelper.replaceAll (s, '-', '_');
    aParts = StringHelper.getExplodedArray ('.', s);
  }

  // Split into pieces and replace all illegal package parts (e.g. only
  // numeric) with valid ones
  for (int i = 0; i < aParts.length; ++i)
    aParts[i] = RegExHelper.getAsIdentifier (aParts[i]);

  return StringHelper.getImploded (".", aParts);
}
 
Example 9
Source File: MainCreateJAXBBinding20.java    From ph-ubl with Apache License 2.0 4 votes vote down vote up
@Nonnull
private static String _convertToPackage (@Nonnull final String sNamespaceURI)
{
  // Lowercase everything
  String s = sNamespaceURI.toLowerCase (Locale.US);

  String [] aParts;
  final URL aURL = URLHelper.getAsURL (sNamespaceURI);
  if (aURL != null)
  {
    // Host
    String sHost = aURL.getHost ();

    // Kick static prefix: www.helger.com -> helger.com
    sHost = StringHelper.trimStart (sHost, "www.");

    // Reverse domain: helger.com -> com.helger
    final ICommonsList <String> x = StringHelper.getExploded ('.', sHost);
    x.reverse ();

    // Path in regular order:
    final String sPath = StringHelper.trimStart (aURL.getPath (), '/');
    x.addAll (StringHelper.getExploded ('/', sPath));

    // Convert to array
    aParts = ArrayHelper.newArray (x, String.class);
  }
  else
  {
    // Kick known prefixes
    for (final String sPrefix : new String [] { "urn:", "http://" })
      if (s.startsWith (sPrefix))
      {
        s = s.substring (sPrefix.length ());
        break;
      }

    // Replace all illegal characters
    s = StringHelper.replaceAll (s, ':', '.');
    s = StringHelper.replaceAll (s, '-', '_');
    aParts = StringHelper.getExplodedArray ('.', s);
  }

  // Split into pieces and replace all illegal package parts (e.g. only
  // numeric) with valid ones
  for (int i = 0; i < aParts.length; ++i)
    aParts[i] = RegExHelper.getAsIdentifier (aParts[i]);

  return StringHelper.getImploded (".", aParts);
}
 
Example 10
Source File: MainCreateJAXBBinding23.java    From ph-ubl with Apache License 2.0 4 votes vote down vote up
@Nonnull
private static String _convertToPackage (@Nonnull final String sNamespaceURI)
{
  // Lowercase everything
  String s = sNamespaceURI.toLowerCase (Locale.US);

  String [] aParts;
  final URL aURL = URLHelper.getAsURL (sNamespaceURI);
  if (aURL != null)
  {
    // Host
    String sHost = aURL.getHost ();

    // Kick static prefix: www.helger.com -> helger.com
    sHost = StringHelper.trimStart (sHost, "www.");

    // Reverse domain: helger.com -> com.helger
    final List <String> x = CollectionHelper.getReverseList (StringHelper.getExploded ('.', sHost));

    // Path in regular order:
    final String sPath = StringHelper.trimStart (aURL.getPath (), '/');
    x.addAll (StringHelper.getExploded ('/', sPath));

    // Convert to array
    aParts = ArrayHelper.newArray (x, String.class);
  }
  else
  {
    // Kick known prefixes
    for (final String sPrefix : new String [] { "urn:", "http://" })
      if (s.startsWith (sPrefix))
      {
        s = s.substring (sPrefix.length ());
        break;
      }

    // Replace all illegal characters
    s = StringHelper.replaceAll (s, ':', '.');
    s = StringHelper.replaceAll (s, '-', '_');
    aParts = StringHelper.getExplodedArray ('.', s);
  }

  // Split into pieces and replace all illegal package parts (e.g. only
  // numeric) with valid ones
  for (int i = 0; i < aParts.length; ++i)
    aParts[i] = RegExHelper.getAsIdentifier (aParts[i]);

  return StringHelper.getImploded (".", aParts);
}