Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.InputStreamReader;
- import java.io.IOException;
- import java.io.Reader;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- /**
- * Formats raw XML to look prettier (i.e., easier to read).
- *
- * @author RedTeaHacker
- */
- public class RawXmlFormatter {
- public static final Pattern PATTERN = Pattern.compile("(<!--[^(\\Q-->\\E)]*-->)|(<((\"[^\"]*\")|('[^']*')|[^\"'>])*>)|([^<]+)",Pattern.DOTALL);
- public static final Pattern COMMENT_TAG_PATTERN = Pattern.compile("<!--.+",Pattern.DOTALL);
- public static final Pattern DECLARATION_TAG_PATTERN = Pattern.compile("<\\?.+",Pattern.DOTALL);
- public static final Pattern END_TAG_PATTERN = Pattern.compile("<\\s*/.+",Pattern.DOTALL);
- public static final Pattern START_TAG_PATTERN = Pattern.compile("<((\"[^\"]*\")|('[^']*')|[^\"'/])+",Pattern.DOTALL);
- public static final int BUFFER_SIZE = 1024;
- public static final String NEWLINE = "\n";
- public static final String TAB = "\t";
- public static String formatRawXml(CharSequence rawXml) {
- return formatRawXml(rawXml,NEWLINE,TAB);
- }
- public static String formatRawXml(CharSequence rawXml,String newline) {
- return formatRawXml(rawXml,newline,TAB);
- }
- public static String formatRawXml(CharSequence rawXml,String newline,String tab) {
- Matcher matcher = PATTERN.matcher(rawXml);
- StringBuilder result = new StringBuilder();
- int tabCount = 0;
- while(matcher.find()) {
- String group = matcher.group();
- boolean isTag = group.length() > 0 && group.charAt(0) == '<'
- && !COMMENT_TAG_PATTERN.matcher(group).matches()
- && !DECLARATION_TAG_PATTERN.matcher(group).matches();
- if(isTag && tabCount > 0 && END_TAG_PATTERN.matcher(group).matches()) {
- --tabCount;
- }
- for(int i = 0; i < tabCount; ++i) {
- result.append(tab);
- }
- result.append(group);
- result.append(newline);
- if(isTag && START_TAG_PATTERN.matcher(group).matches()) {
- ++tabCount;
- }
- }
- return result.toString();
- }
- public static StringBuilder consumeFile(File file) {
- BufferedReader reader = null;
- try {
- reader = new BufferedReader(new FileReader(file));
- return consumeStream(reader);
- }
- catch(FileNotFoundException ex) {
- ex.printStackTrace();
- }
- finally {
- if(reader != null) {
- try { reader.close(); }
- catch(IOException ex) { ex.printStackTrace(); }
- }
- }
- return null;
- }
- public static StringBuilder consumeInput() {
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- return consumeStream(reader);
- }
- public static StringBuilder consumeStream(Reader reader) {
- char[] buffer = new char[BUFFER_SIZE];
- int readCount = 0;
- StringBuilder result = new StringBuilder();
- try {
- while((readCount = reader.read(buffer)) >= 0) {
- result.append(buffer,0,readCount);
- }
- }
- catch(IOException ex) {
- ex.printStackTrace();
- }
- return result;
- }
- public static void main(String[] args) {
- String xmlString = null;
- StringBuilder xmlStringBuilder = null;
- // Piped in (e.g., echo 'example' | java RawXmlFormatter)
- if(args.length < 1) {
- xmlStringBuilder = consumeInput();
- }
- else {
- // Pathname
- File file = new File(args[0]);
- if(file.isFile() && file.exists() && file.canRead()) {
- xmlStringBuilder = consumeFile(file);
- }
- // String
- else {
- xmlString = args[0];
- }
- }
- if(xmlString != null) {
- System.out.print(formatRawXml(xmlString));
- }
- else if(xmlStringBuilder != null) {
- System.out.print(formatRawXml(xmlStringBuilder));
- }
- else {
- System.out.println("No input.");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement