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

The following examples show how to use com.helger.xml.microdom.IMicroElement#getFirstChildElement() . 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: MicroHelperTest.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
@Test
public void testConvertToMicroElementWithNS ()
{
  final String sNS = "<root xmlns='blafoo'><ns2:element xmlns:ns2='ns2:uri' ns2:attr='value'>content</ns2:element></root>";
  final Document aDoc = DOMReader.readXMLDOM (sNS);
  assertNotNull (aDoc);
  final IMicroDocument aMicroDoc = (IMicroDocument) MicroHelper.convertToMicroNode (aDoc);
  assertNotNull (aMicroDoc);
  final IMicroElement eRoot = aMicroDoc.getDocumentElement ();
  assertNotNull (eRoot);
  assertEquals ("blafoo", eRoot.getNamespaceURI ());
  assertEquals ("root", eRoot.getLocalName ());
  assertEquals ("root", eRoot.getTagName ());
  assertEquals (0, eRoot.getAttributeCount ());
  assertEquals (1, eRoot.getChildElementCount ());
  final IMicroElement eElement = eRoot.getFirstChildElement ();
  assertEquals ("ns2:uri", eElement.getNamespaceURI ());
  assertEquals ("element", eElement.getLocalName ());
  assertEquals ("element", eElement.getTagName ());
}
 
Example 2
Source File: SettingsMicroDocumentConverter.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Nonnull
public T convertToNative (final IMicroElement aElement)
{
  // Create new settings object
  final String sSettingsName = aElement.getAttributeValue (ATTR_NAME);
  if (StringHelper.hasNoText (sSettingsName))
    throw new IllegalStateException ("Settings 'name' is missing or empty");
  final T aSettings = m_aSettingFactory.apply (sSettingsName);

  // settings are only on the top-level
  for (final IMicroElement eSetting : aElement.getAllChildElements ())
  {
    final String sFieldName = eSetting.getAttributeValue (ATTR_NAME);

    final String sValue;
    if (eSetting.hasAttribute (ATTR_IS_NULL))
    {
      // Special case for null
      sValue = null;
    }
    else
    {
      // Backwards compatibility
      final IMicroElement eValue = eSetting.getFirstChildElement ("value");
      if (eValue != null)
        sValue = eValue.getTextContent ();
      else
        sValue = eSetting.getTextContent ();
    }
    // Use "put" instead of "putIn" to avoid that the callbacks are invoked!
    // Use "putIn" to ensure that the custom value modifiers are applied and
    // that it is consistent with the properties implementation
    aSettings.putIn (sFieldName, sValue);
  }
  return aSettings;
}
 
Example 3
Source File: MicroTypeConverterTreeXML.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Nullable
public DATATYPE getAsDataValue (@Nonnull final IMicroElement eDataElement)
{
  final IMicroElement eChildElement = eDataElement.getFirstChildElement ();
  if (eChildElement != null)
  {
    if (!EqualsHelper.equals (m_sNamespaceURI, eChildElement.getNamespaceURI ()))
      throw new IllegalStateException ("Namespace mismatch! Expected: " + m_sNamespaceURI);
    if (!m_sElementName.equals (eChildElement.getTagName ()))
      throw new IllegalStateException ("Tag name mismatch! Expected: " + m_sElementName);
  }
  return MicroTypeConverter.convertToNative (eChildElement, m_aNativeClass);
}
 
Example 4
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 5
Source File: MicroHelper.java    From ph-commons with Apache License 2.0 3 votes vote down vote up
/**
 * Helper method to extract the text content of the child element denoted by
 * the parameter sChildElementName of the passed parent element.
 *
 * @param eParentElement
 *        The parent element to use. May not be <code>null</code>.
 * @param sChildElementName
 *        The name of the child element who's text content is to be extracted.
 * @return <code>null</code> if the child element does not exist or the child
 *         element does not contain any text.
 */
@Nullable
public static String getChildTextContent (@Nonnull final IMicroElement eParentElement,
                                          @Nonnull final String sChildElementName)
{
  final IMicroElement eChildElement = eParentElement.getFirstChildElement (sChildElementName);
  return eChildElement != null ? eChildElement.getTextContent () : null;
}
 
Example 6
Source File: MicroHelper.java    From ph-commons with Apache License 2.0 3 votes vote down vote up
/**
 * Helper method to extract the text content of the child element denoted by
 * the parameter sChildElementName of the passed parent element. After
 * concatenation, all leading and trailing spaces are removed.
 *
 * @param eParentElement
 *        The parent element to use. May not be <code>null</code>.
 * @param sChildElementName
 *        The name of the child element who's text content is to be extracted.
 * @return <code>null</code> if the child element does not exist or the child
 *         element does not contain any text.
 */
@Nullable
public static String getChildTextContentTrimmed (@Nonnull final IMicroElement eParentElement,
                                                 @Nonnull final String sChildElementName)
{
  final IMicroElement eChildElement = eParentElement.getFirstChildElement (sChildElementName);
  return eChildElement != null ? eChildElement.getTextContentTrimmed () : null;
}
 
Example 7
Source File: MicroHelper.java    From ph-commons with Apache License 2.0 3 votes vote down vote up
/**
 * Helper method to extract the text content of the child element denoted by
 * the parameter sChildElementName of the passed parent element. The read text
 * content is converted via the
 * {@link com.helger.commons.typeconvert.TypeConverter} to the desired
 * destination type.
 *
 * @param <DSTTYPE>
 *        Destination type
 * @param eParentElement
 *        The parent element to use. May not be <code>null</code>.
 * @param sChildElementName
 *        The name of the child element who's text content is to be extracted.
 * @param aDstClass
 *        The destination class. May not be <code>null</code>.
 * @return <code>null</code> if the child element does not exist or the child
 *         element does not contain any text.
 */
@Nullable
public static <DSTTYPE> DSTTYPE getChildTextContentWithConversion (@Nonnull final IMicroElement eParentElement,
                                                                   @Nonnull final String sChildElementName,
                                                                   @Nonnull final Class <DSTTYPE> aDstClass)
{
  final IMicroElement eChildElement = eParentElement.getFirstChildElement (sChildElementName);
  return eChildElement != null ? eChildElement.getTextContentWithConversion (aDstClass) : null;
}
 
Example 8
Source File: MicroHelper.java    From ph-commons with Apache License 2.0 3 votes vote down vote up
/**
 * Helper method to extract the text content of the child element denoted by
 * the parameters sNamespaceURI and sChildElementName of the passed parent
 * element.
 *
 * @param eParentElement
 *        The parent element to use. May not be <code>null</code>.
 * @param sNamespaceURI
 *        The expected namespace URI of the element.
 * @param sChildElementName
 *        The name of the child element who's text content is to be extracted.
 * @return <code>null</code> if the child element does not exist or the child
 *         element does not contain any text.
 */
@Nullable
public static String getChildTextContent (@Nonnull final IMicroElement eParentElement,
                                          @Nonnull final String sNamespaceURI,
                                          @Nonnull final String sChildElementName)
{
  final IMicroElement eChildElement = eParentElement.getFirstChildElement (sNamespaceURI, sChildElementName);
  return eChildElement != null ? eChildElement.getTextContent () : null;
}
 
Example 9
Source File: MicroHelper.java    From ph-commons with Apache License 2.0 3 votes vote down vote up
/**
 * Helper method to extract the text content of the child element denoted by
 * the parameters sNamespaceURI and sChildElementName of the passed parent
 * element. After concatenation, all leading and trailing spaces are removed.
 *
 * @param eParentElement
 *        The parent element to use. May not be <code>null</code>.
 * @param sNamespaceURI
 *        The expected namespace URI of the element.
 * @param sChildElementName
 *        The name of the child element who's text content is to be extracted.
 * @return <code>null</code> if the child element does not exist or the child
 *         element does not contain any text.
 */
@Nullable
public static String getChildTextContentTrimmed (@Nonnull final IMicroElement eParentElement,
                                                 @Nonnull final String sNamespaceURI,
                                                 @Nonnull final String sChildElementName)
{
  final IMicroElement eChildElement = eParentElement.getFirstChildElement (sNamespaceURI, sChildElementName);
  return eChildElement != null ? eChildElement.getTextContentTrimmed () : null;
}
 
Example 10
Source File: MicroHelper.java    From ph-commons with Apache License 2.0 3 votes vote down vote up
/**
 * Helper method to extract the text content of the child element denoted by
 * the parameters sNamespaceURI and sChildElementName of the passed parent
 * element. The read text content is converted via the
 * {@link com.helger.commons.typeconvert.TypeConverter} to the desired
 * destination type.
 *
 * @param <DSTTYPE>
 *        Destination type
 * @param eParentElement
 *        The parent element to use. May not be <code>null</code>.
 * @param sNamespaceURI
 *        The expected namespace URI of the element.
 * @param sChildElementName
 *        The name of the child element who's text content is to be extracted.
 * @param aDstClass
 *        The destination class. May not be <code>null</code>.
 * @return <code>null</code> if the child element does not exist or the child
 *         element does not contain any text.
 */
@Nullable
public static <DSTTYPE> DSTTYPE getChildTextContentWithConversion (@Nonnull final IMicroElement eParentElement,
                                                                   @Nonnull final String sNamespaceURI,
                                                                   @Nonnull final String sChildElementName,
                                                                   @Nonnull final Class <DSTTYPE> aDstClass)
{
  final IMicroElement eChildElement = eParentElement.getFirstChildElement (sNamespaceURI, sChildElementName);
  return eChildElement != null ? eChildElement.getTextContentWithConversion (aDstClass) : null;
}