/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.sling.commons.log.logback.internal.util; import java.io.StringWriter; import javax.xml.transform.OutputKeys; import javax.xml.transform.Source; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.sax.SAXSource; import javax.xml.transform.stream.StreamResult; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.xml.sax.InputSource; public class XmlUtil { private static Logger log = LoggerFactory.getLogger(XmlUtil.class); public static String prettyPrint(InputSource is) { try { Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); // initialize StreamResult with File object to save to file StreamResult result = new StreamResult(new StringWriter()); Source source = new SAXSource(is); transformer.transform(source, result); return result.getWriter().toString(); } catch (Exception e) { // Catch generic error as panel should still work if xml apis are // not // resolved log.warn("Error occurred while transforming xml", e); } finally { Util.close(is); } return "Source not found"; } public static String escapeXml(final String input) { if (input == null) { return null; } final StringBuilder b = new StringBuilder(input.length()); for(int i = 0;i < input.length(); i++) { final char c = input.charAt(i); if (c == '&') { b.append("&"); } else if(c == '<') { b.append("<"); } else if(c == '>') { b.append(">"); } else if(c == '"') { b.append("""); } else if(c == '\'') { b.append("'"); } else { b.append(c); } } return b.toString().replaceAll("\\$", "%"); } }