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

The following examples show how to use com.helger.xml.microdom.IMicroDocument#getDocumentElement() . 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: MicroReaderTest.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
@Test
public void testSpecialXMLAttrs ()
{
  final String s = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
                   "<root xml:lang=\"en\" xml:space=\"preserve\" xml:base=\"baseuri\" xml:id=\"4711\">" +
                   "Bla" +
                   "</root>";
  final IMicroDocument aDoc = MicroReader.readMicroXML (s);
  assertNotNull (aDoc);
  final IMicroElement eRoot = aDoc.getDocumentElement ();
  assertEquals ("en", eRoot.getAttributeValue (XMLConstants.XML_NS_URI, "lang"));
  assertNull (eRoot.getAttributeValue ("lang"));
  assertEquals ("preserve", eRoot.getAttributeValue (XMLConstants.XML_NS_URI, "space"));
  assertEquals ("baseuri", eRoot.getAttributeValue (XMLConstants.XML_NS_URI, "base"));
  assertEquals ("4711", eRoot.getAttributeValue (XMLConstants.XML_NS_URI, "id"));

  // Ensure they are written as well
  assertEquals (s, MicroWriter.getNodeAsString (aDoc, new XMLWriterSettings ().setIndent (EXMLSerializeIndent.NONE)));
}
 
Example 2
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 3
Source File: ChildrenProviderElementWithNameTest.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
@Test
public void testBasic ()
{
  final IMicroDocument aDoc = _buildTestDoc ();
  final IMicroElement aDocElement = aDoc.getDocumentElement ();

  ChildrenProviderElementWithName x = new ChildrenProviderElementWithName ("any");

  assertTrue (x.hasChildren (aDocElement));
  assertEquals (2, x.getChildCount (aDocElement));
  assertEquals (2, x.getAllChildren (aDocElement).size ());

  x = new ChildrenProviderElementWithName ("namespace", "any");

  assertTrue (x.hasChildren (aDocElement));
  assertEquals (1, x.getChildCount (aDocElement));
  assertEquals (1, x.getAllChildren (aDocElement).size ());
}
 
Example 4
Source File: AbstractWALDAO.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
/**
 * Modify the created document by e.g. adding some comment or digital
 * signature or whatsoever.
 *
 * @param aDoc
 *        The created non-<code>null</code> document.
 */
@OverrideOnDemand
@MustBeLocked (ELockType.WRITE)
protected void modifyWriteData (@Nonnull final IMicroDocument aDoc)
{
  final IMicroComment aComment = new MicroComment ("This file was generated automatically - do NOT modify!\n" +
                                                   "Written at " +
                                                   PDTToString.getAsString (ZonedDateTime.now (Clock.systemUTC ()),
                                                                            Locale.US));
  final IMicroElement eRoot = aDoc.getDocumentElement ();
  // Add a small comment
  if (eRoot != null)
    aDoc.insertBefore (aComment, eRoot);
  else
    aDoc.appendChild (aComment);
}
 
Example 5
Source File: AbstractSimpleDAO.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
/**
 * Modify the created document by e.g. adding some comment or digital
 * signature or whatsoever.
 *
 * @param aDoc
 *        The created non-<code>null</code> document.
 */
@OverrideOnDemand
@MustBeLocked (ELockType.WRITE)
protected void modifyWriteData (@Nonnull final IMicroDocument aDoc)
{
  final IMicroComment aComment = new MicroComment ("This file was generated automatically - do NOT modify!\n" +
                                                   "Written at " +
                                                   PDTToString.getAsString (ZonedDateTime.now (Clock.systemUTC ()),
                                                                            Locale.US));
  final IMicroElement eRoot = aDoc.getDocumentElement ();
  // Add a small comment
  if (eRoot != null)
    aDoc.insertBefore (aComment, eRoot);
  else
    aDoc.appendChild (aComment);
}
 
Example 6
Source File: PSReader.java    From ph-schematron with Apache License 2.0 6 votes vote down vote up
/**
 * Read the schema from the resource supplied in the constructor. First all
 * includes are resolved and than {@link #readSchemaFromXML(IMicroElement)} is
 * called.
 *
 * @return The read {@link PSSchema}.
 * @throws SchematronReadException
 *         If reading fails
 */
@Nonnull
public PSSchema readSchema () throws SchematronReadException
{
  // Resolve all includes as the first action
  final SAXReaderSettings aSettings = new SAXReaderSettings ().setEntityResolver (m_aEntityResolver);

  final IMicroDocument aDoc = SchematronHelper.getWithResolvedSchematronIncludes (m_aResource,
                                                                                  aSettings,
                                                                                  m_aErrorHandler,
                                                                                  m_bLenient);
  if (aDoc == null || aDoc.getDocumentElement () == null)
    throw new SchematronReadException (m_aResource,
                                       "Failed to resolve includes in Schematron resource " + m_aResource);

  if (SchematronDebug.isShowResolvedSourceSchematron ())
    LOGGER.info ("Resolved source Schematron:\n" + MicroWriter.getNodeAsString (aDoc));

  return readSchemaFromXML (aDoc.getDocumentElement ());
}
 
Example 7
Source File: IJAXBWriter.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
/**
 * Convert the passed object to a new micro document and return only the root
 * element (write).
 *
 * @param aObject
 *        The object to be converted. May not be <code>null</code>.
 * @return <code>null</code> if converting the document failed.
 */
@Nullable
default IMicroElement getAsMicroElement (@Nonnull final JAXBTYPE aObject)
{
  final IMicroDocument aDoc = getAsMicroDocument (aObject);
  if (aDoc == null)
    return null;

  final IMicroElement ret = aDoc.getDocumentElement ();
  // Important to detach from document - otherwise the element cannot be
  // re-added somewhere else
  ret.detachFromParent ();
  return ret;
}
 
Example 8
Source File: MicroHelper.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
/**
 * Get the tag name of the passed documents root element.
 *
 * @param aDoc
 *        The document to be evaluated. May be <code>null</code>.
 * @return <code>null</code> if the passed document was <code>null</code> or
 *         if no document element is present. The tag name otherwise.
 */
@Nullable
public static String getDocumentRootElementTagName (@Nullable final IMicroDocument aDoc)
{
  if (aDoc != null)
  {
    final IMicroElement eRoot = aDoc.getDocumentElement ();
    if (eRoot != null)
      return eRoot.getTagName ();
  }
  return null;
}
 
Example 9
Source File: AbstractWALDAO.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
/**
 * This method is used upon recovery to convert a stored object to its native
 * representation. If you overwrite this method, you should consider
 * overriding {@link #convertNativeToWALString(Serializable)} as well.
 *
 * @param sElement
 *        The string representation to be converted. Never <code>null</code>.
 * @return The native representation of the object. If the return value is
 *         <code>null</code>, the recovery will fail with an exception!
 */
@Nullable
@OverrideOnDemand
@IsLocked (ELockType.WRITE)
protected DATATYPE convertWALStringToNative (@Nonnull final String sElement)
{
  final IMicroDocument aDoc = MicroReader.readMicroXML (sElement);
  if (aDoc == null || aDoc.getDocumentElement () == null)
    return null;
  return MicroTypeConverter.convertToNative (aDoc.getDocumentElement (), m_aDataTypeClass);
}