Skip to content

Instantly share code, notes, and snippets.

@michaelcamilleri
Created October 10, 2011 15:08
Show Gist options
  • Save michaelcamilleri/1275558 to your computer and use it in GitHub Desktop.
Save michaelcamilleri/1275558 to your computer and use it in GitHub Desktop.
Pretty-prints XML files. Shell script to process all files in a directory included.
find ./Files -name '*.xml' -type f > files.tmp
cat files.tmp | while read line; do java XMLBeautifier "$line"; done
rm ./files.tmp
import java.io.*;
/**
* Pretty-prints XML files.
*
* @author Michael Camilleri
*/
public class XMLBeautifier {
private static final String BR = System.getProperty("line.separator");
/**
* Start here
*
* @param args XML file name
*/
public static void main(String[] args) {
String fileName;
if (args.length == 1) {
fileName = args[0];
XMLBeautifier xmlBeautifier = new XMLBeautifier();
String xml = xmlBeautifier.readFile(fileName);
if (xml != null && xmlBeautifier.isXML(xml)) {
xml = xml.replaceAll("<", BR + "<").replaceAll(">", ">" + BR).replaceAll("(?m)^\\s+", "");
xml = xmlBeautifier.indent(xml);
xmlBeautifier.saveFile(fileName, xml);
}
} else {
System.out.print("Usage: java XMLBeautifier <XML file>");
}
}
/**
* Returns a String with the contents of the file specified
*
* @param fileName The file name
* @return The contents of the file
*/
private String readFile(String fileName) {
StringBuilder fileContents = null;
BufferedReader reader = null;
try {
FileReader input = new FileReader(fileName);
reader = new BufferedReader(input);
String line;
fileContents = new StringBuilder();
while ((line = reader.readLine()) != null) {
fileContents.append(line);
}
reader.close();
} catch (FileNotFoundException e) {
System.err.println("File " + fileName + " not found");
} catch (IOException e) {
System.err.println("Error reading file " + fileName);
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException ignored) {
}
}
}
String result = null;
if (fileContents != null) {
result = fileContents.toString();
}
return result;
}
/**
* Saves the given XML text into the specified file
*
* @param fileName The name of the file to save into
* @param xml The XML to save
*/
private void saveFile(String fileName, String xml) {
BufferedWriter writer = null;
try {
FileWriter output = new FileWriter(fileName);
writer = new BufferedWriter(output);
writer.write(xml);
writer.flush();
writer.close();
} catch (IOException e) {
System.err.println("Error saving to " + fileName);
} finally {
if (writer != null) {
try {
writer.close();
} catch (IOException ignored) {
}
}
}
}
/**
* Returns <code>true</code> if the given text starts with '<' and ends with '>',
* <code>false</code> otherwise
*
* @param xml The text to test
* @return True or false depending on whether the given text is XML
*/
private boolean isXML(String xml) {
boolean isXML = false;
if (xml != null && xml.trim().startsWith("<") && xml.trim().endsWith(">")) {
isXML = true;
}
return isXML;
}
/**
* Indents the given XML
*
* @param xml Unindented XML
* @return Indented XML
*/
private String indent(String xml) {
StringBuilder result = new StringBuilder();
String[] lines = xml.split(BR);
int tabCount = -1;
for (String line : lines) {
if (line.startsWith("</")) {
tabCount--;
line = addTabs(line, tabCount);
} else if (line.startsWith("<")) {
line = addTabs(line, tabCount);
tabCount++;
} else {
line = addTabs(line, tabCount);
}
if (line.endsWith("/>")) {
tabCount--;
}
result.append(line).append(BR);
}
return result.toString();
}
/**
* Appends the specified number of tabs to the beginning of the given line
*
* @param line The line
* @param tabCount The number of tabs to add
* @return A tabbed line
*/
private String addTabs(String line, int tabCount) {
if (tabCount > 0) {
line = addTabs("\t" + line, --tabCount);
}
return line;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment