Advertisement
RedTeaHacker

Format Raw XML to Be Pretty

Feb 24th, 2012
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.92 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.File;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileReader;
  5. import java.io.InputStreamReader;
  6. import java.io.IOException;
  7. import java.io.Reader;
  8. import java.util.regex.Matcher;
  9. import java.util.regex.Pattern;
  10.  
  11. /**
  12.  * Formats raw XML to look prettier (i.e., easier to read).
  13.  *
  14.  * @author RedTeaHacker
  15.  */
  16. public class RawXmlFormatter {
  17.   public static final Pattern PATTERN = Pattern.compile("(<!--[^(\\Q-->\\E)]*-->)|(<((\"[^\"]*\")|('[^']*')|[^\"'>])*>)|([^<]+)",Pattern.DOTALL);
  18.  
  19.   public static final Pattern COMMENT_TAG_PATTERN = Pattern.compile("<!--.+",Pattern.DOTALL);
  20.   public static final Pattern DECLARATION_TAG_PATTERN = Pattern.compile("<\\?.+",Pattern.DOTALL);
  21.   public static final Pattern END_TAG_PATTERN = Pattern.compile("<\\s*/.+",Pattern.DOTALL);
  22.   public static final Pattern START_TAG_PATTERN = Pattern.compile("<((\"[^\"]*\")|('[^']*')|[^\"'/])+",Pattern.DOTALL);
  23.  
  24.   public static final int BUFFER_SIZE = 1024;
  25.   public static final String NEWLINE = "\n";
  26.   public static final String TAB = "\t";
  27.  
  28.   public static String formatRawXml(CharSequence rawXml) {
  29.     return formatRawXml(rawXml,NEWLINE,TAB);
  30.   }
  31.  
  32.   public static String formatRawXml(CharSequence rawXml,String newline) {
  33.     return formatRawXml(rawXml,newline,TAB);
  34.   }
  35.  
  36.   public static String formatRawXml(CharSequence rawXml,String newline,String tab) {
  37.     Matcher matcher = PATTERN.matcher(rawXml);
  38.     StringBuilder result = new StringBuilder();
  39.     int tabCount = 0;
  40.  
  41.     while(matcher.find()) {
  42.       String group = matcher.group();
  43.       boolean isTag = group.length() > 0 && group.charAt(0) == '<'
  44.         && !COMMENT_TAG_PATTERN.matcher(group).matches()
  45.         && !DECLARATION_TAG_PATTERN.matcher(group).matches();
  46.  
  47.       if(isTag && tabCount > 0 && END_TAG_PATTERN.matcher(group).matches()) {
  48.         --tabCount;
  49.       }
  50.  
  51.       for(int i = 0; i < tabCount; ++i) {
  52.         result.append(tab);
  53.       }
  54.       result.append(group);
  55.       result.append(newline);
  56.  
  57.       if(isTag && START_TAG_PATTERN.matcher(group).matches()) {
  58.         ++tabCount;
  59.       }
  60.     }
  61.     return result.toString();
  62.   }
  63.  
  64.   public static StringBuilder consumeFile(File file) {
  65.     BufferedReader reader = null;
  66.     try {
  67.       reader = new BufferedReader(new FileReader(file));
  68.       return consumeStream(reader);
  69.     }
  70.     catch(FileNotFoundException ex) {
  71.       ex.printStackTrace();
  72.     }
  73.     finally {
  74.       if(reader != null) {
  75.         try { reader.close(); }
  76.         catch(IOException ex) { ex.printStackTrace(); }
  77.       }
  78.     }
  79.     return null;
  80.   }
  81.  
  82.   public static StringBuilder consumeInput() {
  83.     BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  84.     return consumeStream(reader);
  85.   }
  86.  
  87.   public static StringBuilder consumeStream(Reader reader) {
  88.     char[] buffer = new char[BUFFER_SIZE];
  89.     int readCount = 0;
  90.     StringBuilder result = new StringBuilder();
  91.     try {
  92.       while((readCount = reader.read(buffer)) >= 0) {
  93.         result.append(buffer,0,readCount);
  94.       }
  95.     }
  96.     catch(IOException ex) {
  97.       ex.printStackTrace();
  98.     }
  99.     return result;
  100.   }
  101.  
  102.   public static void main(String[] args) {
  103.     String xmlString = null;
  104.     StringBuilder xmlStringBuilder = null;
  105.  
  106.     // Piped in (e.g., echo 'example' | java RawXmlFormatter)
  107.     if(args.length < 1) {
  108.       xmlStringBuilder = consumeInput();
  109.     }
  110.     else {
  111.       // Pathname
  112.       File file = new File(args[0]);
  113.       if(file.isFile() && file.exists() && file.canRead()) {
  114.         xmlStringBuilder = consumeFile(file);
  115.       }
  116.       // String
  117.       else {
  118.         xmlString = args[0];
  119.       }
  120.     }
  121.  
  122.     if(xmlString != null) {
  123.       System.out.print(formatRawXml(xmlString));
  124.     }
  125.     else if(xmlStringBuilder != null) {
  126.       System.out.print(formatRawXml(xmlStringBuilder));
  127.     }
  128.     else {
  129.       System.out.println("No input.");
  130.     }
  131.   }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement