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

The following examples show how to use com.helger.xml.microdom.IMicroDocument#appendElement() . 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: MainCreateJAXBBinding23.java    From ph-ubl with Apache License 2.0 6 votes vote down vote up
@Nonnull
private static IMicroDocument _createBaseDoc ()
{
  final IMicroDocument eDoc = new MicroDocument ();
  final IMicroElement eRoot = eDoc.appendElement (JAXB_NS_URI, "bindings");
  eRoot.setAttribute (XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI,
                      "schemaLocation",
                      JAXB_NS_URI + " http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd");
  // This is the JAXB version, not the UBL version :)
  eRoot.setAttribute ("version", "2.1");

  final IMicroElement eGlobal = eRoot.appendElement (JAXB_NS_URI, "globalBindings");
  eGlobal.setAttribute ("typesafeEnumMaxMembers", "2000");
  eGlobal.setAttribute ("typesafeEnumMemberName", "generateError");
  return eDoc;
}
 
Example 2
Source File: MainCreateJAXBBinding22.java    From ph-ubl with Apache License 2.0 6 votes vote down vote up
@Nonnull
private static IMicroDocument _createBaseDoc ()
{
  final IMicroDocument eDoc = new MicroDocument ();
  final IMicroElement eRoot = eDoc.appendElement (JAXB_NS_URI, "bindings");
  eRoot.setAttribute (XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI,
                      "schemaLocation",
                      JAXB_NS_URI + " http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd");
  // This is the JAXB version, not the UBL version :)
  eRoot.setAttribute ("version", "2.1");

  final IMicroElement eGlobal = eRoot.appendElement (JAXB_NS_URI, "globalBindings");
  eGlobal.setAttribute ("typesafeEnumMaxMembers", "2000");
  eGlobal.setAttribute ("typesafeEnumMemberName", "generateError");
  return eDoc;
}
 
Example 3
Source File: MicroHelperTest.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetPath ()
{
  final IMicroDocument aDoc = new MicroDocument ();
  assertEquals ("#document", MicroHelper.getPath (aDoc, "/"));
  final IMicroElement eRoot = aDoc.appendElement ("root");
  assertEquals ("#document/root", MicroHelper.getPath (eRoot, "/"));
  final IMicroElement eChild = eRoot.appendElement ("child");
  assertEquals ("#document/root/child", MicroHelper.getPath (eChild, "/"));
  assertEquals ("", MicroHelper.getPath (null, "/"));
  try
  {
    MicroHelper.getPath (eChild, null);
    fail ();
  }
  catch (final NullPointerException ex)
  {}
}
 
Example 4
Source File: MimeTypeInfoManager.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Nonnull
@ReturnsMutableCopy
public IMicroDocument getAsDocument ()
{
  final IMicroDocument aDoc = new MicroDocument ();
  final IMicroElement eRoot = aDoc.appendElement ("mime-type-info");

  m_aRWLock.readLocked ( () -> {
    for (final MimeTypeInfo aInfo : m_aList.getSorted (Comparator.comparing (MimeTypeInfo::getPrimaryMimeTypeString)))
      eRoot.appendChild (MicroTypeConverter.convertToMicroElement (aInfo, "item"));
  });

  return aDoc;
}
 
Example 5
Source File: MainCreateJAXBBinding20.java    From ph-ubl with Apache License 2.0 5 votes vote down vote up
@Nonnull
private static IMicroDocument _createBaseDoc ()
{
  final IMicroDocument eDoc = new MicroDocument ();
  final IMicroElement eRoot = eDoc.appendElement (JAXB_NS_URI, "bindings");
  eRoot.setAttribute ("xsi:schemaLocation", JAXB_NS_URI + " http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd");
  eRoot.setAttribute ("version", "2.1");

  final IMicroElement eGlobal = eRoot.appendElement (JAXB_NS_URI, "globalBindings");
  eGlobal.setAttribute ("typesafeEnumMaxMembers", "2000");
  eGlobal.setAttribute ("typesafeEnumMemberName", "generateError");
  return eDoc;
}
 
Example 6
Source File: MainCreateJAXBBinding21.java    From ph-ubl with Apache License 2.0 5 votes vote down vote up
@Nonnull
private static IMicroDocument _createBaseDoc ()
{
  final IMicroDocument eDoc = new MicroDocument ();
  final IMicroElement eRoot = eDoc.appendElement (JAXB_NS_URI, "bindings");
  eRoot.setAttribute ("xsi:schemaLocation", JAXB_NS_URI + " http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd");
  eRoot.setAttribute ("version", "2.1");

  final IMicroElement eGlobal = eRoot.appendElement (JAXB_NS_URI, "globalBindings");
  eGlobal.setAttribute ("typesafeEnumMaxMembers", "2000");
  eGlobal.setAttribute ("typesafeEnumMemberName", "generateError");
  return eDoc;
}
 
Example 7
Source File: AbstractMapBasedWALDAO.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Override
@Nonnull
@MustBeLocked (ELockType.READ)
protected IMicroDocument createWriteData ()
{
  final IMicroDocument aDoc = new MicroDocument ();
  final IMicroElement eRoot = aDoc.appendElement (ELEMENT_ROOT);
  for (final IMPLTYPE aItem : internalGetAllSortedByKey ())
    eRoot.appendChild (MicroTypeConverter.convertToMicroElement (aItem, ELEMENT_ITEM));
  return aDoc;
}
 
Example 8
Source File: ChildrenProviderElementWithNameTest.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
private static IMicroDocument _buildTestDoc ()
{
  final IMicroDocument aDoc = new MicroDocument ();
  final IMicroElement eRoot = aDoc.appendElement ("root");
  eRoot.appendElement ("any");
  eRoot.appendText ("Text");
  eRoot.appendElement ("else");
  eRoot.appendElement ("namespace", "any");
  return aDoc;
}
 
Example 9
Source File: MicroHelperTest.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetDocumentRootElementTagName ()
{
  assertNull (MicroHelper.getDocumentRootElementTagName (null));
  final IMicroDocument aDoc = new MicroDocument ();
  assertNull (MicroHelper.getDocumentRootElementTagName (aDoc));
  aDoc.appendElement ("root");
  assertEquals ("root", MicroHelper.getDocumentRootElementTagName (aDoc));
}
 
Example 10
Source File: MicroDOMInputStreamProviderTest.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Test
public void testSimple ()
{
  final IMicroDocument aDoc = new MicroDocument ();
  aDoc.appendElement ("test");
  assertNotNull (new MicroDOMInputStreamProvider (aDoc, XMLWriterSettings.DEFAULT_XML_CHARSET_OBJ).getInputStream ());
}
 
Example 11
Source File: MicroSerializerTest.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Nonnull
private IMicroDocument _createLargeDoc (@Nonnull final IMicroDocument doc,
                                        final boolean bWithText,
                                        final boolean bWithAttrs)
{
  final IMicroElement aDocElement = doc.appendElement ("root");
  for (int i = 1; i <= 10; ++i)
  {
    final IMicroElement e1 = aDocElement.appendElement ("level1");
    if (bWithAttrs)
    {
      e1.setAttribute ("a1", "Supsi1");
      e1.setAttribute ("a1a", "Supsi1a");
    }
    for (int j = 1; j <= 20; ++j)
    {
      final IMicroElement e2 = e1.appendElement ("level2");
      if (bWithAttrs)
        e2.setAttribute ("a2", "Supsi");
      for (int k = 1; k <= 100; ++k)
      {
        final IMicroElement e3 = e2.appendElement ("level3");
        if (bWithAttrs)
          e3.setAttribute ("a3", "Supsi");
        if (bWithText)
          e3.appendText ("Level 3 text <> " + Double.toString (Math.random ()));
      }
      if (bWithText)
        e2.appendText ("Level 2 text " + Double.toString (Math.random ()));
    }
    if (bWithText)
      e1.appendText ("Level 1 text " + Double.toString (Math.random ()));
  }
  return doc;
}
 
Example 12
Source File: ReadWriteXML11FuncTest.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
private static void _generateXmlFile (final String sFilename, @Nonnegative final int nElementCount)
{
  final IMicroDocument aDoc = new MicroDocument ();
  final IMicroElement eMain = aDoc.appendElement ("main_tag");
  for (int i = 0; i < nElementCount; ++i)
    eMain.appendElement ("test").appendText (StringHelper.getLeadingZero (i, 4));

  assertTrue (MicroWriter.writeToFile (aDoc, new File (sFilename), XWS_11).isSuccess ());
}
 
Example 13
Source File: XMLListHandler.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Nonnull
public static IMicroDocument createListDocument (@Nonnull final Collection <String> aCollection)
{
  ValueEnforcer.notNull (aCollection, "Collection");

  final IMicroDocument aDoc = new MicroDocument ();
  final IMicroElement eRoot = aDoc.appendElement (ELEMENT_LIST);
  for (final String sItem : aCollection)
  {
    final IMicroElement eItem = eRoot.appendElement (ELEMENT_ITEM);
    eItem.setAttribute (ATTR_VALUE, sItem);
  }
  return aDoc;
}
 
Example 14
Source File: XMLMapHandler.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Nonnull
public static IMicroDocument createMapDocument (@Nonnull final Map <String, String> aMap)
{
  ValueEnforcer.notNull (aMap, "Map");

  final IMicroDocument aDoc = new MicroDocument ();
  final IMicroElement eRoot = aDoc.appendElement (ELEMENT_MAPPING);
  for (final Map.Entry <String, String> aEntry : aMap.entrySet ())
  {
    final IMicroElement eMap = eRoot.appendElement (ELEMENT_MAP);
    eMap.setAttribute (ATTR_KEY, aEntry.getKey ());
    eMap.setAttribute (ATTR_VALUE, aEntry.getValue ());
  }
  return aDoc;
}
 
Example 15
Source File: StatisticsExporter.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Nonnull
public static IMicroDocument getAsXMLDocument ()
{
  final IMicroDocument aDoc = new MicroDocument ();
  final IMicroElement eRoot = aDoc.appendElement (ELEMENT_STATISTICS);
  StatisticsVisitor.visitStatistics (new StatisticsVisitorCallbackToXML (eRoot));
  return aDoc;
}
 
Example 16
Source File: XMLResourceBundle.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Nonnull
public static IMicroDocument getAsPropertiesXML (@Nonnull final ICommonsMap <String, String> aMap)
{
  final IMicroDocument ret = new MicroDocument ();
  final IMicroElement eRoot = ret.appendElement ("properties");
  for (final Map.Entry <String, String> aEntry : aMap.entrySet ())
    eRoot.appendElement ("entry").setAttribute ("key", aEntry.getKey ()).appendText (aEntry.getValue ());
  return ret;
}
 
Example 17
Source File: MicroSerializerTest.java    From ph-commons with Apache License 2.0 4 votes vote down vote up
@Test
public void testIndent ()
{
  final IMicroDocument aDoc = new MicroDocument ();
  final IMicroElement eHTML = aDoc.appendElement ("html");
  final IMicroElement eBody = eHTML.appendElement ("body");
  eBody.appendElement ("div");
  eBody.appendElement ("span").appendText ("bla");
  eBody.appendElement ("span").appendElement ("span").appendElement ("span").setAttribute ("a", 3).appendText ("");
  final IMicroElement eSpan3 = eBody.appendElement ("span");
  eSpan3.appendText ("f");
  eSpan3.appendText ("oo");
  eSpan3.appendElement ("strong").appendText ("bar");
  eSpan3.appendText ("baz");
  eBody.appendElement ("div");

  final String sCRLF = XMLWriterSettings.DEFAULT_XML_SETTINGS.getNewLineString ();
  final String s = MicroWriter.getNodeAsString (aDoc,
                                                new XMLWriterSettings ().setIndent (EXMLSerializeIndent.INDENT_AND_ALIGN));
  assertEquals ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
                sCRLF +
                "<html>" +
                sCRLF +
                "  <body>" +
                sCRLF +
                "    <div />" +
                sCRLF +
                "    <span>bla</span>" +
                sCRLF +
                "    <span>" +
                sCRLF +
                "      <span>" +
                sCRLF +
                "        <span a=\"3\"></span>" +
                sCRLF +
                "      </span>" +
                sCRLF +
                "    </span>" +
                sCRLF +
                "    <span>foo<strong>bar</strong>baz</span>" +
                sCRLF +
                "    <div />" +
                sCRLF +
                "  </body>" +
                sCRLF +
                "</html>" +
                sCRLF,
                s);
}
 
Example 18
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 19
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);
}