XmlUtil.java

  1. /**
  2.  *
  3.  * Copyright 2017-2018 Florian Schmaus.
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smack.util;

  18. import java.io.StringReader;
  19. import java.io.StringWriter;
  20. import java.util.logging.Level;
  21. import java.util.logging.Logger;

  22. import javax.xml.transform.OutputKeys;
  23. import javax.xml.transform.Transformer;
  24. import javax.xml.transform.TransformerException;
  25. import javax.xml.transform.TransformerFactory;
  26. import javax.xml.transform.stream.StreamResult;
  27. import javax.xml.transform.stream.StreamSource;

  28. public class XmlUtil {

  29.     private static final Logger LOGGER = Logger.getLogger(XmlUtil.class.getName());

  30.     private static final TransformerFactory transformerFactory = TransformerFactory.newInstance();

  31.     static {
  32.         try {
  33.             transformerFactory.setAttribute("indent-number", 2);
  34.         } catch (IllegalArgumentException e) {
  35.             LOGGER.log(Level.INFO, "XML TransformerFactory does not support indent-number attribute", e);
  36.         }
  37.     }

  38.     public static String prettyFormatXml(CharSequence xml) {
  39.         String xmlString = xml.toString();
  40.         StreamSource source = new StreamSource(new StringReader(xmlString));
  41.         StringWriter stringWriter = new StringWriter();
  42.         StreamResult result = new StreamResult(stringWriter);

  43.         try {
  44.             Transformer transformer = transformerFactory.newTransformer();
  45.             transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
  46.             transformer.setOutputProperty(OutputKeys.INDENT, "yes");
  47.             transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");

  48.             // Transform the requested string into a nice formatted XML string
  49.             transformer.transform(source, result);
  50.         }
  51.         catch (TransformerException | IllegalArgumentException e) {
  52.             LOGGER.log(Level.SEVERE, "Transformer error", e);
  53.             return xmlString;
  54.         }

  55.         return stringWriter.toString();
  56.     }

  57.     public static boolean isClarkNotation(String text) {
  58.         if (text.isEmpty()) {
  59.             return false;
  60.         }

  61.         // TODO: This is currently a mediocre heuristic to check for clark notation.
  62.         return text.charAt(0) == '{';
  63.     }
  64. }