Example usage for org.xml.sax XMLReader parse

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

Introduction

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

Prototype

public void parse(String systemId) throws IOException, SAXException;

Source Link

Document

Parse an XML document from a system identifier (URI).

Usage

From source file:org.kalypso.ogc.gml.serialize.GmlSerializer.java

public static GMLWorkspace createGMLWorkspace(final InputSource inputSource, final URL schemaLocationHint,
        final URL context, final IFeatureProviderFactory factory)
        throws ParserConfigurationException, SAXException, IOException, GMLException {
    TimeLogger perfLogger = null;//ww  w  .  j  a  va  2 s .c om
    if (KalypsoCoreDebug.PERF_SERIALIZE_GML.isEnabled())
        perfLogger = new TimeLogger(Messages.getString("org.kalypso.ogc.gml.serialize.GmlSerializer.7")); //$NON-NLS-1$

    final IFeatureProviderFactory providerFactory = factory == null ? DEFAULT_FACTORY : factory;
    final XMLReader xmlReader = createXMLReader();

    // TODO: also set an error handler here

    final GMLorExceptionContentHandler exceptionHandler = new GMLorExceptionContentHandler(xmlReader,
            schemaLocationHint, context, providerFactory);
    xmlReader.setContentHandler(exceptionHandler);

    xmlReader.parse(inputSource);

    final GMLWorkspace workspace = exceptionHandler.getWorkspace();

    if (perfLogger != null) {
        perfLogger.takeInterimTime();
        perfLogger.printCurrentTotal(Messages.getString("org.kalypso.ogc.gml.serialize.GmlSerializer.8")); //$NON-NLS-1$
    }

    return workspace;
}

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.java  2s. c  o 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  w ww.j  av a2  s  .  c  om*/
        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.TriangulatedSurfaceContentHandlerTest.java

private GM_TriangulatedSurface readTriangles(final InputSource is)
        throws IOException, ParserConfigurationException, SAXException {
    final SAXParserFactory saxFac = SAXParserFactory.newInstance();
    saxFac.setNamespaceAware(true);/*from   w w  w. ja va2s. c om*/

    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  w  ww .j av  a 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  w  w .  j a va 2 s.c  om
    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 {//from  www.j  av a 2 s.  c  o  m

        //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.mycore.common.content.transformer.MCRXSLTransformer.java

@Override
public void transform(MCRContent source, OutputStream out, MCRParameterCollector parameter) throws IOException {
    MCRErrorListener el = null;/*from www.  j  av a 2s .  c om*/
    try {
        LinkedList<TransformerHandler> transformHandlerList = getTransformHandlerList(parameter);
        XMLReader reader = getXMLReader(transformHandlerList);
        TransformerHandler lastTransformerHandler = transformHandlerList.getLast();
        el = (MCRErrorListener) lastTransformerHandler.getTransformer().getErrorListener();
        StreamResult result = new StreamResult(out);
        lastTransformerHandler.setResult(result);
        reader.parse(source.getInputSource());
    } catch (TransformerConfigurationException e) {
        throw new IOException(e);
    } catch (IllegalArgumentException e) {
        throw new IOException(e);
    } catch (SAXException e) {
        throw new IOException(e);
    } catch (RuntimeException e) {
        if (el != null && e.getCause() == null && el.getExceptionThrown() != null) {
            //typically if a RuntimeException has no cause, we can get the "real cause" from MCRErrorListener, yeah!!!
            throw new IOException(el.getExceptionThrown());
        }
        throw e;
    }
}

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

protected MCRContent getTransformedContent(MCRContent source, XMLReader reader,
        TransformerHandler transformerHandler) throws IOException, SAXException {
    MCRByteArrayOutputStream baos = new MCRByteArrayOutputStream(INITIAL_BUFFER_SIZE);
    StreamResult serializer = new StreamResult(baos);
    transformerHandler.setResult(serializer);
    // Parse the source XML, and send the parse events to the
    // TransformerHandler.
    LOGGER.info(/* w w  w  .j a  v  a 2  s  .c om*/
            "Start transforming: " + (source.getSystemId() == null ? source.getName() : source.getSystemId()));
    reader.parse(source.getInputSource());
    MCRContent transformedContent = new MCRByteContent(baos.getBuffer(), 0, baos.size());
    return transformedContent;
}

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
    }/*from w  ww  .  j  a v  a2 s .  c o  m*/
}