Java Code Examples for com.helger.commons.io.resource.FileSystemResource

The following examples show how to use com.helger.commons.io.resource.FileSystemResource. These examples are extracted from open source projects. 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
@Test
public void testBasic ()
{
  final ConfigurationSourceProperties c = new ConfigurationSourceProperties (f);
  assertSame (EConfigSourceType.RESOURCE, c.getSourceType ());
  assertEquals (EConfigSourceType.RESOURCE.getDefaultPriority (), c.getPriority ());
  assertTrue (c.isInitializedAndUsable ());
  assertSame (f, c.getResource ());
  assertEquals ("string", c.getConfigurationValue ("element1").getValue ());
  assertEquals ("2", c.getConfigurationValue ("element2").getValue ());
  assertNull (c.getConfigurationValue ("what a mess"));

  CommonsTestHelper.testDefaultImplementationWithEqualContentObject (c, new ConfigurationSourceProperties (f));
  CommonsTestHelper.testDefaultImplementationWithDifferentContentObject (c, new ConfigurationSourceProperties (1234, f));
  CommonsTestHelper.testDefaultImplementationWithDifferentContentObject (c,
                                                                         new ConfigurationSourceProperties (new FileSystemResource (new File ("bla"))));
}
 
Example 2
Source Project: ph-commons   Source File: InputSourceFactory.java    License: Apache License 2.0 6 votes vote down vote up
@Nullable
public static InputSource create (@Nonnull final IReadableResource aResource)
{
  if (aResource instanceof FileSystemResource)
  {
    final File aFile = aResource.getAsFile ();
    if (aFile != null)
    {
      // Potentially use memory mapped files
      final InputSource ret = create (FileHelper.getInputStream (aFile));
      if (ret != null)
      {
        // Ensure system ID is present - may be helpful for resource
        // resolution
        final URL aURL = aResource.getAsURL ();
        if (aURL != null)
          ret.setSystemId (aURL.toExternalForm ());
      }
      return ret;
    }
  }
  return new ReadableResourceSAXInputSource (aResource);
}
 
Example 3
Source Project: ph-schematron   Source File: DocumentationExamples.java    License: Apache License 2.0 6 votes vote down vote up
public static boolean validateXMLViaPureSchematron2 (@Nonnull final File aSchematronFile,
                                                     @Nonnull final File aXMLFile) throws Exception
{
  // Read the schematron from file
  final PSSchema aSchema = new PSReader (new FileSystemResource (aSchematronFile)).readSchema ();
  if (!aSchema.isValid (new DoNothingPSErrorHandler ()))
    throw new IllegalArgumentException ("Invalid Schematron!");
  // Resolve the query binding to use
  final IPSQueryBinding aQueryBinding = PSQueryBindingRegistry.getQueryBindingOfNameOrThrow (aSchema.getQueryBinding ());
  // Pre-process schema
  final PSPreprocessor aPreprocessor = new PSPreprocessor (aQueryBinding);
  aPreprocessor.setKeepTitles (true);
  final PSSchema aPreprocessedSchema = aPreprocessor.getAsPreprocessedSchema (aSchema);
  // Bind the pre-processed schema
  final IPSBoundSchema aBoundSchema = aQueryBinding.bind (aPreprocessedSchema);
  // Read the XML file
  final Document aXMLNode = DOMReader.readXMLDOM (aXMLFile);
  if (aXMLNode == null)
    return false;
  // Perform the validation
  return aBoundSchema.validatePartially (aXMLNode, FileHelper.getAsURLString (aXMLFile)).isValid ();
}
 
Example 4
Source Project: ph-schematron   Source File: PSPreprocessorTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testIssue51 () throws SchematronException
{
  final PSPreprocessor aPreprocessor = new PSPreprocessor (PSXPathQueryBinding.getInstance ()).setKeepTitles (true)
                                                                                              .setKeepDiagnostics (true);
  final IReadableResource aRes = new FileSystemResource ("src/test/resources/issues/github51/test51.sch");
  final IMicroDocument aDoc = SchematronHelper.getWithResolvedSchematronIncludes (aRes, true);
  final PSReader aReader = new PSReader (aRes).setLenient (true);
  final PSSchema aSchema = aReader.readSchemaFromXML (aDoc.getDocumentElement ());
  final PSSchema aPreprocessedSchema = aPreprocessor.getAsPreprocessedSchema (aSchema);
  assertNotNull (aPreprocessedSchema);
  assertTrue (aPreprocessedSchema.isValid (new DoNothingPSErrorHandler ()));

  final PSWriterSettings aPWS = new PSWriterSettings ();
  aPWS.setXMLWriterSettings (new XMLWriterSettings ().setIndent (EXMLSerializeIndent.INDENT_AND_ALIGN)
                                                     .setPutNamespaceContextPrefixesInRoot (true)
                                                     .setNamespaceContext (PSWriterSettings.createNamespaceMapping (aPreprocessedSchema)));
  LOGGER.info ("Preprocessed:\n" + new PSWriter (aPWS).getXMLString (aPreprocessedSchema));
}
 
Example 5
Source Project: ph-schematron   Source File: Issue99Test.java    License: Apache License 2.0 6 votes vote down vote up
private static void _validateAndProduceSVRL (@Nonnull final File aSchematron,
                                             @Nonnull final File aXML) throws Exception
{
  SchematronDebug.setSaveIntermediateXSLTFiles (true);

  final StopWatch aSW = StopWatch.createdStarted ();
  LOGGER.info ("Start");
  final ISchematronResource aSCH = SchematronResourcePure.fromFile (aSchematron);
  if (aSCH instanceof SchematronResourcePure)
    ((SchematronResourcePure) aSCH).setCustomValidationHandler (new LoggingPSValidationHandler ());

  // Perform validation
  final SchematronOutputType aSVRL = aSCH.applySchematronValidationToSVRL (new FileSystemResource (aXML));
  assertNotNull (aSVRL);

  aSW.stop ();
  LOGGER.info ("Took " + aSW.getSeconds () + " seconds");

  LOGGER.info ("SVRL:\n" + new SVRLMarshaller ().getAsString (aSVRL));

  assertTrue (SVRLHelper.getAllFailedAssertionsAndSuccessfulReports (aSVRL).isEmpty ());
}
 
Example 6
Source Project: ph-schematron   Source File: Issue77Test.java    License: Apache License 2.0 6 votes vote down vote up
private static void _validateAndProduceSVRL (@Nonnull final File aSchematron,
                                             @Nonnull final File aXML,
                                             final boolean bValid) throws Exception
{
  final SchematronResourceSCH aSCH = SchematronResourceSCH.fromFile (aSchematron);
  aSCH.setAllowForeignElements (true);

  // Perform validation
  final SchematronOutputType aSVRL = aSCH.applySchematronValidationToSVRL (new FileSystemResource (aXML));
  assertNotNull (aSVRL);

  if (bValid)
    assertTrue (SVRLHelper.getAllFailedAssertionsAndSuccessfulReports (aSVRL).isEmpty ());
  else
    assertTrue (SVRLHelper.getAllFailedAssertionsAndSuccessfulReports (aSVRL).isNotEmpty ());
}
 
Example 7
Source Project: ph-schematron   Source File: Issue8Test.java    License: Apache License 2.0 6 votes vote down vote up
public static void validateAndProduceSVRL (@Nonnull final File aSchematron, final File aXML) throws Exception
{
  final SchematronResourceSCH aSCH = SchematronResourceSCH.fromFile (aSchematron);

  // Assign custom parameters
  aSCH.parameters ().put ("xyz", "mobile");
  aSCH.parameters ().put ("expected", "");

  if (false)
    System.out.println (XMLWriter.getNodeAsString (aSCH.getXSLTProvider ().getXSLTDocument ()));

  // Perform validation
  final SchematronOutputType aSVRL = aSCH.applySchematronValidationToSVRL (new FileSystemResource (aXML));
  assertNotNull (aSVRL);
  if (false)
    System.out.println (new SVRLMarshaller ().getAsString (aSVRL));
}
 
Example 8
Source Project: ph-schematron   Source File: Issue6Test.java    License: Apache License 2.0 6 votes vote down vote up
@SuppressFBWarnings ("BC_IMPOSSIBLE_INSTANCEOF")
public static void validateAndProduceSVRL (final File schematron, final File xml) throws Exception
{
  final IReadableResource aSchematron = new FileSystemResource (schematron.getAbsoluteFile ());
  final IReadableResource anXMLSource = new FileSystemResource (xml.getAbsoluteFile ());
  final AbstractSchematronResource aSCH = new SchematronResourceSCH (aSchematron);
  if (aSCH instanceof SchematronResourcePure)
    ((SchematronResourcePure) aSCH).setErrorHandler (new LoggingPSErrorHandler ());
  else
    System.out.println (XMLWriter.getNodeAsString (((SchematronResourceSCH) aSCH).getXSLTProvider ()
                                                                                 .getXSLTDocument ()));
  final SchematronOutputType aSVRL = aSCH.applySchematronValidationToSVRL (anXMLSource);
  assertNotNull (aSVRL);
  if (false)
    System.out.println (new SVRLMarshaller ().getAsString (aSVRL));
}
 
Example 9
Source Project: ph-schematron   Source File: Issue48Test.java    License: Apache License 2.0 6 votes vote down vote up
public static void validateAndProduceSVRL (@Nonnull final File aSchematron, final File aXML) throws Exception
{
  final PSSchema aSchema = new PSReader (new FileSystemResource (aSchematron)).readSchema ();
  final PSPreprocessor aPreprocessor = new PSPreprocessor (PSXPathQueryBinding.getInstance ());
  final PSSchema aPreprocessedSchema = aPreprocessor.getAsPreprocessedSchema (aSchema);
  final String sSCH = new PSWriter (new PSWriterSettings ().setXMLWriterSettings (new XMLWriterSettings ())).getXMLString (aPreprocessedSchema);

  if (false)
    System.out.println (sSCH);

  final SchematronResourceSCH aSCH = new SchematronResourceSCH (new ReadableResourceString (sSCH,
                                                                                            StandardCharsets.UTF_8));

  // Perform validation
  final SchematronOutputType aSVRL = aSCH.applySchematronValidationToSVRL (new FileSystemResource (aXML));
  assertNotNull (aSVRL);
  if (false)
    System.out.println (new SVRLMarshaller ().getAsString (aSVRL));
}
 
Example 10
Source Project: ph-schematron   Source File: Issue88Test.java    License: Apache License 2.0 6 votes vote down vote up
private static void _validateAndProduceSVRL (@Nonnull final File aSchematron,
                                             @Nonnull final File aXML) throws Exception
{
  SchematronDebug.setSaveIntermediateXSLTFiles (true);
  final ISchematronResource aSCH = SchematronResourcePure.fromFile (aSchematron);
  if (aSCH instanceof SchematronResourcePure)
    ((SchematronResourcePure) aSCH).setCustomValidationHandler (new LoggingPSValidationHandler ());

  // Perform validation
  final SchematronOutputType aSVRL = aSCH.applySchematronValidationToSVRL (new FileSystemResource (aXML));
  assertNotNull (aSVRL);

  LOGGER.info ("SVRL:\n" + new SVRLMarshaller ().getAsString (aSVRL));

  assertTrue (SVRLHelper.getAllFailedAssertionsAndSuccessfulReports (aSVRL).isEmpty ());
}
 
Example 11
Source Project: ph-schematron   Source File: Issue16Test.java    License: Apache License 2.0 6 votes vote down vote up
public static boolean validateXMLViaPureSchematron2 (@Nonnull final File aSchematronFile,
                                                     @Nonnull final File aXMLFile) throws Exception
{
  // Read the schematron from file
  final PSSchema aSchema = new PSReader (new FileSystemResource (aSchematronFile)).readSchema ();
  if (!aSchema.isValid (new DoNothingPSErrorHandler ()))
    throw new IllegalArgumentException ("Invalid Schematron!");
  // Resolve the query binding to use
  final IPSQueryBinding aQueryBinding = PSQueryBindingRegistry.getQueryBindingOfNameOrThrow (aSchema.getQueryBinding ());
  // Pre-process schema
  final PSPreprocessor aPreprocessor = new PSPreprocessor (aQueryBinding);
  aPreprocessor.setKeepTitles (true);
  final PSSchema aPreprocessedSchema = aPreprocessor.getAsPreprocessedSchema (aSchema);
  // Bind the pre-processed schema
  final IPSBoundSchema aBoundSchema = aQueryBinding.bind (aPreprocessedSchema);
  // Read the XML file
  final Document aXMLNode = DOMReader.readXMLDOM (aXMLFile);
  if (aXMLNode == null)
    return false;
  // Perform the validation
  return aBoundSchema.validatePartially (aXMLNode, FileHelper.getAsURLString (aXMLFile)).isValid ();
}
 
Example 12
Source Project: ph-commons   Source File: ConfigurationSourceJsonTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testBasic ()
{
  final ConfigurationSourceJson c = new ConfigurationSourceJson (f);
  assertSame (EConfigSourceType.RESOURCE, c.getSourceType ());
  assertEquals (EConfigSourceType.RESOURCE.getDefaultPriority (), c.getPriority ());
  assertTrue (c.isInitializedAndUsable ());
  assertSame (f, c.getResource ());
  assertEquals ("string", c.getConfigurationValue ("element1").getValue ());
  assertEquals ("2", c.getConfigurationValue ("element2").getValue ());
  assertEquals ("1234", c.getConfigurationValue ("element5.network.port").getValue ());
  assertNull (c.getConfigurationValue ("what a mess"));

  // Check array
  assertEquals ("4", c.getConfigurationValue ("element6.$count").getValue ());
  assertEquals ("17", c.getConfigurationValue ("element6.0").getValue ());
  assertEquals ("12", c.getConfigurationValue ("element6.3").getValue ());

  assertEquals ("3", c.getConfigurationValue ("element7.$count").getValue ());
  assertEquals ("10", c.getConfigurationValue ("element7.0.key").getValue ());
  assertNull (c.getConfigurationValue ("element7.0.value"));
  assertEquals ("3", c.getConfigurationValue ("element7.0.value.$count").getValue ());
  assertEquals ("bar", c.getConfigurationValue ("element7.0.value.2").getValue ());
  assertEquals ("plain value", c.getConfigurationValue ("element7.1").getValue ());
  assertEquals ("blub", c.getConfigurationValue ("element7.2.value.0").getValue ());
  // Check additional
  assertEquals ("value", c.getConfigurationValue ("element7.sub.key").getValue ());

  CommonsTestHelper.testDefaultImplementationWithEqualContentObject (c, new ConfigurationSourceJson (f));
  CommonsTestHelper.testDefaultImplementationWithDifferentContentObject (c, new ConfigurationSourceJson (1234, f));
  CommonsTestHelper.testDefaultImplementationWithDifferentContentObject (c,
                                                                         new ConfigurationSourceJson (new FileSystemResource (new File ("bla"))));
}
 
Example 13
Source Project: ph-commons   Source File: ConfigurationSourceJsonTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testNonExisting ()
{
  final IReadableResource f2 = new FileSystemResource (new File ("bla"));
  assertFalse (f2.exists ());
  final ConfigurationSourceJson c = new ConfigurationSourceJson (f2);
  assertSame (EConfigSourceType.RESOURCE, c.getSourceType ());
  assertEquals (EConfigSourceType.RESOURCE.getDefaultPriority (), c.getPriority ());
  assertFalse (c.isInitializedAndUsable ());
  assertSame (f2, c.getResource ());
  assertNull (c.getConfigurationValue ("element1"));
  assertNull (c.getConfigurationValue ("element2"));
  assertNull (c.getConfigurationValue ("element5.network.port"));
}
 
Example 14
@Test
public void testNonExisting ()
{
  final IReadableResource f2 = new FileSystemResource (new File ("bla"));
  assertFalse (f2.exists ());
  final ConfigurationSourceProperties c = new ConfigurationSourceProperties (f2);
  assertSame (EConfigSourceType.RESOURCE, c.getSourceType ());
  assertEquals (EConfigSourceType.RESOURCE.getDefaultPriority (), c.getPriority ());
  assertFalse (c.isInitializedAndUsable ());
  assertSame (f2, c.getResource ());
  assertNull (c.getConfigurationValue ("element1"));
}
 
Example 15
Source Project: ph-commons   Source File: DOMReaderTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testExternalEntityExpansion ()
{
  // Include a dummy file
  final File aFile = new File ("src/test/resources/test1.txt");
  assertTrue (aFile.exists ());
  final String sFileContent = StreamHelper.getAllBytesAsString (new FileSystemResource (aFile),
                                                                StandardCharsets.ISO_8859_1);

  // The XML with XXE problem
  final String sXML = "<?xml version='1.0' encoding='utf-8'?>" +
                      "<!DOCTYPE root [" +
                      " <!ELEMENT root ANY >" +
                      " <!ENTITY xxe SYSTEM \"" +
                      FileHelper.getAsURLString (aFile) +
                      "\" >]>" +
                      "<root>&xxe;</root>";
  final DOMReaderSettings aDRS = new DOMReaderSettings ().setEntityResolver ( (publicId,
                                                                               systemId) -> InputSourceFactory.create (new URLResource (systemId)));

  // Read successful - entity expansion!
  final Document aDoc = DOMReader.readXMLDOM (sXML, aDRS);
  assertNotNull (aDoc);
  assertEquals (sFileContent, aDoc.getDocumentElement ().getTextContent ());

  // Should fail because inline DTD is present
  final CollectingSAXErrorHandler aCEH = new CollectingSAXErrorHandler ();
  assertNull (DOMReader.readXMLDOM (sXML,
                                    aDRS.getClone ()
                                        .setFeatureValues (EXMLParserFeature.AVOID_XXE_SETTINGS)
                                        .setErrorHandler (aCEH)));
  // Expected
  assertEquals (1, aCEH.getErrorList ().size ());
  assertTrue (aCEH.getErrorList ()
                  .getFirst ()
                  .getErrorText (Locale.ROOT)
                  .contains ("http://apache.org/xml/features/disallow-doctype-decl"));
}
 
Example 16
Source Project: ph-commons   Source File: AbstractWALDAO.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Trigger the registered custom exception handlers for read errors.
 *
 * @param t
 *        Thrown exception. Never <code>null</code>.
 * @param bIsInitialization
 *        <code>true</code> if this happened during initialization of a new
 *        file, <code>false</code> if it happened during regular reading.
 * @param aFile
 *        The file that was read. May be <code>null</code> for in-memory DAOs.
 */
protected static void triggerExceptionHandlersRead (@Nonnull final Throwable t,
                                                    final boolean bIsInitialization,
                                                    @Nullable final File aFile)
{
  // Custom exception handler for reading
  if (exceptionHandlersRead ().isNotEmpty ())
  {
    final IReadableResource aRes = aFile == null ? null : new FileSystemResource (aFile);
    exceptionHandlersRead ().forEach (aCB -> aCB.onDAOReadException (t, bIsInitialization, aRes));
  }
}
 
Example 17
Source Project: ph-commons   Source File: AbstractWALDAO.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Trigger the registered custom exception handlers for read errors.
 *
 * @param t
 *        Thrown exception. Never <code>null</code>.
 * @param sErrorFilename
 *        The filename tried to write to. Never <code>null</code>.
 * @param aDoc
 *        The XML content that should be written. May be <code>null</code> if
 *        the error occurred in XML creation.
 */
protected static void triggerExceptionHandlersWrite (@Nonnull final Throwable t,
                                                     @Nonnull final String sErrorFilename,
                                                     @Nullable final IMicroDocument aDoc)
{
  // Check if a custom exception handler is present
  if (exceptionHandlersWrite ().isNotEmpty ())
  {
    final IReadableResource aRes = new FileSystemResource (sErrorFilename);
    final String sXMLContent = aDoc == null ? "no XML document created" : MicroWriter.getNodeAsString (aDoc);
    exceptionHandlersWrite ().forEach (aCB -> aCB.onDAOWriteException (t, aRes, sXMLContent));
  }
}
 
Example 18
Source Project: ph-commons   Source File: AbstractWALDAO.java    License: Apache License 2.0 5 votes vote down vote up
@Nonnull
@MustBeLocked (ELockType.WRITE)
private ESuccess _writeWALFile (@Nonnull @Nonempty final List <DATATYPE> aModifiedElements,
                                @Nonnull final EDAOActionType eActionType,
                                @Nonnull @Nonempty final String sWALFilename)
{
  final FileSystemResource aWALRes = m_aIO.getResource (sWALFilename);
  try (final DataOutputStream aDOS = new DataOutputStream (aWALRes.getOutputStream (EAppend.APPEND)))
  {
    // Write action type ID
    StreamHelper.writeSafeUTF (aDOS, eActionType.getID ());
    // Write number of elements
    aDOS.writeInt (aModifiedElements.size ());
    // Write all data elements as XML Strings :)
    for (final DATATYPE aModifiedElement : aModifiedElements)
    {
      final String sElement = convertNativeToWALString (aModifiedElement);
      StreamHelper.writeSafeUTF (aDOS, sElement);
    }
    return ESuccess.SUCCESS;
  }
  catch (final Exception ex)
  {
    if (LOGGER.isErrorEnabled ())
      LOGGER.error ("Error writing WAL file " + aWALRes, ex);
    triggerExceptionHandlersWrite (ex, sWALFilename, (IMicroDocument) null);
  }
  return ESuccess.FAILURE;
}
 
Example 19
Source Project: ph-commons   Source File: AbstractSimpleDAO.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Trigger the registered custom exception handlers for read errors.
 *
 * @param t
 *        Thrown exception. Never <code>null</code>.
 * @param bIsInitialization
 *        <code>true</code> if this happened during initialization of a new
 *        file, <code>false</code> if it happened during regular reading.
 * @param aFile
 *        The file that was read. May be <code>null</code> for in-memory DAOs.
 */
protected static void triggerExceptionHandlersRead (@Nonnull final Throwable t,
                                                    final boolean bIsInitialization,
                                                    @Nullable final File aFile)
{
  // Custom exception handler for reading
  if (exceptionHandlersRead ().isNotEmpty ())
  {
    final IReadableResource aRes = aFile == null ? null : new FileSystemResource (aFile);
    exceptionHandlersRead ().forEach (aCB -> aCB.onDAOReadException (t, bIsInitialization, aRes));
  }
}
 
Example 20
Source Project: ph-commons   Source File: AbstractSimpleDAO.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Trigger the registered custom exception handlers for read errors.
 *
 * @param t
 *        Thrown exception. Never <code>null</code>.
 * @param sErrorFilename
 *        The filename tried to write to. Never <code>null</code>.
 * @param aDoc
 *        The XML content that should be written. May be <code>null</code> if
 *        the error occurred in XML creation.
 */
protected static void triggerExceptionHandlersWrite (@Nonnull final Throwable t,
                                                     @Nonnull final String sErrorFilename,
                                                     @Nullable final IMicroDocument aDoc)
{
  // Check if a custom exception handler is present
  if (exceptionHandlersWrite ().isNotEmpty ())
  {
    final IReadableResource aRes = new FileSystemResource (sErrorFilename);
    final String sXMLContent = aDoc == null ? "no XML document created" : MicroWriter.getNodeAsString (aDoc);
    exceptionHandlersWrite ().forEach (aCB -> aCB.onDAOWriteException (t, aRes, sXMLContent));
  }
}
 
Example 21
Source Project: ph-commons   Source File: PropertiesHelper.java    License: Apache License 2.0 5 votes vote down vote up
@Nullable
public static NonBlockingProperties loadProperties (@Nonnull final File aFile)
{
  ValueEnforcer.notNull (aFile, "File");

  return loadProperties (new FileSystemResource (aFile));
}
 
Example 22
Source Project: ph-commons   Source File: PropertiesHelper.java    License: Apache License 2.0 5 votes vote down vote up
@Nullable
public static NonBlockingProperties loadProperties (@Nonnull final File aFile, @Nonnull final Charset aCharset)
{
  ValueEnforcer.notNull (aFile, "File");

  return loadProperties (new FileSystemResource (aFile), aCharset);
}
 
Example 23
Source Project: ph-commons   Source File: DefaultResourceResolver.java    License: Apache License 2.0 5 votes vote down vote up
@Nonnull
private static FileSystemResource _getChildResource (@Nullable final File aBaseFile, @Nonnull final File aSystemFile)
{
  if (aBaseFile == null)
    return new FileSystemResource (aSystemFile);

  final File aParent = aBaseFile.isDirectory () ? aBaseFile : aBaseFile.getParentFile ();
  final File aRealFile = new File (aParent, aSystemFile.getPath ());
  // path is cleaned (canonicalized) inside FileSystemResource
  return new FileSystemResource (aRealFile);
}
 
Example 24
Source Project: ph-commons   Source File: URLHelperTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
@SuppressFBWarnings ("DMI_HARDCODED_ABSOLUTE_FILENAME")
public void testGetAsFile ()
{
  final URL u = URLHelper.getAsURL ("file:/../dir/include.xml");
  final File f = URLHelper.getAsFile (u);
  assertNotNull (f);
  assertEquals (new File ("/../dir/include.xml").getAbsolutePath (), f.getAbsolutePath ());
  final FileSystemResource fs = new FileSystemResource (f);
  assertEquals (new File ("/dir/include.xml").getAbsolutePath (), fs.getPath ());
}
 
Example 25
@Nullable
private static String _findBaseURL (@Nonnull final IReadableResource aRes)
{
  if (aRes instanceof FileSystemResource)
  {
    // Use parent file as resolution base
    return FileHelper.getAsURLString (((FileSystemResource) aRes).getAsFile ().getParentFile ());
  }

  // Generic URL
  final URL aBaseURL = aRes.getAsURL ();
  return aBaseURL != null ? aBaseURL.toExternalForm () : null;
}
 
Example 26
Source Project: ph-schematron   Source File: DocumentationExamples.java    License: Apache License 2.0 5 votes vote down vote up
public static boolean readModifyAndWrite (@Nonnull final File aSchematronFile) throws Exception
{
  final PSSchema aSchema = new PSReader (new FileSystemResource (aSchematronFile)).readSchema ();
  final PSTitle aTitle = new PSTitle ();
  aTitle.addText ("Created by ph-schematron");
  aSchema.setTitle (aTitle);
  return MicroWriter.writeToFile (aSchema.getAsMicroElement (), aSchematronFile).isSuccess ();
}
 
Example 27
@Test
public void testInvalidSchematron ()
{
  assertFalse (new SchematronResourceSCH (new ClassPathResource ("test-sch/invalid01.sch")).isValidSchematron ());
  assertFalse (new SchematronResourceSCH (new ClassPathResource ("test-sch/this.file.does.not.exists")).isValidSchematron ());

  assertFalse (new SchematronResourceSCH (new FileSystemResource ("src/test/resources/test-sch/invalid01.sch")).isValidSchematron ());
  assertFalse (new SchematronResourceSCH (new FileSystemResource ("src/test/resources/test-sch/this.file.does.not.exists")).isValidSchematron ());
}
 
Example 28
Source Project: ph-schematron   Source File: IssueGC6Test.java    License: Apache License 2.0 5 votes vote down vote up
public static void validateAndProduceSVRL (final File schematron, final File xml) throws Exception
{
  final IReadableResource aSchematron = new FileSystemResource (schematron.getAbsoluteFile ());
  final IReadableResource anXMLSource = new FileSystemResource (xml.getAbsoluteFile ());
  final AbstractSchematronResource pure = new SchematronResourcePure (aSchematron);
  final SchematronOutputType aSVRL = pure.applySchematronValidationToSVRL (anXMLSource);
  assertNotNull (aSVRL);
  if (false)
    System.out.println (new SVRLMarshaller ().getAsString (aSVRL));
}
 
Example 29
Source Project: ph-schematron   Source File: Issue44Test.java    License: Apache License 2.0 5 votes vote down vote up
public static void validateAndProduceSVRL (@Nonnull final File aSchematron, final File aXML) throws Exception
{
  // SchematronResourcePure fails!
  ISchematronResource aSCH = SchematronResourcePure.fromFile (aSchematron);
  // Parsing Schematron works!
  aSCH = SchematronResourceSCH.fromFile (aSchematron);

  // Perform validation
  final SchematronOutputType aSVRL = aSCH.applySchematronValidationToSVRL (new FileSystemResource (aXML));
  assertNotNull (aSVRL);
  if (false)
    System.out.println (new SVRLMarshaller ().getAsString (aSVRL));

  assertEquals (3, SVRLHelper.getAllFailedAssertionsAndSuccessfulReports (aSVRL).size ());
}
 
Example 30
Source Project: ph-schematron   Source File: IssueXsltKeyTest.java    License: Apache License 2.0 5 votes vote down vote up
public static void validateAndProduceSVRL (final File schematron, final File xml) throws Exception
{
  final IReadableResource aSchematron = new FileSystemResource (schematron.getAbsoluteFile ());
  final IReadableResource anXMLSource = new FileSystemResource (xml.getAbsoluteFile ());
  final AbstractSchematronResource pure = new SchematronResourceSCH (aSchematron);
  final SchematronOutputType aSVRL = pure.applySchematronValidationToSVRL (anXMLSource);
  assertNotNull (aSVRL);
  if (false)
    System.out.println (new SVRLMarshaller ().getAsString (aSVRL));
}