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

The following examples show how to use com.helger.commons.string.StringHelper#getNotNull() . 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 locale string
 *
 * @param sLanguage
 *        The language to use. May be <code>null</code> or empty.
 * @param sCountry
 *        Optional country to use. May be <code>null</code>.
 * @param sVariant
 *        Optional variant. May be <code>null</code>.
 * @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 all the passed parameters are
 *         <code>null</code> or empty
 * @since 9.3.9
 */
@Nullable
public Locale getLocale (@Nullable final String sLanguage,
                         @Nullable final String sCountry,
                         @Nullable final String sVariant,
                         @Nullable final IMissingLocaleHandler aMissingHandler)
{
  final String sRealLanguage = StringHelper.getNotNull (LocaleHelper.getValidLanguageCode (sLanguage));
  final String sRealCountry = StringHelper.getNotNull (LocaleHelper.getValidCountryCode (sCountry));
  final String sRealVariant = StringHelper.getNotNull (sVariant);
  final String sLocaleKey = _buildLocaleString (sRealLanguage, sRealCountry, sRealVariant);

  Locale aLocale = null;
  if (sLocaleKey.length () > 0)
  {
    // try to resolve locale
    aLocale = m_aRWLock.readLockedGet ( () -> m_aLocales.get (sLocaleKey));
  }

  if (aLocale == null && aMissingHandler != null)
    aLocale = aMissingHandler.onMissingLocale (sLocaleKey, sRealLanguage, sRealCountry, sRealVariant);

  return aLocale;
}
 
Example 2
Source File: AbstractXMLSerializer.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Nullable
public String getElementNamespacePrefixToUse (@Nonnull final String sNamespaceURI,
                                              final boolean bIsRootElement,
                                              @Nonnull final Map <QName, String> aAttrMap)
{
  final String sDefaultNamespaceURI = StringHelper.getNotNull (_getDefaultNamespaceURI ());
  if (sNamespaceURI.equals (sDefaultNamespaceURI))
  {
    // It's the default namespace
    return null;
  }

  // Check if an existing prefix is in use
  String sNSPrefix = _getUsedPrefixOfNamespace (sNamespaceURI);

  // Do we need to create a prefix?
  if (sNSPrefix == null && (!bIsRootElement || sNamespaceURI.length () > 0))
  {
    // Ensure to use the correct prefix (check if defined in namespace
    // context)
    sNSPrefix = _getMappedPrefix (sNamespaceURI);

    // Do not create a prefix for the root element
    if (sNSPrefix == null && !bIsRootElement)
      sNSPrefix = _createUniquePrefix ();

    // Add and remember the attribute
    aAttrMap.put (XMLHelper.getXMLNSAttrQName (sNSPrefix), sNamespaceURI);
    addNamespaceMapping (sNSPrefix, sNamespaceURI);
  }
  return sNSPrefix;
}
 
Example 3
Source File: LocaleCache.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Nonnull
private static String _createLocaleKey (@Nullable final String sLanguage,
                                        @Nullable final String sCountry,
                                        @Nullable final String sVariant)
{
  final String sRealLanguage = StringHelper.getNotNull (LocaleHelper.getValidLanguageCode (sLanguage));
  final String sRealCountry = StringHelper.getNotNull (LocaleHelper.getValidCountryCode (sCountry));
  final String sRealVariant = StringHelper.getNotNull (sVariant);
  return _buildLocaleString (sRealLanguage, sRealCountry, sRealVariant);
}
 
Example 4
Source File: CSSSourceLocation.java    From ph-css with Apache License 2.0 5 votes vote down vote up
@Nonnull
@Nonempty
public String getLocationAsString ()
{
  final String sFirst = getFirstTokenLocationAsString ();
  final String sLast = getLastTokenLocationAsString ();
  return StringHelper.getNotNull (sFirst, "") + "-" + StringHelper.getNotNull (sLast, "");
}
 
Example 5
Source File: JAXBDocumentType.java    From ph-commons with Apache License 2.0 4 votes vote down vote up
/**
 * Constructor
 *
 * @param aClass
 *        The JAXB generated class of the root element. May not be
 *        <code>null</code>. This class must have the <code>@XmlType</code>
 *        annotation and the package the class resides in must have the
 *        <code>@XmlSchema</code> annotation with a non-null
 *        <code>namespace</code> property!
 * @param aXSDs
 *        The classpath relative paths to the XML Schema. May not be
 *        <code>null</code> but maybe empty. If the main XSD imports another
 *        XSD, the imported XSD must come first in the list. So the XSDs
 *        without any dependencies must come first!
 * @param aTypeToElementNameMapper
 *        An optional function to determine element name from type name. E.g.
 *        in UBL the type has an additional "Type" at the end that may not
 *        occur here. SBDH in contrary does not have such a suffix. May be
 *        <code>null</code> indicating that no name mapping is necessary.
 */
public JAXBDocumentType (@Nonnull final Class <?> aClass,
                         @Nullable final List <? extends ClassPathResource> aXSDs,
                         @Nullable final Function <? super String, ? extends String> aTypeToElementNameMapper)
{
  ValueEnforcer.notNull (aClass, "Class");
  if (aXSDs != null)
  {
    ValueEnforcer.notEmptyNoNullValue (aXSDs, "XSDs");
    for (final ClassPathResource aRes : aXSDs)
      ValueEnforcer.isTrue (aRes.hasClassLoader (),
                            () -> "ClassPathResource " + aRes + " should define its ClassLoader for OSGI handling!");
  }

  // Check whether it is an @XmlType class
  final XmlType aXmlType = aClass.getAnnotation (XmlType.class);
  if (aXmlType == null)
    throw new IllegalArgumentException ("The passed class '" +
                                        aClass.getName () +
                                        "' does not have an @XmlType annotation!");

  // Get the package of the passed Class
  final Package aPackage = aClass.getPackage ();

  // The package must have the annotation "XmlSchema" with the corresponding
  // namespace it supports (maybe empty but not null). If the base XSD does
  // not contain any namespace URI, the XMLSchema annotation might be missing!
  final XmlSchema aXmlSchema = aPackage.getAnnotation (XmlSchema.class);
  if (aXmlSchema != null && aXmlSchema.namespace () == null)
    throw new IllegalArgumentException ("The package '" +
                                        aPackage.getName () +
                                        "' has no namespace URI in the @XmlSchema annotation!");

  // Depending on the generation mode, the class may have the @XmlRootElement
  // annotation or not. If it is present, use the namespace URI and the local
  // name from it, else try to deduce the name from the type.
  String sNamespaceURI;
  String sLocalName;
  final XmlRootElement aRootElement = aClass.getAnnotation (XmlRootElement.class);
  if (aRootElement != null)
  {
    // Annotation is present
    sNamespaceURI = aRootElement.namespace ();
    if (JAXB_DEFAULT.equals (sNamespaceURI) && aXmlSchema != null)
      sNamespaceURI = aXmlSchema.namespace ();

    sLocalName = aRootElement.name ();
    if (JAXB_DEFAULT.equals (sLocalName))
      sLocalName = aXmlType.name ();
  }
  else
  {
    // Hack: build the element name from the type name
    if (aXmlSchema != null)
      sNamespaceURI = aXmlSchema.namespace ();
    else
      sNamespaceURI = null;
    sLocalName = aXmlType.name ();
  }
  // Call customizer (if provided)
  if (aTypeToElementNameMapper != null)
    sLocalName = aTypeToElementNameMapper.apply (sLocalName);
  if (StringHelper.hasNoText (sLocalName))
    throw new IllegalArgumentException ("Failed to determine the local name of the element to be created!");

  m_aClass = aClass;
  if (aXSDs != null)
    m_aXSDs.addAll (aXSDs);
  m_sNamespaceURI = StringHelper.getNotNull (sNamespaceURI);
  m_sLocalName = sLocalName;
}
 
Example 6
Source File: AbstractXMLSerializer.java    From ph-commons with Apache License 2.0 4 votes vote down vote up
@Nullable
public String getAttributeNamespacePrefixToUse (@Nonnull final String sNamespaceURI,
                                                @Nonnull final String sName,
                                                @Nonnull final String sValue,
                                                @Nonnull final Map <QName, String> aAttrMap)
{
  final String sDefaultNamespaceURI = StringHelper.getNotNull (_getDefaultNamespaceURI ());
  if (sNamespaceURI.equals (sDefaultNamespaceURI))
  {
    // It's the default namespace
    return null;
  }

  String sNSPrefix = _getUsedPrefixOfNamespace (sNamespaceURI);

  // Do we need to create a prefix?
  if (sNSPrefix == null)
  {
    if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals (sNamespaceURI))
    {
      // It is an "xmlns:xyz" attribute
      sNSPrefix = sName;
      // Don't emit "xmlns:xmlns"
      if (!XMLConstants.XMLNS_ATTRIBUTE.equals (sName))
      {
        // Add and remember the attribute
        aAttrMap.put (XMLHelper.getXMLNSAttrQName (sNSPrefix), sValue);
      }
      addNamespaceMapping (sNSPrefix, sValue);
    }
    else
    {
      // Ensure to use the correct prefix (namespace context)
      sNSPrefix = _getMappedPrefix (sNamespaceURI);

      // Do not create a prefix for the root element
      if (sNSPrefix == null)
      {
        if (sNamespaceURI.length () == 0)
        {
          // Don't create a namespace mapping for attributes without a
          // namespace URI
          return null;
        }

        sNSPrefix = _createUniquePrefix ();
      }

      // Don't emit "xmlns:xml"
      if (!XMLConstants.XML_NS_PREFIX.equals (sNSPrefix))
      {
        // Add and remember the attribute
        aAttrMap.put (XMLHelper.getXMLNSAttrQName (sNSPrefix), sNamespaceURI);
      }
      addNamespaceMapping (sNSPrefix, sNamespaceURI);
    }
  }
  return sNSPrefix;
}