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

The following examples show how to use com.helger.commons.string.StringHelper#trimStart() . 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: FilenameHelper.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
/**
 * Tries to express the passed file path relative to the passed parent
 * directory. If the parent directory is null or not actually a parent of the
 * passed file, the passed file name will be returned unchanged.
 *
 * @param aFile
 *        The file which is to be described relatively. May not be
 *        <code>null</code>.
 * @param aParentDirectory
 *        The parent directory of the file to which the relative path
 *        expression will relate to. May be <code>null</code>.
 * @return The relative path or the unchanged absolute file path using Unix
 *         path separators instead of Operating System dependent separator. Or
 *         <code>null</code> if the passed file contains a path traversal at
 *         the beginning
 * @see #getCleanPath(File)
 * @see #startsWithPathSeparatorChar(CharSequence)
 */
@Nullable
public static String getRelativeToParentDirectory (@Nonnull final File aFile, @Nullable final File aParentDirectory)
{
  ValueEnforcer.notNull (aFile, "File");

  final String sCleanedFile = getCleanPath (aFile);
  if (aParentDirectory == null)
    return sCleanedFile;

  String sRelative = StringHelper.trimStart (sCleanedFile, getCleanPath (aParentDirectory));
  if (sRelative.equals (sCleanedFile))
  {
    // The passed file contains a path traversal!
    return null;
  }

  if (startsWithPathSeparatorChar (sRelative))
  {
    // Ignore any leading path separator char
    sRelative = sRelative.substring (1);
  }
  return sRelative;
}
 
Example 2
Source File: CertificateHelper.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
@Nullable
public static PrivateKey convertStringToPrivateKey (@Nullable final String sPrivateKey) throws GeneralSecurityException
{
  if (StringHelper.hasNoText (sPrivateKey))
    return null;

  String sRealPrivateKey = StringHelper.trimStart (sPrivateKey, BEGIN_PRIVATE_KEY);
  sRealPrivateKey = StringHelper.trimEnd (sRealPrivateKey, END_PRIVATE_KEY);
  sRealPrivateKey = StringHelper.getWithoutAnySpaces (sRealPrivateKey);
  final byte [] aPrivateKeyBytes = Base64.safeDecode (sRealPrivateKey);
  if (aPrivateKeyBytes == null)
    return null;

  final KeyFactory aKeyFactory = KeyFactory.getInstance ("RSA");
  final PKCS8EncodedKeySpec aKeySpec = new PKCS8EncodedKeySpec (aPrivateKeyBytes);
  return aKeyFactory.generatePrivate (aKeySpec);
}
 
Example 3
Source File: FormatterStringSkipPrefixAndSuffix.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Override
public String apply (@Nullable final Object aValue)
{
  String sValue = getValueAsString (aValue);
  // strip prefix and suffix
  if (m_sPrefix.length () > 0)
    sValue = StringHelper.trimStart (sValue, m_sPrefix);
  if (m_sSuffix.length () > 0)
    sValue = StringHelper.trimEnd (sValue, m_sSuffix);
  return sValue;
}
 
Example 4
Source File: CertificateHelper.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
/**
 * Remove any eventually preceding {@value #BEGIN_CERTIFICATE} and succeeding
 * {@value #END_CERTIFICATE} values from the passed certificate string.
 * Additionally all whitespaces of the string are removed.
 *
 * @param sCertificate
 *        The source certificate string. May be <code>null</code>.
 * @return <code>null</code> if the input string is <code>null</code> or
 *         empty, the stripped down string otherwise.
 */
@Nullable
public static String getWithoutPEMHeader (@Nullable final String sCertificate)
{
  if (StringHelper.hasNoText (sCertificate))
    return null;

  // Remove special begin and end stuff
  String sRealCertificate = sCertificate.trim ();

  /**
   * Handle certain misconfiguration issues. E.g. for 9906:testconsip on
   *
   * <pre>
   * http://b-c073e04afb234f70e74d3444ba3f8eaa.iso6523-actorid-upis.acc.edelivery.tech.ec.europa.eu/iso6523-actorid-upis%3A%3A9906%3Atestconsip/services/busdox-docid-qns%3A%3Aurn%3Aoasis%3Anames%3Aspecification%3Aubl%3Aschema%3Axsd%3AOrder-2%3A%3AOrder%23%23urn%3Awww.cenbii.eu%3Atransaction%3Abiitrns001%3Aver2.0%3Aextended%3Aurn%3Awww.peppol.eu%3Abis%3Apeppol3a%3Aver2.0%3A%3A2.1
   * </pre>
   */
  sRealCertificate = StringHelper.trimStart (sRealCertificate, BEGIN_CERTIFICATE_INVALID);
  sRealCertificate = StringHelper.trimEnd (sRealCertificate, END_CERTIFICATE_INVALID);

  // Remove regular PEM headers also
  sRealCertificate = StringHelper.trimStart (sRealCertificate, BEGIN_CERTIFICATE);
  sRealCertificate = StringHelper.trimEnd (sRealCertificate, END_CERTIFICATE);

  // Remove all existing whitespace characters
  return StringHelper.getWithoutAnySpaces (sRealCertificate);
}
 
Example 5
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 6
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 7
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 8
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);
}