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

The following examples show how to use com.helger.commons.io.resource.IReadableResource#getResourceID() . 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: SchematronHelper.java    From ph-schematron with Apache License 2.0 7 votes vote down vote up
/**
 * Apply the passed schematron on the passed XML resource using a custom error
 * handler.
 *
 * @param aSchematron
 *        The Schematron resource. May not be <code>null</code>.
 * @param aXML
 *        The XML resource. May not be <code>null</code>.
 * @return <code>null</code> if either the Schematron or the XML could not be
 *         read.
 * @throws IllegalStateException
 *         if the processing throws an unexpected exception.
 */
@Nullable
public static SchematronOutputType applySchematron (@Nonnull final ISchematronResource aSchematron,
                                                    @Nonnull final IReadableResource aXML)
{
  ValueEnforcer.notNull (aSchematron, "SchematronResource");
  ValueEnforcer.notNull (aXML, "XMLSource");

  try
  {
    // Apply Schematron on XML
    return aSchematron.applySchematronValidationToSVRL (aXML);
  }
  catch (final Exception ex)
  {
    throw new IllegalArgumentException ("Failed to apply Schematron " +
                                        aSchematron.getID () +
                                        " onto XML resource " +
                                        aXML.getResourceID (),
                                        ex);
  }
}
 
Example 2
Source File: SchematronResourceXSLTCache.java    From ph-schematron with Apache License 2.0 5 votes vote down vote up
@Nullable
public static SchematronProviderXSLTPrebuild createSchematronXSLTProvider (@Nonnull final IReadableResource aXSLTResource,
                                                                           @Nullable final ErrorListener aCustomErrorListener,
                                                                           @Nullable final URIResolver aCustomURIResolver)
{
  if (LOGGER.isInfoEnabled ())
    LOGGER.info ("Compiling XSLT instance " + aXSLTResource.toString ());

  final CollectingTransformErrorListener aCEH = new CollectingTransformErrorListener ();
  final SchematronProviderXSLTPrebuild aXSLTPreprocessor = new SchematronProviderXSLTPrebuild (aXSLTResource,
                                                                                               aCEH.andThen (aCustomErrorListener != null ? aCustomErrorListener
                                                                                                                                          : new LoggingTransformErrorListener (Locale.US)),
                                                                                               aCustomURIResolver);
  if (!aXSLTPreprocessor.isValidSchematron ())
  {
    // Schematron is invalid -> parsing failed
    LOGGER.warn ("The XSLT resource '" + aXSLTResource.getResourceID () + "' is invalid!");
    for (final IError aError : aCEH.getErrorList ())
      LOGGER.warn ("  " + aError.getAsString (Locale.US));
    return null;
  }

  // If it is a valid schematron, there must be a result XSLT present!
  if (aXSLTPreprocessor.getXSLTDocument () == null)
  {
    // Note: this should never occur, as it is in the Prebuild implementation
    // the same as "isValidSchematron" but to be implementation agnostic, we
    // leave the check anyway.
    throw new IllegalStateException ("No XSLT document retrieved from XSLT resource '" +
                                     aXSLTResource.getResourceID () +
                                     "'!");
  }

  // Create the main validator for the schematron
  return aXSLTPreprocessor;
}
 
Example 3
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 4
Source File: SchematronResourceSCHCache.java    From ph-schematron with Apache License 2.0 5 votes vote down vote up
/**
 * Create a new Schematron validator for the passed resource.
 *
 * @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 createSchematronXSLTProvider (@Nonnull final IReadableResource aSchematronResource,
                                                                          @Nonnull final SCHTransformerCustomizer aTransformerCustomizer)
{
  if (LOGGER.isDebugEnabled ())
    LOGGER.debug ("Compiling Schematron instance " + aSchematronResource.toString ());

  final SchematronProviderXSLTFromSCH aXSLTPreprocessor = new SchematronProviderXSLTFromSCH (aSchematronResource,
                                                                                             aTransformerCustomizer);
  if (!aXSLTPreprocessor.isValidSchematron ())
  {
    // Schematron is invalid -> parsing failed
    LOGGER.warn ("The Schematron resource '" + aSchematronResource.getResourceID () + "' is invalid!");
    if (LOGGER.isDebugEnabled () && aXSLTPreprocessor.getXSLTDocument () != null)
    {
      // Log the created XSLT document for better error tracking
      LOGGER.debug ("  Created XSLT document:\n" + XMLWriter.getNodeAsString (aXSLTPreprocessor.getXSLTDocument ()));
    }
    return null;
  }

  // If it is a valid schematron, there must be a result XSLT present!
  if (aXSLTPreprocessor.getXSLTDocument () == null)
    throw new IllegalStateException ("No XSLT document retrieved from Schematron resource '" +
                                     aSchematronResource.getResourceID () +
                                     "'!");

  if (LOGGER.isDebugEnabled ())
    LOGGER.debug ("Finished compiling Schematron instance " + aSchematronResource.toString ());

  // Create the main validator for the schematron
  return aXSLTPreprocessor;
}
 
Example 5
Source File: AbstractSchematronResource.java    From ph-schematron with Apache License 2.0 5 votes vote down vote up
/**
 * Constructor
 *
 * @param aResource
 *        The Schematron resource. May not be <code>null</code>.
 */
public AbstractSchematronResource (@Nonnull final IReadableResource aResource)
{
  m_aResource = ValueEnforcer.notNull (aResource, "Resource");
  m_sResourceID = aResource.getResourceID ();
  // Set a default entity resolver
  m_aEntityResolver = DefaultEntityResolver.createOnDemand (aResource);
}
 
Example 6
Source File: ResourceStreamSource.java    From ph-commons with Apache License 2.0 4 votes vote down vote up
public ResourceStreamSource (@Nonnull final IReadableResource aResource)
{
  this (aResource, aResource.getResourceID ());
}
 
Example 7
Source File: CachingTransformStreamSource.java    From ph-commons with Apache License 2.0 4 votes vote down vote up
public CachingTransformStreamSource (@Nonnull final IReadableResource aResource)
{
  this (aResource.getInputStream (), aResource.getResourceID ());
}
 
Example 8
Source File: ResourceLSInput.java    From ph-commons with Apache License 2.0 4 votes vote down vote up
public ResourceLSInput (@Nonnull final IReadableResource aResource)
{
  this (aResource, aResource.getResourceID ());
}
 
Example 9
Source File: CachingSAXInputSource.java    From ph-commons with Apache License 2.0 4 votes vote down vote up
public CachingSAXInputSource (@Nonnull final IReadableResource aRes)
{
  this (aRes.getInputStream (), aRes.getResourceID ());
}
 
Example 10
Source File: ReadableResourceSAXInputSource.java    From ph-commons with Apache License 2.0 4 votes vote down vote up
public ReadableResourceSAXInputSource (@Nonnull final IReadableResource aResource)
{
  this (aResource, aResource.getResourceID ());
}