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.exoplatform.services.document.impl.MSXPPTDocumentReader.java

/**
 * Extracts the text content of the n first slides 
 *//*  w  w w  .java  2 s. c o  m*/
public String getContentAsText(InputStream is, int maxSlides) throws IOException, DocumentReadException {
    if (is == null) {
        throw new IllegalArgumentException("InputStream is null.");
    }
    StringBuilder appendText = new StringBuilder();
    try {

        int slideCount = 0;
        ZipInputStream zis = new ZipInputStream(is);

        try {
            ZipEntry ze = zis.getNextEntry();

            if (ze == null)
                return "";

            XMLReader xmlReader = SAXHelper.newXMLReader();
            xmlReader.setFeature("http://xml.org/sax/features/validation", false);
            xmlReader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
            Map<Integer, String> slides = new TreeMap<Integer, String>();
            // PPTX: ppt/slides/slide<slide_no>.xml
            while (ze != null && slideCount < maxSlides) {
                String zeName = ze.getName();
                if (zeName.startsWith(PPTX_SLIDE_PREFIX) && zeName.length() > PPTX_SLIDE_PREFIX.length()) {
                    String slideNumberStr = zeName.substring(PPTX_SLIDE_PREFIX.length(),
                            zeName.indexOf(".xml"));
                    int slideNumber = -1;
                    try {
                        slideNumber = Integer.parseInt(slideNumberStr);
                    } catch (NumberFormatException e) {
                        LOG.warn("Could not parse the slide number: " + e.getMessage());
                    }
                    if (slideNumber > -1 && slideNumber <= maxSlides) {
                        MSPPTXContentHandler contentHandler = new MSPPTXContentHandler();
                        xmlReader.setContentHandler(contentHandler);
                        xmlReader.parse(new InputSource((new ByteArrayInputStream(IOUtils.toByteArray(zis)))));
                        slides.put(slideNumber, contentHandler.getContent());
                        slideCount++;
                    }
                }
                ze = zis.getNextEntry();
            }
            for (String slide : slides.values()) {
                appendText.append(slide);
                appendText.append(' ');
            }
        } finally {
            try {
                zis.close();
            } catch (IOException e) {
                if (LOG.isTraceEnabled()) {
                    LOG.trace("An exception occurred: " + e.getMessage());
                }
            }
        }
        return appendText.toString();
    } catch (ParserConfigurationException e) {
        throw new DocumentReadException(e.getMessage(), e);
    } catch (SAXException e) {
        throw new DocumentReadException(e.getMessage(), e);
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            if (LOG.isTraceEnabled()) {
                LOG.trace("An exception occurred: " + e.getMessage());
            }
        }
    }
}

From source file:org.exoplatform.services.document.impl.MSXPPTDocumentReader.java

public Properties getProperties(InputStream is) throws IOException, DocumentReadException {
    try {/*w w  w .j a v  a 2s  . c o  m*/

        ZipInputStream zis = new ZipInputStream(is);
        try {
            ZipEntry ze = zis.getNextEntry();

            while (ze != null && !ze.getName().equals(PPTX_CORE_NAME)) {
                ze = zis.getNextEntry();
            }

            if (ze == null)
                return new Properties();

            MSPPTXMetaHandler metaHandler = new MSPPTXMetaHandler();
            XMLReader xmlReader = SAXHelper.newXMLReader();

            xmlReader.setFeature("http://xml.org/sax/features/validation", false);
            xmlReader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
            xmlReader.setFeature("http://xml.org/sax/features/namespaces", true);
            xmlReader.setContentHandler(metaHandler);
            xmlReader.parse(new InputSource(zis));
            return metaHandler.getProperties();
        } finally {
            zis.close();
        }

    } catch (ParserConfigurationException e) {
        throw new DocumentReadException(e.getMessage(), e);
    } catch (SAXException e) {
        throw new DocumentReadException(e.getMessage(), e);
    } finally {
        if (is != null)
            try {
                is.close();
            } catch (IOException e) {
                if (LOG.isTraceEnabled()) {
                    LOG.trace("An exception occurred: " + e.getMessage());
                }
            }
    }
}

From source file:org.expath.exist.ftclient.GetResourceMetadataFunction.java

@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {

    Sequence result = new ValueSequence();

    StreamResult resultAsStreamResult = null;

    try {/*  w  w w  .  ja  v  a 2 s.  co m*/
        resultAsStreamResult = ro.kuberam.libs.java.ftclient.GetResourceMetadata
                .getResourceMetadata(ExistExpathFTClientModule.retrieveRemoteConnection(context,
                        ((IntegerValue) args[0].itemAt(0)).getLong()), args[1].getStringValue());
    } catch (Exception ex) {
        throw new XPathException(ex.getMessage());
    }

    ByteArrayInputStream resultDocAsInputStream = null;
    try {
        resultDocAsInputStream = new ByteArrayInputStream(
                resultAsStreamResult.getWriter().toString().getBytes("UTF-8"));
    } catch (UnsupportedEncodingException ex) {
        throw new XPathException(ex.getMessage());
    }

    XMLReader reader = null;

    context.pushDocumentContext();
    try {
        InputSource src = new InputSource(new CloseShieldInputStream(resultDocAsInputStream));

        reader = context.getBroker().getBrokerPool().getParserPool().borrowXMLReader();
        MemTreeBuilder builder = context.getDocumentBuilder();
        DocumentBuilderReceiver receiver = new DocumentBuilderReceiver(builder, true);
        reader.setContentHandler(receiver);
        reader.parse(src);
        Document doc = receiver.getDocument();

        result = (NodeValue) doc;
    } catch (SAXException saxe) {
        // do nothing, we will default to trying to return a string below
    } catch (IOException ioe) {
        // do nothing, we will default to trying to return a string below
    } finally {
        context.popDocumentContext();

        if (reader != null) {
            context.getBroker().getBrokerPool().getParserPool().returnXMLReader(reader);
        }
    }

    return result;
}

From source file:org.getobjects.foundation.NSXMLPropertyListParser.java

public Object parse(InputStream _in) {
    if (_in == null)
        return null;

    final SAXParserFactory factory = SAXParserFactory.newInstance();

    try {//  w  ww.j a v a 2  s.  c  o  m
        final NSXMLPlistHandler handler = new NSXMLPlistHandler();
        final XMLReader reader = factory.newSAXParser().getXMLReader();
        final InputSource input = new InputSource(_in);
        reader.setContentHandler(handler);
        reader.setEntityResolver(handler);
        try {
            reader.setFeature("http://xml.org/sax/features/validation", false);
            reader.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
        } catch (Exception e) {
            log.error("Couldn't turn validation off: " + e);
        }
        reader.parse(input);
        return handler.rootObject();
    } catch (ParserConfigurationException e) {
        log.error("error during parser instantiation: " + e);
    } catch (SAXException e) {
        log.error("error during parsing: " + e);
    } catch (IOException e) {
        log.error("error during parsing: " + e);
    }
    return null;
}

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  a v a  2  s.c o 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./*from  w w w  .  j a va  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.h2gis.drivers.osm.OSMParser.java

/**
 * Read the OSM file and create its corresponding tables.
 *
 * @param inputFile/*w w  w  . j av a 2s  . c  o m*/
 * @param tableName
 * @param connection
 * @param progress
 * @return
 * @throws SQLException
 */
public boolean read(Connection connection, String tableName, File inputFile, ProgressVisitor progress)
        throws SQLException {
    this.progress = progress.subProcess(100);
    // Initialisation
    final boolean isH2 = JDBCUtilities.isH2DataBase(connection.getMetaData());
    boolean success = false;
    TableLocation requestedTable = TableLocation.parse(tableName, isH2);
    String osmTableName = requestedTable.getTable();
    checkOSMTables(connection, isH2, requestedTable, osmTableName);
    createOSMDatabaseModel(connection, isH2, requestedTable, osmTableName);

    FileInputStream fs = null;
    try {
        fs = new FileInputStream(inputFile);
        this.fc = fs.getChannel();
        this.fileSize = fc.size();
        // Given the file size and an average node file size.
        // Skip how many nodes in order to update progression at a step of 1%
        readFileSizeEachNode = Math.max(1, (this.fileSize / AVERAGE_NODE_SIZE) / 100);
        nodeCountProgress = 0;
        XMLReader parser = XMLReaderFactory.createXMLReader();
        parser.setErrorHandler(this);
        parser.setContentHandler(this);
        if (inputFile.getName().endsWith(".osm")) {
            parser.parse(new InputSource(fs));
        } else if (inputFile.getName().endsWith(".osm.gz")) {
            parser.parse(new InputSource(new GZIPInputStream(fs)));
        } else if (inputFile.getName().endsWith(".osm.bz2")) {
            parser.parse(new InputSource(new BZip2CompressorInputStream(fs)));
        }
        success = true;
    } catch (SAXException ex) {
        throw new SQLException(ex);
    } catch (IOException ex) {
        throw new SQLException("Cannot parse the file " + inputFile.getAbsolutePath(), ex);
    } finally {
        try {
            if (fs != null) {
                fs.close();
            }
        } catch (IOException ex) {
            throw new SQLException("Cannot close the file " + inputFile.getAbsolutePath(), ex);
        }
        // When the reading ends, close() method has to be called
        if (nodePreparedStmt != null) {
            nodePreparedStmt.close();
        }
        if (nodeTagPreparedStmt != null) {
            nodeTagPreparedStmt.close();
        }
        if (wayPreparedStmt != null) {
            wayPreparedStmt.close();
        }
        if (wayTagPreparedStmt != null) {
            wayTagPreparedStmt.close();
        }
        if (wayNodePreparedStmt != null) {
            wayNodePreparedStmt.close();
        }
        if (relationPreparedStmt != null) {
            relationPreparedStmt.close();
        }
        if (relationTagPreparedStmt != null) {
            relationTagPreparedStmt.close();
        }
        if (nodeMemberPreparedStmt != null) {
            nodeMemberPreparedStmt.close();
        }
        if (wayMemberPreparedStmt != null) {
            wayMemberPreparedStmt.close();
        }
        if (relationMemberPreparedStmt != null) {
            relationMemberPreparedStmt.close();
        }
        if (tagPreparedStmt != null) {
            tagPreparedStmt.close();
        }
    }

    return success;
}

From source file:org.h2gis.functions.io.osm.OSMParser.java

/**
 * Read the OSM file and create its corresponding tables.
 *
 * @param inputFile//w  w  w.java2  s . c om
 * @param tableName
 * @param connection
 * @param progress
 * @return
 * @throws SQLException
 */
public boolean read(Connection connection, String tableName, File inputFile, ProgressVisitor progress)
        throws SQLException {
    this.progress = progress.subProcess(100);
    // Initialisation
    final boolean isH2 = JDBCUtilities.isH2DataBase(connection.getMetaData());
    boolean success = false;
    TableLocation requestedTable = TableLocation.parse(tableName, isH2);
    String osmTableName = requestedTable.getTable();
    checkOSMTables(connection, isH2, requestedTable, osmTableName);
    createOSMDatabaseModel(connection, isH2, requestedTable, osmTableName);

    FileInputStream fs = null;
    try {
        fs = new FileInputStream(inputFile);
        this.fc = fs.getChannel();
        this.fileSize = fc.size();
        // Given the file size and an average node file size.
        // Skip how many nodes in order to update progression at a step of 1%
        readFileSizeEachNode = Math.max(1, (this.fileSize / AVERAGE_NODE_SIZE) / 100);
        nodeCountProgress = 0;
        XMLReader parser = XMLReaderFactory.createXMLReader();
        parser.setErrorHandler(this);
        parser.setContentHandler(this);
        if (inputFile.getName().endsWith(".osm")) {
            parser.parse(new InputSource(fs));
        } else if (inputFile.getName().endsWith(".osm.gz")) {
            parser.parse(new InputSource(new GZIPInputStream(fs)));
        } else if (inputFile.getName().endsWith(".osm.bz2")) {
            parser.parse(new InputSource(new BZip2CompressorInputStream(fs)));
        } else {
            throw new SQLException("Supported formats are .osm, .osm.gz, .osm.bz2");
        }
        success = true;
    } catch (SAXException ex) {
        throw new SQLException(ex);
    } catch (IOException ex) {
        throw new SQLException("Cannot parse the file " + inputFile.getAbsolutePath(), ex);
    } finally {
        try {
            if (fs != null) {
                fs.close();
            }
        } catch (IOException ex) {
            throw new SQLException("Cannot close the file " + inputFile.getAbsolutePath(), ex);
        }
        // When the reading ends, close() method has to be called
        if (nodePreparedStmt != null) {
            nodePreparedStmt.close();
        }
        if (nodeTagPreparedStmt != null) {
            nodeTagPreparedStmt.close();
        }
        if (wayPreparedStmt != null) {
            wayPreparedStmt.close();
        }
        if (wayTagPreparedStmt != null) {
            wayTagPreparedStmt.close();
        }
        if (wayNodePreparedStmt != null) {
            wayNodePreparedStmt.close();
        }
        if (relationPreparedStmt != null) {
            relationPreparedStmt.close();
        }
        if (relationTagPreparedStmt != null) {
            relationTagPreparedStmt.close();
        }
        if (nodeMemberPreparedStmt != null) {
            nodeMemberPreparedStmt.close();
        }
        if (wayMemberPreparedStmt != null) {
            wayMemberPreparedStmt.close();
        }
        if (relationMemberPreparedStmt != null) {
            relationMemberPreparedStmt.close();
        }
        if (tagPreparedStmt != null) {
            tagPreparedStmt.close();
        }
    }

    return success;
}

From source file:org.hw.parlance.ParlanceActivity.java

/**
 * Method to parse XML response from Yahoo URL and add markers of restaurants on the map
 *//*from w ww .j  av a  2s .c  om*/
private synchronized void xmlParsing(String XMLResponse) {
    try {
        URLConnection conn = new URL(XMLResponse).openConnection();
        conn.setConnectTimeout(50000);
        conn.setReadTimeout(50000);
        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();
        XMLReader xr = sp.getXMLReader();
        ItemHandler itemHandler = new ItemHandler();
        xr.setContentHandler(itemHandler);
        xr.parse(new InputSource(conn.getInputStream()));

        _itemAdapter.arr = itemHandler.getList();

        for (int index = 0; index < _itemAdapter.arr.size(); index++) {

            Marker temp_marker = mMap.addMarker(new MarkerOptions()
                    .position(new LatLng(Double.parseDouble(_itemAdapter.arr.get(index).getLat()),
                            Double.parseDouble(_itemAdapter.arr.get(index).getLon())))
                    .title(_itemAdapter.arr.get(index).getName())
                    .icon(BitmapDescriptorFactory.fromResource(numMarkerIcon[index])));

            this._markList.add(temp_marker);
        }

        _itemAdapter.notifyDataSetChanged();

    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ParserConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SAXException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:org.infoscoop.request.filter.DetectTypeFilter.java

public static void main(String[] args) throws Exception {
    new DetectTypeFilter();

    DetectManager h = new DetectTypeFilter.DetectManager();
    XMLReader reader = factory.newSAXParser().getXMLReader();
    reader.setEntityResolver(NoOpEntityResolver.getInstance());
    reader.setContentHandler(h);
    reader.parse(new InputSource(new FileInputStream("src/main/webapp/generalMessages_rss.xml")));

}