Java Code Examples for com.helger.xml.serialize.write.XMLWriterSettings#setIndent()

The following examples show how to use com.helger.xml.serialize.write.XMLWriterSettings#setIndent() . 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: MicroWriterTest.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
private static void _testGetNodeAsXHTMLString (final IMicroNode aNode)
{
  // try all permutations
  final XMLWriterSettings aSettings = XMLWriterSettings.createForXHTML ();
  for (int nCharSet = 0; nCharSet < 2; ++nCharSet)
  {
    aSettings.setCharset (nCharSet == 1 ? StandardCharsets.ISO_8859_1 : StandardCharsets.UTF_8);
    for (final EXMLSerializeIndent eIndent : EXMLSerializeIndent.values ())
    {
      aSettings.setIndent (eIndent);
      for (final EXMLSerializeDocType eDocType : EXMLSerializeDocType.values ())
      {
        aSettings.setSerializeDocType (eDocType);
        assertNotNull (MicroWriter.getNodeAsString (aNode, aSettings));
      }
    }
  }
}
 
Example 2
Source File: MicroWriterTest.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
private static void _testGetNodeAsXMLString (final IMicroNode aNode)
{
  // try all permutations
  final XMLWriterSettings aSettings = new XMLWriterSettings ();
  for (int nCharSet = 0; nCharSet < 2; ++nCharSet)
  {
    aSettings.setCharset (nCharSet == 1 ? StandardCharsets.ISO_8859_1 : StandardCharsets.UTF_8);
    for (final EXMLSerializeIndent eIndent : EXMLSerializeIndent.values ())
    {
      aSettings.setIndent (eIndent);
      for (final EXMLSerializeDocType eDocType : EXMLSerializeDocType.values ())
      {
        aSettings.setSerializeDocType (eDocType);
        assertNotNull (MicroWriter.getNodeAsString (aNode, aSettings));
      }
    }
  }
}
 
Example 3
Source File: MicroReaderTest.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
/**
 * Test: use namespaces all over the place and mix them quite complex
 */
@Test
public void testNamespaces ()
{
  final XMLWriterSettings xs = new XMLWriterSettings ();
  xs.setIndent (EXMLSerializeIndent.NONE);

  final String s = "<?xml version=\"1.0\"?>" +
                   "<verrryoot>" +
                   "<root xmlns=\"myuri\" xmlns:a='foo'>" +
                   "<child xmlns=\"\">" +
                   "<a:child2>Value text - no entities!</a:child2>" +
                   "</child>" +
                   "</root>" +
                   "</verrryoot>";
  final IMicroDocument aDoc = MicroReader.readMicroXML (s);
  assertNotNull (aDoc);

  final NonBlockingByteArrayOutputStream baos = new NonBlockingByteArrayOutputStream ();
  new MicroSerializer (xs).write (aDoc, baos);
  final String sXML = baos.getAsString (StandardCharsets.UTF_8);
  assertEquals ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
                "<verrryoot>" +
                "<root xmlns=\"myuri\">" +
                "<ns0:child xmlns:ns0=\"\">" +
                "<ns1:child2 xmlns:ns1=\"foo\">Value text - no entities!</ns1:child2>" +
                "</ns0:child>" +
                "</root>" +
                "</verrryoot>",
                sXML);
}
 
Example 4
Source File: MicroReaderTest.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
/**
 * Test: Use 2 different namespaces and use them both more than once
 */
@Test
public void testNamespaces2 ()
{
  final XMLWriterSettings xs = new XMLWriterSettings ();
  xs.setIndent (EXMLSerializeIndent.NONE);

  final String s = "<?xml version=\"1.0\"?>" +
                   "<verrryoot xmlns='uri1'>" +
                   "<root>" +
                   "<child xmlns='uri2'>" +
                   "<child2>Value text - no entities!</child2>" +
                   "</child>" +
                   "</root>" +
                   "</verrryoot>";
  final IMicroDocument aDoc = MicroReader.readMicroXML (s);
  assertNotNull (aDoc);

  final NonBlockingByteArrayOutputStream baos = new NonBlockingByteArrayOutputStream ();
  new MicroSerializer (xs).write (aDoc, baos);
  final String sXML = baos.getAsString (StandardCharsets.UTF_8);
  assertEquals ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
                "<verrryoot xmlns=\"uri1\">" +
                "<root>" +
                "<ns0:child xmlns:ns0=\"uri2\">" +
                "<ns0:child2>Value text - no entities!</ns0:child2>" +
                "</ns0:child>" +
                "</root>" +
                "</verrryoot>",
                sXML);
}
 
Example 5
Source File: MicroReaderTest.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
/**
 * Test: declare all namespaces in the root element and use them in nested
 * elements
 */
@Test
public void testNamespaces3 ()
{
  final XMLWriterSettings xs = new XMLWriterSettings ();
  xs.setIndent (EXMLSerializeIndent.NONE);
  xs.setUseDoubleQuotesForAttributes (false);

  final String s = "<?xml version=\"1.0\"?>" +
                   "<verrryoot xmlns='uri1' xmlns:a='uri2'>" +
                   "<root>" +
                   "<a:child>" +
                   "<a:child2>Value text - no entities!</a:child2>" +
                   "</a:child>" +
                   "</root>" +
                   "</verrryoot>";
  final IMicroDocument aDoc = MicroReader.readMicroXML (s);
  assertNotNull (aDoc);

  final NonBlockingByteArrayOutputStream baos = new NonBlockingByteArrayOutputStream ();
  new MicroSerializer (xs).write (aDoc, baos);
  final String sXML = baos.getAsString (StandardCharsets.UTF_8);
  assertEquals ("<?xml version='1.0' encoding='UTF-8'?>" +
                "<verrryoot xmlns='uri1'>" +
                "<root>" +
                "<ns0:child xmlns:ns0='uri2'>" +
                "<ns0:child2>Value text - no entities!</ns0:child2>" +
                "</ns0:child>" +
                "</root>" +
                "</verrryoot>",
                sXML);
}
 
Example 6
Source File: MicroReaderTest.java    From ph-commons with Apache License 2.0 4 votes vote down vote up
/**
 * Test: same as namespace3 test but with a namespace context map
 */
@Test
public void testNamespaces3a ()
{
  final XMLWriterSettings xs = new XMLWriterSettings ();
  xs.setIndent (EXMLSerializeIndent.NONE);
  xs.setUseDoubleQuotesForAttributes (false);
  xs.setNamespaceContext (new MapBasedNamespaceContext ().addMapping ("a1", "uri1").addMapping ("a2", "uri2"));

  final String s = "<?xml version=\"1.0\"?>" +
                   "<verrryoot xmlns='uri1' xmlns:a='uri2'>" +
                   "<root>" +
                   "<a:child>" +
                   "<a:child2>Value text - no entities!</a:child2>" +
                   "</a:child>" +
                   "</root>" +
                   "</verrryoot>";
  final IMicroDocument aDoc = MicroReader.readMicroXML (s);
  assertNotNull (aDoc);

  String sXML = MicroWriter.getNodeAsString (aDoc, xs);
  assertEquals ("<?xml version='1.0' encoding='UTF-8'?>" +
                "<a1:verrryoot xmlns:a1='uri1'>" +
                "<a1:root>" +
                "<a2:child xmlns:a2='uri2'>" +
                "<a2:child2>Value text - no entities!</a2:child2>" +
                "</a2:child>" +
                "</a1:root>" +
                "</a1:verrryoot>",
                sXML);

  xs.setPutNamespaceContextPrefixesInRoot (true);
  sXML = MicroWriter.getNodeAsString (aDoc, xs);
  assertEquals ("<?xml version='1.0' encoding='UTF-8'?>" +
                "<a1:verrryoot xmlns:a1='uri1' xmlns:a2='uri2'>" +
                "<a1:root>" +
                "<a2:child>" +
                "<a2:child2>Value text - no entities!</a2:child2>" +
                "</a2:child>" +
                "</a1:root>" +
                "</a1:verrryoot>",
                sXML);
}