Example usage for org.xml.sax ContentHandler ContentHandler

List of usage examples for org.xml.sax ContentHandler ContentHandler

Introduction

In this page you can find the example usage for org.xml.sax ContentHandler ContentHandler.

Prototype

ContentHandler

Source Link

Usage

From source file:com.sap.prd.mobile.ios.mios.VersionInfoManager.java

private static String getSchemaVersion(final File f) throws SAXException, IOException {

    class StopParsingException extends SAXException {
        private static final long serialVersionUID = -7499102356648608429L;
    }//w  ww.  j  a va 2 s.  c om
    ;

    XMLReader xmlReader = XMLReaderFactory.createXMLReader();

    final InputStream s = new FileInputStream(f);

    final String[] result = new String[1];

    try {

        xmlReader.setContentHandler(new ContentHandler() {

            boolean repositoryTagFound = false;

            @Override
            public void startPrefixMapping(String prefix, String uri) throws SAXException {
            }

            @Override
            public void startElement(String uri, String localName, String qName, Attributes atts)
                    throws SAXException {
                if (localName.equals("versions")) {
                    result[0] = atts.getValue("schemaVersion");

                    if (result[0] != null)
                        throw new StopParsingException();
                }

                if (localName.equals("repository")) {
                    repositoryTagFound = true;
                }
            }

            @Override
            public void startDocument() throws SAXException {
            }

            @Override
            public void skippedEntity(String name) throws SAXException {
            }

            @Override
            public void setDocumentLocator(Locator locator) {
            }

            @Override
            public void processingInstruction(String target, String data) throws SAXException {
            }

            @Override
            public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
            }

            @Override
            public void endPrefixMapping(String prefix) throws SAXException {
            }

            @Override
            public void endElement(String uri, String localName, String qName) throws SAXException {
            }

            @Override
            public void endDocument() throws SAXException {
                if (!repositoryTagFound && result[0] == null)
                    result[0] = "1.2.0";
            }

            @Override
            public void characters(char[] ch, int start, int length) throws SAXException {
            }
        });

        try {
            xmlReader.parse(new InputSource(s));
        } catch (StopParsingException ex) {
            //OK, StopParsingException is thrown when we know all we need from the document.
        }
    } finally {
        IOUtils.closeQuietly(s);
    }
    return result[0];
}

From source file:org.apache.openmeetings.db.dao.label.LabelDao.java

private static List<StringLabel> getLabels(InputStream is) throws Exception {
    final List<StringLabel> labels = new ArrayList<StringLabel>();
    SAXParserFactory spf = SAXParserFactory.newInstance();
    spf.setNamespaceAware(true);/*ww w.  j  ava2 s. c  om*/
    try {
        SAXParser parser = spf.newSAXParser();
        XMLReader xr = parser.getXMLReader();
        xr.setContentHandler(new ContentHandler() {
            StringLabel label = null;

            @Override
            public void startPrefixMapping(String prefix, String uri) throws SAXException {
            }

            @Override
            public void startElement(String uri, String localName, String qName, Attributes atts)
                    throws SAXException {
                if (ENTRY_ELEMENT.equals(localName)) {
                    label = new StringLabel(atts.getValue(KEY_ATTR), "");
                }
            }

            @Override
            public void startDocument() throws SAXException {
            }

            @Override
            public void skippedEntity(String name) throws SAXException {
            }

            @Override
            public void setDocumentLocator(Locator locator) {
            }

            @Override
            public void processingInstruction(String target, String data) throws SAXException {
            }

            @Override
            public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
            }

            @Override
            public void endPrefixMapping(String prefix) throws SAXException {
            }

            @Override
            public void endElement(String uri, String localName, String qName) throws SAXException {
                if (ENTRY_ELEMENT.equals(localName)) {
                    labels.add(label);
                }
            }

            @Override
            public void endDocument() throws SAXException {
            }

            @Override
            public void characters(char[] ch, int start, int length) throws SAXException {
                StringBuilder sb = new StringBuilder(label.getValue());
                sb.append(ch, start, length);
                label.setValue(sb.toString());
            }
        });
        xr.parse(new InputSource(is));
    } catch (Exception e) {
        throw e;
    }
    return labels;
}

From source file:uk.ac.cam.caret.sakai.rwiki.component.service.impl.XSLTEntityHandler.java

public void parseToSAX(final String toRender, final ContentHandler ch) throws IOException, SAXException {
    /**/*from ww  w. jav  a  2  s. c  om*/
     * create a proxy for the stream, filtering out the start element and
     * end element events
     */
    ContentHandler proxy = new ContentHandler() {
        public void setDocumentLocator(Locator arg0) {
            ch.setDocumentLocator(arg0);
        }

        public void startDocument() throws SAXException {
            // ignore
        }

        public void endDocument() throws SAXException {
            // ignore
        }

        public void startPrefixMapping(String arg0, String arg1) throws SAXException {
            ch.startPrefixMapping(arg0, arg1);
        }

        public void endPrefixMapping(String arg0) throws SAXException {
            ch.endPrefixMapping(arg0);
        }

        public void startElement(String arg0, String arg1, String arg2, Attributes arg3) throws SAXException {
            ch.startElement(arg0, arg1, arg2, arg3);
        }

        public void endElement(String arg0, String arg1, String arg2) throws SAXException {
            ch.endElement(arg0, arg1, arg2);
        }

        public void characters(char[] arg0, int arg1, int arg2) throws SAXException {
            ch.characters(arg0, arg1, arg2);
        }

        public void ignorableWhitespace(char[] arg0, int arg1, int arg2) throws SAXException {
            ch.ignorableWhitespace(arg0, arg1, arg2);
        }

        public void processingInstruction(String arg0, String arg1) throws SAXException {
            ch.processingInstruction(arg0, arg1);
        }

        public void skippedEntity(String arg0) throws SAXException {
            ch.skippedEntity(arg0);
        }

    };
    InputSource ins = new InputSource(new StringReader(toRender));
    XMLReader xmlReader;
    try {
        SAXParser saxParser = saxParserFactory.newSAXParser();

        xmlReader = saxParser.getXMLReader();
    }

    catch (Exception e) {
        log.error("SAXException when creating XMLReader", e); //$NON-NLS-1$
        // rethrow!!
        throw new SAXException(e);
    }
    xmlReader.setContentHandler(proxy);
    xmlReader.parse(ins);
}