Example usage for org.xml.sax XMLReader setContentHandler

List of usage examples for org.xml.sax XMLReader setContentHandler

Introduction

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

Prototype

public void setContentHandler(ContentHandler handler);

Source Link

Document

Allow an application to register a content event handler.

Usage

From source file:org.kalypsodeegree_impl.io.sax.test.MultiPolygonContentHandlerTest.java

private GM_MultiSurface parseMultiPolygon(final InputStream inputStream)
        throws ParserConfigurationException, SAXException, IOException {
    final InputSource is = new InputSource(inputStream);

    final SAXParser saxParser = m_saxFactory.newSAXParser();
    final XMLReader reader = saxParser.getXMLReader();

    final GM_MultiSurface[] result = new GM_MultiSurface[1];
    final UnmarshallResultEater resultEater = new UnmarshallResultEater() {
        @Override//from   w  w w . j  a  v  a 2 s.co m
        public void unmarshallSuccesful(final Object value) {
            assertTrue(value instanceof GM_MultiSurface);
            result[0] = (GM_MultiSurface) value;
        }
    };

    final MultiPolygonContentHandler contentHandler = new MultiPolygonContentHandler(reader, resultEater, null);
    reader.setContentHandler(contentHandler);
    reader.parse(is);

    return result[0];
}

From source file:org.kalypsodeegree_impl.io.sax.test.PolygonContentHandlerTest.java

private GM_Polygon parsePolygon(final InputStream inputStream)
        throws ParserConfigurationException, SAXException, IOException {
    final InputSource is = new InputSource(inputStream);

    final SAXParser saxParser = m_saxFactory.newSAXParser();
    final XMLReader reader = saxParser.getXMLReader();

    final GM_Polygon[] result = new GM_Polygon[1];
    final UnmarshallResultEater resultEater = new UnmarshallResultEater() {
        @Override//from www. j ava  2 s. c  o  m
        public void unmarshallSuccesful(final Object value) {
            assertTrue(value instanceof GM_Polygon);
            result[0] = (GM_Polygon) value;
        }
    };

    final PolygonContentHandler contentHandler = new PolygonContentHandler(reader, resultEater, null);
    reader.setContentHandler(contentHandler);
    reader.parse(is);

    return result[0];
}

From source file:org.kalypsodeegree_impl.io.sax.test.SaxParserTestUtils.java

/**
 * Create an XMLReader that writes all xml into the given {@link OutputStream}.
 *//*from   w  ww . j a  v a 2s  .  c  o  m*/
public static XMLReader createXMLReader(final OutputStream os) throws SAXException {
    final ToXMLStream xmlStream = new ToXMLStream();
    xmlStream.setOutputStream(os);
    // Configure content handler. IMPORTANT: call after setOutputStream!
    xmlStream.setLineSepUse(true);
    xmlStream.setIndent(true);
    xmlStream.setIndentAmount(1);
    xmlStream.setEncoding(ENCODING);

    final XMLReader reader = XMLReaderFactory.createXMLReader();
    reader.setContentHandler(xmlStream);

    return reader;
}

From source file:org.kalypsodeegree_impl.io.sax.test.TriangulatedSurfaceContentHandlerTest.java

private GM_TriangulatedSurface readTriangles(final InputSource is)
        throws IOException, ParserConfigurationException, SAXException {
    final SAXParserFactory saxFac = SAXParserFactory.newInstance();
    saxFac.setNamespaceAware(true);/*from ww w  .  ja v a  2s.  co m*/

    final SAXParser saxParser = saxFac.newSAXParser();
    // make namespace-prefixes visible to content handler
    // used to allow necessary schemas from gml document
    final XMLReader reader = saxParser.getXMLReader();
    reader.setFeature("http://xml.org/sax/features/namespace-prefixes", Boolean.TRUE); //$NON-NLS-1$

    final GM_TriangulatedSurface[] result = new GM_TriangulatedSurface[1];
    final UnmarshallResultEater resultEater = new UnmarshallResultEater() {
        @Override
        public void unmarshallSuccesful(final Object value) {
            assertTrue(value instanceof GM_TriangulatedSurface);
            result[0] = (GM_TriangulatedSurface) value;
        }
    };

    final TriangulatedSurfaceContentHandler contentHandler = new TriangulatedSurfaceContentHandler(reader,
            resultEater);

    reader.setContentHandler(contentHandler);
    reader.parse(is);

    return result[0];
}

From source file:org.kitodo.production.plugin.opac.pica.GetOpac.java

private OpacResponseHandler parseOpacResponse(String opacResponse)
        throws IOException, SAXException, ParserConfigurationException {
    opacResponse = opacResponse.replace("&", "&").replace(""", """)
            .replace("<", "<").replace(">", ">");

    XMLReader parser = null;
    OpacResponseHandler ids = new OpacResponseHandler();
    /* Use Java 1.4 methods to create default parser. */
    SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setNamespaceAware(true);/*from ww w . j a  va 2 s . c  o  m*/
    parser = factory.newSAXParser().getXMLReader();

    parser.setContentHandler(ids);
    parser.parse(new InputSource(new StringReader(opacResponse)));

    return ids;
}

From source file:org.kuali.rice.devtools.jpa.eclipselink.conv.ojb.OjbUtil.java

private static Object readMetadataFromXML(InputSource source, Class target)
        throws ParserConfigurationException, SAXException, IOException {
    // get a xml reader instance:
    SAXParserFactory factory = SAXParserFactory.newInstance();
    LOG.debug("RepositoryPersistor using SAXParserFactory : " + factory.getClass().getName());

    SAXParser p = factory.newSAXParser();
    XMLReader reader = p.getXMLReader();

    Object result;//from   w ww  .  j a v a2 s  .  c o m
    if (DescriptorRepository.class.equals(target)) {
        // create an empty repository:
        DescriptorRepository repository = new DescriptorRepository();
        // create handler for building the repository structure
        org.xml.sax.ContentHandler handler = new RepositoryXmlHandler(repository);
        // tell parser to use our handler:
        reader.setContentHandler(handler);
        reader.parse(source);
        result = repository;
    } else if (ConnectionRepository.class.equals(target)) {
        // create an empty repository:
        ConnectionRepository repository = new ConnectionRepository();
        // create handler for building the repository structure
        org.xml.sax.ContentHandler handler = new ConnectionDescriptorXmlHandler(repository);
        // tell parser to use our handler:
        reader.setContentHandler(handler);
        reader.parse(source);
        //LoggerFactory.getBootLogger().info("loading XML took " + (stop - start) + " msecs");
        result = repository;
    } else
        throw new MetadataException(
                "Could not build a repository instance for '" + target + "', using source " + source);
    return result;
}

From source file:org.lnicholls.galleon.apps.iTunes.PlaylistParser.java

public PlaylistParser(String path) {

    try {/* w  ww . j a v a 2s .  com*/

        //path = "D:/galleon/iTunes Music Library.xml";

        ArrayList currentPlaylists = new ArrayList();

        // Read all tracks

        XMLReader trackReader = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");

        TrackParser trackParser = new TrackParser();

        trackReader.setContentHandler(trackParser);

        trackReader.setErrorHandler(trackParser);

        trackReader.setFeature("http://xml.org/sax/features/validation", false);

        File file = new File(path);

        if (file.exists()) {

            InputStream inputStream = Tools.getInputStream(file);

            trackReader.parse(new InputSource(inputStream));

            inputStream.close();

        }

        // Read all playlists

        XMLReader listReader = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");

        ListParser listParser = new ListParser(currentPlaylists);

        listReader.setContentHandler(listParser);

        listReader.setErrorHandler(listParser);

        listReader.setFeature("http://xml.org/sax/features/validation", false);

        file = new File(path);

        if (file.exists()) {

            InputStream inputStream = Tools.getInputStream(file);

            listReader.parse(new InputSource(inputStream));

            inputStream.close();

        }

        // Remove old playlists

        List list = PlaylistsManager.listAll();

        if (list != null && list.size() > 0)

        {

            Iterator playlistIterator = list.iterator();

            while (playlistIterator.hasNext())

            {

                Playlists playlist = (Playlists) playlistIterator.next();

                boolean found = false;

                Iterator iterator = currentPlaylists.iterator();

                while (iterator.hasNext())

                {

                    String externalId = (String) iterator.next();

                    if (externalId.equals(playlist.getExternalId()))

                    {

                        found = true;

                        break;

                    }

                }

                if (!found)

                {

                    PlaylistsManager.deletePlaylistsTracks(playlist);

                    PlaylistsManager.deletePlaylists(playlist);

                    log.debug("Removed playlist: " + playlist.getTitle());

                }

            }

            list.clear();

        }

        currentPlaylists.clear();

    } catch (IOException ex) {

        Tools.logException(PlaylistParser.class, ex);

    } catch (SAXException ex) {

        Tools.logException(PlaylistParser.class, ex);

    } catch (Exception ex) {

        Tools.logException(PlaylistParser.class, ex);

    }

}

From source file:org.mule.config.spring.MuleDocumentLoader.java

protected XMLReader createSaxAnnotator(Document doc) throws ParserConfigurationException, SAXException {
    SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
    SAXParser saxParser = saxParserFactory.newSAXParser();
    XMLReader documentReader = saxParser.getXMLReader();
    documentReader.setContentHandler(new XmlMetadataAnnotator(doc));
    return documentReader;
}

From source file:org.mycore.common.content.transformer.MCRXSLTransformer.java

protected XMLReader getXMLReader(LinkedList<TransformerHandler> transformHandlerList) throws SAXException {
    XMLReader reader = XMLReaderFactory.createXMLReader();
    reader.setEntityResolver(ENTITY_RESOLVER);
    reader.setContentHandler(transformHandlerList.getFirst());
    return reader;
}

From source file:org.n52.sos.XmlToExiConverter.java

protected void encode(String fileName) {
    try (InputStream exiIS = FileUtils.openInputStream(getFile(fileName, XML_EXTENSION));
            OutputStream exiOS = FileUtils.openOutputStream(getFile(fileName, EXI_EXTENSION))) {
        EXIResult exiResult = new EXIResult();
        exiResult.setOutputStream(exiOS);
        XMLReader xmlReader = XMLReaderFactory.createXMLReader();
        xmlReader.setContentHandler(exiResult.getHandler());
        xmlReader.parse(new InputSource(exiIS));
    } catch (Exception e) {
        // TODO: handle exception
    }// w  ww.  j a v a 2 s .co m
}