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

The following examples show how to use com.helger.xml.microdom.IMicroElement#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: SettingsMicroDocumentConverter.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
@Nonnull
public IMicroElement convertToMicroElement (@Nonnull final T aObject,
                                            @Nullable final String sNamespaceURI,
                                            @Nonnull final String sTagName)
{
  final IMicroElement eRoot = new MicroElement (sNamespaceURI, sTagName);
  eRoot.setAttribute (ATTR_NAME, aObject.getName ());

  // Sort fields to have them deterministic
  for (final Map.Entry <String, Object> aEntry : aObject.getSortedByKey (Comparator.naturalOrder ()).entrySet ())
  {
    final String sFieldName = aEntry.getKey ();
    final Object aValue = aEntry.getValue ();

    final IMicroElement eSetting = eRoot.appendElement (sNamespaceURI, ELEMENT_SETTING);
    eSetting.setAttribute (ATTR_NAME, sFieldName);
    if (aValue == null)
      eSetting.setAttribute (ATTR_IS_NULL, true);
    else
    {
      final String sValue = TypeConverter.convert (aValue, String.class);
      eSetting.appendText (sValue);
    }
  }
  return eRoot;
}
 
Example 2
Source File: MultilingualTextMicroTypeConverterRegistrar.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
@Nonnull
public final IMicroElement convertToMicroElement (@Nonnull final T aSource,
                                                  @Nullable final String sNamespaceURI,
                                                  @Nonnull @Nonempty final String sTagName)
{
  final IMicroElement eMText = new MicroElement (sNamespaceURI, sTagName);
  for (final Map.Entry <Locale, String> aEntry : aSource.texts ()
                                                        .getSortedByKey (Comparator.comparing (Locale::toString))
                                                        .entrySet ())
  {
    final IMicroElement eText = eMText.appendElement (sNamespaceURI, ELEMENT_TEXT);
    eText.setAttribute (ATTR_LOCALE, aEntry.getKey ().toString ());
    eText.appendText (aEntry.getValue ());
  }
  return eMText;
}
 
Example 3
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 4
Source File: MicroHelperTest.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetChildTextContentWithConversionAndNS ()
{
  final String sNSURI = "my-namespace-uri";
  final IMicroElement e = new MicroElement (sNSURI, "x");
  assertNull (MicroHelper.getChildTextContentWithConversion (e, sNSURI, "y", BigInteger.class));
  final IMicroElement y = e.appendElement (sNSURI, "y");
  assertNull (MicroHelper.getChildTextContentWithConversion (e, sNSURI, "y", BigInteger.class));
  y.appendText ("100");
  assertEquals (CGlobal.BIGINT_100, MicroHelper.getChildTextContentWithConversion (e, sNSURI, "y", BigInteger.class));
  y.appendElement ("a");
  assertEquals (CGlobal.BIGINT_100, MicroHelper.getChildTextContentWithConversion (e, sNSURI, "y", BigInteger.class));
  y.appendCDATA ("234");
  assertEquals (BigInteger.valueOf (100234),
                MicroHelper.getChildTextContentWithConversion (e, sNSURI, "y", BigInteger.class));
}
 
Example 5
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 6
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 7
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 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 testGetChildTextContentWithNS ()
{
  final String sNSURI = "my-namespace-uri";
  final IMicroElement e = new MicroElement (sNSURI, "x");
  assertNull (MicroHelper.getChildTextContent (e, sNSURI, "y"));
  final IMicroElement y = e.appendElement (sNSURI, "y");
  assertNull (MicroHelper.getChildTextContent (e, sNSURI, "y"));
  y.appendText ("Text");
  assertEquals ("Text", MicroHelper.getChildTextContent (e, sNSURI, "y"));
  y.appendElement ("z1");
  assertEquals ("Text", MicroHelper.getChildTextContent (e, sNSURI, "y"));
  y.appendCDATA ("data");
  assertEquals ("Textdata", MicroHelper.getChildTextContent (e, sNSURI, "y"));
}
 
Example 10
Source File: MicroHelperTest.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetChildTextContent ()
{
  final IMicroElement e = new MicroElement ("x");
  assertNull (MicroHelper.getChildTextContent (e, "y"));
  final IMicroElement y = e.appendElement ("y");
  assertNull (MicroHelper.getChildTextContent (e, "y"));
  y.appendText ("Text");
  assertEquals ("Text", MicroHelper.getChildTextContent (e, "y"));
  y.appendElement ("z1");
  assertEquals ("Text", MicroHelper.getChildTextContent (e, "y"));
  y.appendCDATA ("data");
  assertEquals ("Textdata", MicroHelper.getChildTextContent (e, "y"));
}
 
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: 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 13
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 14
Source File: ThreadDescriptorList.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Nonnull
public IMicroElement getAsMicroNode ()
{
  final IMicroElement eRet = new MicroElement ("threadlist");
  if (StringHelper.hasText (m_sError))
    eRet.appendElement ("error").appendText (m_sError);

  // Overall thread count
  eRet.setAttribute ("threadcount", m_aList.size ());

  // Emit thread IDs grouped by state
  final ICommonsMap <State, ICommonsNavigableSet <Long>> aStateMap = _getStateMap ();
  for (final State eState : State.values ())
  {
    final ICommonsSet <Long> aThreadIDs = aStateMap.get (eState);
    final int nSize = aThreadIDs.size ();

    final IMicroElement eThreadState = eRet.appendElement ("threadstate");
    eThreadState.setAttribute ("id", eState.toString ());
    eThreadState.setAttribute ("threadcount", nSize);
    if (nSize > 0)
      eThreadState.appendText (StringHelper.getImploded (',', aThreadIDs));
  }

  // Append all stack traces at the end
  for (final ThreadDescriptor aDescriptor : m_aList)
    eRet.appendChild (aDescriptor.getAsMicroNode ());
  return eRet;
}
 
Example 15
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 16
Source File: MainCreateJAXBBinding20.java    From ph-ubl with Apache License 2.0 4 votes vote down vote up
private static void _generateExplicitEnumMapping (@Nonnull final IMicroDocument aDoc,
                                                  @Nonnull @Nonempty final String sFilename,
                                                  @Nonnull final IMicroElement eBindings)
{
  final ICommonsSet <String> aUsedNames = new CommonsHashSet <> ();
  final ICommonsNavigableMap <String, String> aValueToConstants = new CommonsTreeMap <> ();
  final IMicroElement eSimpleType = aDoc.getDocumentElement ().getFirstChildElement ();

  final IMicroElement eInnerBindings = eBindings.appendElement (JAXB_NS_URI, "bindings")
                                                .setAttribute ("node",
                                                               "xsd:simpleType[@name='" +
                                                                       eSimpleType.getAttributeValue ("name") +
                                                                       "']");
  final IMicroElement eTypesafeEnumClass = eInnerBindings.appendElement (JAXB_NS_URI, "typesafeEnumClass");

  final IMicroElement eRestriction = eSimpleType.getFirstChildElement ();
  for (final IMicroElement eEnumeration : eRestriction.getAllChildElements (XMLConstants.W3C_XML_SCHEMA_NS_URI,
                                                                            "enumeration"))
  {
    final IMicroElement eAnnotation = eEnumeration.getFirstChildElement (XMLConstants.W3C_XML_SCHEMA_NS_URI,
                                                                         "annotation");
    if (eAnnotation == null)
      throw new IllegalStateException ("annotation is missing");
    final IMicroElement eDocumentation = eAnnotation.getFirstChildElement (XMLConstants.W3C_XML_SCHEMA_NS_URI,
                                                                           "documentation");
    if (eDocumentation == null)
      throw new IllegalStateException ("documentation is missing");
    final IMicroElement eCodeName = eDocumentation.getFirstChildElement ("urn:un:unece:uncefact:documentation:2",
                                                                         "CodeName");
    if (eCodeName == null)
      throw new IllegalStateException ("CodeName is missing");

    final String sValue = eEnumeration.getAttributeValue ("value");
    // Create an upper case Java identifier, without duplicate "_"
    String sCodeName = RegExHelper.getAsIdentifier (eCodeName.getTextContent ().trim ().toUpperCase (Locale.US))
                                  .replaceAll ("_+", "_");

    if (!aUsedNames.add (sCodeName))
    {
      // Ensure uniqueness of the enum member name
      int nSuffix = 1;
      while (true)
      {
        final String sSuffixedCodeName = sCodeName + "_" + nSuffix;
        if (aUsedNames.add (sSuffixedCodeName))
        {
          sCodeName = sSuffixedCodeName;
          break;
        }
        ++nSuffix;
      }
    }

    eTypesafeEnumClass.appendElement (JAXB_NS_URI, "typesafeEnumMember")
                      .setAttribute ("value", sValue)
                      .setAttribute ("name", sCodeName);
    aValueToConstants.put (sValue, sCodeName);
  }

  // Write out the mapping file for easy later-on resolving
  XMLMapHandler.writeMap (aValueToConstants,
                          new FileSystemResource ("src/main/resources/schemas/" + sFilename + ".mapping"));
}
 
Example 17
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 18
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 19
Source File: MockHasNameConverter.java    From ph-commons with Apache License 2.0 4 votes vote down vote up
public void appendDataValue (@Nonnull final IMicroElement eDataElement, @Nullable final MockHasName aAnyName)
{
  final IMicroElement eName = eDataElement.appendElement (getNamespaceURI (), "name");
  if (aAnyName != null)
    eName.appendText (aAnyName.getName ());
}
 
Example 20
Source File: ThreadDescriptor.java    From ph-commons with Apache License 2.0 4 votes vote down vote up
@Nonnull
public IMicroElement getAsMicroNode ()
{
  final IMicroElement eRet = new MicroElement ("thread");
  eRet.setAttribute ("id", m_nID);
  eRet.setAttribute ("name", m_sName);
  if (m_eState != null)
    eRet.setAttribute ("state", m_eState.toString ());
  eRet.setAttribute ("priority", m_nPriority);
  eRet.setAttribute ("threadgroup", m_sThreadGroup);
  eRet.appendElement ("stacktrace").appendText (getStackTraceNotNull ());
  if (m_aThreadInfo != null)
  {
    final IMicroElement eThreadInfo = eRet.appendElement ("threadinfo");
    try
    {
      final MonitorInfo [] aMonitorInfos = m_aThreadInfo.getLockedMonitors ();
      if (ArrayHelper.isNotEmpty (aMonitorInfos))
      {
        final IMicroElement eMonitorInfos = eThreadInfo.appendElement ("monitorinfos");
        eMonitorInfos.setAttribute ("count", aMonitorInfos.length);
        for (final MonitorInfo aMonitorInfo : aMonitorInfos)
        {
          final IMicroElement eMonitor = eMonitorInfos.appendElement ("monitor");
          eMonitor.setAttribute ("classname", aMonitorInfo.getClassName ());
          eMonitor.setAttribute ("identity", Integer.toHexString (aMonitorInfo.getIdentityHashCode ()));
          if (aMonitorInfo.getLockedStackFrame () != null)
            eMonitor.setAttribute ("stackframe", aMonitorInfo.getLockedStackFrame ().toString ());
          if (aMonitorInfo.getLockedStackDepth () >= 0)
            eMonitor.setAttribute ("stackdepth", aMonitorInfo.getLockedStackDepth ());
        }
      }

      final LockInfo [] aSynchronizers = m_aThreadInfo.getLockedSynchronizers ();
      if (ArrayHelper.isNotEmpty (aSynchronizers))
      {
        final IMicroElement eSynchronizers = eThreadInfo.appendElement ("synchronizers");
        eSynchronizers.setAttribute ("count", aSynchronizers.length);
        for (final LockInfo aSynchronizer : aSynchronizers)
        {
          final IMicroElement eSynchronizer = eSynchronizers.appendElement ("synchronizer");
          eSynchronizer.setAttribute ("classname", aSynchronizer.getClassName ());
          eSynchronizer.setAttribute ("identity", Integer.toHexString (aSynchronizer.getIdentityHashCode ()));
        }
      }
    }
    catch (final Exception ex)
    {
      eThreadInfo.setAttribute ("error", ex.getMessage ()).appendText (StackTraceHelper.getStackAsString (ex));
    }
  }
  return eRet;
}