com.helger.xml.serialize.read.DOMReaderSettings Java Examples

The following examples show how to use com.helger.xml.serialize.read.DOMReaderSettings. 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: XMLWriterTest.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
private static void _testC14 (final String sSrc, final String sDst)
{
  final Document aDoc = DOMReader.readXMLDOM (sSrc, new DOMReaderSettings ().setEntityResolver ( (x, y) -> {
    return "world.txt".equals (new File (y).getName ()) ? new StringSAXInputSource ("world")
                                                        : new StringSAXInputSource ("");
  }));
  assertNotNull (aDoc);

  final MapBasedNamespaceContext aCtx = new MapBasedNamespaceContext ();
  aCtx.addMapping ("a", "http://www.w3.org");
  aCtx.addMapping ("b", "http://www.ietf.org");
  final String sC14 = XMLWriter.getNodeAsString (aDoc,
                                                 XMLWriterSettings.createForCanonicalization ()
                                                                  .setIndentationString ("   ")
                                                                  .setNamespaceContext (aCtx)
                                                                  .setSerializeComments (EXMLSerializeComments.IGNORE));
  assertEquals (sDst, sC14);
}
 
Example #2
Source File: AbstractSchematronResource.java    From ph-schematron with Apache License 2.0 6 votes vote down vote up
/**
 * @return The {@link DOMReaderSettings} to be used for reading the XML files
 *         to be validated. This includes the {@link EntityResolver} to be
 *         used.
 * @see #getEntityResolver()
 */
@Nonnull
@ReturnsMutableCopy
protected DOMReaderSettings internalCreateDOMReaderSettings ()
{
  final DOMReaderSettings aDRS = new DOMReaderSettings ();
  aDRS.setFeatureValues (EXMLParserFeature.AVOID_XML_ATTACKS);
  if (m_aEntityResolver != null)
    aDRS.setEntityResolver (m_aEntityResolver);
  if (false)
  {
    final boolean m_bLoadExternalSchemas = false;
    aDRS.setFeatureValue (EXMLParserFeature.EXTERNAL_GENERAL_ENTITIES, m_bLoadExternalSchemas);
    aDRS.setFeatureValue (EXMLParserFeature.EXTERNAL_PARAMETER_ENTITIES, m_bLoadExternalSchemas);
    aDRS.setFeatureValue (EXMLParserFeature.LOAD_EXTERNAL_DTD, m_bLoadExternalSchemas);
    aDRS.setFeatureValue (EXMLParserFeature.VALIDATION, true);
    aDRS.setFeatureValue (EXMLParserFeature.NAMESPACES, true);
  }
  return aDRS;
}
 
Example #3
Source File: CollectingSAXErrorHandlerTest.java    From ph-commons with 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 ());
}
 
Example #4
Source File: AbstractSchematronResource.java    From ph-schematron with Apache License 2.0 5 votes vote down vote up
@Nullable
protected Node getAsNode (@Nonnull final Source aXMLSource) throws Exception
{
  DOMReaderSettings settings = internalCreateDOMReaderSettings ();
  settings.setFeatureValue (EXMLParserFeature.DISALLOW_DOCTYPE_DECL, false);
  // Convert to Node
  return SchematronResourceHelper.getNodeOfSource (aXMLSource, settings);
}
 
Example #5
Source File: SchematronResourcePureTest.java    From ph-schematron with Apache License 2.0 5 votes vote down vote up
@Test
public void testFunctXAreDistinctValuesWithXSD () throws Exception
{
  final String sTest = "<?xml version='1.0' encoding='iso-8859-1'?>\n" +
                       "<schema xmlns='http://purl.oclc.org/dsdl/schematron'>\n" +
                       "  <ns prefix=\"xs\" uri=\"http://www.w3.org/2001/XMLSchema\"/>\n" +
                       "  <ns prefix='fn' uri='http://www.w3.org/2005/xpath-functions' />\n" +
                       "  <ns prefix='functx' uri='http://www.functx.com' />\n" +
                       "  <pattern name='toto'>\n" +
                       "    <title>A very simple pattern with a title</title>\n" +
                       "    <rule context='chapter'>\n" +
                       "      <assert test='fn:count(fn:distinct-values(para)) = fn:count(para)'>Should have distinct values</assert>\n" +
                       "      </rule>\n" +
                       "  </pattern>\n" +
                       "</schema>";

  final MapBasedXPathFunctionResolver aFunctionResolver = new XQueryAsXPathFunctionConverter ().loadXQuery (ClassPathResource.getInputStream ("xquery/functx-1.0-nodoc-2007-01.xq"));

  final Schema aSchema = XMLSchemaCache.getInstance ()
                                       .getSchema (new ClassPathResource ("issues/20141124/chapter.xsd"));
  final Document aTestDoc = DOMReader.readXMLDOM ("<?xml version='1.0'?>" +
                                                  "<chapter>" +
                                                  " <title />" +
                                                  " <para>09</para>" +
                                                  " <para>9</para>" +
                                                  "</chapter>",
                                                  new DOMReaderSettings ().setSchema (aSchema));

  final IXPathConfig aXPathConfig = new XPathConfigBuilder ().setXPathFunctionResolver (aFunctionResolver).build ();
  final SchematronOutputType aOT = SchematronResourcePure.fromByteArray (sTest.getBytes (StandardCharsets.UTF_8))
                                                         .setXPathConfig (aXPathConfig)
                                                         .applySchematronValidationToSVRL (aTestDoc, null);
  assertNotNull (aOT);
  if (SVRLHelper.getAllFailedAssertions (aOT).isNotEmpty ())
  {
    LOGGER.info (SVRLHelper.getAllFailedAssertions (aOT).get (0).getText ());
  }
  assertTrue (SVRLHelper.getAllFailedAssertions (aOT).isEmpty ());
}
 
Example #6
Source File: UBL22InvoiceHelperTest.java    From ph-ubl with Apache License 2.0 5 votes vote down vote up
@Test
public void testComvertBackAndForth ()
{
  for (final String sFilename : MockUBL22TestDocuments.getUBL22TestDocuments (EUBL22DocumentType.INVOICE))
  {
    LOGGER.info (sFilename);

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

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

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

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

    // Validate Invoice again
    aErrors = UBL22Validator.invoice ().validate (aInvoice2);
    assertNotNull (sFilename, aErrors);
    assertFalse (sFilename, aErrors.containsAtLeastOneError ());
  }
}
 
Example #7
Source File: UBL21InvoiceHelperTest.java    From ph-ubl with Apache License 2.0 5 votes vote down vote up
@Test
public void testComvertBackAndForth ()
{
  for (final String sFilename : MockUBL21TestDocuments.getUBL21TestDocuments (EUBL21DocumentType.INVOICE))
  {
    LOGGER.info (sFilename);

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

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

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

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

    // Validate Invoice again
    aErrors = UBL21Validator.invoice ().validate (aInvoice2);
    assertNotNull (sFilename, aErrors);
    assertFalse (sFilename, aErrors.containsAtLeastOneError ());
  }
}
 
Example #8
Source File: UBL20InvoiceHelperTest.java    From ph-ubl with Apache License 2.0 5 votes vote down vote up
@Test
public void testComvertBackAndForth ()
{
  for (final String sFilename : MockUBL20TestDocuments.getUBL20TestDocuments (EUBL20DocumentType.INVOICE))
  {
    LOGGER.info (sFilename);

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

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

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

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

    // Validate Invoice again
    aErrors = UBL20Validator.invoice ().validate (aInvoice2);
    assertNotNull (sFilename, aErrors);
    assertFalse (sFilename, aErrors.containsAtLeastOneError ());
  }
}
 
Example #9
Source File: UBL23InvoiceHelperTest.java    From ph-ubl with 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 ());
  }
}