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

The following examples show how to use com.helger.commons.io.resource.ClassPathResource. 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
Source Project: ph-commons   Source File: GenericJAXBMarshaller.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Constructor with XSD paths.
 *
 * @param aType
 *        The class of the JAXB document implementation type. May not be
 *        <code>null</code>.
 * @param aXSDs
 *        The XSDs used to validate document. May be <code>null</code> or
 *        empty indicating, that no XSD check is needed.
 * @param aJAXBElementWrapper
 *        Wrap the passed domain object into a {@link JAXBElement} for
 *        marshalling (writing). This can usually be done using the
 *        respective's package ObjectFactory implementation. May not be
 *        <code>null</code>.
 */
public GenericJAXBMarshaller (@Nonnull final Class <JAXBTYPE> aType,
                              @Nullable final List <? extends ClassPathResource> aXSDs,
                              @Nonnull final IFunction <? super JAXBTYPE, ? extends JAXBElement <JAXBTYPE>> aJAXBElementWrapper)
{
  m_aType = ValueEnforcer.notNull (aType, "Type");
  if (aXSDs != null)
  {
    ValueEnforcer.notEmptyNoNullValue (aXSDs, "XSDs");
    m_aXSDs.addAll (aXSDs);
  }
  for (final ClassPathResource aRes : m_aXSDs)
    ValueEnforcer.isTrue (aRes.hasClassLoader (),
                          () -> "ClassPathResource " + aRes + " should define its ClassLoader for OSGI handling!");

  m_aJAXBElementWrapper = ValueEnforcer.notNull (aJAXBElementWrapper, "JAXBElementWrapper");
  // By default this class loader of the type to be marshaled should be used
  // This is important for OSGI application containers and ANT tasks
  m_aClassLoader = new WeakReference <> (aType.getClassLoader ());
  m_aReadExceptionCallbacks.add (new LoggingJAXBReadExceptionHandler ());
  m_aWriteExceptionCallbacks.add (new LoggingJAXBWriteExceptionHandler ());
}
 
Example 2
Source Project: ph-commons   Source File: ConfigFactoryTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testSysPropConfigUrlJson ()
{
  SystemProperties.setPropertyValue ("config.url", new ClassPathResource ("sysprops/url.json").getAsURL ().toExternalForm ());
  try
  {
    final IConfig aConfig = Config.create (ConfigFactory.createDefaultValueProvider ());
    assertEquals ("from-sysprop-url-json0", aConfig.getAsString ("element0"));
    assertEquals ("from-private-application-properties1", aConfig.getAsString ("element1"));
    assertEquals ("from-application-json2", aConfig.getAsString ("element2"));
    assertEquals ("from-application-properties3", aConfig.getAsString ("element3"));
    assertEquals ("from-reference-properties4", aConfig.getAsString ("element4"));
    assertNull (aConfig.getAsString ("element5"));
  }
  finally
  {
    SystemProperties.removePropertyValue ("config.url");
  }
}
 
Example 3
Source Project: ph-commons   Source File: ConfigFactoryTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testSysPropConfigUrlProperties ()
{
  SystemProperties.setPropertyValue ("config.url", new ClassPathResource ("sysprops/url.properties").getAsURL ().toExternalForm ());
  try
  {
    final IConfig aConfig = Config.create (ConfigFactory.createDefaultValueProvider ());
    assertEquals ("from-sysprop-url-properties0", aConfig.getAsString ("element0"));
    assertEquals ("from-private-application-properties1", aConfig.getAsString ("element1"));
    assertEquals ("from-application-json2", aConfig.getAsString ("element2"));
    assertEquals ("from-application-properties3", aConfig.getAsString ("element3"));
    assertEquals ("from-reference-properties4", aConfig.getAsString ("element4"));
    assertNull (aConfig.getAsString ("element5"));
  }
  finally
  {
    SystemProperties.removePropertyValue ("config.url");
  }
}
 
Example 4
@Test
public void testAll ()
{
  for (int i = 0; i < 2; ++i)
  {
    final DefaultTransformURIResolver res = new DefaultTransformURIResolver (i == 0 ? null
                                                                                    : new LoggingTransformURIResolver ());
    final TransformerFactory fac = XMLTransformerFactory.createTransformerFactory (null, res);
    assertNotNull (fac);

    // Read valid XSLT
    Templates t1 = XMLTransformerFactory.newTemplates (fac, new ClassPathResource ("xml/test1.xslt"));
    assertNotNull (t1);

    // Read valid XSLT with valid include
    t1 = XMLTransformerFactory.newTemplates (fac, new ClassPathResource ("xml/test2.xslt"));
    assertNotNull (t1);

    // Read valid XSLT with invalid include
    t1 = XMLTransformerFactory.newTemplates (fac, new ClassPathResource ("xml/test3.xslt"));
    assertNull (t1);

    CommonsTestHelper.testToStringImplementation (res);
  }
}
 
Example 5
@Test
public void testAll ()
{
  final LoggingTransformErrorListener el = new LoggingTransformErrorListener (L_EN);
  final TransformerFactory fac = XMLTransformerFactory.createTransformerFactory (el,
                                                                                 new LoggingTransformURIResolver ());
  assertNotNull (fac);

  // Read valid XSLT
  Templates t1 = XMLTransformerFactory.newTemplates (fac, new ClassPathResource ("xml/test1.xslt"));
  assertNotNull (t1);

  // Read valid XSLT (with import)
  t1 = XMLTransformerFactory.newTemplates (fac, new ClassPathResource ("xml/test2.xslt"));
  assertNotNull (t1);

  // Read invalid XSLT
  assertNull (XMLTransformerFactory.newTemplates (fac, new ClassPathResource ("test1.txt")));

  CommonsTestHelper.testToStringImplementation (el);
}
 
Example 6
Source Project: ph-css   Source File: Issue19Test.java    License: Apache License 2.0 6 votes vote down vote up
@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 7
Source Project: ph-commons   Source File: MicroReaderTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testReadEntity ()
{
  // Read file with notation
  final IMicroDocument doc = MicroReader.readMicroXML (new ClassPathResource ("xml/xml-entity-public.xml"),
                                                       new SAXReaderSettings ().setEntityResolver (new EmptyEntityResolver ()));
  assertNotNull (doc);

  final MicroSAXHandler aHdl = new MicroSAXHandler (true, new EmptyEntityResolver (), true);
  final ISAXReaderSettings aSettings = new SAXReaderSettings ().setEntityResolver (aHdl)
                                                               .setDTDHandler (aHdl)
                                                               .setContentHandler (aHdl)
                                                               .setErrorHandler (aHdl)
                                                               .setLexicalHandler (aHdl);
  assertTrue (SAXReader.readXMLSAX (InputSourceFactory.create (ClassPathResource.getInputStream ("xml/xml-entity-public.xml")),
                                    aSettings)
                       .isSuccess ());
  assertNotNull (aHdl.getDocument ());

  // Write again
  assertNotNull (MicroWriter.getNodeAsString (doc));
}
 
Example 8
Source Project: ph-commons   Source File: XMLMapHandlerTest.java    License: Apache License 2.0 6 votes vote down vote up
@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 9
Source Project: ph-commons   Source File: DefaultResourceResolver.java    License: Apache License 2.0 6 votes vote down vote up
@Nonnull
private static ClassPathResource _resolveClassPathResource (final String sSystemId,
                                                            final String sBaseURI,
                                                            final ClassLoader aClassLoader)
{
  // Skip leading "cp:" or "classpath:"
  final String sBaseURIWithoutPrefix = ClassPathResource.getWithoutClassPathPrefix (sBaseURI);

  // Get the parent path of the base path
  final File aBaseFile = new File (sBaseURIWithoutPrefix).getParentFile ();

  // Concatenate the path with the URI to search
  final String sNewPath = FilenameHelper.getCleanPath (aBaseFile == null ? sSystemId
                                                                         : aBaseFile.getPath () + '/' + sSystemId);

  final ClassPathResource ret = new ClassPathResource (sNewPath, aClassLoader);
  if (isDebugResolve ())
    if (LOGGER.isInfoEnabled ())
      LOGGER.info ("  [ClassPath] resolved base + system to " + ret);
  return ret;
}
 
Example 10
Source Project: ph-schematron   Source File: Issue29Test.java    License: Apache License 2.0 6 votes vote down vote up
@Nullable
static SchematronOutputType validateXmlUsingSchematron (@Nonnull final IReadableResource aRes)
{
  SchematronOutputType ob = null;

  // Must use the XSLT based version, because of "key" usage
  final ISchematronResource aResSCH = new SchematronResourcePure (new ClassPathResource ("issues/github29/pbs.sch"));
  if (!aResSCH.isValidSchematron ())
    throw new IllegalArgumentException ("Invalid Schematron!");
  try
  {
    final Document aDoc = aResSCH.applySchematronValidation (new ResourceStreamSource (aRes));
    if (aDoc != null)
    {
      final SVRLMarshaller marshaller = new SVRLMarshaller ();
      ob = marshaller.read (aDoc);
    }
  }
  catch (final Exception pE)
  {
    pE.printStackTrace ();
  }
  return ob;
}
 
Example 11
Source Project: ph-schematron   Source File: SVRLMarshallerTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testWriteValid () throws Exception
{
  final Document aDoc = SchematronResourceSCH.fromClassPath (VALID_SCHEMATRON)
                                             .applySchematronValidation (new ClassPathResource (VALID_XMLINSTANCE));
  assertNotNull (aDoc);
  final SchematronOutputType aSO = new SVRLMarshaller ().read (aDoc);

  // Create XML
  final Document aDoc2 = new SVRLMarshaller ().getAsDocument (aSO);
  assertNotNull (aDoc2);
  assertEquals (CSVRL.SVRL_NAMESPACE_URI, aDoc2.getDocumentElement ().getNamespaceURI ());

  // Create String
  final String sDoc2 = new SVRLMarshaller ().getAsString (aSO);
  assertTrue (StringHelper.hasText (sDoc2));
  assertTrue (sDoc2.contains (CSVRL.SVRL_NAMESPACE_URI));
}
 
Example 12
@Test
public void testSVRL () throws IOException
{
  // Check the document
  try
  {
    // Get a validator from the schema.
    final Validator aValidator = RelaxNGCompactSchemaCache.getInstance ()
                                                          .getValidator (new ClassPathResource ("schemas/svrl-2006.rnc"));

    aValidator.validate (TransformSourceFactory.create (new ClassPathResource ("test-svrl/test1.svrl")));
    // Success
  }
  catch (final SAXException ex)
  {
    fail (ex.getMessage ());
  }
}
 
Example 13
Source Project: ph-css   Source File: CSSWriterFuncTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testCompressCSS_Size ()
{
  final CascadingStyleSheet aCSS = CSSReader.readFromStream (new ClassPathResource ("/testfiles/css21/good/phloc/test/content.css"),
                                                             StandardCharsets.UTF_8,
                                                             ECSSVersion.CSS30);
  assertNotNull (aCSS);

  // Only whitespace optimization
  final CSSWriterSettings aSettings = new CSSWriterSettings (ECSSVersion.CSS21, true);
  String sContent = new CSSWriter (aSettings).getCSSAsString (aCSS);
  assertEquals (2846, sContent.length ());

  // Also remove empty declarations
  aSettings.setRemoveUnnecessaryCode (true);
  sContent = new CSSWriter (aSettings).getCSSAsString (aCSS);
  assertEquals (2839, sContent.length ());
}
 
Example 14
Source Project: ph-css   Source File: Issue8Test.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testIssue8 ()
{
  final IReadableResource aRes = new ClassPathResource ("testfiles/css30/good/issue8.css");
  assertTrue (aRes.exists ());
  final CascadingStyleSheet aCSS = CSSReader.readFromStream (aRes,
                                                             StandardCharsets.UTF_8,
                                                             ECSSVersion.CSS30,
                                                             new LoggingCSSParseErrorHandler ());
  assertNotNull (aCSS);

  assertEquals (1, aCSS.getStyleRuleCount ());
  final CSSStyleRule aStyleRule = aCSS.getStyleRuleAtIndex (0);
  assertNotNull (aStyleRule);

  assertEquals (4, aStyleRule.getDeclarationCount ());
}
 
Example 15
Source Project: ph-css   Source File: Issue21Test.java    License: Apache License 2.0 6 votes vote down vote up
@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 16
Source Project: ph-commons   Source File: GenericJAXBMarshaller.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * @return A list of all XSD resources used for validation. Never
 *         <code>null</code>.
 */
@Nonnull
@Nonempty
@ReturnsMutableCopy
public final ICommonsList <ClassPathResource> getOriginalXSDs ()
{
  return m_aXSDs.getClone ();
}
 
Example 17
Source Project: ph-commons   Source File: JAXBContextCacheTest.java    License: Apache License 2.0 5 votes vote down vote up
private <T> void _testReadWrite (final Class <T> aClass) throws JAXBException
{
  JAXBContext aCtx = JAXBContextCache.getInstance ().getFromCache (aClass);
  assertNotNull (aCtx);

  // retrieve again
  assertSame (aCtx, JAXBContextCache.getInstance ().getFromCache (aClass));

  final Unmarshaller um = aCtx.createUnmarshaller ();

  // read valid
  JAXBElement <T> o = um.unmarshal (TransformSourceFactory.create (new ClassPathResource ("xml/test-archive-01.xml")),
                                    aClass);
  assertNotNull (o);

  // read invalid
  o = um.unmarshal (TransformSourceFactory.create (new ClassPathResource ("xml/buildinfo.xml")), aClass);
  assertNotNull (o);

  // Read invalid (but close to valid)
  o = um.unmarshal (TransformSourceFactory.create (new ClassPathResource ("xml/test-archive-03.xml")), aClass);
  assertNotNull (o);

  // Clear cache
  assertTrue (JAXBContextCache.getInstance ().clearCache ().isChanged ());
  assertFalse (JAXBContextCache.getInstance ().clearCache ().isChanged ());

  // Get context again
  aCtx = JAXBContextCache.getInstance ().getFromCache (aClass);
  assertNotNull (aCtx);

  // And remove manually
  assertTrue (JAXBContextCache.getInstance ().removeFromCache (aClass.getPackage ()).isChanged ());
  assertFalse (JAXBContextCache.getInstance ().removeFromCache (aClass.getPackage ()).isChanged ());
}
 
Example 18
Source Project: ph-css   Source File: IssueGC22Test.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testIssue22 ()
{
  final IReadableResource aRes = new ClassPathResource ("testfiles/css30/good/issue-gc-22.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 19
Source Project: ph-commons   Source File: ResourceStreamSourceTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testBasic ()
{
  final IReadableResource aRes = new ClassPathResource ("xml/test1.xslt");
  assertTrue (aRes.exists ());
  final ResourceStreamSource src = new ResourceStreamSource (aRes);
  final InputStream is = src.getInputStream ();
  assertNotNull (is);
  StreamHelper.close (is);
  assertEquals (aRes.getResourceID (), src.getSystemId ());
  assertNull (src.getPublicId ());

  CommonsTestHelper.testToStringImplementation (src);
}
 
Example 20
@Test
public void testAll () throws TransformerConfigurationException, TransformerException
{
  final CollectingTransformErrorListener el = new CollectingTransformErrorListener ();
  final TransformerFactory fac = XMLTransformerFactory.createTransformerFactory (el.andThen (new LoggingTransformErrorListener (L_EN)),
                                                                                 new LoggingTransformURIResolver ());
  assertNotNull (fac);

  // Read valid XSLT
  Templates t1 = XMLTransformerFactory.newTemplates (fac, new ClassPathResource ("xml/test1.xslt"));
  assertNotNull (t1);
  // May contain warning in JDK 1.7
  // (http://javax.xml.XMLConstants/property/accessExternalDTD is unknown)
  assertTrue (el.getErrorList ().containsNoError ());

  // Try a real transformation
  {
    final Document aDoc = XMLFactory.newDocument ();
    t1.newTransformer ()
      .transform (TransformSourceFactory.create (new ClassPathResource ("xml/xslt1.xml")), new DOMResult (aDoc));
    assertNotNull (aDoc);
    assertNotNull (aDoc.getDocumentElement ());
    assertEquals ("html", aDoc.getDocumentElement ().getTagName ());
  }

  // Read valid XSLT (with import)
  t1 = XMLTransformerFactory.newTemplates (fac, new ClassPathResource ("xml/test2.xslt"));
  assertNotNull (t1);
  // May contain warning in JDK 1.7
  // (http://javax.xml.XMLConstants/property/accessExternalDTD is unknown)
  assertTrue (el.getErrorList ().containsNoError ());

  // Read invalid XSLT
  assertNull (XMLTransformerFactory.newTemplates (fac, new ClassPathResource ("test1.txt")));
  assertTrue (el.getErrorList ().containsAtLeastOneError ());

  CommonsTestHelper.testToStringImplementation (el);
}
 
Example 21
@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 22
Source Project: ph-commons   Source File: DOMReaderTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testReadProcessingInstruction ()
{
  // Read file with processing instruction
  final Document doc = DOMReader.readXMLDOM (new ClassPathResource ("xml/xml-processing-instruction.xml"));
  assertNotNull (doc);

  // Write again
  assertNotNull (XMLWriter.getNodeAsString (doc));
}
 
Example 23
Source Project: ph-ubl   Source File: UBL23InvoiceHelperTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testComvertBackAndForth ()
{
  for (final String sFilename : MockUBL23TestDocuments.getUBL23TestDocuments (EUBL23DocumentType.INVOICE))
  {
    LOGGER.info (sFilename);

    // Read
    final Document aDoc = DOMReader.readXMLDOM (new ClassPathResource (sFilename),
                                                new DOMReaderSettings ().setSchema (EUBL23DocumentType.INVOICE.getSchema ()));
    assertNotNull (sFilename, aDoc);
    final InvoiceType aUBLObject = UBL23Reader.invoice ().read (aDoc);
    assertNotNull (sFilename, aUBLObject);

    // Convert Invoice to CreditNote
    final CreditNoteType aCreditNote = new CreditNoteType ();
    UBL23InvoiceHelper.cloneInvoiceToCreditNote (aUBLObject, aCreditNote);

    // Validate CreditNote
    IErrorList aErrors = UBL23Validator.creditNote ().validate (aCreditNote);
    assertNotNull (sFilename, aErrors);
    assertFalse (sFilename, aErrors.containsAtLeastOneError ());

    // Convert CreditNote back to Invoice
    final InvoiceType aInvoice2 = new InvoiceType ();
    UBL23CreditNoteHelper.cloneCreditNoteToInvoice (aCreditNote, aInvoice2);

    // Validate Invoice again
    aErrors = UBL23Validator.invoice ().validate (aInvoice2);
    assertNotNull (sFilename, aErrors);
    assertFalse (sFilename, aErrors.containsAtLeastOneError ());
  }
}
 
Example 24
Source Project: ph-commons   Source File: SAXReaderTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testMultithreadedSAX_ReadableResourceSAXInputSource ()
{
  CommonsTestHelper.testInParallel (1000,
                                    (IThrowingRunnable <SAXException>) () -> assertTrue (SAXReader.readXMLSAX (new ClassPathResource ("xml/buildinfo.xml"),
                                                                                                               new SAXReaderSettings ().setContentHandler (new DefaultHandler ()))
                                                                                                  .isSuccess ()));
}
 
Example 25
Source Project: ph-css   Source File: Issue26Test.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testIssue ()
{
  final IReadableResource aRes = new ClassPathResource ("testfiles/css30/good/issue26.css");
  assertTrue (aRes.exists ());
  final CascadingStyleSheet aCSS = CSSReader.readFromStream (aRes,
                                                             new CSSReaderSettings ().setFallbackCharset (StandardCharsets.UTF_8)
                                                                                     .setBrowserCompliantMode (true)
                                                                                     .setCustomErrorHandler (new LoggingCSSParseErrorHandler ()));
  assertNotNull (aCSS);
  if (false)
    System.out.println (new CSSWriter ().getCSSAsString (aCSS));
}
 
Example 26
Source Project: ph-commons   Source File: MicroReaderTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testReadProcessingInstruction ()
{
  // Read file with processing instruction
  final IMicroDocument doc = MicroReader.readMicroXML (new ClassPathResource ("xml/xml-processing-instruction.xml"));
  assertNotNull (doc);

  // Write again
  assertNotNull (MicroWriter.getNodeAsString (doc));
}
 
Example 27
Source Project: ph-commons   Source File: MicroReaderTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testReadNotation ()
{
  // Read file with notation
  final IMicroDocument doc = MicroReader.readMicroXML (new ClassPathResource ("xml/xml-notation.xml"));
  assertNotNull (doc);

  // Write again
  assertNotNull (MicroWriter.getNodeAsString (doc));
}
 
Example 28
Source Project: ph-commons   Source File: XMLListHandlerTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testRead ()
{
  ICommonsList <String> aList = new CommonsArrayList <> ();
  final IReadableResource aRes = new ClassPathResource ("xml/list.xml");
  XMLListHandler.readList (aRes, aList);
  assertEquals (3, aList.size ());
  assertTrue (aList.contains ("item1"));

  assertNotNull (XMLListHandler.readList (aRes));
  assertNull (XMLListHandler.readList (new ClassPathResource ("test1.txt")));
  assertEquals (3, XMLListHandler.readList (aRes).size ());
  assertTrue (XMLListHandler.readList (aRes).contains ("item1"));

  assertTrue (XMLListHandler.writeList (aList, new NonBlockingByteArrayOutputStream ()).isSuccess ());

  final String sXML = "<root><item/><item value='a' /><item value='a' /></root>";
  aList = XMLListHandler.readList (new StringInputStreamProvider (sXML, StandardCharsets.ISO_8859_1));
  assertNotNull (aList);
  assertEquals (2, aList.size ());
  assertEquals ("a", aList.get (0));
  assertEquals ("a", aList.get (1));

  final ICommonsSet <String> aSet = new CommonsHashSet <> ();
  XMLListHandler.readList (new StringInputStreamProvider (sXML, StandardCharsets.ISO_8859_1), aSet);
  assertEquals (1, aSet.size ());
  assertTrue (aSet.contains ("a"));

  assertTrue (XMLListHandler.writeList (aList, new NonBlockingByteArrayOutputStream ()).isSuccess ());
  assertTrue (XMLListHandler.writeList (aList, new ByteArrayOutputStreamProvider ()).isSuccess ());
}
 
Example 29
Source Project: ph-commons   Source File: CachingSAXInputSourceTest.java    License: Apache License 2.0 5 votes vote down vote up
@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 30
Source Project: ph-commons   Source File: CollectingSAXErrorHandlerTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testAll ()
{
  CollectingSAXErrorHandler aCEH = new CollectingSAXErrorHandler ();
  assertNotNull (DOMReader.readXMLDOM (new ClassPathResource ("xml/buildinfo.xml"),
                                       new DOMReaderSettings ().setErrorHandler (aCEH)));
  assertTrue (aCEH.getErrorList ().isEmpty ());
  assertNotNull (aCEH.toString ());

  aCEH = new CollectingSAXErrorHandler ();
  assertNull (DOMReader.readXMLDOM (new ClassPathResource ("test1.txt"),
                                    new DOMReaderSettings ().setErrorHandler (aCEH)));
  assertFalse (aCEH.getErrorList ().isEmpty ());
}