PageRenderTime 44ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/payments-service/src/main/java/org/integration/payments/server/util/XmlUtil.java

https://github.com/oburakevych/Integration-Payments
Java | 142 lines | 118 code | 24 blank | 0 comment | 13 complexity | b910b2685181b3e753bc9d9d4e15c398 MD5 | raw file
  1. package org.integration.payments.server.util;
  2. import java.io.ByteArrayInputStream;
  3. import java.io.ByteArrayOutputStream;
  4. import java.io.IOException;
  5. import java.nio.charset.Charset;
  6. import javax.xml.parsers.DocumentBuilder;
  7. import javax.xml.parsers.DocumentBuilderFactory;
  8. import javax.xml.stream.XMLInputFactory;
  9. import javax.xml.stream.XMLStreamException;
  10. import javax.xml.stream.XMLStreamReader;
  11. import javax.xml.transform.OutputKeys;
  12. import javax.xml.transform.Transformer;
  13. import javax.xml.transform.TransformerFactory;
  14. import javax.xml.transform.dom.DOMSource;
  15. import javax.xml.transform.stream.StreamResult;
  16. import org.w3c.dom.Document;
  17. import org.w3c.dom.Element;
  18. import org.w3c.dom.Node;
  19. import org.w3c.dom.NodeList;
  20. import com.google.common.base.Predicate;
  21. import org.xml.sax.EntityResolver;
  22. import org.xml.sax.InputSource;
  23. import org.xml.sax.SAXException;
  24. public class XmlUtil {
  25. private static XMLInputFactory xif = XMLInputFactory.newInstance();
  26. private static final byte[] XML_DECL = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>".getBytes(Charset.forName("UTF-8"));
  27. private static DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  28. static {
  29. dbf.setNamespaceAware(true);
  30. dbf.setValidating(false);
  31. }
  32. public static boolean hasValidXmlDeclaration(byte[] doc) throws Exception {
  33. try {
  34. XMLStreamReader r = xif.createXMLStreamReader(new ByteArrayInputStream(doc), null);
  35. if (!"UTF-8".equalsIgnoreCase(r.getEncoding())) return false;
  36. if (!"1.0".equals(r.getVersion())) return false;
  37. r.close();
  38. return true;
  39. } catch (XMLStreamException e) {
  40. throw new Exception("Unable to parse xml", e);
  41. }
  42. }
  43. public static byte[] prependXmlDeclaration(byte[] doc) throws Exception {
  44. if (!hasValidXmlDeclaration(doc)) {
  45. byte[] newdoc = new byte[doc.length + XML_DECL.length];
  46. System.arraycopy(XML_DECL, 0, newdoc, 0, XML_DECL.length);
  47. System.arraycopy(doc, 0, newdoc, XML_DECL.length, doc.length);
  48. return newdoc;
  49. } else {
  50. return doc;
  51. }
  52. }
  53. public static Document parse(byte[] xml) {
  54. try {
  55. EntityResolver resolver = new EntityResolver() {
  56. @Override
  57. public InputSource resolveEntity(String s, String s1) throws SAXException, IOException {
  58. return null;
  59. }
  60. };
  61. dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
  62. DocumentBuilder documentBuilder = dbf.newDocumentBuilder();
  63. documentBuilder.setEntityResolver(resolver);
  64. Document doc = documentBuilder.parse(new ByteArrayInputStream(xml));
  65. return doc;
  66. } catch (Exception e) {
  67. throw new IllegalArgumentException("Invalid xml", e);
  68. }
  69. }
  70. public static byte[] toByteArray(Node doc) {
  71. try {
  72. Transformer transformer = TransformerFactory.newInstance().newTransformer();
  73. transformer.setOutputProperty(OutputKeys.INDENT, "yes");
  74. ByteArrayOutputStream os = new ByteArrayOutputStream();
  75. StreamResult result = new StreamResult(os);
  76. DOMSource source = new DOMSource(doc);
  77. transformer.transform(source, result);
  78. return os.toByteArray();
  79. } catch (Exception e) {
  80. throw new RuntimeException(e);
  81. }
  82. }
  83. public static Element getChildElement(Element parent, String childName, String childNamespace) {
  84. NodeList ns = parent.getChildNodes();
  85. for (int i = 0; i < ns.getLength(); i++) {
  86. Node n = ns.item(i);
  87. if (n instanceof Element) {
  88. Element child = (Element) n;
  89. if (childName.equals(child.getLocalName()) && childNamespace.equals(child.getNamespaceURI())) {
  90. return child;
  91. }
  92. }
  93. }
  94. return null;
  95. }
  96. public static Element find(Element parent, Predicate<Element> matcher) {
  97. NodeList ns = parent.getChildNodes();
  98. for (int i = 0; i < ns.getLength(); i++) {
  99. Node n = ns.item(i);
  100. if (n instanceof Element) {
  101. if (matcher.apply((Element) n)) {
  102. return (Element) n;
  103. }
  104. }
  105. }
  106. return null;
  107. }
  108. public static String getElementValue(Element parent, String childName, String childNamespace) {
  109. Element child = getChildElement(parent, childName, childNamespace);
  110. if (child == null) return null;
  111. return child.getTextContent();
  112. }
  113. public static void insertValue(Element parent, String childName, String childNamespace, String value) {
  114. Element child = parent.getOwnerDocument().createElementNS(childNamespace, childName);
  115. child.setTextContent(value);
  116. parent.appendChild(child);
  117. }
  118. }