com.helger.commons.regex.RegExHelper Java Examples

The following examples show how to use com.helger.commons.regex.RegExHelper. 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: LocaleHelper.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
@Nullable
public static String getValidCountryCode (@Nullable final String sCode)
{
  // Allow for 2 or 3 letter codes ("AT" and "AUT")
  if (StringHelper.hasText (sCode))
  {
    // Allow for 2 letter codes ("AT")
    if (RegExHelper.stringMatchesPattern ("[a-zA-Z]{2}|[0-9]{3}", sCode))
    {
      return sCode.toUpperCase (CGlobal.LOCALE_FIXED_NUMBER_FORMAT);
    }

    // Allow for 3 letter codes ("AUT")
    if (RegExHelper.stringMatchesPattern ("[a-zA-Z]{3}", sCode))
    {
      final String sAlpha3 = sCode.toUpperCase (CGlobal.LOCALE_FIXED_NUMBER_FORMAT);
      final String sAlpha2 = COUNTRY_ISO3TO2.get (sAlpha3);
      return sAlpha2 != null ? sAlpha2 : sAlpha3;
    }
  }
  return null;
}
 
Example #2
Source File: AbstractReadOnlyMapBasedMultilingualText.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
private static void _performConsistencyChecks (@Nonnull final String sValue)
{
  // String contains masked newline? warning only!
  if (sValue.contains ("\\n"))
    if (LOGGER.isWarnEnabled ())
      LOGGER.warn ("Passed string contains a masked newline - replace with an inline one:\n" + sValue);

  if (sValue.contains ("{0}"))
  {
    // When formatting is used, 2 single quotes are required!
    if (RegExHelper.stringMatchesPattern ("^'[^'].*", sValue))
      throw new IllegalArgumentException ("The passed string seems to start with unclosed single quotes: " + sValue);
    if (RegExHelper.stringMatchesPattern (".*[^']'[^'].*", sValue))
      throw new IllegalArgumentException ("The passed string seems to contain unclosed single quotes: " + sValue);
  }
  else
  {
    // When no formatting is used, single quotes are required!
    if (RegExHelper.stringMatchesPattern (".*''.*", sValue))
      throw new IllegalArgumentException ("The passed string seems to contain 2 single quotes: " + sValue);
  }
}
 
Example #3
Source File: IFileFilter.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
/**
 * Create a file filter that matches, if it matches one of the provided
 * regular expressions
 *
 * @param aRegExs
 *        The regular expressions to match against. May neither be
 *        <code>null</code> nor empty.
 * @return The created {@link IFileFilter}. Never <code>null</code>.
 * @see #filenameMatchNoRegEx(String...)
 * @see #filenameMatchAny(String...)
 * @see #filenameMatchNone(String...)
 */
@Nonnull
static IFileFilter filenameMatchAnyRegEx (@Nonnull @Nonempty final String... aRegExs)
{
  ValueEnforcer.notEmpty (aRegExs, "RegularExpressions");
  return aFile -> {
    if (aFile != null)
    {
      final String sRealName = FilenameHelper.getSecureFilename (aFile.getName ());
      if (sRealName != null)
        for (final String sRegEx : aRegExs)
          if (RegExHelper.stringMatchesPattern (sRegEx, sRealName))
            return true;
    }
    return false;
  };
}
 
Example #4
Source File: IFileFilter.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
/**
 * Create a file filter that matches, if it matches none of the provided
 * regular expressions
 *
 * @param aRegExs
 *        The regular expressions to match against. May neither be
 *        <code>null</code> nor empty.
 * @return The created {@link IFileFilter}. Never <code>null</code>.
 * @see #filenameMatchAnyRegEx(String...)
 * @see #filenameMatchAny(String...)
 * @see #filenameMatchNone(String...)
 */
@Nonnull
static IFileFilter filenameMatchNoRegEx (@Nonnull @Nonempty final String... aRegExs)
{
  ValueEnforcer.notEmpty (aRegExs, "RegularExpressions");
  return aFile -> {
    if (aFile == null)
      return false;
    final String sRealName = FilenameHelper.getSecureFilename (aFile.getName ());
    if (sRealName == null)
      return false;
    for (final String sRegEx : aRegExs)
      if (RegExHelper.stringMatchesPattern (sRegEx, sRealName))
        return false;
    return true;
  };
}
 
Example #5
Source File: SVRLHelper.java    From ph-schematron with Apache License 2.0 6 votes vote down vote up
/**
 * Convert an "unsexy" location string in the for, of
 * <code>*:xx[namespace-uri()='yy']</code> to something more readable like
 * <code>prefix:xx</code> by using the mapping registered in the
 * {@link SVRLLocationBeautifierRegistry}.
 *
 * @param sLocation
 *        The original location string. May not be <code>null</code>.
 * @return The beautified string. Never <code>null</code>. Might be identical
 *         to the original string if the pattern was not found.
 * @since 5.0.1
 */
@Nonnull
public static String getBeautifiedLocation (@Nonnull final String sLocation)
{
  String sResult = sLocation;
  // Handle namespaces:
  // Search for "*:xx[namespace-uri()='yy']" where xx is the localname and yy
  // is the namespace URI
  final Matcher aMatcher = RegExHelper.getMatcher ("\\Q*:\\E([a-zA-Z0-9_]+)\\Q[namespace-uri()='\\E([^']+)\\Q']\\E",
                                                   sResult);
  while (aMatcher.find ())
  {
    final String sLocalName = aMatcher.group (1);
    final String sNamespaceURI = aMatcher.group (2);

    // Check if there is a known beautifier for this pair of namespace and
    // local name
    final String sBeautified = SVRLLocationBeautifierRegistry.getBeautifiedLocation (sNamespaceURI, sLocalName);
    if (sBeautified != null)
      sResult = StringHelper.replaceAll (sResult, aMatcher.group (), sBeautified);
  }
  return sResult;
}
 
Example #6
Source File: CSSPropertyEnumOrColors.java    From ph-css with Apache License 2.0 6 votes vote down vote up
@Override
@OverridingMethodsMustInvokeSuper
public boolean isValidValue (@Nullable final String sValue)
{
  if (sValue == null)
    return false;

  // Split by whitespaces " a b " results in { "a", "b" }
  final String [] aParts = RegExHelper.getSplitToArray (sValue.trim (), "\\s+");
  if (aParts.length < getMinimumArgumentCount () || aParts.length > getMaximumArgumentCount ())
    return false;

  // Check each value
  for (final String aPart : aParts)
  {
    final String sTrimmedPart = aPart.trim ();
    if (!super.isValidValue (sTrimmedPart) && !CSSColorHelper.isColorValue (sTrimmedPart))
      return false;
  }
  return true;
}
 
Example #7
Source File: CSSPropertyNumbers.java    From ph-css with Apache License 2.0 6 votes vote down vote up
@Override
@OverridingMethodsMustInvokeSuper
public boolean isValidValue (@Nullable final String sValue)
{
  if (super.isValidValue (sValue))
    return true;

  if (sValue == null)
    return false;

  // Split by whitespaces
  final String [] aParts = RegExHelper.getSplitToArray (sValue.trim (), "\\s+");
  if (aParts.length < m_nMinArgCount || aParts.length > m_nMaxArgCount)
    return false;

  // Check if each part is a valid number
  for (final String sPart : aParts)
    if (!CSSNumberHelper.isValueWithUnit (sPart.trim (), m_bWithPercentage))
      return false;
  return true;
}
 
Example #8
Source File: CSSPropertyEnums.java    From ph-css with Apache License 2.0 6 votes vote down vote up
@Override
@OverridingMethodsMustInvokeSuper
public boolean isValidValue (@Nullable final String sValue)
{
  if (sValue == null)
    return false;

  // Split by whitespaces " a b " results in { "a", "b" }
  final String [] aParts = RegExHelper.getSplitToArray (sValue.trim (), "\\s+");
  if (aParts.length < getMinimumArgumentCount () || aParts.length > getMaximumArgumentCount ())
    return false;

  for (final String sPart : aParts)
    if (!super.isValidValue (sPart.trim ()))
      return false;
  return true;
}
 
Example #9
Source File: CSSPropertyColors.java    From ph-css with Apache License 2.0 6 votes vote down vote up
@Override
@OverridingMethodsMustInvokeSuper
public boolean isValidValue (@Nullable final String sValue)
{
  if (sValue == null)
    return false;

  // Split by whitespaces " a b " results in { "a", "b" }
  final String [] aParts = RegExHelper.getSplitToArray (sValue.trim (), "\\s+");
  if (aParts.length < getMinimumArgumentCount () || aParts.length > getMaximumArgumentCount ())
    return false;

  // Check each value
  for (final String aPart : aParts)
  {
    final String sTrimmedPart = aPart.trim ();
    if (!super.isValidValue (sTrimmedPart) && !CSSColorHelper.isColorValue (sTrimmedPart))
      return false;
  }
  return true;
}
 
Example #10
Source File: CSSPropertyEnumOrNumbers.java    From ph-css with Apache License 2.0 6 votes vote down vote up
@Override
@OverridingMethodsMustInvokeSuper
public boolean isValidValue (@Nullable final String sValue)
{
  if (sValue == null)
    return false;

  // Split by whitespaces " a b " results in { "a", "b" }
  final String [] aParts = RegExHelper.getSplitToArray (sValue.trim (), "\\s+");
  if (aParts.length < m_nMinNumbers || aParts.length > m_nMaxNumbers)
    return false;

  for (final String aPart : aParts)
  {
    final String sTrimmedPart = aPart.trim ();
    if (!super.isValidValue (sTrimmedPart) && !CSSNumberHelper.isValueWithUnit (sTrimmedPart, m_bWithPercentage))
      return false;
  }
  return true;
}
 
Example #11
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 #12
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 #13
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 #14
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 #15
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 #16
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 #17
Source File: PSAssertReport.java    From ph-schematron with Apache License 2.0 5 votes vote down vote up
/**
 * Set the diagnostics, as an IDREFS value (multiple IDREF values separated by
 * spaces)
 *
 * @param sDiagnostics
 *        The value to be set. May be <code>null</code>.
 */
public void setDiagnostics (@Nullable final String sDiagnostics)
{
  if (StringHelper.hasText (sDiagnostics))
    setDiagnostics (RegExHelper.getSplitToList (sDiagnostics.trim (), "\\s+"));
  else
    m_aDiagnostics = null;
}
 
Example #18
Source File: LocaleHelper.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Nullable
public static String getValidLanguageCode (@Nullable final String sCode)
{
  if (StringHelper.hasText (sCode) &&
      (RegExHelper.stringMatchesPattern ("[a-zA-Z]{2,8}", sCode) || isSpecialLocaleCode (sCode)))
  {
    return sCode.toLowerCase (CGlobal.LOCALE_FIXED_NUMBER_FORMAT);
  }
  return null;
}
 
Example #19
Source File: MainFindMaskedXMLChars.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
private static boolean _containsER (final String s, final int c)
{
  if (s.indexOf ("&#" + c + ";") >= 0)
    return true;
  if (s.indexOf ("&#x" + Integer.toString (c, 16) + ";") >= 0)
    return true;
  return RegExHelper.stringMatchesPattern (".+&[a-z]+;.+", s);
}
 
Example #20
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 #21
Source File: MainCreateJAXBBinding20.java    From ph-ubl with Apache License 2.0 4 votes vote down vote up
private static void _generateExplicitEnumMapping (@Nonnull final IMicroDocument aDoc,
                                                  @Nonnull @Nonempty final String sFilename,
                                                  @Nonnull final IMicroElement eBindings)
{
  final ICommonsSet <String> aUsedNames = new CommonsHashSet <> ();
  final ICommonsNavigableMap <String, String> aValueToConstants = new CommonsTreeMap <> ();
  final IMicroElement eSimpleType = aDoc.getDocumentElement ().getFirstChildElement ();

  final IMicroElement eInnerBindings = eBindings.appendElement (JAXB_NS_URI, "bindings")
                                                .setAttribute ("node",
                                                               "xsd:simpleType[@name='" +
                                                                       eSimpleType.getAttributeValue ("name") +
                                                                       "']");
  final IMicroElement eTypesafeEnumClass = eInnerBindings.appendElement (JAXB_NS_URI, "typesafeEnumClass");

  final IMicroElement eRestriction = eSimpleType.getFirstChildElement ();
  for (final IMicroElement eEnumeration : eRestriction.getAllChildElements (XMLConstants.W3C_XML_SCHEMA_NS_URI,
                                                                            "enumeration"))
  {
    final IMicroElement eAnnotation = eEnumeration.getFirstChildElement (XMLConstants.W3C_XML_SCHEMA_NS_URI,
                                                                         "annotation");
    if (eAnnotation == null)
      throw new IllegalStateException ("annotation is missing");
    final IMicroElement eDocumentation = eAnnotation.getFirstChildElement (XMLConstants.W3C_XML_SCHEMA_NS_URI,
                                                                           "documentation");
    if (eDocumentation == null)
      throw new IllegalStateException ("documentation is missing");
    final IMicroElement eCodeName = eDocumentation.getFirstChildElement ("urn:un:unece:uncefact:documentation:2",
                                                                         "CodeName");
    if (eCodeName == null)
      throw new IllegalStateException ("CodeName is missing");

    final String sValue = eEnumeration.getAttributeValue ("value");
    // Create an upper case Java identifier, without duplicate "_"
    String sCodeName = RegExHelper.getAsIdentifier (eCodeName.getTextContent ().trim ().toUpperCase (Locale.US))
                                  .replaceAll ("_+", "_");

    if (!aUsedNames.add (sCodeName))
    {
      // Ensure uniqueness of the enum member name
      int nSuffix = 1;
      while (true)
      {
        final String sSuffixedCodeName = sCodeName + "_" + nSuffix;
        if (aUsedNames.add (sSuffixedCodeName))
        {
          sCodeName = sSuffixedCodeName;
          break;
        }
        ++nSuffix;
      }
    }

    eTypesafeEnumClass.appendElement (JAXB_NS_URI, "typesafeEnumMember")
                      .setAttribute ("value", sValue)
                      .setAttribute ("name", sCodeName);
    aValueToConstants.put (sValue, sCodeName);
  }

  // Write out the mapping file for easy later-on resolving
  XMLMapHandler.writeMap (aValueToConstants,
                          new FileSystemResource ("src/main/resources/schemas/" + sFilename + ".mapping"));
}
 
Example #22
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 #23
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 #24
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);
}
 
Example #25
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 #26
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 hex color value. Example value:
 * <code>#ff0000</code>
 *
 * @param sValue
 *        The value to check. May be <code>null</code>.
 * @return <code>true</code> if it is a CSS hex color value,
 *         <code>false</code> if not
 */
public static boolean isHexColorValue (@Nullable final String sValue)
{
  final String sRealValue = StringHelper.trim (sValue);
  return StringHelper.hasText (sRealValue) &&
         sRealValue.charAt (0) == CCSSValue.PREFIX_HEX &&
         RegExHelper.stringMatchesPattern (PATTERN_HEX, sRealValue);
}
 
Example #27
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 #28
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 #29
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 #30
Source File: IErrorList.java    From ph-commons with Apache License 2.0 3 votes vote down vote up
/**
 * Get a sub-list with all entries that have field names matching the passed
 * regular expression.
 *
 * @param sRegExp
 *        The regular expression to compare the entries against.
 * @return Never <code>null</code>.
 */
@Nonnull
@ReturnsMutableCopy
default IErrorList getListOfFieldsRegExp (@Nonnull @Nonempty @RegEx final String sRegExp)
{
  return getSubList (x -> x.hasErrorFieldName () &&
                          RegExHelper.stringMatchesPattern (sRegExp, x.getErrorFieldName ()));
}