Changeset 2606


Ignore:
Timestamp:
04/12/07 12:50:22 (13 years ago)
Author:
curtis
Message:

Pretty print XML output when using showinf's -omexml option.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/loci/formats/FormatTools.java

    r2601 r2606  
    3131import java.util.Arrays; 
    3232import java.util.Hashtable; 
     33import java.util.StringTokenizer; 
    3334 
    3435/** A utility class for format reader and writer implementations. */ 
     
    570571        try { 
    571572          Method m = ms.getClass().getMethod("dumpXML", (Class[]) null); 
    572           System.out.println(m.invoke(ms, (Object[]) null)); 
    573           System.out.println(); 
     573          String xml = (String) m.invoke(ms, (Object[]) null); 
     574          System.out.println(indentXML(xml)); 
    574575        } 
    575576        catch (Throwable t) { 
     
    877878  } 
    878879 
     880  // -- Utility methods - XML -- 
     881 
     882  /** Indents XML to be more readable. */ 
     883  public static String indentXML(String xml) { return indentXML(xml, 3); } 
     884 
     885  /** Indents XML by the given spacing to be more readable. */ 
     886  public static String indentXML(String xml, int spacing) { 
     887    int indent = 0; 
     888    StringBuffer sb = new StringBuffer(); 
     889    StringTokenizer st = new StringTokenizer(xml, "<>", true); 
     890    boolean element = false; 
     891    while (st.hasMoreTokens()) { 
     892      String token = st.nextToken().trim(); 
     893      if (token.equals("")) continue; 
     894      if (token.equals("<")) { 
     895        element = true; 
     896        continue; 
     897      } 
     898      if (element && token.equals(">")) { 
     899        element = false; 
     900        continue; 
     901      } 
     902      if (element && token.startsWith("/")) indent -= spacing; 
     903      for (int j=0; j<indent; j++) sb.append(" "); 
     904      if (element) sb.append("<"); 
     905      sb.append(token); 
     906      if (element) sb.append(">"); 
     907      sb.append("\n"); 
     908      if (element && !token.startsWith("?") && 
     909        !token.startsWith("/") && !token.endsWith("/")) 
     910      { 
     911        indent += spacing; 
     912      } 
     913    } 
     914    return sb.toString(); 
     915  } 
     916 
    879917} 
  • trunk/utils/XMLIndent.java

    r1655 r2606  
    33// 
    44 
    5 import java.io.*; 
    6 import java.util.StringTokenizer; 
     5import java.io.BufferedReader; 
     6import java.io.FileReader; 
     7import loci.formats.FormatTools; 
    78 
    89/** Indents XML to be more readable. Does not handle CDATA. */ 
     
    2021      } 
    2122      in.close(); 
    22       StringTokenizer st = new StringTokenizer(sb.toString(), "<>"); 
    23       int indent = 0; 
    24       while (st.hasMoreTokens()) { 
    25         String token = st.nextToken().trim(); 
    26         if (token.equals("")) continue; 
    27         if (token.startsWith("/")) indent -= 2; 
    28         for (int j=0; j<indent; j++) System.out.print(" "); 
    29         System.out.println("<" + token + ">"); 
    30         if (!token.startsWith("/") && !token.endsWith("/")) indent += 2; 
    31       } 
    32       System.out.println(); 
     23      System.out.println(FormatTools.indentXML(sb.toString())); 
    3324    } 
    3425  } 
Note: See TracChangeset for help on using the changeset viewer.