Java Code Examples for com.helger.commons.io.resource.IReadableResource#exists()

The following examples show how to use com.helger.commons.io.resource.IReadableResource#exists() . 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: DefaultTransformURIResolver.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Override
@Nullable
protected Source internalResolve (final String sHref, final String sBase) throws TransformerException
{
  final String sRealBase;
  if (StringHelper.hasText (sBase))
  {
    // A base was provided - use it
    sRealBase = sBase;
  }
  else
    if (StringHelper.hasText (m_sDefaultBase))
    {
      // No base provided but a default base present - use default base
      sRealBase = m_sDefaultBase;
    }
    else
    {
      // Neither nor - leave as is
      sRealBase = sBase;
    }

  try
  {
    final IReadableResource aRes = DefaultResourceResolver.getResolvedResource (sHref, sRealBase);
    if (aRes != null && aRes.exists ())
      return TransformSourceFactory.create (aRes);
  }
  catch (final Exception ex)
  {
    throw new TransformerException (sHref + "//" + sBase + "//" + sRealBase, ex);
  }

  // Nothing to resolve
  return null;
}
 
Example 2
Source File: SchematronResourceXSLTCache.java    From ph-schematron with Apache License 2.0 5 votes vote down vote up
/**
 * Return an existing or create a new Schematron XSLT provider for the passed
 * resource.
 *
 * @param aXSLTResource
 *        The resource of the Schematron rules. May not be <code>null</code>.
 * @param aCustomErrorListener
 *        The custom error listener to be used. May be <code>null</code>.
 * @param aCustomURIResolver
 *        The custom URI resolver to be used. May be <code>null</code>.
 * @return <code>null</code> if the passed Schematron XSLT resource does not
 *         exist.
 */
@Nullable
public static SchematronProviderXSLTPrebuild getSchematronXSLTProvider (@Nonnull final IReadableResource aXSLTResource,
                                                                        @Nullable final ErrorListener aCustomErrorListener,
                                                                        @Nullable final URIResolver aCustomURIResolver)
{
  ValueEnforcer.notNull (aXSLTResource, "resource");

  if (!aXSLTResource.exists ())
  {
    LOGGER.warn ("XSLT resource " + aXSLTResource + " does not exist!");
    return null;
  }

  // Determine the unique resource ID for caching
  final String sResourceID = aXSLTResource.getResourceID ();

  // Validator already in the cache?
  final SchematronProviderXSLTPrebuild aProvider = s_aRWLock.readLockedGet ( () -> s_aCache.get (sResourceID));
  if (aProvider != null)
    return aProvider;

  return s_aRWLock.writeLockedGet ( () -> {
    // Check again in write lock
    SchematronProviderXSLTPrebuild aProvider2 = s_aCache.get (sResourceID);
    if (aProvider2 == null)
    {
      // Create new object and put in cache
      aProvider2 = createSchematronXSLTProvider (aXSLTResource, aCustomErrorListener, aCustomURIResolver);
      if (aProvider2 != null)
        s_aCache.put (sResourceID, aProvider2);
    }
    return aProvider2;
  });
}
 
Example 3
Source File: SchematronResourceSCHCache.java    From ph-schematron with Apache License 2.0 4 votes vote down vote up
/**
 * Get the Schematron validator for the passed resource. If no custom
 * parameter are present, the result is cached. The respective cache key is a
 * combination of the Schematron resource path, the phase and the language
 * code.
 *
 * @param aSchematronResource
 *        The resource of the Schematron rules. May not be <code>null</code>.
 * @param aTransformerCustomizer
 *        The XSLT transformer customizer to be used. May not be
 *        <code>null</code>.
 * @return <code>null</code> if the passed Schematron resource does not exist
 *         or is invalid.
 */
@Nullable
public static SchematronProviderXSLTFromSCH getSchematronXSLTProvider (@Nonnull final IReadableResource aSchematronResource,
                                                                       @Nonnull final SCHTransformerCustomizer aTransformerCustomizer)
{
  ValueEnforcer.notNull (aSchematronResource, "SchematronResource");
  ValueEnforcer.notNull (aTransformerCustomizer, "TransformerCustomizer");

  if (!aSchematronResource.exists ())
  {
    LOGGER.warn ("Schematron resource " + aSchematronResource + " does not exist!");
    return null;
  }

  if (!aTransformerCustomizer.canCacheResult ())
  {
    // Create new object and return without cache handling because the custom
    // parameters may have side effects on the created XSLT!
    return createSchematronXSLTProvider (aSchematronResource, aTransformerCustomizer);
  }

  // Determine the unique resource ID for caching
  final String sCacheKey = StringHelper.<String> getImploded (':',
                                                              aSchematronResource.getResourceID (),
                                                              StringHelper.getNotNull (aTransformerCustomizer.getPhase ()),
                                                              StringHelper.getNotNull (aTransformerCustomizer.getLanguageCode ()));

  s_aLock.lock ();
  try
  {
    // Validator already in the cache?
    SchematronProviderXSLTFromSCH aProvider = s_aCache.get (sCacheKey);
    if (aProvider == null)
    {
      // Create new object and put in cache
      aProvider = createSchematronXSLTProvider (aSchematronResource, aTransformerCustomizer);
      if (aProvider != null)
        s_aCache.put (sCacheKey, aProvider);
    }
    return aProvider;
  }
  finally
  {
    s_aLock.unlock ();
  }
}