Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 Milyn - Copyright (C) 2006 - 2010
    
 This library is free software; you can redistribute it and/or
 modify it under the terms of the GNU Lesser General Public
 License (version 2.1) as published by the Free Software
 Foundation.
    
 This library is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    
 See the GNU Lesser General Public License for more details:
 http://www.gnu.org/licenses/lgpl.txt
 */

import java.util.Arrays;

public class Main {
    private static final String COMMENT_START = "<!--";
    private static final String COMMENT_END = "-->";
    private static final String CDATA_START = "<![CDATA[";
    private static final String CDATA_END = "]]>";

    /**
     * Indent the supplied XML string by the number of spaces specified in the
     * 'indent' param.
     * <p/>
     * The indents are only inserted after newlines, where the first non-whitespace character
     * is '<'.
     *
     * @param xml The XML to indent.
     * @param indent The number of spaces to insert as the indent.
     * @return The indented XML string.
     */
    public static String indent(String xml, int indent) {
        StringBuffer indentedXml = new StringBuffer();
        int xmlLen = xml.length();
        char[] indentChars = new char[indent];

        Arrays.fill(indentChars, ' ');

        int i = 0;
        while (i < xmlLen) {
            if (isStartOf(xml, i, COMMENT_START)) {
                int commentEnd = xml.indexOf(COMMENT_END, i);
                indentedXml.append(xml, i, commentEnd);
                i = commentEnd;
            } else if (isStartOf(xml, i, CDATA_START)) {
                int cdataEnd = xml.indexOf(CDATA_END, i);
                indentedXml.append(xml, i, cdataEnd);
                i = cdataEnd;
            } else {
                char nextChar = xml.charAt(i);

                indentedXml.append(nextChar);

                if (nextChar == '\n') {
                    // We're at the start of a new line.  Need to determine
                    // if the next sequence of non-whitespace characters are the start/end of
                    // an XML element.  If it is... add an indent before....
                    while (i < xmlLen) {
                        i++;

                        char preXmlChar = xml.charAt(i);
                        if (!Character.isWhitespace(preXmlChar)) {
                            if (preXmlChar == '<') {
                                if (!isStartOf(xml, i, COMMENT_START) && !isStartOf(xml, i, CDATA_START)) {
                                    indentedXml.append(indentChars);
                                }
                            }
                            break;
                        } else {
                            indentedXml.append(preXmlChar);
                        }
                    }
                } else {
                    i++;
                }
            }
        }

        return indentedXml.toString();
    }

    private static boolean isStartOf(String xml, int i, String substring) {
        return xml.regionMatches(i, substring, 0, substring.length());
    }
}