Code example for InputSource

Methods: setSystemId

0
     * @param r The document reader. 
     * @exception IOException if an error occured while reading the document. 
     */ 
    public Document createDocument(String ns, String root, String uri,
                                   Reader r) throws IOException {
        InputSource inp = new InputSource(r);
        inp.setSystemId(uri);
        return createDocument(ns, root, uri, inp);
    } 
 
    /** 
     * Creates a Document instance. 
     * @param ns The namespace URI of the root element of the document. 
     * @param root The name of the root element of the document. 
     * @param uri The document URI. 
     * @param r an XMLReaderInstance 
     * @exception IOException if an error occured while reading the document. 
     */ 
    public Document createDocument(String ns, String root, String uri,
                                   XMLReader r) throws IOException {
        r.setContentHandler(this);
        r.setDTDHandler(this);
        r.setEntityResolver(this);
        try { 
            r.parse(uri);
        } catch (SAXException e) {
            Exception ex = e.getException();
            if (ex != null && ex instanceof InterruptedIOException) {
                throw (InterruptedIOException) ex;
            } 
            throw new SAXIOException(e);
        } 
        currentNode = null;
        Document ret = document;
        document = null;
        return ret;
    } 
 
    /** 
     * Creates a Document instance. 
     * @param uri The document URI. 
     * @param r The document reader. 
     * @exception IOException if an error occured while reading the document. 
     */ 
    public Document createDocument(String uri, Reader r) throws IOException {
        InputSource inp = new InputSource(r);
        inp.setSystemId(uri);
        return createDocument(inp);
    } 
 
    /** 
     * Creates a Document. 
     * @param ns The namespace URI of the root element. 
     * @param root The name of the root element. 
     * @param uri The document URI. 
     * @param is  The document input source. 
     * @exception IOException if an error occured while reading the document. 
     */ 
    protected Document createDocument(String ns, String root, String uri,
                                      InputSource is)
        throws IOException { 
        Document ret = createDocument(is);
        Element docElem = ret.getDocumentElement();
 
        String lname = root;
        String nsURI = ns;
        if (ns == null) {
            int idx = lname.indexOf(':');
            String nsp = (idx == -1 || idx == lname.length()-1)
                ? "" 
                : lname.substring(0, idx);
            nsURI = namespaces.get(nsp);
            if (idx != -1 && idx != lname.length()-1) {
                lname = lname.substring(idx+1);
            } 
        } 
 
 
        String docElemNS = docElem.getNamespaceURI();
        if ((docElemNS != nsURI) &&
            ((docElemNS == null) || (!docElemNS.equals(nsURI))))
            throw new IOException
                ("Root element namespace does not match that requested:\n" + 
                 "Requested: " + nsURI + "\n" +
                 "Found: " + docElemNS);
 
        if (docElemNS != null) {
            if (!docElem.getLocalName().equals(lname))
                throw new IOException
                    ("Root element does not match that requested:\n" + 
                     "Requested: " + lname + "\n" +
                     "Found: " + docElem.getLocalName());
        } else { 
            if (!docElem.getNodeName().equals(lname))
                throw new IOException
                    ("Root element does not match that requested:\n" + 
                     "Requested: " + lname + "\n" +
                     "Found: " + docElem.getNodeName());
        } 
 
        return ret;
    } 
 
    static SAXParserFactory saxFactory;
    static { 
        saxFactory = SAXParserFactory.newInstance();
    } 
 
    /** 
     * Creates a Document. 
     * @param is  The document input source. 
     * @exception IOException if an error occured while reading the document. 
     */ 
    protected Document createDocument(InputSource is)
        throws IOException { 
        try { 
            if (parserClassName != null) {
                parser = XMLReaderFactory.createXMLReader(parserClassName);
            } else { 
                SAXParser saxParser;
                try { 
                    saxParser = saxFactory.newSAXParser();
                } catch (ParserConfigurationException pce) {
                    throw new IOException("Could not create SAXParser: "
                            + pce.getMessage());
                } 
                parser = saxParser.getXMLReader();
            } 
 
            parser.setContentHandler(this);
            parser.setDTDHandler(this);
            parser.setEntityResolver(this);
            parser.setErrorHandler((errorHandler == null) ?
                                   this : errorHandler);
 
            parser.setFeature("http://xml.org/sax/features/namespaces",
                              true); 
            parser.setFeature("http://xml.org/sax/features/namespace-prefixes",
                              true); 
            parser.setFeature("http://xml.org/sax/features/validation",
                              isValidating);
            parser.setProperty("http://xml.org/sax/properties/lexical-handler",
                               this);
            parser.parse(is);
        } catch (SAXException e) {
            Exception ex = e.getException();
            if (ex != null && ex instanceof InterruptedIOException) {
                throw (InterruptedIOException)ex;
            }