Java Code Examples for com.helger.xml.microdom.serialize.MicroWriter#getNodeAsString()

The following examples show how to use com.helger.xml.microdom.serialize.MicroWriter#getNodeAsString() . 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: XMLTestHelper.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
/**
 * Test if the {@link MicroTypeConverter} is OK. It converts it to XML and
 * back and than uses
 * {@link CommonsTestHelper#testDefaultImplementationWithEqualContentObject(Object, Object)}
 * to check for equality.
 *
 * @param <T>
 *        The data type to be used and returned
 * @param aObj
 *        The object to test
 * @return The object read after conversion
 */
public static <T> T testMicroTypeConversion (@Nonnull final T aObj)
{
  assertNotNull (aObj);

  // Write to XML
  final IMicroElement e = MicroTypeConverter.convertToMicroElement (aObj, "test");
  assertNotNull (e);

  // Read from XML
  final Object aObj2 = MicroTypeConverter.convertToNative (e, aObj.getClass ());
  assertNotNull (aObj2);

  // Write to XML again
  final IMicroElement e2 = MicroTypeConverter.convertToMicroElement (aObj2, "test");
  assertNotNull (e2);

  // Ensure XML representation is identical
  final String sXML1 = MicroWriter.getNodeAsString (e);
  final String sXML2 = MicroWriter.getNodeAsString (e2);
  CommonsTestHelper._assertEquals ("XML representation must be identical", sXML1, sXML2);

  // Ensure they are equals
  CommonsTestHelper.testDefaultImplementationWithEqualContentObject (aObj, aObj2);

  return GenericReflection.uncheckedCast (aObj2);
}
 
Example 2
Source File: AbstractWALDAO.java    From ph-commons with 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 3
Source File: AbstractWALDAO.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Nonnull
@OverrideOnDemand
protected String convertNativeToWALString (@Nonnull final DATATYPE aModifiedElement)
{
  final IMicroElement aElement = MicroTypeConverter.convertToMicroElement (aModifiedElement, "item");
  if (aElement == null)
    throw new IllegalStateException ("Failed to convert " +
                                     aModifiedElement +
                                     " of class " +
                                     aModifiedElement.getClass ().getName () +
                                     " to XML!");
  return MicroWriter.getNodeAsString (aElement, getWALXMLWriterSettings ());
}
 
Example 4
Source File: AbstractSimpleDAO.java    From ph-commons with 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 5
Source File: PSReaderTest.java    From ph-schematron with Apache License 2.0 5 votes vote down vote up
@Test
public void testReadAll () throws Exception
{
  for (final IReadableResource aRes : SchematronTestHelper.getAllValidSchematronFiles ())
  {
    final PSReader aReader = new PSReader (aRes);

    // Parse the schema
    final PSSchema aSchema1 = aReader.readSchema ();
    assertNotNull (aSchema1);
    final CollectingPSErrorHandler aLogger = new CollectingPSErrorHandler ();
    assertTrue (aRes.getPath (), aSchema1.isValid (aLogger));
    assertTrue (aLogger.isEmpty ());

    // Convert back to XML
    final IMicroElement e1 = aSchema1.getAsMicroElement ();
    final String sXML1 = MicroWriter.getNodeAsString (e1);

    // Re-read the created XML and re-create it
    final PSSchema aSchema2 = aReader.readSchemaFromXML (e1);
    final IMicroElement e2 = aSchema2.getAsMicroElement ();
    final String sXML2 = MicroWriter.getNodeAsString (e2);

    // Originally created XML and re-created-written XML must match
    assertEquals (sXML1, sXML2);
  }
}
 
Example 6
Source File: PSPreprocessorTest.java    From ph-schematron with Apache License 2.0 5 votes vote down vote up
@Test
public void testBasic () throws Exception
{
  final PSPreprocessor aPreprocessor = new PSPreprocessor (PSXPathQueryBinding.getInstance ());
  for (final IReadableResource aRes : SchematronTestHelper.getAllValidSchematronFiles ())
  {
    // Resolve all includes
    final IMicroDocument aDoc = SchematronHelper.getWithResolvedSchematronIncludes (aRes, false);
    assertNotNull (aDoc);

    // Read to domain object
    final PSReader aReader = new PSReader (aRes);
    final PSSchema aSchema = aReader.readSchemaFromXML (aDoc.getDocumentElement ());
    assertNotNull (aSchema);

    // Ensure the schema is valid
    final CollectingPSErrorHandler aErrHdl = new CollectingPSErrorHandler ();
    assertTrue (aRes.getPath (), aSchema.isValid (aErrHdl));
    assertTrue (aErrHdl.isEmpty ());

    // Convert to minified schema if not-yet minimal
    final PSSchema aPreprocessedSchema = aPreprocessor.getAsMinimalSchema (aSchema);
    assertNotNull (aPreprocessedSchema);

    if (false)
    {
      final String sXML = MicroWriter.getNodeAsString (aPreprocessedSchema.getAsMicroElement ());
      SimpleFileIO.writeFile (new File ("test-minified",
                                        FilenameHelper.getWithoutPath (aRes.getPath ()) + ".min-pure.sch"),
                              sXML,
                              XMLWriterSettings.DEFAULT_XML_CHARSET_OBJ);
    }

    // Ensure it is still valid and minimal
    assertTrue (aRes.getPath (), aPreprocessedSchema.isValid (aErrHdl));
    assertTrue (aRes.getPath (), aPreprocessedSchema.isMinimal ());
  }
}
 
Example 7
Source File: PSWriter.java    From ph-schematron with Apache License 2.0 3 votes vote down vote up
/**
 * Get the passed Schematron element as a String
 *
 * @param aPSElement
 *        The schematron element to convert to a string. May not be
 *        <code>null</code>.
 * @return The passed element as a string or <code>null</code> if
 *         serialization failed.
 */
@Nullable
public String getXMLString (@Nonnull final IPSElement aPSElement)
{
  ValueEnforcer.notNull (aPSElement, "PSElement");
  final IMicroElement eXML = aPSElement.getAsMicroElement ();
  return MicroWriter.getNodeAsString (getAsDocument (eXML), m_aWriterSettings.getXMLWriterSettings ());
}