Example usage for javax.xml.parsers SAXParserFactory setNamespaceAware

List of usage examples for javax.xml.parsers SAXParserFactory setNamespaceAware

Introduction

In this page you can find the example usage for javax.xml.parsers SAXParserFactory setNamespaceAware.

Prototype


public void setNamespaceAware(boolean awareness) 

Source Link

Document

Specifies that the parser produced by this code will provide support for XML namespaces.

Usage

From source file:org.exjello.mail.Exchange2003Connection.java

private void listFolder(DefaultHandler handler, String folder) throws Exception {
    synchronized (this) {
        if (!isConnected()) {
            throw new IllegalStateException("Not connected.");
        }/*www  .j  a  v a2s  . co  m*/
        HttpClient client = getClient();
        ExchangeMethod op = new ExchangeMethod(SEARCH_METHOD, folder);
        op.setHeader("Content-Type", XML_CONTENT_TYPE);
        if (limit > 0)
            op.setHeader("Range", "rows=0-" + limit);
        op.setHeader("Brief", "t");

        /* Mirco: Manage of custom query */
        if ((filterLastCheck == null || "".equals(filterLastCheck))
                && (filterFrom == null || "".equals(filterFrom))
                && (filterNotFrom == null || "".equals(filterNotFrom))
                && (filterTo == null || "".equals(filterTo))) {
            op.setRequestEntity(unfiltered ? createAllInboxEntity() : createUnreadInboxEntity());
        } else {
            op.setRequestEntity(
                    createCustomInboxEntity(unfiltered, filterLastCheck, filterFrom, filterNotFrom, filterTo));
        }
        InputStream stream = null;
        try {
            int status = client.executeMethod(op);
            stream = op.getResponseBodyAsStream();
            if (status >= 300) {
                throw new IllegalStateException("Unable to obtain " + folder + ".");
            }
            SAXParserFactory spf = SAXParserFactory.newInstance();
            spf.setNamespaceAware(true);
            SAXParser parser = spf.newSAXParser();
            parser.parse(stream, handler);
            stream.close();
            stream = null;
        } finally {
            try {
                if (stream != null) {
                    byte[] buf = new byte[65536];
                    try {
                        if (session.getDebug()) {
                            PrintStream log = session.getDebugOut();
                            log.println("Response Body:");
                            int count;
                            while ((count = stream.read(buf, 0, 65536)) != -1) {
                                log.write(buf, 0, count);
                            }
                            log.flush();
                            log.println();
                        } else {
                            while (stream.read(buf, 0, 65536) != -1)
                                ;
                        }
                    } catch (Exception ignore) {
                    } finally {
                        try {
                            stream.close();
                        } catch (Exception ignore2) {
                        }
                    }
                }
            } finally {
                op.releaseConnection();
            }
        }
    }
}

From source file:org.exjello.mail.Exchange2003Connection.java

private void findInbox() throws Exception {
    inbox = null;//ww  w  .  jav a2 s.  c om
    drafts = null;
    submissionUri = null;
    sentitems = null;
    outbox = null;
    HttpClient client = getClient();
    ExchangeMethod op = new ExchangeMethod(PROPFIND_METHOD, server + "/exchange/" + mailbox);
    op.setHeader("Content-Type", XML_CONTENT_TYPE);
    op.setHeader("Depth", "0");
    op.setHeader("Brief", "t");
    op.setRequestEntity(createFindInboxEntity());
    InputStream stream = null;
    try {
        int status = client.executeMethod(op);
        stream = op.getResponseBodyAsStream();
        if (status >= 300) {
            throw new IllegalStateException("Unable to obtain inbox.");
        }
        SAXParserFactory spf = SAXParserFactory.newInstance();
        spf.setNamespaceAware(true);
        SAXParser parser = spf.newSAXParser();
        parser.parse(stream, new DefaultHandler() {
            private final StringBuilder content = new StringBuilder();

            public void startElement(String uri, String localName, String qName, Attributes attributes)
                    throws SAXException {
                content.setLength(0);
            }

            public void characters(char[] ch, int start, int length) throws SAXException {
                content.append(ch, start, length);
            }

            public void endElement(String uri, String localName, String qName) throws SAXException {
                if (!HTTPMAIL_NAMESPACE.equals(uri))
                    return;
                if ("inbox".equals(localName)) {
                    Exchange2003Connection.this.inbox = content.toString();
                } else if ("drafts".equals(localName)) {
                    Exchange2003Connection.this.drafts = content.toString();
                } else if ("sentitems".equals(localName)) {
                    Exchange2003Connection.this.sentitems = content.toString();
                } else if ("outbox".equals(localName)) {
                    Exchange2003Connection.this.outbox = content.toString();
                } else if ("sendmsg".equals(localName)) {
                    Exchange2003Connection.this.submissionUri = content.toString();
                }
            }
        });
        stream.close();
        stream = null;
    } finally {
        try {
            if (stream != null) {
                byte[] buf = new byte[65536];
                try {
                    if (session.getDebug()) {
                        PrintStream log = session.getDebugOut();
                        log.println("Response Body:");
                        int count;
                        while ((count = stream.read(buf, 0, 65536)) != -1) {
                            log.write(buf, 0, count);
                        }
                        log.flush();
                        log.println();
                    } else {
                        while (stream.read(buf, 0, 65536) != -1)
                            ;
                    }
                } catch (Exception ignore) {
                } finally {
                    try {
                        stream.close();
                    } catch (Exception ignore2) {
                    }
                }
            }
        } finally {
            op.releaseConnection();
        }
    }
}

From source file:org.exjello.mail.ExchangeConnection.java

private void findInbox() throws Exception {
    inbox = null;/*from   www.j ava  2 s .com*/
    drafts = null;
    submissionUri = null;
    sentitems = null;
    outbox = null;
    HttpClient client = getClient();
    ExchangeMethod op = new ExchangeMethod(PROPFIND_METHOD, server + "/exchange/" + mailbox);
    op.setHeader("Content-Type", XML_CONTENT_TYPE);
    op.setHeader("Depth", "0");
    op.setHeader("Brief", "t");
    op.setRequestEntity(createFindInboxEntity());
    InputStream stream = null;
    try {
        int status = client.executeMethod(op);
        stream = op.getResponseBodyAsStream();
        if (status >= 300) {
            throw new IllegalStateException("Unable to obtain inbox.");
        }
        SAXParserFactory spf = SAXParserFactory.newInstance();
        spf.setNamespaceAware(true);
        SAXParser parser = spf.newSAXParser();
        parser.parse(stream, new DefaultHandler() {
            private final StringBuilder content = new StringBuilder();

            public void startElement(String uri, String localName, String qName, Attributes attributes)
                    throws SAXException {
                content.setLength(0);
            }

            public void characters(char[] ch, int start, int length) throws SAXException {
                content.append(ch, start, length);
            }

            public void endElement(String uri, String localName, String qName) throws SAXException {
                if (!HTTPMAIL_NAMESPACE.equals(uri))
                    return;
                if ("inbox".equals(localName)) {
                    ExchangeConnection.this.inbox = content.toString();
                } else if ("drafts".equals(localName)) {
                    ExchangeConnection.this.drafts = content.toString();
                } else if ("sentitems".equals(localName)) {
                    ExchangeConnection.this.sentitems = content.toString();
                } else if ("outbox".equals(localName)) {
                    ExchangeConnection.this.outbox = content.toString();
                } else if ("sendmsg".equals(localName)) {
                    ExchangeConnection.this.submissionUri = content.toString();
                }
            }
        });
        stream.close();
        stream = null;
    } finally {
        try {
            if (stream != null) {
                byte[] buf = new byte[65536];
                try {
                    if (session.getDebug()) {
                        PrintStream log = session.getDebugOut();
                        log.println("Response Body:");
                        int count;
                        while ((count = stream.read(buf, 0, 65536)) != -1) {
                            log.write(buf, 0, count);
                        }
                        log.flush();
                        log.println();
                    } else {
                        while (stream.read(buf, 0, 65536) != -1)
                            ;
                    }
                } catch (Exception ignore) {
                } finally {
                    try {
                        stream.close();
                    } catch (Exception ignore2) {
                    }
                }
            }
        } finally {
            op.releaseConnection();
        }
    }
}

From source file:org.formix.dsx.XmlElement.java

private static SAXParser createSaxParser()
        throws ParserConfigurationException, SAXNotRecognizedException, SAXNotSupportedException, SAXException {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setNamespaceAware(false);

    factory.setValidating(false);/*from   ww  w  .  ja  v a 2 s.c  o m*/
    factory.setFeature("http://xml.org/sax/features/namespaces", false);
    factory.setFeature("http://xml.org/sax/features/validation", false);
    factory.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
    factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);

    SAXParser parser = factory.newSAXParser();
    return parser;
}

From source file:org.gcaldaemon.core.sendmail.SendMail.java

private final void sendXML(String email, String content, GmailEntry entry) throws Exception {
    log.debug("Parsing XML file...");
    SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setValidating(false);/*from www .j  a va  2  s.  co  m*/
    factory.setNamespaceAware(false);
    SAXParser parser = factory.newSAXParser();
    InputSource source = new InputSource(new StringReader(content));
    MailParser mailParser = new MailParser(username);
    parser.parse(source, mailParser);

    // Submit mail
    String toList = "";
    String ccList = "";
    String bccList = "";

    Iterator i = mailParser.getTo().iterator();
    while (i.hasNext()) {
        toList += (String) i.next() + ",";
    }
    i = mailParser.getCc().iterator();
    while (i.hasNext()) {
        ccList += (String) i.next() + ",";
    }
    i = mailParser.getBcc().iterator();
    while (i.hasNext()) {
        bccList += (String) i.next() + ",";
    }
    if (toList.length() == 0) {
        toList = username;
    }

    String msg = mailParser.getBody();
    boolean isHTML = msg.indexOf("/>") != -1 || msg.indexOf("</") != -1;
    if (isHTML) {
        msg = cropBody(msg);
    }
    if (isHTML) {
        log.debug("Sending HTML mail...");
    } else {
        log.debug("Sending plain-text mail...");
    }
    entry.send(toList, ccList, bccList, mailParser.getSubject(), msg, isHTML);
    log.debug("Mail submission finished.");
}

From source file:org.globus.mds.bigindex.impl.database.xml.xindice.XindiceDriver.java

/**
 * Adds a document file to a collection.
 * @param parentCol - name of the parent collection
 * @param fileName - name of the file//from   www.j  ava 2 s  .co m
 * @param docName - name of the document
 * @return String - the document ID.
 * @exception Exception - if the collection does not exist.
 */
public String addDocumentFile(String parentCol, String fileName, String docName) throws Exception {
    checkInitialized();

    Collection col = null;
    String docID = null;
    InputStream fis = null;

    try {
        if (this.isProfiling) {
            this.performanceLogger.start();
        }

        // Create a collection instance
        String colURI = normalizeCollectionURI(parentCol, this.isLocal);
        col = DatabaseManager.getCollection(colURI);
        if (col == null) {
            String err = "XINDICE ERROR: Collection not found! " + colURI;
            if (logger.isDebugEnabled()) {
                logger.debug(err);
            }
            throw new Exception(err);
        }

        // Parse in XML using Xerces
        File file = new File(fileName);
        fis = new FileInputStream(file);

        SAXParserFactory spf = javax.xml.parsers.SAXParserFactory.newInstance();
        spf.setNamespaceAware(true);

        XMLReader saxReader = spf.newSAXParser().getXMLReader();
        StringSerializer ser = new StringSerializer(null);

        saxReader.setContentHandler(ser);
        saxReader.setProperty("http://xml.org/sax/properties/lexical-handler", ser);
        saxReader.parse(new InputSource(fis));

        // Create the XMLResource and store the document
        XMLResource resource = (XMLResource) col.createResource(docName, "XMLResource");
        resource.setContent(ser.toString());
        col.storeResource(resource);
        docID = resource.getId();

        if (logger.isDebugEnabled()) {
            logger.debug("STORED Document: " + colURI + "/" + docID);
        }
        resource = null;
    } finally {
        if (fis != null) {
            try {
                fis.close();
            } catch (IOException e) {
                logger.debug("Failed to close: " + e.getMessage(), e);
            }
        }
        if (col != null) {
            col.close();
        }
        col = null;
        if (this.isProfiling) {
            this.performanceLogger.stop("addDocumentFile");
        }
    }
    return docID;
}

From source file:org.globus.mds.bigindex.impl.database.xml.xindice.XindiceDriver.java

/**
 * Adds a Document to a collection, where the input Document is in String form.
 * @param parentCol - name of the parent collection
 * @param docstr - String representation of the Document to add
 * @param docName - name of the document
 * @return String - the document ID.// w  w w .j  a v  a  2s  .c  o m
 */
public String addDocumentString(String parentCol, String docstr, String docName) throws Exception {
    checkInitialized();

    Collection col = null;
    String docID = null;
    try {
        if (this.isProfiling) {
            this.performanceLogger.start();
        }

        // Create a collection instance
        String colURI = normalizeCollectionURI(parentCol, this.isLocal);
        col = DatabaseManager.getCollection(colURI);
        if (col == null) {
            String err = "XINDICE ERROR: Collection not found! " + colURI;
            if (logger.isDebugEnabled()) {
                logger.debug(err);
            }
            throw new Exception(err);
        }

        // Parse in XML using Xerces
        StringReader reader = new StringReader(docstr);

        SAXParserFactory spf = javax.xml.parsers.SAXParserFactory.newInstance();
        spf.setNamespaceAware(true);

        XMLReader saxReader = spf.newSAXParser().getXMLReader();
        StringSerializer ser = new StringSerializer(null);

        saxReader.setContentHandler(ser);
        saxReader.setProperty("http://xml.org/sax/properties/lexical-handler", ser);
        saxReader.parse(new InputSource(reader));
        reader.close();

        // Create the XMLResource and store the document
        XMLResource resource = (XMLResource) col.createResource(docName, "XMLResource");
        resource.setContent(ser.toString());
        col.storeResource(resource);
        docID = resource.getId();

        if (logger.isDebugEnabled()) {
            logger.debug("STORED Document: " + colURI + "/" + docID);
        }
        resource = null;
    } finally {
        if (col != null) {
            col.close();
        }
        col = null;

        if (this.isProfiling) {
            this.performanceLogger.stop("addDocumentString");
        }
    }
    return docID;
}

From source file:org.infoscoop.request.filter.ical.ICalendarUtil.java

public static Reader convertRdf2Ics(InputStream is) throws SAXException, IOException {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setValidating(false);//from   www . j av  a  2s. c o m
    factory.setNamespaceAware(true);
    XMLReader reader = null;
    try {
        reader = factory.newSAXParser().getXMLReader();
        reader.setEntityResolver(NoOpEntityResolver.getInstance());
    } catch (ParserConfigurationException e) {
        log.error("", e);
    }

    Rdf2IcsHandler xmlHandler = new Rdf2IcsHandler();

    reader.setContentHandler(xmlHandler);
    reader.parse(new InputSource(is));
    return new StringReader(xmlHandler.getResult());
}

From source file:org.iterx.miru.bean.factory.XmlBeanParser.java

public void parse(StreamSource source) throws IOException {
    try {/*from  ww  w.j a  v  a  2s. c  o  m*/
        SAXParserFactory factory;
        SAXParser parser;

        factory = SAXParserFactory.newInstance();
        factory.setNamespaceAware(true);
        factory.setValidating(true);

        parser = factory.newSAXParser();
        parser.parse(source.getInputStream(), this);
    } catch (ParserConfigurationException e) {
        throw new RuntimeException(e);
    } catch (SAXException e) {
        if (LOGGER.isErrorEnabled())
            LOGGER.error(e, e);
        throw new IOException("Invalid xml stream [" + source + "]. " + e.getMessage());
    }
}

From source file:org.iterx.miru.dispatcher.handler.factory.XmlHandlerChainParser.java

public void parse(StreamSource source) throws IOException {
    try {//from   w ww  . jav  a2  s  .co  m
        SAXParserFactory factory;
        SAXParser parser;

        factory = SAXParserFactory.newInstance();
        factory.setNamespaceAware(true);
        factory.setValidating(true);

        parser = factory.newSAXParser();
        parser.parse(source.getInputStream(), this);
    } catch (ParserConfigurationException e) {
        throw new RuntimeException(e);
    } catch (SAXException e) {
        if (LOGGER.isErrorEnabled())
            LOGGER.error(e);
        throw new IOException("Invalid xml stream [" + source + "]. " + e.getMessage());
    }
}