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

The following examples show how to use com.helger.xml.serialize.write.XMLWriterSettings#setPutNamespaceContextPrefixesInRoot() . 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 4 votes vote down vote up
@Test
public void testWithNamespaceContext ()
{
  final XMLWriterSettings aSettings = new XMLWriterSettings ().setIndent (EXMLSerializeIndent.NONE)
                                                              .setCharset (StandardCharsets.ISO_8859_1);
  final IMicroDocument aDoc = new MicroDocument ();
  final IMicroElement eRoot = aDoc.appendElement ("ns1url", "root");
  eRoot.appendElement ("ns2url", "child1");
  eRoot.appendElement ("ns2url", "child2").setAttribute ("attr1", "a");
  eRoot.appendElement ("ns3url", "child3").setAttribute ("ns3url", "attr1", "a");
  eRoot.appendElement ("ns3url", "child4").setAttribute ("ns4url", "attr1", "a");

  String s = MicroWriter.getNodeAsString (aDoc, aSettings);
  assertEquals ("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>" +
                "<root xmlns=\"ns1url\">" +
                "<ns0:child1 xmlns:ns0=\"ns2url\" />" +
                "<ns0:child2 xmlns:ns0=\"ns2url\" attr1=\"a\" />" +
                "<ns0:child3 xmlns:ns0=\"ns3url\" ns0:attr1=\"a\" />" +
                "<ns0:child4 xmlns:ns0=\"ns3url\" xmlns:ns1=\"ns4url\" ns1:attr1=\"a\" />" +
                "</root>",
                s);

  final MapBasedNamespaceContext aCtx = new MapBasedNamespaceContext ();
  aCtx.addMapping ("a", "ns1url");
  aSettings.setNamespaceContext (aCtx);
  s = MicroWriter.getNodeAsString (aDoc, aSettings);
  assertEquals ("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>" +
                "<a:root xmlns:a=\"ns1url\">" +
                "<ns0:child1 xmlns:ns0=\"ns2url\" />" +
                "<ns0:child2 xmlns:ns0=\"ns2url\" attr1=\"a\" />" +
                "<ns0:child3 xmlns:ns0=\"ns3url\" ns0:attr1=\"a\" />" +
                "<ns0:child4 xmlns:ns0=\"ns3url\" xmlns:ns1=\"ns4url\" ns1:attr1=\"a\" />" +
                "</a:root>",
                s);

  // Add mapping to namespace context
  aCtx.addMapping ("xy", "ns2url");
  s = MicroWriter.getNodeAsString (aDoc, aSettings);
  assertEquals ("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>" +
                "<a:root xmlns:a=\"ns1url\">" +
                "<xy:child1 xmlns:xy=\"ns2url\" />" +
                "<xy:child2 xmlns:xy=\"ns2url\" attr1=\"a\" />" +
                "<ns0:child3 xmlns:ns0=\"ns3url\" ns0:attr1=\"a\" />" +
                "<ns0:child4 xmlns:ns0=\"ns3url\" xmlns:ns1=\"ns4url\" ns1:attr1=\"a\" />" +
                "</a:root>",
                s);

  // Put namespace context mappings in root
  aSettings.setPutNamespaceContextPrefixesInRoot (true);
  s = MicroWriter.getNodeAsString (aDoc, aSettings);
  assertEquals ("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>" +
                "<a:root xmlns:a=\"ns1url\" xmlns:xy=\"ns2url\">" +
                "<xy:child1 />" +
                "<xy:child2 attr1=\"a\" />" +
                "<ns0:child3 xmlns:ns0=\"ns3url\" ns0:attr1=\"a\" />" +
                "<ns0:child4 xmlns:ns0=\"ns3url\" xmlns:ns1=\"ns4url\" ns1:attr1=\"a\" />" +
                "</a:root>",
                s);

  eRoot.appendElement ("ns3url", "zz");
  s = MicroWriter.getNodeAsString (aDoc, aSettings);
  assertEquals ("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>" +
                "<a:root xmlns:a=\"ns1url\" xmlns:xy=\"ns2url\">" +
                "<xy:child1 />" +
                "<xy:child2 attr1=\"a\" />" +
                "<ns0:child3 xmlns:ns0=\"ns3url\" ns0:attr1=\"a\" />" +
                "<ns0:child4 xmlns:ns0=\"ns3url\" xmlns:ns1=\"ns4url\" ns1:attr1=\"a\" />" +
                "<ns0:zz xmlns:ns0=\"ns3url\" />" +
                "</a:root>",
                s);
}
 
Example 2
Source File: MicroWriterTest.java    From ph-commons with Apache License 2.0 4 votes vote down vote up
@Test
public void testWithoutEmitNamespaces ()
{
  final XMLWriterSettings aSettings = new XMLWriterSettings ().setIndent (EXMLSerializeIndent.NONE)
                                                              .setCharset (StandardCharsets.ISO_8859_1);
  final IMicroDocument aDoc = new MicroDocument ();
  final IMicroElement eRoot = aDoc.appendElement ("ns1url", "root");
  eRoot.appendElement ("ns2url", "child1");
  eRoot.appendElement ("ns2url", "child2").setAttribute ("attr1", "a");
  eRoot.appendElement ("ns3url", "child3").setAttribute ("ns3url", "attr1", "a");
  eRoot.appendElement ("ns3url", "child4").setAttribute ("ns4url", "attr1", "a");

  String s = MicroWriter.getNodeAsString (aDoc, aSettings);
  assertEquals ("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>" +
                "<root xmlns=\"ns1url\">" +
                "<ns0:child1 xmlns:ns0=\"ns2url\" />" +
                "<ns0:child2 xmlns:ns0=\"ns2url\" attr1=\"a\" />" +
                "<ns0:child3 xmlns:ns0=\"ns3url\" ns0:attr1=\"a\" />" +
                "<ns0:child4 xmlns:ns0=\"ns3url\" xmlns:ns1=\"ns4url\" ns1:attr1=\"a\" />" +
                "</root>",
                s);

  aSettings.setPutNamespaceContextPrefixesInRoot (true);
  s = MicroWriter.getNodeAsString (aDoc, aSettings);
  assertEquals ("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>" +
                "<root xmlns=\"ns1url\">" +
                "<ns0:child1 xmlns:ns0=\"ns2url\" />" +
                "<ns0:child2 xmlns:ns0=\"ns2url\" attr1=\"a\" />" +
                "<ns0:child3 xmlns:ns0=\"ns3url\" ns0:attr1=\"a\" />" +
                "<ns0:child4 xmlns:ns0=\"ns3url\" xmlns:ns1=\"ns4url\" ns1:attr1=\"a\" />" +
                "</root>",
                s);

  aSettings.setPutNamespaceContextPrefixesInRoot (false);
  aSettings.setEmitNamespaces (false);
  s = MicroWriter.getNodeAsString (aDoc, aSettings);
  assertEquals ("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>" +
                "<root>" +
                "<child1 />" +
                "<child2 attr1=\"a\" />" +
                "<child3 attr1=\"a\" />" +
                "<child4 attr1=\"a\" />" +
                "</root>",
                s);

  aSettings.setPutNamespaceContextPrefixesInRoot (true);
  s = MicroWriter.getNodeAsString (aDoc, aSettings);
  assertEquals ("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>" +
                "<root>" +
                "<child1 />" +
                "<child2 attr1=\"a\" />" +
                "<child3 attr1=\"a\" />" +
                "<child4 attr1=\"a\" />" +
                "</root>",
                s);
}
 
Example 3
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);
}