Example usage for javax.xml.parsers SAXParserFactory newSAXParser

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

Introduction

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

Prototype


public abstract SAXParser newSAXParser() throws ParserConfigurationException, SAXException;

Source Link

Document

Creates a new instance of a SAXParser using the currently configured factory parameters.

Usage

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

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

    final SAXParserFactory factory = SAXParserFactory.newInstance();

    try {// ww w .jav 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//  w w w  .  j  a  va2 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./* www  .  j  av  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.gluu.oxtrust.ldap.service.Shibboleth2ConfService.java

private EntityIDHandler parseMetadata(File metadataFile) {
    if (!metadataFile.exists()) {
        log.error("Failed to get entityId from metadata file '{0}'", metadataFile.getAbsolutePath());
        return null;
    }/*  w  ww.  j a v  a  2 s  .  c  o m*/

    InputStream is = null;
    InputStreamReader isr = null;
    EntityIDHandler handler = null;
    try {
        is = FileUtils.openInputStream(metadataFile);
        SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
        SAXParser saxParser = saxParserFactory.newSAXParser();

        handler = new EntityIDHandler();
        is = FileUtils.openInputStream(metadataFile);
        saxParser.parse(is, handler);

    } catch (IOException ex) {
        log.error("Failed to read metadata file '{0}'", ex, metadataFile.getAbsolutePath());
    } catch (ParserConfigurationException e) {
        log.error("Failed to confugure SAX parser for file '{0}'", e, metadataFile.getAbsolutePath());
    } catch (SAXException e) {
        log.error("Failed to parse file '{0}'", e, metadataFile.getAbsolutePath());
    } finally {
        IOUtils.closeQuietly(isr);
        IOUtils.closeQuietly(is);
    }

    return handler;
}

From source file:org.gluu.saml.metadata.SAMLMetadataParser.java

public static EntityIDHandler parseMetadata(InputStream is) {
    InputStreamReader isr = null;
    EntityIDHandler handler = null;/*w w  w  . j ava2  s  . co  m*/
    try {
        SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
        SAXParser saxParser = saxParserFactory.newSAXParser();

        handler = new EntityIDHandler();
        saxParser.parse(is, handler);
    } catch (IOException ex) {
        log.error("Failed to read SAML metadata", ex);
    } catch (ParserConfigurationException e) {
        log.error("Failed to confugure SAX parser", e);
    } catch (SAXException e) {
        log.error("Failed to parse SAML metadata", e);
    } finally {
        IOUtils.closeQuietly(isr);
        IOUtils.closeQuietly(is);
    }

    return handler;
}

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

/**
 * Method to parse XML response from Yahoo URL and add markers of restaurants on the map
 *//*  w ww  .  java2s.  c  o  m*/
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.ical.ICalendarUtil.java

public static Reader convertRdf2Ics(InputStream is) throws SAXException, IOException {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setValidating(false);/*  ww  w . ja v  a2s  . com*/
    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.intermine.webservice.server.ServicesListingsServlet.java

private SAXParser getParser() {
    SAXParserFactory fac = SAXParserFactory.newInstance();
    SAXParser parser = null;// ww  w  .  j  av a2  s  .  c o  m
    try {
        parser = fac.newSAXParser();
    } catch (SAXException e) {
        LOGGER.error(e);
    } catch (ParserConfigurationException e) {
        LOGGER.error(e);
    }
    if (parser == null) {
        throw new InternalErrorException("Could not create a SAX parser");
    }
    return parser;
}

From source file:org.intermine.webservice.server.widget.ReportWidgetsServlet.java

private SAXParser parser() {
    SAXParserFactory fac = SAXParserFactory.newInstance();
    SAXParser parser = null;//from ww w . j a  va  2  s. c  o m
    try {
        parser = fac.newSAXParser();
    } catch (SAXException e) {

    } catch (ParserConfigurationException e) {
    }
    if (parser == null) {
        throw new InternalErrorException("Could not create a SAX parser");
    }
    return parser;
}

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

public void parse(StreamSource source) throws IOException {
    try {//www.  j a va2  s .  com
        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());
    }
}