001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 *
019 */
020package org.apache.mina.util;
021
022import java.io.IOException;
023import java.io.LineNumberReader;
024import java.io.PrintWriter;
025import java.io.StringReader;
026import java.io.StringWriter;
027import java.util.ArrayList;
028
029/**
030 * Utility class for working with xml data
031 *
032 * Implementation is heavily based on org.apache.log4j.helpers.Transform
033 * 
034 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
035 */
036public class Transform {
037
038    private static final String CDATA_START = "<![CDATA[";
039
040    private static final String CDATA_END = "]]>";
041
042    private static final String CDATA_PSEUDO_END = "]]&gt;";
043
044    private static final String CDATA_EMBEDED_END = CDATA_END + CDATA_PSEUDO_END + CDATA_START;
045
046    private static final int CDATA_END_LEN = CDATA_END.length();
047
048    /**
049     * This method takes a string which may contain HTML tags (ie,
050     * &lt;b&gt;, &lt;table&gt;, etc) and replaces any
051     * '&lt;',  '&gt;' , '&amp;' or '"'
052     * characters with respective predefined entity references.
053     *
054     * @param input The text to be converted.
055     * @return The input string with the special characters replaced.
056     * */
057    static public String escapeTags(final String input) {
058        // Check if the string is null, zero length or devoid of special characters
059        // if so, return what was sent in.
060
061        if (input == null
062                || input.length() == 0
063                || (input.indexOf('"') == -1 && input.indexOf('&') == -1 && input.indexOf('<') == -1 && input
064                        .indexOf('>') == -1)) {
065            return input;
066        }
067
068        StringBuilder buf = new StringBuilder(input.length() + 6);
069        char ch;
070
071        int len = input.length();
072        for (int i = 0; i < len; i++) {
073            ch = input.charAt(i);
074            if (ch > '>') {
075                buf.append(ch);
076            } else if (ch == '<') {
077                buf.append("&lt;");
078            } else if (ch == '>') {
079                buf.append("&gt;");
080            } else if (ch == '&') {
081                buf.append("&amp;");
082            } else if (ch == '"') {
083                buf.append("&quot;");
084            } else {
085                buf.append(ch);
086            }
087        }
088        return buf.toString();
089    }
090
091    /**
092     * Ensures that embeded CDEnd strings (]]&gt;) are handled properly
093     * within message, NDC and throwable tag text.
094     *
095     * @param buf StringBuffer holding the XML data to this point.  The
096     * initial CDStart (&lt;![CDATA[) and final CDEnd (]]&gt;) of the CDATA
097     * section are the responsibility of the calling method.
098     * @param str The String that is inserted into an existing CDATA Section within buf.
099     * */
100    static public void appendEscapingCDATA(final StringBuffer buf, final String str) {
101        if (str != null) {
102            int end = str.indexOf(CDATA_END);
103            if (end < 0) {
104                buf.append(str);
105            } else {
106                int start = 0;
107                while (end > -1) {
108                    buf.append(str.substring(start, end));
109                    buf.append(CDATA_EMBEDED_END);
110                    start = end + CDATA_END_LEN;
111                    if (start < str.length()) {
112                        end = str.indexOf(CDATA_END, start);
113                    } else {
114                        return;
115                    }
116                }
117                buf.append(str.substring(start));
118            }
119        }
120    }
121
122    /**
123     * Converts a Throwable into an array of Strings
124     * @param throwable The Throwable to convert 
125     * @return string representation of the throwable
126     */
127    public static String[] getThrowableStrRep(Throwable throwable) {
128        StringWriter sw = new StringWriter();
129        PrintWriter pw = new PrintWriter(sw);
130        throwable.printStackTrace(pw);
131        pw.flush();
132        LineNumberReader reader = new LineNumberReader(new StringReader(sw.toString()));
133        ArrayList<String> lines = new ArrayList<String>();
134        try {
135            String line = reader.readLine();
136            while (line != null) {
137                lines.add(line);
138                line = reader.readLine();
139            }
140        } catch (IOException ex) {
141            lines.add(ex.toString());
142        }
143        String[] rep = new String[lines.size()];
144        lines.toArray(rep);
145        return rep;
146    }
147
148}