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

The following examples show how to use com.helger.xml.microdom.IMicroElement#appendText() . 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: PSDir.java    From ph-schematron with Apache License 2.0 6 votes vote down vote up
@Nonnull
public IMicroElement getAsMicroElement ()
{
  final IMicroElement ret = new MicroElement (CSchematron.NAMESPACE_SCHEMATRON, CSchematronXML.ELEMENT_DIR);
  if (m_eValue != null)
    ret.setAttribute (CSchematronXML.ATTR_VALUE, m_eValue.getID ());
  for (final Object aContent : m_aContent)
    if (aContent instanceof IMicroElement)
      ret.appendChild (((IMicroElement) aContent).getClone ());
    else
      ret.appendText ((String) aContent);
  if (m_aForeignAttrs != null)
    for (final Map.Entry <String, String> aEntry : m_aForeignAttrs.entrySet ())
      ret.setAttribute (aEntry.getKey (), aEntry.getValue ());
  return ret;
}
 
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: PSDiagnostic.java    From ph-schematron with Apache License 2.0 6 votes vote down vote up
@Nonnull
public IMicroElement getAsMicroElement ()
{
  final IMicroElement ret = new MicroElement (CSchematron.NAMESPACE_SCHEMATRON, CSchematronXML.ELEMENT_DIAGNOSTIC);
  ret.setAttribute (CSchematronXML.ATTR_ID, m_sID);
  if (m_aRich != null)
    m_aRich.fillMicroElement (ret);
  for (final Object aContent : m_aContent)
    if (aContent instanceof IMicroElement)
      ret.appendChild (((IMicroElement) aContent).getClone ());
    else
      if (aContent instanceof String)
        ret.appendText ((String) aContent);
      else
        ret.appendChild (((IPSElement) aContent).getAsMicroElement ());
  if (m_aForeignAttrs != null)
    for (final Map.Entry <String, String> aEntry : m_aForeignAttrs.entrySet ())
      ret.setAttribute (aEntry.getKey (), aEntry.getValue ());
  return ret;
}
 
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: 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 6
Source File: PSP.java    From ph-schematron with Apache License 2.0 6 votes vote down vote up
@Nonnull
public IMicroElement getAsMicroElement ()
{
  final IMicroElement ret = new MicroElement (CSchematron.NAMESPACE_SCHEMATRON, CSchematronXML.ELEMENT_P);
  ret.setAttribute (CSchematronXML.ATTR_ID, m_sID);
  ret.setAttribute (CSchematronXML.ATTR_CLASS, m_sClass);
  ret.setAttribute (CSchematronXML.ATTR_ICON, m_sIcon);
  for (final Object aContent : m_aContent)
    if (aContent instanceof IMicroElement)
      ret.appendChild (((IMicroElement) aContent).getClone ());
    else
      if (aContent instanceof String)
        ret.appendText ((String) aContent);
      else
        ret.appendChild (((IPSElement) aContent).getAsMicroElement ());
  if (m_aForeignAttrs != null)
    for (final Map.Entry <String, String> aEntry : m_aForeignAttrs.entrySet ())
      ret.setAttribute (aEntry.getKey (), aEntry.getValue ());
  return ret;
}
 
Example 7
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 8
Source File: MainCreateSupportedCSSPropertiesFile.java    From ph-css with Apache License 2.0 5 votes vote down vote up
private static void _boolean (@Nonnull final IMicroElement td, final boolean bSet, @Nullable final String sTitle)
{
  if (bSet)
  {
    td.setAttribute ("class", "center").appendText ("X");
    if (StringHelper.hasText (sTitle))
      td.setAttribute ("title", sTitle);
  }
  else
    td.appendText ("");
}
 
Example 9
Source File: PSEmph.java    From ph-schematron with Apache License 2.0 5 votes vote down vote up
@Nonnull
public IMicroElement getAsMicroElement ()
{
  final IMicroElement ret = new MicroElement (CSchematron.NAMESPACE_SCHEMATRON, CSchematronXML.ELEMENT_EMPH);
  for (final String sContent : m_aContent)
    ret.appendText (sContent);
  return ret;
}
 
Example 10
Source File: PSTitle.java    From ph-schematron with Apache License 2.0 5 votes vote down vote up
@Nonnull
public IMicroElement getAsMicroElement ()
{
  final IMicroElement ret = new MicroElement (CSchematron.NAMESPACE_SCHEMATRON, CSchematronXML.ELEMENT_TITLE);
  for (final Object aContent : m_aContent)
    if (aContent instanceof String)
      ret.appendText ((String) aContent);
    else
      ret.appendChild (((IPSElement) aContent).getAsMicroElement ());
  return ret;
}
 
Example 11
Source File: PSAssertReport.java    From ph-schematron with Apache License 2.0 5 votes vote down vote up
@Nonnull
public IMicroElement getAsMicroElement ()
{
  final IMicroElement ret = new MicroElement (CSchematron.NAMESPACE_SCHEMATRON,
                                              m_bIsAssert ? CSchematronXML.ELEMENT_ASSERT
                                                          : CSchematronXML.ELEMENT_REPORT);
  ret.setAttribute (CSchematronXML.ATTR_ID, m_sID);
  ret.setAttribute (CSchematronXML.ATTR_FLAG, m_sFlag);
  ret.setAttribute (CSchematronXML.ATTR_TEST, m_sTest);
  if (CollectionHelper.isNotEmpty (m_aDiagnostics))
    ret.setAttribute (CSchematronXML.ATTR_DIAGNOSTICS, StringHelper.getImploded (' ', m_aDiagnostics));
  if (m_aRich != null)
    m_aRich.fillMicroElement (ret);
  if (m_aLinkable != null)
    m_aLinkable.fillMicroElement (ret);
  for (final Object aContent : m_aContent)
    if (aContent instanceof IMicroElement)
      ret.appendChild (((IMicroElement) aContent).getClone ());
    else
      if (aContent instanceof String)
        ret.appendText ((String) aContent);
      else
        ret.appendChild (((IPSElement) aContent).getAsMicroElement ());
  if (m_aForeignAttrs != null)
    for (final Map.Entry <String, String> aEntry : m_aForeignAttrs.entrySet ())
      ret.setAttribute (aEntry.getKey (), aEntry.getValue ());
  return ret;
}
 
Example 12
Source File: PSSpan.java    From ph-schematron with Apache License 2.0 5 votes vote down vote up
@Nonnull
public IMicroElement getAsMicroElement ()
{
  final IMicroElement ret = new MicroElement (CSchematron.NAMESPACE_SCHEMATRON, CSchematronXML.ELEMENT_SPAN);
  ret.setAttribute (CSchematronXML.ATTR_CLASS, m_sClass);
  for (final Object aContent : m_aContent)
    if (aContent instanceof IMicroElement)
      ret.appendChild (((IMicroElement) aContent).getClone ());
    else
      ret.appendText ((String) aContent);
  if (m_aForeignAttrs != null)
    for (final Map.Entry <String, String> aEntry : m_aForeignAttrs.entrySet ())
      ret.setAttribute (aEntry.getKey (), aEntry.getValue ());
  return ret;
}
 
Example 13
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 14
Source File: MicroHelperTest.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetChildTextContentWithConversion ()
{
  final IMicroElement e = new MicroElement ("x");
  assertNull (MicroHelper.getChildTextContentWithConversion (e, "y", BigInteger.class));
  final IMicroElement y = e.appendElement ("y");
  assertNull (MicroHelper.getChildTextContentWithConversion (e, "y", BigInteger.class));
  y.appendText ("100");
  assertEquals (CGlobal.BIGINT_100, MicroHelper.getChildTextContentWithConversion (e, "y", BigInteger.class));
  y.appendElement ("a");
  assertEquals (CGlobal.BIGINT_100, MicroHelper.getChildTextContentWithConversion (e, "y", BigInteger.class));
  y.appendCDATA ("234");
  assertEquals (BigInteger.valueOf (100234),
                MicroHelper.getChildTextContentWithConversion (e, "y", BigInteger.class));
}
 
Example 15
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 16
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 17
Source File: StringMicroTypeConverter.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Nonnull
public IMicroElement convertToMicroElement (@Nonnull final String aObject,
                                            @Nullable final String sNamespaceURI,
                                            @Nonnull @Nonempty final String sTagName)
{
  final IMicroElement e = new MicroElement (sNamespaceURI, sTagName);
  e.appendText (aObject);
  return e;
}
 
Example 18
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 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: 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);
}