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:bbcdataservice.BBCProgrammesParser.java

protected static boolean parse(HashMap<Date, MutableChannelDayProgram> dayPrograms, File file, Channel channel,
        Date date) throws Exception {
    mHasNextDay = false;//from   w  ww  .  j  a va  2  s.c  o  m
    SAXParserFactory fac = SAXParserFactory.newInstance();
    fac.setValidating(false);
    SAXParser sax = fac.newSAXParser();
    sax.parse(file, new BBCProgrammesParser(dayPrograms, channel, date));
    return mHasNextDay;
}

From source file:org.apache.clerezza.commons.rdf.impl.sparql.SparqlResultParser.java

static Object parse(InputStream in) throws IOException {
    try {//from  w w w. j  a  v  a 2 s.co m
        SAXParserFactory spf = SAXParserFactory.newInstance();
        spf.setNamespaceAware(true);
        SAXParser saxParser = spf.newSAXParser();
        XMLReader xmlReader = saxParser.getXMLReader();
        final SparqlsResultsHandler sparqlsResultsHandler = new SparqlsResultsHandler();
        xmlReader.setContentHandler(sparqlsResultsHandler);
        xmlReader.parse(new InputSource(in));
        return sparqlsResultsHandler.getResults();
    } catch (ParserConfigurationException | SAXException ex) {
        throw new RuntimeException(ex);
    }
}

From source file:de.unisb.cs.st.javalanche.rhino.coverage.CoberturaParser.java

public static void parseXmlFile(File file, DefaultHandler handler, boolean validating) {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setValidating(validating);/*from  w  w  w.j a  va 2  s  .c om*/

    try {
        factory.newSAXParser().parse(file, handler);
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    }
}

From source file:info.unyttig.helladroid.newzbin.NewzBinController.java

/**
 * Fetches a report from Newzbin based on a given id.
 * However if the report is already cached, its just fetched from the hashmap.
 * @param id//w  ww.  ja v  a  2s . c  om
 */
public static NewzBinReport getReportInfo(int id) {
    if (detailedReports.containsKey(id))
        return detailedReports.get(id);

    String url = NBAPIURL + "reportinfo/";
    HashMap<String, String> searchOptions = new HashMap<String, String>();
    searchOptions.put("id", "" + id);
    try {
        HttpResponse response = doPost(url, searchOptions);
        checkReturnCode(response.getStatusLine().getStatusCode(), false);
        InputStream is = response.getEntity().getContent();

        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();
        XMLReader xr = sp.getXMLReader();
        NewzBinDRHandler handler = new NewzBinDRHandler();
        if (reports.containsKey(id))
            handler.nbdr = reports.get(id);
        xr.setContentHandler(handler);
        xr.parse(new InputSource(is));
        detailedReports.put(id, handler.getParsedData());
        // Temp
        ArrayList<NewzBinReportComment> comments = handler.nbdr.getComments();
        Log.i(LOG_NAME, "Comments size: " + comments.size());
        Iterator<NewzBinReportComment> sd = comments.iterator();
        while (sd.hasNext()) {
            NewzBinReportComment nrc = sd.next();
            Log.i(LOG_NAME, nrc.toString());
        }
        return handler.getParsedData();
    } catch (ClientProtocolException e) {
        Log.e(LOG_NAME, "ClientProtocol thrown: ", e);
    } catch (IOException e) {
        Log.e(LOG_NAME, "IOException thrown: ", e);
    } catch (NewzBinPostReturnCodeException e) {
        Log.e(LOG_NAME, "POST ReturnCode error: " + e.toString());
    } catch (ParserConfigurationException e) {
        Log.e(LOG_NAME, "ParserError thrown: ", e);
    } catch (SAXException e) {
        Log.e(LOG_NAME, "SAXError thrown: ", e);
    }
    return null;
}

From source file:com.haulmont.bali.util.Dom4j.java

public static SAXParser getParser() {
    SAXParser parser = saxParserHolder.get();
    if (parser == null) {
        SAXParserFactory factory = SAXParserFactory.newInstance();
        factory.setValidating(false);//from w  w  w  .  ja  v a  2 s .c o m
        factory.setNamespaceAware(false);
        try {
            parser = factory.newSAXParser();
        } catch (ParserConfigurationException | SAXException e) {
            throw new RuntimeException("Unable to create SAX parser", e);
        }
        saxParserHolder.set(parser);
    }
    return parser;
}

From source file:Main.java

/**
 * Constructs a secure SAX Parser.//w w w.j a  v a  2s . c  o m
 *
 * @return a SAX Parser
 * @throws ParserConfigurationException thrown if there is a parser
 * configuration exception
 * @throws SAXNotRecognizedException thrown if there is an unrecognized
 * feature
 * @throws SAXNotSupportedException thrown if there is a non-supported
 * feature
 * @throws SAXException is thrown if there is a SAXException
 */
public static SAXParser buildSecureSaxParser()
        throws ParserConfigurationException, SAXNotRecognizedException, SAXNotSupportedException, SAXException {
    final SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
    factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
    factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
    return factory.newSAXParser();
}

From source file:ch.cern.security.saml2.utils.xml.XMLUtils.java

/**
 * Creates a SamlVO instance with the minimum data for performing the SLO  
 * //from  w w  w . j a  va  2s . c  o  m
 * @param xmlLogoutRequest
 * @param isDebugEnabled
 * @return SamlVO instance
 * @throws ParserConfigurationException
 * @throws SAXException
 * @throws IOException
 */
public static SamlVO parseXMLmessage(String xmlLogoutRequest, boolean isDebugEnabled)
        throws ParserConfigurationException, SAXException, IOException {

    // Parse the XML. SAX approach, we just need the ID attribute
    SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();

    // If we want to validate the doc we need to load the DTD
    // saxParserFactory.setValidating(true);

    // Get a SAXParser instance
    SAXParser saxParser = saxParserFactory.newSAXParser();

    // Parse it
    XMLhandler xmLhandler = new XMLhandler(isDebugEnabled);
    saxParser.parse(new ByteArrayInputStream(xmlLogoutRequest.getBytes()), xmLhandler);

    // Return the SamlVO
    return xmLhandler.getSamlVO();
}

From source file:architecture.common.model.factory.ModelTypeFactory.java

private static List<ModelList> parseLegacyXmlFile(List<ModelList> list) throws Exception {
    ClassLoader cl = Thread.currentThread().getContextClassLoader();
    Enumeration<URL> enumeration = cl.getResources(IF_PLUGIN_PATH);
    SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setNamespaceAware(true);//w  w  w .ja  v a 2 s .  co  m
    XMLReader xmlreader = factory.newSAXParser().getXMLReader();
    ImplFactoryParsingHandler handler = new ImplFactoryParsingHandler();
    xmlreader.setContentHandler(handler);
    xmlreader.setDTDHandler(handler);
    xmlreader.setEntityResolver(handler);
    xmlreader.setErrorHandler(handler);
    System.out.println("Model Enum:");
    do {
        if (!enumeration.hasMoreElements())
            break;
        URL url = (URL) enumeration.nextElement();
        System.out.println(" - " + url);
        try {
            xmlreader.parse(new InputSource(url.openStream()));
            ModelList factorylist = new ModelList();
            factorylist.rank = handler.rank;
            factorylist.modelTypes.addAll(handler.getModels());
            list.add(factorylist);
        } catch (Exception exception) {
        }
    } while (true);

    return list;
}

From source file:Main.java

/**
 * Convenience method: creates and returns a SAXParser.
 * /*  www.  j a v a 2 s. co m*/
 * @param namespaceAware specifies whether the parser produced 
 * by this code will provide support for XML namespaces
 * @param validating specifies whether the parser produced by 
 * this code will validate documents against their DTD
 * @param xIncludeAware specifies whether the parser produced by 
 * this code will process XIncludes
 * @return newly created SAXParser
 * @exception ParserConfigurationException if a parser cannot be created 
 * which satisfies the requested configuration
 * @exception SAXException for SAX errors
 */
public static SAXParser newSAXParser(boolean namespaceAware, boolean validating, boolean xIncludeAware)
        throws ParserConfigurationException, SAXException {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setNamespaceAware(namespaceAware);
    factory.setValidating(validating);
    factory.setXIncludeAware(xIncludeAware);

    // For Xerces which otherwise, does not support "x-MacRoman".
    try {
        factory.setFeature("http://apache.org/xml/features/allow-java-encodings", true);
    } catch (Exception ignored) {
    }

    return factory.newSAXParser();
}

From source file:gov.nih.nci.ncicb.tcga.dcc.common.jaxb.JAXBUtil.java

private static SAXSource applyMetaDataNamespaceFilter(final Unmarshaller unmarshaller,
        final Reader xmlFileReader) throws SAXException, ParserConfigurationException, FileNotFoundException {

    final SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setNamespaceAware(true); // this should not be changed!
    final XMLReader reader = factory.newSAXParser().getXMLReader();
    final XMLFilterImpl xmlFilter = new MetaDataXMLNamespaceFilter(reader);
    reader.setContentHandler(unmarshaller.getUnmarshallerHandler());

    return new SAXSource(xmlFilter, new InputSource(xmlFileReader));
}