1 5 package org.h2.tools.doc; 6 7 import java.io.File ; 8 import java.io.FileReader ; 9 import java.io.IOException ; 10 import java.io.Reader ; 11 import java.io.StringWriter ; 12 import java.util.Stack ; 13 14 public class XMLChecker { 15 16 public static void main(String [] args) throws Exception { 17 new XMLChecker().run(args); 18 } 19 20 private void run(String [] args) throws Exception { 21 String dir = "."; 22 for(int i=0; i<args.length; i++) { 23 if("-dir".equals(args[i])) { 24 dir = args[++i]; 25 } 26 } 27 process(dir+"/src"); 28 process(dir+"/docs"); 29 } 30 31 void process(String path) throws Exception { 32 if(path.endsWith("/CVS") || path.endsWith("/.svn")) { 33 return; 34 } 35 File file = new File (path); 36 if(file.isDirectory()) { 37 String [] list = file.list(); 38 for(int i=0; i<list.length; i++) { 39 process(path + "/" + list[i]); 40 } 41 } else { 42 processFile(path); 43 } 44 } 45 46 void processFile(String fileName) throws Exception { 47 int idx = fileName.lastIndexOf('.'); 48 if(idx < 0) { 49 return; 50 } 51 String suffix = fileName.substring(idx + 1); 52 if(!suffix.equals("html") && !suffix.equals("xml") && !suffix.equals("jsp")) { 53 return; 54 } 55 System.out.println("Checking file:" + fileName); 56 FileReader reader = new FileReader (fileName); 57 String s = readStringAndClose(reader, -1); 58 Exception last = null; 59 try { 60 checkXML(s, !suffix.equals("xml")); 61 } catch(Exception e) { 62 last = e; 63 System.out.println("ERROR: " + e.toString()); 64 } 65 if(last != null) { 66 last.printStackTrace(); 67 } 68 } 69 70 public static String readStringAndClose(Reader in, int length) throws IOException { 71 if(length <= 0) { 72 length = Integer.MAX_VALUE; 73 } 74 int block = Math.min(4096, length); 75 StringWriter out=new StringWriter (length == Integer.MAX_VALUE ? block : length); 76 char[] buff=new char[block]; 77 while(length > 0) { 78 int len = Math.min(block, length); 79 len = in.read(buff, 0, len); 80 if(len < 0) { 81 break; 82 } 83 out.write(buff, 0, len); 84 length -= len; 85 } 86 in.close(); 87 return out.toString(); 88 } 89 90 private static void checkXML(String xml, boolean html) throws Exception { 91 String lastElement = null; 92 String [] noclose = new String []{ 93 "li", "link", "meta", "br", "img", "input", "hr", "frame" 94 }; 95 XMLParser parser = new XMLParser(xml); 96 Stack stack = new Stack (); 97 boolean rootElement = false; 98 while(true) { 99 int event = parser.next(); 100 if(event == XMLParser.END_DOCUMENT) { 101 break; 102 } else if(event == XMLParser.START_ELEMENT) { 103 if(stack.size() == 0) { 104 if(rootElement) { 105 throw new Exception ("Second root element at " + parser.getRemaining()); 106 } 107 rootElement = true; 108 } 109 String name = parser.getName(); 110 if(html && name.equals("table") && lastElement.trim().length() > 0) { 111 throw new Exception ("Test before table: " + lastElement); 112 } 113 for(int i=0; html && i<noclose.length; i++) { 114 if(name.equals(noclose[i])) { 115 name = null; 116 break; 117 } 118 } 119 if(name != null) { 120 stack.add(name); 121 } 122 } else if(event == XMLParser.END_ELEMENT) { 123 String name = parser.getName(); 124 for(int i=0; html && i<noclose.length; i++) { 125 if(name.equals(noclose[i])) { 126 throw new Exception ("Unnecessary closing element " + name + " at " + parser.getRemaining()); 127 } 128 } 129 while(true) { 130 String pop = (String ) stack.pop(); 131 if(pop.equals(name)) { 132 break; 133 } 134 throw new Exception ("Unclosed element " + pop + " at " + parser.getRemaining()); 135 } 136 } else if(event == XMLParser.CHARACTERS) { 137 lastElement = parser.getText(); 138 } else if(event == XMLParser.DTD) { 139 } else if(event == XMLParser.COMMENT) { 140 } else { 141 int eventType = parser.getEventType(); 142 throw new Exception ("Unexpected event " + eventType + " at " + parser.getRemaining()); 143 } 144 } 145 if(stack.size() != 0) { 146 throw new Exception ("Unclosed root element"); 147 } 148 } 149 150 } 151 | Popular Tags |