FileDocCategorySizeDatePackage
DelayedInput.javaAPI DocApache Xerces 3.0.115905Fri Sep 14 20:33:58 BST 2007socket

DelayedInput

public class DelayedInput extends DefaultHandler
This sample delays the input to the SAX parser to simulate reading data from a socket where data is not always immediately available. An XML parser should be able to parse the input and perform the necessary callbacks as data becomes available. So this is a good way to test any parser that implements the SAX2 XMLReader interface to see if it can parse data as it arrives.

Note: This sample uses NSGMLS-like output of elements and attributes interspersed with information about how many bytes are being read at a time.

author
Andy Clark, IBM
version
$Id: DelayedInput.java 447688 2006-09-19 02:39:49Z mrglavas $

Fields Summary
protected static final String
NAMESPACES_FEATURE_ID
Namespaces feature id (http://xml.org/sax/features/namespaces).
protected static final String
VALIDATION_FEATURE_ID
Validation feature id (http://xml.org/sax/features/validation).
protected static final String
SCHEMA_VALIDATION_FEATURE_ID
Schema validation feature id (http://apache.org/xml/features/validation/schema).
protected static final String
SCHEMA_FULL_CHECKING_FEATURE_ID
Schema full checking feature id (http://apache.org/xml/features/validation/schema-full-checking).
protected static final String
DEFAULT_PARSER_NAME
Default parser name.
protected static final boolean
DEFAULT_NAMESPACES
Default namespaces support (true).
protected static final boolean
DEFAULT_VALIDATION
Default validation support (false).
protected static final boolean
DEFAULT_SCHEMA_VALIDATION
Default Schema validation support (false).
protected static final boolean
DEFAULT_SCHEMA_FULL_CHECKING
Default Schema full checking support (false).
protected PrintWriter
fOut
Print writer.
Constructors Summary
public DelayedInput()
Default constructor.


    //
    // Constructors
    //

       
      
    
Methods Summary
public voidendElement(java.lang.String uri, java.lang.String localpart, java.lang.String rawname)
End element.

        System.out.println(")"+rawname);
    
public voiderror(org.xml.sax.SAXParseException ex)
Error.

        printError("Error", ex);
    
public voidfatalError(org.xml.sax.SAXParseException ex)
Fatal error.

        printError("Fatal Error", ex);
        throw ex;
    
public static voidmain(java.lang.String[] argv)
Main program entry point.


        // is there anything to do?
        if (argv.length == 0) {
            printUsage();
            System.exit(1);
        }

        // variables
        DefaultHandler handler = new DelayedInput();
        XMLReader parser = null;
        boolean namespaces = DEFAULT_NAMESPACES;
        boolean validation = DEFAULT_VALIDATION;
        boolean schemaValidation = DEFAULT_SCHEMA_VALIDATION;
        boolean schemaFullChecking = DEFAULT_SCHEMA_FULL_CHECKING;

        // process arguments
        for (int i = 0; i < argv.length; i++) {
            String arg = argv[i];
            if (arg.startsWith("-")) {
                String option = arg.substring(1);
                if (option.equals("p")) {
                    // get parser name
                    if (++i == argv.length) {
                        System.err.println("error: Missing argument to -p option.");
                    }
                    String parserName = argv[i];

                    // create parser
                    try {
                        parser = XMLReaderFactory.createXMLReader(parserName);
                    }
                    catch (Exception e) {
                        try {
                            Parser sax1Parser = ParserFactory.makeParser(parserName);
                            parser = new ParserAdapter(sax1Parser);
                            System.err.println("warning: Features and properties not supported on SAX1 parsers.");
                        }
                        catch (Exception ex) {
                            parser = null;
                            System.err.println("error: Unable to instantiate parser ("+DEFAULT_PARSER_NAME+")");
                        }
                    }
                    continue;
                }
                if (option.equalsIgnoreCase("n")) {
                    namespaces = option.equals("n");
                    continue;
                }
                if (option.equalsIgnoreCase("v")) {
                    validation = option.equals("v");
                    continue;
                }
                if (option.equalsIgnoreCase("s")) {
                    schemaValidation = option.equals("s");
                    continue;
                }
                if (option.equalsIgnoreCase("f")) {
                    schemaFullChecking = option.equals("f");
                    continue;
                }
                if (option.equals("h")) {
                    printUsage();
                    continue;
                }
            }

            // use default parser?
            if (parser == null) {

                // create parser
                try {
                    parser = XMLReaderFactory.createXMLReader(DEFAULT_PARSER_NAME);
                }
                catch (Exception e) {
                    System.err.println("error: Unable to instantiate parser ("+DEFAULT_PARSER_NAME+")");
                    continue;
                }
            }

            // set parser features
            try {
                parser.setFeature(NAMESPACES_FEATURE_ID, namespaces);
            }
            catch (SAXException e) {
                System.err.println("warning: Parser does not support feature ("+NAMESPACES_FEATURE_ID+")");
            }
            try {
                parser.setFeature(VALIDATION_FEATURE_ID, validation);
            }
            catch (SAXException e) {
                System.err.println("warning: Parser does not support feature ("+VALIDATION_FEATURE_ID+")");
            }
            try {
                parser.setFeature(SCHEMA_VALIDATION_FEATURE_ID, schemaValidation);
            }
            catch (SAXException e) {
                System.err.println("warning: Parser does not support feature ("+SCHEMA_VALIDATION_FEATURE_ID+")");
            }
            try {
                parser.setFeature(SCHEMA_FULL_CHECKING_FEATURE_ID, schemaFullChecking);
            }
            catch (SAXException e) {
                System.err.println("warning: Parser does not support feature ("+SCHEMA_FULL_CHECKING_FEATURE_ID+")");
            }

            // parse file
            parser.setContentHandler(handler);
            parser.setErrorHandler(handler);
            try {
                System.out.println("# filename: "+arg);
                InputStream stream = new DelayedInputStream(new FileInputStream(arg));
                InputSource source = new InputSource(stream);
                source.setSystemId(arg);
                parser.parse(source);
            }
            catch (SAXParseException e) {
                // ignore
            }
            catch (Exception e) {
                System.err.println("error: Parse error occurred - "+e.getMessage());
                if (e instanceof SAXException) {
                    e = ((SAXException)e).getException();
                }
                e.printStackTrace(System.err);
            }
        }

    
protected voidprintError(java.lang.String type, org.xml.sax.SAXParseException ex)
Prints the error message.


        System.err.print("[");
        System.err.print(type);
        System.err.print("] ");
        String systemId = ex.getSystemId();
        if (systemId != null) {
            int index = systemId.lastIndexOf('/");
            if (index != -1)
                systemId = systemId.substring(index + 1);
            System.err.print(systemId);
        }
        System.err.print(':");
        System.err.print(ex.getLineNumber());
        System.err.print(':");
        System.err.print(ex.getColumnNumber());
        System.err.print(": ");
        System.err.print(ex.getMessage());
        System.err.println();
        System.err.flush();

    
private static voidprintUsage()
Prints the usage.


        System.err.println("usage: java socket.DelayedInput (options) filename ...");
        System.err.println();

        System.err.println("options:");
        System.err.println("  -p name  Select parser by name.");
        System.err.println("  -n | -N  Turn on/off namespace processing.");
        System.err.println("  -v | -V  Turn on/off validation.");
        System.err.println("  -s | -S  Turn on/off Schema validation support.");
        System.err.println("           NOTE: Not supported by all parsers.");
        System.err.println("  -f  | -F Turn on/off Schema full checking.");
        System.err.println("           NOTE: Requires use of -s and not supported by all parsers.");
        System.err.println("  -h       This help screen.");
        System.err.println();

        System.err.println("defaults:");
        System.err.println("  Parser:     "+DEFAULT_PARSER_NAME);
        System.err.print("  Namespaces: ");
        System.err.println(DEFAULT_NAMESPACES ? "on" : "off");
        System.err.print("  Validation: ");
        System.err.println(DEFAULT_VALIDATION ? "on" : "off");
        System.err.print("  Schema:     ");
        System.err.println(DEFAULT_SCHEMA_VALIDATION ? "on" : "off");
        System.err.print("  Schema full checking:     ");
        System.err.println(DEFAULT_SCHEMA_FULL_CHECKING ? "on" : "off");

    
public voidstartElement(java.lang.String uri, java.lang.String localpart, java.lang.String rawname, org.xml.sax.Attributes attrs)
Start element.


        System.out.println("("+rawname);
        int length = attrs != null ? attrs.getLength() : 0;
        for (int i = 0; i < length; i++) {
            System.out.println("A"+attrs.getQName(i)+' "+attrs.getValue(i));
        }

    
public voidwarning(org.xml.sax.SAXParseException ex)
Warning.

        printError("Warning", ex);