1 16 17 package socket; 18 19 import java.io.FileInputStream ; 20 import java.io.FilterInputStream ; 21 import java.io.IOException ; 22 import java.io.InputStream ; 23 import java.io.PrintWriter ; 24 import java.util.Random ; 25 26 import org.xml.sax.Attributes ; 27 import org.xml.sax.InputSource ; 28 import org.xml.sax.Parser ; 29 import org.xml.sax.SAXException ; 30 import org.xml.sax.SAXParseException ; 31 import org.xml.sax.XMLReader ; 32 import org.xml.sax.helpers.DefaultHandler ; 33 import org.xml.sax.helpers.ParserAdapter ; 34 import org.xml.sax.helpers.ParserFactory ; 35 import org.xml.sax.helpers.XMLReaderFactory ; 36 37 53 public class DelayedInput 54 extends DefaultHandler { 55 56 60 62 63 protected static final String NAMESPACES_FEATURE_ID = "http://xml.org/sax/features/namespaces"; 64 65 66 protected static final String VALIDATION_FEATURE_ID = "http://xml.org/sax/features/validation"; 67 68 69 protected static final String SCHEMA_VALIDATION_FEATURE_ID = "http://apache.org/xml/features/validation/schema"; 70 71 72 protected static final String SCHEMA_FULL_CHECKING_FEATURE_ID = "http://apache.org/xml/features/validation/schema-full-checking"; 73 74 76 77 protected static final String DEFAULT_PARSER_NAME = "org.apache.xerces.parsers.SAXParser"; 78 79 80 protected static final boolean DEFAULT_NAMESPACES = true; 81 82 83 protected static final boolean DEFAULT_VALIDATION = false; 84 85 86 protected static final boolean DEFAULT_SCHEMA_VALIDATION = false; 87 88 89 protected static final boolean DEFAULT_SCHEMA_FULL_CHECKING = false; 90 91 95 96 protected PrintWriter fOut; 97 98 102 103 public DelayedInput() { 104 } 106 110 111 public void startElement(String uri, String localpart, String rawname, 112 Attributes attrs) throws SAXException { 113 114 System.out.println("("+rawname); 115 int length = attrs != null ? attrs.getLength() : 0; 116 for (int i = 0; i < length; i++) { 117 System.out.println("A"+attrs.getQName(i)+' '+attrs.getValue(i)); 118 } 119 120 } 122 123 public void endElement(String uri, String localpart, String rawname) 124 throws SAXException { 125 System.out.println(")"+rawname); 126 } 128 132 133 public void warning(SAXParseException ex) throws SAXException { 134 printError("Warning", ex); 135 } 137 138 public void error(SAXParseException ex) throws SAXException { 139 printError("Error", ex); 140 } 142 143 public void fatalError(SAXParseException ex) throws SAXException { 144 printError("Fatal Error", ex); 145 throw ex; 146 } 148 152 153 protected void printError(String type, SAXParseException ex) { 154 155 System.err.print("["); 156 System.err.print(type); 157 System.err.print("] "); 158 String systemId = ex.getSystemId(); 159 if (systemId != null) { 160 int index = systemId.lastIndexOf('/'); 161 if (index != -1) 162 systemId = systemId.substring(index + 1); 163 System.err.print(systemId); 164 } 165 System.err.print(':'); 166 System.err.print(ex.getLineNumber()); 167 System.err.print(':'); 168 System.err.print(ex.getColumnNumber()); 169 System.err.print(": "); 170 System.err.print(ex.getMessage()); 171 System.err.println(); 172 System.err.flush(); 173 174 } 176 180 189 static class DelayedInputStream 190 extends FilterInputStream { 191 192 196 197 private Random fRandom = new Random (System.currentTimeMillis()); 198 199 203 204 public DelayedInputStream(InputStream in) { 205 super(in); 206 } 208 212 213 public int read(byte[] buffer, int offset, int length) throws IOException { 214 215 if (length > 48) { 217 length = 48; 218 } 219 int count = 0; 220 221 long before = System.currentTimeMillis(); 223 count = in.read(buffer, offset, length); 224 try { 225 Thread.currentThread().sleep(Math.abs(fRandom.nextInt()) % 2000); 226 } 227 catch (InterruptedException e) { 228 e.printStackTrace(System.err); 229 } 230 long after = System.currentTimeMillis(); 231 232 System.out.print("read "+count+" bytes in "+(after-before)+" ms: "); 234 printBuffer(buffer, offset, count); 235 System.out.println(); 236 237 return count; 239 240 } 242 246 247 private void printBuffer(byte[] buffer, int offset, int length) { 248 249 if (length <= 0) { 251 System.out.print("no data read"); 252 return; 253 } 254 255 System.out.print('['); 257 for (int i = 0; i < length; i++) { 258 switch ((char)buffer[offset + i]) { 259 case '\r': { 260 System.out.print("\\r"); 261 break; 262 } 263 case '\n': { 264 System.out.print("\\n"); 265 break; 266 } 267 default: { 268 System.out.print((char)buffer[offset + i]); 269 } 270 } 271 } 272 System.out.print(']'); 273 274 } 276 } 278 282 283 public static void main(String argv[]) { 284 285 if (argv.length == 0) { 287 printUsage(); 288 System.exit(1); 289 } 290 291 DefaultHandler handler = new DelayedInput(); 293 PrintWriter out = new PrintWriter (System.out); 294 XMLReader parser = null; 295 boolean namespaces = DEFAULT_NAMESPACES; 296 boolean validation = DEFAULT_VALIDATION; 297 boolean schemaValidation = DEFAULT_SCHEMA_VALIDATION; 298 boolean schemaFullChecking = DEFAULT_SCHEMA_FULL_CHECKING; 299 300 for (int i = 0; i < argv.length; i++) { 302 String arg = argv[i]; 303 if (arg.startsWith("-")) { 304 String option = arg.substring(1); 305 if (option.equals("p")) { 306 if (++i == argv.length) { 308 System.err.println("error: Missing argument to -p option."); 309 } 310 String parserName = argv[i]; 311 312 try { 314 parser = XMLReaderFactory.createXMLReader(parserName); 315 } 316 catch (Exception e) { 317 try { 318 Parser sax1Parser = ParserFactory.makeParser(parserName); 319 parser = new ParserAdapter (sax1Parser); 320 System.err.println("warning: Features and properties not supported on SAX1 parsers."); 321 } 322 catch (Exception ex) { 323 parser = null; 324 System.err.println("error: Unable to instantiate parser ("+DEFAULT_PARSER_NAME+")"); 325 } 326 } 327 continue; 328 } 329 if (option.equalsIgnoreCase("n")) { 330 namespaces = option.equals("n"); 331 continue; 332 } 333 if (option.equalsIgnoreCase("v")) { 334 validation = option.equals("v"); 335 continue; 336 } 337 if (option.equalsIgnoreCase("s")) { 338 schemaValidation = option.equals("s"); 339 continue; 340 } 341 if (option.equalsIgnoreCase("f")) { 342 schemaFullChecking = option.equals("f"); 343 continue; 344 } 345 if (option.equals("h")) { 346 printUsage(); 347 continue; 348 } 349 } 350 351 if (parser == null) { 353 354 try { 356 parser = XMLReaderFactory.createXMLReader(DEFAULT_PARSER_NAME); 357 } 358 catch (Exception e) { 359 System.err.println("error: Unable to instantiate parser ("+DEFAULT_PARSER_NAME+")"); 360 continue; 361 } 362 } 363 364 try { 366 parser.setFeature(NAMESPACES_FEATURE_ID, namespaces); 367 } 368 catch (SAXException e) { 369 System.err.println("warning: Parser does not support feature ("+NAMESPACES_FEATURE_ID+")"); 370 } 371 try { 372 parser.setFeature(VALIDATION_FEATURE_ID, validation); 373 } 374 catch (SAXException e) { 375 System.err.println("warning: Parser does not support feature ("+VALIDATION_FEATURE_ID+")"); 376 } 377 try { 378 parser.setFeature(SCHEMA_VALIDATION_FEATURE_ID, schemaValidation); 379 } 380 catch (SAXException e) { 381 System.err.println("warning: Parser does not support feature ("+SCHEMA_VALIDATION_FEATURE_ID+")"); 382 } 383 try { 384 parser.setFeature(SCHEMA_FULL_CHECKING_FEATURE_ID, schemaFullChecking); 385 } 386 catch (SAXException e) { 387 System.err.println("warning: Parser does not support feature ("+SCHEMA_FULL_CHECKING_FEATURE_ID+")"); 388 } 389 390 parser.setContentHandler(handler); 392 parser.setErrorHandler(handler); 393 try { 394 System.out.println("# filename: "+arg); 395 InputStream stream = new DelayedInputStream(new FileInputStream (arg)); 396 InputSource source = new InputSource (stream); 397 source.setSystemId(arg); 398 parser.parse(source); 399 } 400 catch (SAXParseException e) { 401 } 403 catch (Exception e) { 404 System.err.println("error: Parse error occurred - "+e.getMessage()); 405 if (e instanceof SAXException ) { 406 e = ((SAXException )e).getException(); 407 } 408 e.printStackTrace(System.err); 409 } 410 } 411 412 } 414 418 419 private static void printUsage() { 420 421 System.err.println("usage: java socket.DelayedInput (options) filename ..."); 422 System.err.println(); 423 424 System.err.println("options:"); 425 System.err.println(" -p name Select parser by name."); 426 System.err.println(" -n | -N Turn on/off namespace processing."); 427 System.err.println(" -v | -V Turn on/off validation."); 428 System.err.println(" -s | -S Turn on/off Schema validation support."); 429 System.err.println(" NOTE: Not supported by all parsers."); 430 System.err.println(" -f | -F Turn on/off Schema full checking."); 431 System.err.println(" NOTE: Requires use of -s and not supported by all parsers."); 432 System.err.println(" -h This help screen."); 433 System.err.println(); 434 435 System.err.println("defaults:"); 436 System.err.println(" Parser: "+DEFAULT_PARSER_NAME); 437 System.err.print(" Namespaces: "); 438 System.err.println(DEFAULT_NAMESPACES ? "on" : "off"); 439 System.err.print(" Validation: "); 440 System.err.println(DEFAULT_VALIDATION ? "on" : "off"); 441 System.err.print(" Schema: "); 442 System.err.println(DEFAULT_SCHEMA_VALIDATION ? "on" : "off"); 443 System.err.print(" Schema full checking: "); 444 System.err.println(DEFAULT_SCHEMA_FULL_CHECKING ? "on" : "off"); 445 446 } 448 } | Popular Tags |