com.helger.commons.io.resource.IReadableResource Java Examples
The following examples show how to use
com.helger.commons.io.resource.IReadableResource.
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: InputSourceFactory.java From ph-commons with Apache License 2.0 | 6 votes |
@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 #2
Source File: XMLMapHandlerTest.java From ph-commons with Apache License 2.0 | 6 votes |
@Test public void testReadBuildInfo () { final ICommonsMap <String, String> aMap = new CommonsHashMap <> (); final IReadableResource aRes = new ClassPathResource ("xml/buildinfo.xml"); assertTrue (XMLMapHandler.readMap (aRes, aMap).isSuccess ()); assertNull (XMLMapHandler.readMap (new ClassPathResource ("test1.txt"))); assertTrue (aMap.containsKey ("buildinfo.version")); assertEquals ("1", aMap.get ("buildinfo.version")); assertTrue (XMLMapHandler.readMap (aRes).containsKey ("buildinfo.version")); assertEquals ("1", XMLMapHandler.readMap (aRes).get ("buildinfo.version")); assertTrue (XMLMapHandler.writeMap (aMap, new ByteArrayOutputStreamProvider ()).isSuccess ()); assertTrue (XMLMapHandler.writeMap (aMap, new NonBlockingByteArrayOutputStream ()).isSuccess ()); }
Example #3
Source File: EUBLTRDocumentTypeTest.java From ph-ubl with Apache License 2.0 | 6 votes |
@Test public void testAll () { for (final EUBLTRDocumentType e : EUBLTRDocumentType.values ()) { assertNotNull (e.getImplementationClass ()); assertTrue (StringHelper.hasText (e.getLocalName ())); assertTrue (StringHelper.hasText (e.getNamespaceURI ())); assertTrue (e.getAllXSDResources ().size () >= 1); for (final IReadableResource aRes : e.getAllXSDResources ()) assertTrue (e.name (), aRes.exists ()); assertNotNull (e.getSchema ()); assertSame (e.getSchema (), e.getSchema ()); assertSame (e, EUBLTRDocumentType.valueOf (e.name ())); } }
Example #4
Source File: EDianUBLDocumentTypeTest.java From ph-ubl with Apache License 2.0 | 6 votes |
@Test public void testAll () { for (final EDianUBLDocumentType e : EDianUBLDocumentType.values ()) { assertNotNull (e.getImplementationClass ()); assertTrue (StringHelper.hasText (e.getLocalName ())); assertTrue (StringHelper.hasText (e.getNamespaceURI ())); assertTrue (e.getAllXSDResources ().size () >= 1); for (final IReadableResource aRes : e.getAllXSDResources ()) assertTrue (e.name (), aRes.exists ()); assertNotNull (e.getSchema ()); assertSame (e.getSchema (), e.getSchema ()); assertSame (e, EDianUBLDocumentType.valueOf (e.name ())); } }
Example #5
Source File: Issue19Test.java From ph-css with Apache License 2.0 | 6 votes |
@Test public void testIssue () { // Multiple errors contained final IReadableResource aRes = new ClassPathResource ("testfiles/css30/bad_but_browsercompliant/issue19.css"); assertTrue (aRes.exists ()); final CascadingStyleSheet aCSS = CSSReader.readFromStream (aRes, new CSSReaderSettings ().setFallbackCharset (StandardCharsets.UTF_8) .setCSSVersion (ECSSVersion.CSS30) .setCustomErrorHandler (new LoggingCSSParseErrorHandler ()) .setBrowserCompliantMode (true)); assertNotNull (aCSS); if (false) System.out.println (new CSSWriter (ECSSVersion.CSS30).getCSSAsString (aCSS)); assertEquals (1, aCSS.getRuleCount ()); assertEquals (1, aCSS.getStyleRuleCount ()); }
Example #6
Source File: ImageDataManager.java From ph-commons with Apache License 2.0 | 6 votes |
/** * Remove a single resource from the cache. * * @param aRes * The resource to be removed. May be <code>null</code>. * @return Never <code>null</code>. */ @Nonnull public EChange clearCachedSize (@Nullable final IReadableResource aRes) { if (aRes == null) return EChange.UNCHANGED; return m_aRWLock.writeLockedGet ( () -> { // Existing resource? if (m_aImageData.remove (aRes) != null) return EChange.CHANGED; // Non-existing resource? if (m_aNonExistingResources.remove (aRes)) return EChange.CHANGED; return EChange.UNCHANGED; }); }
Example #7
Source File: PSXPathBoundSchemaTest.java From ph-schematron with Apache License 2.0 | 6 votes |
@Test public void testBindAllInvalidSchematrons () { for (final IReadableResource aRes : SchematronTestHelper.getAllInvalidSchematronFiles ()) { LOGGER.info (aRes.toString ()); try { // Parse the schema final PSSchema aSchema = new PSReader (aRes).readSchema (); final CollectingPSErrorHandler aCEH = new CollectingPSErrorHandler (); PSXPathQueryBinding.getInstance ().bind (aSchema, null, aCEH, null, null); // Either an ERROR was collected or an exception was thrown assertTrue (aCEH.getErrorList ().getMostSevereErrorLevel ().isGE (EErrorLevel.ERROR)); } catch (final SchematronException ex) { LOGGER.error (" " + ex.getMessage ()); } } }
Example #8
Source File: AbstractConfigurationSourceResource.java From ph-commons with Apache License 2.0 | 6 votes |
protected AbstractConfigurationSourceResource (final int nPriority, @Nonnull final IReadableResource aRes) { super (SOURCE_TYPE, nPriority); ValueEnforcer.notNull (aRes, "Resource"); m_aRes = aRes; final File aFile = aRes.getAsFile (); if (aFile != null) { if (aFile.isFile ()) { if (!aFile.canRead ()) LOGGER.warn ("The configuration file '" + aFile.getAbsolutePath () + "' exists, but is not readable"); } else { // Non existing files are okay if (aFile.isDirectory ()) LOGGER.warn ("The configuration file '" + aFile.getAbsolutePath () + "' exists, but is a directory"); } } }
Example #9
Source File: PSBoundSchemaCacheKey.java From ph-schematron with Apache License 2.0 | 6 votes |
public PSBoundSchemaCacheKey (@Nonnull final IReadableResource aResource, @Nullable final String sPhase, @Nullable final IPSErrorHandler aErrorHandler, @Nullable final IPSValidationHandler aCustomValidationHandler, @Nonnull final IXPathConfig aXPathConfig, @Nullable final EntityResolver aEntityResolver, final boolean bLenient) { ValueEnforcer.notNull (aResource, "Resource"); ValueEnforcer.notNull (aXPathConfig, "XPathConfig"); m_aResource = aResource; m_sPhase = sPhase; m_aErrorHandler = aErrorHandler; m_aCustomValidationHandler = aCustomValidationHandler; m_aXPathConfig = aXPathConfig; m_aEntityResolver = aEntityResolver; m_bLenient = bLenient; }
Example #10
Source File: Issue21Test.java From ph-css with Apache License 2.0 | 6 votes |
@Test public void testIssue () { // Multiple errors contained final IReadableResource aRes = new ClassPathResource ("testfiles/css30/good/issue21.css"); assertTrue (aRes.exists ()); final CascadingStyleSheet aCSS = CSSReader.readFromStream (aRes, new CSSReaderSettings ().setFallbackCharset (StandardCharsets.UTF_8) .setCustomErrorHandler (new LoggingCSSParseErrorHandler ()) .setBrowserCompliantMode (true)); assertNotNull (aCSS); if (false) System.out.println (new CSSWriter (ECSSVersion.CSS30).getCSSAsString (aCSS)); assertEquals (2, aCSS.getRuleCount ()); assertEquals (2, aCSS.getStyleRuleCount ()); }
Example #11
Source File: EUBL22DocumentTypeTest.java From ph-ubl with Apache License 2.0 | 6 votes |
@Test public void testAll () { final ICommonsSet <Class <?>> aClasses = new CommonsHashSet <> (); final ICommonsSet <String> aFilenames = new CommonsHashSet <> (); for (final EUBL22DocumentType e : EUBL22DocumentType.values ()) { assertNotNull (e.getImplementationClass ()); assertTrue (StringHelper.hasText (e.getLocalName ())); assertTrue (StringHelper.hasText (e.getNamespaceURI ())); assertTrue (e.getAllXSDResources ().size () >= 1); for (final IReadableResource aRes : e.getAllXSDResources ()) assertTrue (e.name (), aRes.exists ()); assertNotNull (e.getSchema ()); assertSame (e.getSchema (), e.getSchema ()); assertSame (e, EUBL22DocumentType.valueOf (e.name ())); assertTrue (aClasses.add (e.getImplementationClass ())); assertTrue (aFilenames.add (e.getAllXSDResources ().getFirst ().getPath ())); } }
Example #12
Source File: IssueGC6Test.java From ph-schematron with Apache License 2.0 | 5 votes |
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 #13
Source File: Issue17Test.java From ph-css with Apache License 2.0 | 5 votes |
@Test public void testIssue () { // Multiple errors contained final IReadableResource aRes = new ClassPathResource ("testfiles/css30/bad_but_browsercompliant/issue17.css"); assertTrue (aRes.exists ()); final CascadingStyleSheet aCSS = CSSReader.readFromStream (aRes, new CSSReaderSettings ().setFallbackCharset (StandardCharsets.UTF_8) .setCSSVersion (ECSSVersion.CSS30) .setCustomErrorHandler (new LoggingCSSParseErrorHandler ()) .setBrowserCompliantMode (true)); assertNotNull (aCSS); }
Example #14
Source File: IssueXsltKeyTest.java From ph-schematron with Apache License 2.0 | 5 votes |
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)); }
Example #15
Source File: AbstractWALDAO.java From ph-commons with Apache License 2.0 | 5 votes |
/** * 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 #16
Source File: ReadableResourceProviderChain.java From ph-commons with Apache License 2.0 | 5 votes |
@Nullable public IReadableResource getReadableResourceIf (@Nonnull final String sName, @Nonnull final Predicate <? super IReadableResource> aReturnFilter) { // Use the first resource provider that supports the name for (final IReadableResourceProvider aResProvider : m_aReadingResourceProviders) if (aResProvider.supportsReading (sName)) { final IReadableResource aRes = aResProvider.getReadableResource (sName); if (aReturnFilter.test (aRes)) return aRes; } return null; }
Example #17
Source File: CachingSAXInputSourceTest.java From ph-commons with Apache License 2.0 | 5 votes |
@Test public void testBasic () { final IReadableResource aRes = new ClassPathResource ("xml/list.xml"); CachingSAXInputSource is = new CachingSAXInputSource (aRes); assertEquals (aRes.getResourceID (), is.getSystemId ()); assertNotNull (StreamHelper.getAllBytes (is.getByteStream ())); is = new CachingSAXInputSource ((IHasInputStream) aRes); assertNull (is.getSystemId ()); assertNotNull (StreamHelper.getAllBytes (is.getByteStream ())); is = new CachingSAXInputSource (aRes, "sysid"); assertEquals ("sysid", is.getSystemId ()); assertNotNull (StreamHelper.getAllBytes (is.getByteStream ())); is = new CachingSAXInputSource (aRes.getInputStream ()); assertNull (is.getSystemId ()); assertNotNull (StreamHelper.getAllBytes (is.getByteStream ())); is = new CachingSAXInputSource (aRes.getInputStream (), "sysid"); assertEquals ("sysid", is.getSystemId ()); assertNotNull (StreamHelper.getAllBytes (is.getByteStream ())); CommonsTestHelper.testToStringImplementation (is); }
Example #18
Source File: Issue25Test.java From ph-schematron with Apache License 2.0 | 5 votes |
@Test public void testIssue25Invalid () throws Exception { final IReadableResource aSCH = new ClassPathResource ("test-sch/xfront/example05/check-classifications.sch"); final IReadableResource aXML = new ClassPathResource ("test-sch/xfront/example05/invalid-document.xml"); final SchematronOutputType aSOT = SchematronHelper.applySchematron (new SchematronResourcePure (aSCH), aXML); assertNotNull (aSOT); assertTrue (SVRLHelper.getAllFailedAssertions (aSOT).isNotEmpty ()); }
Example #19
Source File: AbstractSimpleDAO.java From ph-commons with Apache License 2.0 | 5 votes |
/** * 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 File: MicroReaderTest.java From ph-commons with Apache License 2.0 | 5 votes |
@Test public void testNull () { assertNull (MicroReader.readMicroXML ((InputSource) null)); assertNull (MicroReader.readMicroXML ((InputStream) null)); assertNull (MicroReader.readMicroXML ((IReadableResource) null)); assertNull (MicroReader.readMicroXML ((IHasInputStream) null)); assertNull (MicroReader.readMicroXML ((Reader) null)); assertNull (MicroReader.readMicroXML ((String) null)); assertNull (MicroReader.readMicroXML ((CharSequence) null)); assertNull (MicroReader.readMicroXML ((ByteBuffer) null)); assertNull (MicroReader.readMicroXML ((byte []) null)); }
Example #21
Source File: SVRLMarshallerTest.java From ph-schematron with Apache License 2.0 | 5 votes |
@Test public void testRead () { for (final IReadableResource aRes : SchematronTestHelper.getAllValidSVRLFiles ()) { LOGGER.info ("Reading " + aRes.getPath ()); assertNotNull (aRes.getPath (), new SVRLMarshaller ().read (aRes)); } }
Example #22
Source File: MicroReader.java From ph-commons with Apache License 2.0 | 5 votes |
@Nullable public static IMicroDocument readMicroXML (@Nullable final IReadableResource aRes, @Nullable final ISAXReaderSettings aSettings) { if (aRes == null) return null; return readMicroXML (InputSourceFactory.create (aRes), aSettings); }
Example #23
Source File: SchematronHelper.java From ph-schematron with Apache License 2.0 | 5 votes |
/** * Resolve all Schematron includes of the passed resource. * * @param aResource * The Schematron resource to read. May not be <code>null</code>. * @return <code>null</code> if the passed resource could not be read as XML * document */ @Nullable public static IMicroDocument getWithResolvedSchematronIncludes (@Nonnull final IReadableResource aResource) { return getWithResolvedSchematronIncludes (aResource, (ISAXReaderSettings) null, new LoggingPSErrorHandler (), CSchematron.DEFAULT_ALLOW_DEPRECATED_NAMESPACES); }
Example #24
Source File: SchematronResourcePure.java From ph-schematron with Apache License 2.0 | 5 votes |
public SchematronResourcePure (@Nonnull final IReadableResource aResource, @Nullable final String sPhase, @Nullable final IPSErrorHandler aErrorHandler, final boolean bLenient) { super (aResource); setPhase (sPhase); setErrorHandler (aErrorHandler); setLenient (bLenient); }
Example #25
Source File: CachingTransformStreamSourceTest.java From ph-commons with Apache License 2.0 | 5 votes |
@Test public void testBasic () { final IReadableResource aRes = new ClassPathResource ("xml/test1.xslt"); assertTrue (aRes.exists ()); CachingTransformStreamSource src = new CachingTransformStreamSource (aRes); InputStream is = src.getInputStream (); assertNotNull (is); StreamHelper.close (is); is = src.getInputStream (); assertNotNull (is); StreamHelper.close (is); assertEquals (aRes.getResourceID (), src.getSystemId ()); assertNull (src.getPublicId ()); src = new CachingTransformStreamSource ((IHasInputStream) aRes); is = src.getInputStream (); assertNotNull (is); StreamHelper.close (is); is = src.getInputStream (); assertNotNull (is); StreamHelper.close (is); assertNull (src.getSystemId ()); assertNull (src.getPublicId ()); src = new CachingTransformStreamSource (aRes.getInputStream ()); is = src.getInputStream (); assertNotNull (is); StreamHelper.close (is); is = src.getInputStream (); assertNotNull (is); StreamHelper.close (is); assertNull (src.getSystemId ()); assertNull (src.getPublicId ()); CommonsTestHelper.testToStringImplementation (src); }
Example #26
Source File: URLResourceProvider.java From ph-commons with Apache License 2.0 | 5 votes |
public IReadableResource getReadableResource (@Nonnull final String sURL) { ValueEnforcer.notNull (sURL, "URL"); try { return new URLResource (sURL); } catch (final MalformedURLException ex) { throw new IllegalArgumentException ("Passed name '" + sURL + "' is not a URL!", ex); } }
Example #27
Source File: InputSourceFactory.java From ph-commons with Apache License 2.0 | 5 votes |
@Nullable public static InputSource create (@Nonnull final IHasInputStream aISP) { if (aISP instanceof IReadableResource) return create ((IReadableResource) aISP); return create (aISP.getInputStream ()); }
Example #28
Source File: SchematronResourceXSLTCache.java From ph-schematron with Apache License 2.0 | 5 votes |
/** * 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 #29
Source File: IssueGC18Test.java From ph-css with Apache License 2.0 | 5 votes |
@Test public void testIssue18 () { final IReadableResource aRes = new ClassPathResource ("testfiles/css30/good/issue-gc-18.css"); assertTrue (aRes.exists ()); final CascadingStyleSheet aCSS = CSSReader.readFromStream (aRes, StandardCharsets.UTF_8, ECSSVersion.CSS30, new LoggingCSSParseErrorHandler ()); assertNotNull (aCSS); if (false) System.out.println (new CSSWriter (ECSSVersion.CSS30).getCSSAsString (aCSS)); }
Example #30
Source File: DefaultEntityResolver.java From ph-commons with Apache License 2.0 | 5 votes |
@Nullable public InputSource resolveEntity (@Nullable final String sPublicID, @Nullable final String sSystemID) throws SAXException, IOException { final IReadableResource aResolvedRes = DefaultResourceResolver.getResolvedResource (sSystemID, m_sBaseURI); return InputSourceFactory.create (aResolvedRes); }