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

The following examples show how to use com.helger.commons.string.StringHelper#trim() . 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: CSSRectHelper.java    From ph-css with Apache License 2.0 6 votes vote down vote up
/**
 * Check if the passed value is CSS rectangle definition or not. It checks
 * both the current syntax (<code>rect(a,b,c,d)</code>) and the old syntax (
 * <code>rect(a b c d)</code>).
 *
 * @param sCSSValue
 *        The value to check. May be <code>null</code>.
 * @return <code>true</code> if the passed value is a rect value,
 *         <code>false</code> if not
 */
public static boolean isRectValue (@Nullable final String sCSSValue)
{
  final String sRealValue = StringHelper.trim (sCSSValue);
  if (StringHelper.hasText (sRealValue))
  {
    // Current syntax: rect(a,b,c,d)
    if (RegExHelper.stringMatchesPattern (PATTERN_CURRENT_SYNTAX, sRealValue))
      return true;

    // Backward compatible syntax: rect(a b c d)
    if (RegExHelper.stringMatchesPattern (PATTERN_OLD_SYNTAX, sRealValue))
      return true;
  }
  return false;
}
 
Example 2
Source File: CSSNumberHelper.java    From ph-css with Apache License 2.0 6 votes vote down vote up
/**
 * Convert the passed string value with unit into a structured
 * {@link CSSSimpleValueWithUnit}. Example: parsing <code>5px</code> will
 * result in the numeric value <code>5</code> and the unit
 * <code>ECSSUnit.PX</code>. The special value "0" is returned with the unit
 * "px".
 *
 * @param sCSSValue
 *        The value to be parsed. May be <code>null</code> and is trimmed
 *        inside this method.
 * @param bWithPerc
 *        <code>true</code> to include the percentage unit, <code>false</code>
 *        to exclude the percentage unit.
 * @return <code>null</code> if the passed value could not be converted to
 *         value and unit.
 */
@Nullable
public static CSSSimpleValueWithUnit getValueWithUnit (@Nullable final String sCSSValue, final boolean bWithPerc)
{
  String sRealValue = StringHelper.trim (sCSSValue);
  if (StringHelper.hasText (sRealValue))
  {
    // Special case for 0!
    if (sRealValue.equals ("0"))
      return new CSSSimpleValueWithUnit (BigDecimal.ZERO, ECSSUnit.PX);

    final ECSSUnit eUnit = bWithPerc ? getMatchingUnitInclPercentage (sRealValue)
                                     : getMatchingUnitExclPercentage (sRealValue);
    if (eUnit != null)
    {
      // Cut the unit off
      sRealValue = sRealValue.substring (0, sRealValue.length () - eUnit.getName ().length ()).trim ();
      final BigDecimal aValue = StringParser.parseBigDecimal (sRealValue);
      if (aValue != null)
        return new CSSSimpleValueWithUnit (aValue, eUnit);
    }
  }
  return null;
}
 
Example 3
Source File: AbstractSVRLMessage.java    From ph-schematron with Apache License 2.0 5 votes vote down vote up
public AbstractSVRLMessage (@Nullable final List <DiagnosticReference> aDiagnosticReferences,
                            @Nullable final String sText,
                            @Nullable final String sLocation,
                            @Nullable final String sTest,
                            @Nullable final String sRole,
                            @Nullable final IErrorLevel aFlag)
{
  m_aDiagnosticReferences = new CommonsArrayList <> (aDiagnosticReferences);
  m_sText = StringHelper.trim (sText);
  m_sLocation = sLocation;
  m_sTest = sTest;
  m_sRole = sRole;
  m_aFlag = aFlag;
}
 
Example 4
Source File: CSSURLHelper.java    From ph-css with Apache License 2.0 5 votes vote down vote up
/**
 * Check if the passed CSS value is an URL value. This is either a URL
 * starting with "url(" or it is the string "none".
 *
 * @param sValue
 *        The value to be checked.
 * @return <code>true</code> if the passed value starts with "url(" and ends
 *         with ")" - <code>false</code> otherwise.
 */
public static boolean isURLValue (@Nullable final String sValue)
{
  final String sRealValue = StringHelper.trim (sValue);
  if (StringHelper.hasNoText (sRealValue))
    return false;

  if (sRealValue.equals (CCSSValue.NONE))
    return true;

  // 5 = "url(".length () + ")".length
  return sRealValue.length () > 5 &&
         sRealValue.startsWith (CCSSValue.PREFIX_URL_OPEN) &&
         sRealValue.endsWith (CCSSValue.SUFFIX_URL_CLOSE);
}
 
Example 5
Source File: CSSRectHelper.java    From ph-css with Apache License 2.0 5 votes vote down vote up
/**
 * Get all the values from within a CSS rectangle definition.
 *
 * @param sCSSValue
 *        The CSS values to check. May be <code>null</code>.
 * @return <code>null</code> if the passed String is not a CSS rectangle. An
 *         array with 4 Strings if the passed value is a CSS rectangle.
 */
@Nullable
public static String [] getRectValues (@Nullable final String sCSSValue)
{
  String [] ret = null;
  final String sRealValue = StringHelper.trim (sCSSValue);
  if (StringHelper.hasText (sRealValue))
  {
    ret = RegExHelper.getAllMatchingGroupValues (PATTERN_CURRENT_SYNTAX, sRealValue);
    if (ret == null)
      ret = RegExHelper.getAllMatchingGroupValues (PATTERN_OLD_SYNTAX, sRealValue);
  }
  return ret;
}
 
Example 6
Source File: CSSColorHelper.java    From ph-css with Apache License 2.0 5 votes vote down vote up
/**
 * Extract the CSS RGB color value from the passed String. Example value:
 * <code>rgb(255,0,0)</code>
 *
 * @param sValue
 *        The value to extract the value from. May be <code>null</code>.
 * @return <code>null</code> if the passed value is not a valid CSS RGB color
 *         value.
 */
@Nullable
public static CSSRGB getParsedRGBColorValue (@Nullable final String sValue)
{
  final String sRealValue = StringHelper.trim (sValue);
  if (StringHelper.hasText (sRealValue) && sRealValue.startsWith (CCSSValue.PREFIX_RGB))
  {
    final String [] aValues = RegExHelper.getAllMatchingGroupValues (PATTERN_RGB, sRealValue);
    if (aValues != null)
      return new CSSRGB (aValues[0], aValues[1], aValues[2]);
  }
  return null;
}
 
Example 7
Source File: TrimmedValueSettings.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Override
@Nonnull
public EChange putIn (@Nonnull @Nonempty final String sFieldName, @Nullable final Object aNewValue)
{
  final String sRealValue = StringHelper.trim ((String) aNewValue);
  return super.putIn (sFieldName, sRealValue);
}
 
Example 8
Source File: CSSColorHelper.java    From ph-css with Apache License 2.0 5 votes vote down vote up
@Nullable
public static CSSRGBA getParsedRGBAColorValue (@Nullable final String sValue)
{
  final String sRealValue = StringHelper.trim (sValue);
  if (StringHelper.hasText (sRealValue) && sRealValue.startsWith (CCSSValue.PREFIX_RGBA))
  {
    final String [] aValues = RegExHelper.getAllMatchingGroupValues (PATTERN_RGBA, sRealValue);
    if (aValues != null)
      return new CSSRGBA (aValues[0], aValues[1], aValues[2], aValues[3]);
  }
  return null;
}
 
Example 9
Source File: CSSColorHelper.java    From ph-css with Apache License 2.0 5 votes vote down vote up
@Nullable
public static CSSHSL getParsedHSLColorValue (@Nullable final String sValue)
{
  final String sRealValue = StringHelper.trim (sValue);
  if (StringHelper.hasText (sRealValue) && sRealValue.startsWith (CCSSValue.PREFIX_HSL))
  {
    final String [] aValues = RegExHelper.getAllMatchingGroupValues (PATTERN_HSL, sRealValue);
    if (aValues != null)
      return new CSSHSL (aValues[0], aValues[1], aValues[2]);
  }
  return null;
}
 
Example 10
Source File: CSSColorHelper.java    From ph-css with Apache License 2.0 5 votes vote down vote up
@Nullable
public static CSSHSLA getParsedHSLAColorValue (@Nullable final String sValue)
{
  final String sRealValue = StringHelper.trim (sValue);
  if (StringHelper.hasText (sRealValue) && sRealValue.startsWith (CCSSValue.PREFIX_HSLA))
  {
    final String [] aValues = RegExHelper.getAllMatchingGroupValues (PATTERN_HSLA, sRealValue);
    if (aValues != null)
      return new CSSHSLA (aValues[0], aValues[1], aValues[2], aValues[3]);
  }
  return null;
}
 
Example 11
Source File: CSSUnknownRule.java    From ph-css with Apache License 2.0 4 votes vote down vote up
@Nonnull
public CSSUnknownRule setParameterList (@Nullable final String sParameterList)
{
  m_sParameterList = StringHelper.trim (sParameterList);
  return this;
}
 
Example 12
Source File: MimeTypeParser.java    From ph-commons with Apache License 2.0 4 votes vote down vote up
/**
 * Try to convert the string representation of a MIME type to an object. The
 * default quoting algorithm {@link CMimeType#DEFAULT_QUOTING} is used to
 * un-quote strings.
 *
 * @param sMimeType
 *        The string representation to be converted. May be <code>null</code>.
 * @param eQuotingAlgorithm
 *        The quoting algorithm to be used to un-quote parameter values. May
 *        not be <code>null</code>.
 * @return <code>null</code> if the parsed string is empty.
 * @throws MimeTypeParserException
 *         In case of an error
 */
@Nullable
public static MimeType parseMimeType (@Nullable final String sMimeType, @Nonnull final EMimeQuoting eQuotingAlgorithm)
{
  ValueEnforcer.notNull (eQuotingAlgorithm, "QuotingAlgorithm");

  // Trim
  final String sRealMimeType = StringHelper.trim (sMimeType);

  // No content -> no mime type
  if (StringHelper.hasNoText (sRealMimeType))
    return null;

  // Special case use sometimes from within browsers
  if (sRealMimeType.equals ("*"))
  {
    // Interpret as "*/*"
    return new MimeType (EMimeContentType._STAR, "*");
  }

  // Find the separator between content type and sub type ("/")
  final int nSlashIndex = sRealMimeType.indexOf (CMimeType.SEPARATOR_CONTENTTYPE_SUBTYPE);
  if (nSlashIndex < 0)
    throw new MimeTypeParserException ("MimeType '" + sRealMimeType + "' is missing the main '/' separator char");

  // Use the main content type
  final String sContentType = sRealMimeType.substring (0, nSlashIndex).trim ();
  final EMimeContentType eContentType = EMimeContentType.getFromIDOrNull (sContentType);
  if (eContentType == null)
    throw new MimeTypeParserException ("MimeType '" +
                                       sRealMimeType +
                                       "' uses an unknown content type '" +
                                       sContentType +
                                       "'");

  // Extract the rest (sub type + parameters)
  final String sRest = sRealMimeType.substring (nSlashIndex + 1);
  final int nSemicolonIndex = sRest.indexOf (CMimeType.SEPARATOR_PARAMETER);
  String sContentSubType;
  String sParameters;
  if (nSemicolonIndex >= 0)
  {
    sContentSubType = sRest.substring (0, nSemicolonIndex).trim ();
    // everything after the first ';' as parameters
    sParameters = sRest.substring (nSemicolonIndex + 1).trim ();
  }
  else
  {
    sContentSubType = sRest.trim ();
    sParameters = null;
  }

  if (StringHelper.hasNoText (sContentSubType))
    throw new MimeTypeParserException ("MimeType '" +
                                       sRealMimeType +
                                       "' uses an empty content sub type '" +
                                       sRealMimeType +
                                       "'");

  final MimeType ret = new MimeType (eContentType, sContentSubType);
  if (StringHelper.hasText (sParameters))
  {
    // We have parameters to extract
    _parseAndAddParameters (ret, sParameters, eQuotingAlgorithm);
  }
  return ret;
}
 
Example 13
Source File: CSSColorHelper.java    From ph-css with Apache License 2.0 4 votes vote down vote up
/**
 * Check if the passed string is any color value.
 *
 * @param sValue
 *        The value to check. May be <code>null</code>.
 * @return <code>true</code> if the passed value is not <code>null</code>, not
 *         empty and a valid CSS color value.
 * @see #isRGBColorValue(String)
 * @see #isRGBAColorValue(String)
 * @see #isHSLColorValue(String)
 * @see #isHSLAColorValue(String)
 * @see #isHexColorValue(String)
 * @see ECSSColor#isDefaultColorName(String)
 * @see ECSSColorName#isDefaultColorName(String)
 * @see CCSSValue#CURRENTCOLOR
 * @see CCSSValue#TRANSPARENT
 */
public static boolean isColorValue (@Nullable final String sValue)
{
  final String sRealValue = StringHelper.trim (sValue);
  if (StringHelper.hasNoText (sRealValue))
    return false;

  return isRGBColorValue (sRealValue) ||
         isRGBAColorValue (sRealValue) ||
         isHSLColorValue (sRealValue) ||
         isHSLAColorValue (sRealValue) ||
         isHexColorValue (sRealValue) ||
         ECSSColor.isDefaultColorName (sRealValue) ||
         ECSSColorName.isDefaultColorName (sRealValue) ||
         sRealValue.equals (CCSSValue.CURRENTCOLOR) ||
         sRealValue.equals (CCSSValue.TRANSPARENT);
}
 
Example 14
Source File: CSSUnknownRule.java    From ph-css with Apache License 2.0 4 votes vote down vote up
@Nonnull
public CSSUnknownRule setBody (@Nullable final String sBody)
{
  m_sBody = StringHelper.trim (sBody);
  return this;
}
 
Example 15
Source File: CSSColorHelper.java    From ph-css with Apache License 2.0 3 votes vote down vote up
/**
 * Check if the passed String is valid CSS RGBA color value. Example value:
 * <code>rgba(255,0,0, 0.1)</code>
 *
 * @param sValue
 *        The value to check. May be <code>null</code>.
 * @return <code>true</code> if it is a CSS RGBA color value,
 *         <code>false</code> if not
 */
public static boolean isRGBAColorValue (@Nullable final String sValue)
{
  final String sRealValue = StringHelper.trim (sValue);
  return StringHelper.hasText (sRealValue) &&
         sRealValue.startsWith (CCSSValue.PREFIX_RGBA) &&
         RegExHelper.stringMatchesPattern (PATTERN_RGBA, sRealValue);
}
 
Example 16
Source File: CSSColorHelper.java    From ph-css with Apache License 2.0 3 votes vote down vote up
/**
 * Check if the passed String is valid CSS HSLA color value. Example value:
 * <code>hsla(255,0%,0%, 0.1)</code>
 *
 * @param sValue
 *        The value to check. May be <code>null</code>.
 * @return <code>true</code> if it is a CSS HSLA color value,
 *         <code>false</code> if not
 */
public static boolean isHSLAColorValue (@Nullable final String sValue)
{
  final String sRealValue = StringHelper.trim (sValue);
  return StringHelper.hasText (sRealValue) &&
         sRealValue.startsWith (CCSSValue.PREFIX_HSLA) &&
         RegExHelper.stringMatchesPattern (PATTERN_HSLA, sRealValue);
}
 
Example 17
Source File: CSSColorHelper.java    From ph-css with Apache License 2.0 3 votes vote down vote up
/**
 * Check if the passed String is valid CSS HSL color value. Example value:
 * <code>hsl(255,0%,0%)</code>
 *
 * @param sValue
 *        The value to check. May be <code>null</code>.
 * @return <code>true</code> if it is a CSS HSL color value,
 *         <code>false</code> if not
 */
public static boolean isHSLColorValue (@Nullable final String sValue)
{
  final String sRealValue = StringHelper.trim (sValue);
  return StringHelper.hasText (sRealValue) &&
         sRealValue.startsWith (CCSSValue.PREFIX_HSL) &&
         RegExHelper.stringMatchesPattern (PATTERN_HSL, sRealValue);
}
 
Example 18
Source File: CSSColorHelper.java    From ph-css with Apache License 2.0 3 votes vote down vote up
/**
 * Check if the passed String is valid CSS RGB color value. Example value:
 * <code>rgb(255,0,0)</code>
 *
 * @param sValue
 *        The value to check. May be <code>null</code>.
 * @return <code>true</code> if it is a CSS RGB color value,
 *         <code>false</code> if not
 */
public static boolean isRGBColorValue (@Nullable final String sValue)
{
  final String sRealValue = StringHelper.trim (sValue);
  // startsWith as speedup
  return StringHelper.hasText (sRealValue) &&
         sRealValue.startsWith (CCSSValue.PREFIX_RGB) &&
         RegExHelper.stringMatchesPattern (PATTERN_RGB, sRealValue);
}
 
Example 19
Source File: CSSNumberHelper.java    From ph-css with Apache License 2.0 2 votes vote down vote up
/**
 * Check if the passed value is a pure numeric value without a unit.
 *
 * @param sCSSValue
 *        The value to be checked. May be <code>null</code> and is
 *        automatically trimmed inside.
 * @return <code>true</code> if the passed value is a pure decimal numeric
 *         value after trimming, <code>false</code> otherwise.
 */
public static boolean isNumberValue (@Nullable final String sCSSValue)
{
  final String sRealValue = StringHelper.trim (sCSSValue);
  return StringHelper.hasText (sRealValue) && StringParser.isDouble (sRealValue);
}
 
Example 20
Source File: IMicroNodeWithChildren.java    From ph-commons with Apache License 2.0 2 votes vote down vote up
/**
 * Get the concatenated text content of all direct {@link IMicroText} child
 * nodes of this element. After concatenation, all leading and trailing spaces
 * are removed.
 *
 * @return <code>null</code> if the element contains no text node as child
 */
@Nullable
default String getTextContentTrimmed ()
{
  return StringHelper.trim (getTextContent ());
}