Java Code Examples for com.helger.xml.microdom.IMicroDocument#appendChild()

The following examples show how to use com.helger.xml.microdom.IMicroDocument#appendChild() . 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: SettingsPersistenceXML.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
@Nonnull
public ESuccess writeSettings (@Nonnull final ISettings aSettings, @Nonnull @WillClose final OutputStream aOS)
{
  ValueEnforcer.notNull (aSettings, "Settings");
  ValueEnforcer.notNull (aOS, "OutputStream");

  try
  {
    // Inside try so that OS is closed
    ValueEnforcer.notNull (aSettings, "Settings");

    // No event manager invocation on writing
    final SettingsMicroDocumentConverter <T> aConverter = new SettingsMicroDocumentConverter <> (m_aSettingsFactory);
    final IMicroDocument aDoc = new MicroDocument ();
    aDoc.appendChild (aConverter.convertToMicroElement (GenericReflection.uncheckedCast (aSettings),
                                                        getWriteNamespaceURI (),
                                                        getWriteElementName ()));

    // auto-closes the stream
    return MicroWriter.writeToStream (aDoc, aOS, m_aXWS);
  }
  finally
  {
    StreamHelper.close (aOS);
  }
}
 
Example 2
Source File: AbstractWALDAO.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
/**
 * Modify the created document by e.g. adding some comment or digital
 * signature or whatsoever.
 *
 * @param aDoc
 *        The created non-<code>null</code> document.
 */
@OverrideOnDemand
@MustBeLocked (ELockType.WRITE)
protected void modifyWriteData (@Nonnull final IMicroDocument aDoc)
{
  final IMicroComment aComment = new MicroComment ("This file was generated automatically - do NOT modify!\n" +
                                                   "Written at " +
                                                   PDTToString.getAsString (ZonedDateTime.now (Clock.systemUTC ()),
                                                                            Locale.US));
  final IMicroElement eRoot = aDoc.getDocumentElement ();
  // Add a small comment
  if (eRoot != null)
    aDoc.insertBefore (aComment, eRoot);
  else
    aDoc.appendChild (aComment);
}
 
Example 3
Source File: AbstractSimpleDAO.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
/**
 * Modify the created document by e.g. adding some comment or digital
 * signature or whatsoever.
 *
 * @param aDoc
 *        The created non-<code>null</code> document.
 */
@OverrideOnDemand
@MustBeLocked (ELockType.WRITE)
protected void modifyWriteData (@Nonnull final IMicroDocument aDoc)
{
  final IMicroComment aComment = new MicroComment ("This file was generated automatically - do NOT modify!\n" +
                                                   "Written at " +
                                                   PDTToString.getAsString (ZonedDateTime.now (Clock.systemUTC ()),
                                                                            Locale.US));
  final IMicroElement eRoot = aDoc.getDocumentElement ();
  // Add a small comment
  if (eRoot != null)
    aDoc.insertBefore (aComment, eRoot);
  else
    aDoc.appendChild (aComment);
}
 
Example 4
Source File: PSWriter.java    From ph-schematron with Apache License 2.0 5 votes vote down vote up
@Nonnull
@OverrideOnDemand
protected IMicroNode getAsDocument (@Nonnull final IMicroElement aElement)
{
  final IMicroDocument aDoc = new MicroDocument ();
  aDoc.appendChild (aElement);
  return aDoc;
}
 
Example 5
Source File: SchematronPreprocessMojo.java    From ph-schematron with Apache License 2.0 4 votes vote down vote up
public void execute () throws MojoExecutionException, MojoFailureException
{
  StaticLoggerBinder.getSingleton ().setMavenLog (getLog ());
  if (m_aSourceFile == null)
    throw new MojoExecutionException ("No Source file specified!");
  if (m_aSourceFile.exists () && !m_aSourceFile.isFile ())
    throw new MojoExecutionException ("The specified Source file " + m_aSourceFile + " is not a file!");

  if (m_aTargetFile == null)
    throw new MojoExecutionException ("No Target file specified!");
  if (m_aTargetFile.exists ())
  {
    if (!m_bOverwriteWithoutNotice)
    {
      // 3.1 Not overwriting the existing file
      getLog ().debug ("Skipping Target file '" + m_aTargetFile.getPath () + "' because it already exists!");
    }
    else
    {
      if (!m_aTargetFile.isFile ())
        throw new MojoExecutionException ("The specified Target file " + m_aTargetFile + " is not a file!");
    }
  }

  try
  {
    final PSSchema aSchema = new PSReader (new FileSystemResource (m_aSourceFile), null, null).readSchema ();
    final IPSQueryBinding aQueryBinding = PSQueryBindingRegistry.getQueryBindingOfNameOrThrow (aSchema.getQueryBinding ());

    final PSPreprocessor aPreprocessor = new PSPreprocessor (aQueryBinding);
    aPreprocessor.setKeepTitles (m_bKeepTitles);
    aPreprocessor.setKeepDiagnostics (m_bKeepDiagnostics);
    aPreprocessor.setKeepReports (m_bKeepReports);
    aPreprocessor.setKeepEmptyPatterns (m_bKeepEmptyPatterns);

    // Pre-process
    final PSSchema aPreprocessedSchema = aPreprocessor.getForcedPreprocessedSchema (aSchema);
    if (aPreprocessedSchema == null)
      throw new SchematronPreprocessException ("Failed to preprocess schema " +
                                               aSchema +
                                               " with query binding " +
                                               aQueryBinding);

    // Convert to XML string
    final MapBasedNamespaceContext aNSCtx = new MapBasedNamespaceContext ();
    aNSCtx.addDefaultNamespaceURI (CSchematron.NAMESPACE_SCHEMATRON);
    aNSCtx.addMapping ("xsl", CSchematron.NAMESPACE_URI_XSL);
    aNSCtx.addMapping ("svrl", CSVRL.SVRL_NAMESPACE_URI);

    // Add all <ns> elements from schema as NS context
    for (final PSNS aItem : aSchema.getAllNSs ())
      aNSCtx.setMapping (aItem.getPrefix (), aItem.getUri ());

    final IXMLWriterSettings XWS = new XMLWriterSettings ().setIndent (EXMLSerializeIndent.INDENT_AND_ALIGN)
                                                           .setNamespaceContext (aNSCtx);
    final IMicroDocument aDoc = new MicroDocument ();
    aDoc.appendChild (aPreprocessedSchema.getAsMicroElement ());
    if (MicroWriter.writeToFile (aDoc, m_aTargetFile, XWS).isSuccess ())
      getLog ().info ("Successfully wrote preprocessed Schematron file '" + m_aTargetFile.getPath () + "'");
    else
      getLog ().error ("Error writing preprocessed Schematron file to '" + m_aTargetFile.getPath () + "'");
  }
  catch (final SchematronException ex)
  {
    throw new MojoExecutionException ("Error preprocessing Schematron file '" + m_aSourceFile + "'", ex);
  }
}