@Test public void testInsertIndentedTabsWithRoot() throws Exception { String xml = "<Configuration>\n" + "\t<Loggers>\n" + "\t\t<Root/>\n" + "\t</Loggers>\n" + "</Configuration>"; String out = insertIndented(xml, false); assertEquals( "<Configuration>\n" + "\t<Loggers>\n" + "\t\t<Root/>\n" + "\t\t<Logger/>\n" + "\t</Loggers>\n" + "</Configuration>", out); out = insertIndented(xml, true); assertEquals( "<Configuration>\n" + "\t<Loggers>\n" + "\t\t<Logger/>\n" + "\t\t<Root/>\n" + "\t</Loggers>\n" + "</Configuration>", out); }
static void insertIndented(Element loggers, Element element, boolean atBeginning) { NodeList loggerElements = loggers.getElementsByTagName("*"); if (atBeginning && loggerElements.getLength() > 0) { Node insertBefore = loggers.getFirstChild(); if (insertBefore != null) { if (insertBefore.getNodeType() == Node.TEXT_NODE) { String indent = loggers.getFirstChild().getTextContent(); Node node = loggers.getOwnerDocument().createTextNode(indent); loggers.insertBefore(node, insertBefore); } loggers.insertBefore(element, insertBefore); } else { loggers.appendChild(element); } } else { Node insertAfter = loggerElements.getLength() > 0 ? loggerElements.item(loggerElements.getLength() - 1) : null; if (insertAfter != null) { if (insertAfter.getPreviousSibling() != null && insertAfter.getPreviousSibling().getNodeType() == Node.TEXT_NODE) { String indent = insertAfter.getPreviousSibling().getTextContent(); Node node = loggers.getOwnerDocument().createTextNode(indent); if (insertAfter.getNextSibling() != null) { loggers.insertBefore(node, insertAfter.getNextSibling()); insertAfter = node; } else { loggers.appendChild(node); } } if (insertAfter.getNextSibling() != null) { loggers.insertBefore(element, insertAfter.getNextSibling()); } else { loggers.appendChild(element); } } else { if (loggers.getPreviousSibling() != null && loggers.getPreviousSibling().getNodeType() == Node.TEXT_NODE) { String indent = loggers.getPreviousSibling().getTextContent(); String prev = indent; if (indent.endsWith("\t")) { indent += "\t"; } else { int nl = indent.lastIndexOf('\n'); if (nl >= 0) { indent = indent + indent.substring(nl + 1); } else { indent += "\t"; } } if (loggers.getFirstChild() != null && loggers.getPreviousSibling().getNodeType() == Node.TEXT_NODE) { loggers.removeChild(loggers.getFirstChild()); } loggers.appendChild(loggers.getOwnerDocument().createTextNode(indent)); loggers.appendChild(element); loggers.appendChild(loggers.getOwnerDocument().createTextNode(prev)); } else { loggers.appendChild(element); } } } }
LogServiceLog4j2XmlImpl implements LogServiceInternal { static void insertIndented(Element loggers, Element element, boolean atBeginning) { NodeList loggerElements = loggers.getElementsByTagName("*"); if (atBeginning && loggerElements.getLength() > 0) { Node insertBefore = loggers.getFirstChild(); if (insertBefore != null) { if (insertBefore.getNodeType() == Node.TEXT_NODE) { String indent = loggers.getFirstChild().getTextContent(); Node node = loggers.getOwnerDocument().createTextNode(indent); loggers.insertBefore(node, insertBefore); } loggers.insertBefore(element, insertBefore); } else { loggers.appendChild(element); } } else { Node insertAfter = loggerElements.getLength() > 0 ? loggerElements.item(loggerElements.getLength() - 1) : null; if (insertAfter != null) { if (insertAfter.getPreviousSibling() != null && insertAfter.getPreviousSibling().getNodeType() == Node.TEXT_NODE) { String indent = insertAfter.getPreviousSibling().getTextContent(); Node node = loggers.getOwnerDocument().createTextNode(indent); if (insertAfter.getNextSibling() != null) { loggers.insertBefore(node, insertAfter.getNextSibling()); insertAfter = node; } else { loggers.appendChild(node); } } if (insertAfter.getNextSibling() != null) { loggers.insertBefore(element, insertAfter.getNextSibling()); } else { loggers.appendChild(element); } } else { if (loggers.getPreviousSibling() != null && loggers.getPreviousSibling().getNodeType() == Node.TEXT_NODE) { String indent = loggers.getPreviousSibling().getTextContent(); String prev = indent; if (indent.endsWith("\t")) { indent += "\t"; } else { int nl = indent.lastIndexOf('\n'); if (nl >= 0) { indent = indent + indent.substring(nl + 1); } else { indent += "\t"; } } if (loggers.getFirstChild() != null && loggers.getPreviousSibling().getNodeType() == Node.TEXT_NODE) { loggers.removeChild(loggers.getFirstChild()); } loggers.appendChild(loggers.getOwnerDocument().createTextNode(indent)); loggers.appendChild(element); loggers.appendChild(loggers.getOwnerDocument().createTextNode(prev)); } else { loggers.appendChild(element); } } } } }
LogServiceLog4j2XmlImpl implements LogServiceInternal { static void insertIndented(Element loggers, Element element, boolean atBeginning) { NodeList loggerElements = loggers.getElementsByTagName("*"); if (atBeginning && loggerElements.getLength() > 0) { Node insertBefore = loggers.getFirstChild(); if (insertBefore != null) { if (insertBefore.getNodeType() == Node.TEXT_NODE) { String indent = loggers.getFirstChild().getTextContent(); Node node = loggers.getOwnerDocument().createTextNode(indent); loggers.insertBefore(node, insertBefore); } loggers.insertBefore(element, insertBefore); } else { loggers.appendChild(element); } } else { Node insertAfter = loggerElements.getLength() > 0 ? loggerElements.item(loggerElements.getLength() - 1) : null; if (insertAfter != null) { if (insertAfter.getPreviousSibling() != null && insertAfter.getPreviousSibling().getNodeType() == Node.TEXT_NODE) { String indent = insertAfter.getPreviousSibling().getTextContent(); Node node = loggers.getOwnerDocument().createTextNode(indent); if (insertAfter.getNextSibling() != null) { loggers.insertBefore(node, insertAfter.getNextSibling()); insertAfter = node; } else { loggers.appendChild(node); } } if (insertAfter.getNextSibling() != null) { loggers.insertBefore(element, insertAfter.getNextSibling()); } else { loggers.appendChild(element); } } else { if (loggers.getPreviousSibling() != null && loggers.getPreviousSibling().getNodeType() == Node.TEXT_NODE) { String indent = loggers.getPreviousSibling().getTextContent(); String prev = indent; if (indent.endsWith("\t")) { indent += "\t"; } else { int nl = indent.lastIndexOf('\n'); if (nl >= 0) { indent = indent + indent.substring(nl + 1); } else { indent += "\t"; } } if (loggers.getFirstChild() != null && loggers.getPreviousSibling().getNodeType() == Node.TEXT_NODE) { loggers.removeChild(loggers.getFirstChild()); } loggers.appendChild(loggers.getOwnerDocument().createTextNode(indent)); loggers.appendChild(element); loggers.appendChild(loggers.getOwnerDocument().createTextNode(prev)); } else { loggers.appendChild(element); } } } } LogServiceLog4j2XmlImpl(String file); }
LogServiceLog4j2XmlImpl implements LogServiceInternal { static void insertIndented(Element loggers, Element element, boolean atBeginning) { NodeList loggerElements = loggers.getElementsByTagName("*"); if (atBeginning && loggerElements.getLength() > 0) { Node insertBefore = loggers.getFirstChild(); if (insertBefore != null) { if (insertBefore.getNodeType() == Node.TEXT_NODE) { String indent = loggers.getFirstChild().getTextContent(); Node node = loggers.getOwnerDocument().createTextNode(indent); loggers.insertBefore(node, insertBefore); } loggers.insertBefore(element, insertBefore); } else { loggers.appendChild(element); } } else { Node insertAfter = loggerElements.getLength() > 0 ? loggerElements.item(loggerElements.getLength() - 1) : null; if (insertAfter != null) { if (insertAfter.getPreviousSibling() != null && insertAfter.getPreviousSibling().getNodeType() == Node.TEXT_NODE) { String indent = insertAfter.getPreviousSibling().getTextContent(); Node node = loggers.getOwnerDocument().createTextNode(indent); if (insertAfter.getNextSibling() != null) { loggers.insertBefore(node, insertAfter.getNextSibling()); insertAfter = node; } else { loggers.appendChild(node); } } if (insertAfter.getNextSibling() != null) { loggers.insertBefore(element, insertAfter.getNextSibling()); } else { loggers.appendChild(element); } } else { if (loggers.getPreviousSibling() != null && loggers.getPreviousSibling().getNodeType() == Node.TEXT_NODE) { String indent = loggers.getPreviousSibling().getTextContent(); String prev = indent; if (indent.endsWith("\t")) { indent += "\t"; } else { int nl = indent.lastIndexOf('\n'); if (nl >= 0) { indent = indent + indent.substring(nl + 1); } else { indent += "\t"; } } if (loggers.getFirstChild() != null && loggers.getPreviousSibling().getNodeType() == Node.TEXT_NODE) { loggers.removeChild(loggers.getFirstChild()); } loggers.appendChild(loggers.getOwnerDocument().createTextNode(indent)); loggers.appendChild(element); loggers.appendChild(loggers.getOwnerDocument().createTextNode(prev)); } else { loggers.appendChild(element); } } } } LogServiceLog4j2XmlImpl(String file); Map<String, String> getLevel(String logger); void setLevel(String logger, String level); }
LogServiceLog4j2XmlImpl implements LogServiceInternal { static void insertIndented(Element loggers, Element element, boolean atBeginning) { NodeList loggerElements = loggers.getElementsByTagName("*"); if (atBeginning && loggerElements.getLength() > 0) { Node insertBefore = loggers.getFirstChild(); if (insertBefore != null) { if (insertBefore.getNodeType() == Node.TEXT_NODE) { String indent = loggers.getFirstChild().getTextContent(); Node node = loggers.getOwnerDocument().createTextNode(indent); loggers.insertBefore(node, insertBefore); } loggers.insertBefore(element, insertBefore); } else { loggers.appendChild(element); } } else { Node insertAfter = loggerElements.getLength() > 0 ? loggerElements.item(loggerElements.getLength() - 1) : null; if (insertAfter != null) { if (insertAfter.getPreviousSibling() != null && insertAfter.getPreviousSibling().getNodeType() == Node.TEXT_NODE) { String indent = insertAfter.getPreviousSibling().getTextContent(); Node node = loggers.getOwnerDocument().createTextNode(indent); if (insertAfter.getNextSibling() != null) { loggers.insertBefore(node, insertAfter.getNextSibling()); insertAfter = node; } else { loggers.appendChild(node); } } if (insertAfter.getNextSibling() != null) { loggers.insertBefore(element, insertAfter.getNextSibling()); } else { loggers.appendChild(element); } } else { if (loggers.getPreviousSibling() != null && loggers.getPreviousSibling().getNodeType() == Node.TEXT_NODE) { String indent = loggers.getPreviousSibling().getTextContent(); String prev = indent; if (indent.endsWith("\t")) { indent += "\t"; } else { int nl = indent.lastIndexOf('\n'); if (nl >= 0) { indent = indent + indent.substring(nl + 1); } else { indent += "\t"; } } if (loggers.getFirstChild() != null && loggers.getPreviousSibling().getNodeType() == Node.TEXT_NODE) { loggers.removeChild(loggers.getFirstChild()); } loggers.appendChild(loggers.getOwnerDocument().createTextNode(indent)); loggers.appendChild(element); loggers.appendChild(loggers.getOwnerDocument().createTextNode(prev)); } else { loggers.appendChild(element); } } } } LogServiceLog4j2XmlImpl(String file); Map<String, String> getLevel(String logger); void setLevel(String logger, String level);  }