Example usage for javax.xml.parsers SAXParser parse

List of usage examples for javax.xml.parsers SAXParser parse

Introduction

In this page you can find the example usage for javax.xml.parsers SAXParser parse.

Prototype

public void parse(InputSource is, DefaultHandler dh) throws SAXException, IOException 

Source Link

Document

Parse the content given org.xml.sax.InputSource as XML using the specified org.xml.sax.helpers.DefaultHandler .

Usage

From source file:net.sf.jasperreports.renderers.util.XmlDataSniffer.java

public static XmlSniffResult sniffXml(byte[] data) {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setValidating(false);/* w  ww  . j a  v a  2 s  .  c  o  m*/
    factory.setNamespaceAware(false);
    factory.setXIncludeAware(false);
    setParserFeature(factory, FEATURE_EXTERNAL_GENERAL_ENTITIES, false);
    setParserFeature(factory, FEATURE_EXTERNAL_PARAMETER_ENTITIES, false);
    setParserFeature(factory, FEATURE_LOAD_EXTERNAL_DTD, false);

    SaxHandler handler = new SaxHandler();

    ByteArrayInputStream bais = new ByteArrayInputStream(data);

    try {
        SAXParser saxParser = factory.newSAXParser();
        saxParser.parse(bais, handler);
        return new XmlSniffResult(handler.rootElementName);
    } catch (ValidXmlSAXException e) {
        return new XmlSniffResult(handler.rootElementName);
    } catch (SAXException | ParserConfigurationException | IOException e) {
        return null;
    }
}

From source file:com.truven.runtime.DataTablesParser.java

/**
 * Parses the tables./*www.  j  av a 2  s . c o m*/
 * 
 * @param file
 *            the file
 * @return the hash map
 */
public static HashMap<String, DataTable> parseTables(final File file) {
    DataTablesParser dtp = new DataTablesParser();

    try {
        SAXParserFactory saxfactory = SAXParserFactory.newInstance();
        SAXParser saxParser = saxfactory.newSAXParser();
        saxParser.parse(file, dtp);
    } catch (Exception e) {
        e.printStackTrace();
    }

    return dtp.getTables();

}

From source file:Main.java

public static void saxParserValidation(InputStream streamDaValidare, InputStream inputSchema,
        DefaultHandler handler) throws Exception {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setNamespaceAware(true);//from   ww  w .j a  va2 s  .  co m
    if (inputSchema != null) {
        factory.setValidating(true);
    } else {
        factory.setValidating(false);
    }
    SAXParser parser = factory.newSAXParser();
    if (inputSchema != null) {
        parser.setProperty(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
        parser.setProperty(JAXP_SCHEMA_SOURCE, inputSchema);
    }
    parser.parse(streamDaValidare, handler);
}

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 ava 2  s  .co  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:com.microsoft.tfs.core.clients.workitem.internal.form.WIFormParseHandler.java

public static WIFormElement parse(final String xml) {
    final WIFormParseHandler handler = new WIFormParseHandler();

    try {/* w  w w  .  ja v a  2 s .  co m*/
        final SAXParser parser = SAXUtils.newSAXParser();
        parser.parse(new InputSource(new StringReader(xml)), handler);
    } catch (final SAXException ex) {
        ex.initCause(ex.getException());
        throw new RuntimeException(ex);
    } catch (final Exception ex) {
        throw new RuntimeException(ex);
    }

    return handler.root;
}

From source file:com.microsoft.tfs.core.util.CodePageData.java

private static void initialize() {
    initialized = true;/*from   www .ja  v  a2s . co  m*/

    final InputStream inputStream = CodePageData.class.getResourceAsStream("codepages.xml"); //$NON-NLS-1$

    if (inputStream == null) {
        log.error("Unable to load the codepages.xml resource"); //$NON-NLS-1$
        return;
    }

    try {
        final SAXParser parser = SAXUtils.newSAXParser();
        final InputSource inputSource = new InputSource(inputStream);
        parser.parse(inputSource, new CodePagesHandler());
    } catch (final Exception e) {
        log.error(Messages.getString("CodePageData.ErrorDuringParsing"), e); //$NON-NLS-1$
    } finally {
        try {
            inputStream.close();
        } catch (final IOException e) {
        }
    }
}

From source file:com.shin1ogawa.appengine.marketplace.gdata.LicensingAPI.java

static List<Map<String, String>> parseLicenseFeed(InputStream is)
        throws SAXException, IOException, ParserConfigurationException {
    SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
    final List<Map<String, String>> list = Lists.newArrayList();
    parser.parse(is, new DefaultHandler() {

        boolean entity = false;

        String currentElement;/*from  w w  w  .ja v  a  2  s . co m*/

        Map<String, String> map;

        @Override
        public void characters(char[] ch, int start, int length) {
            if (entity) {
                map.put(currentElement, new String(ch, start, length));
            }
        }

        @Override
        public void startElement(String uri, String localName, String qName, Attributes attributes) {
            if (entity == false && StringUtils.equals(qName, "entity")) {
                entity = true;
                map = Maps.newHashMap();
                list.add(map);
                return;
            }
            currentElement = qName;
        }

        @Override
        public void endElement(String uri, String localName, String qName) {
            if (entity && StringUtils.equals(qName, "entity")) {
                entity = false;
            }
        }
    });
    return list;
}

From source file:eu.annocultor.utils.XmlUtils.java

/**
 * /*from  w ww  . j  av  a2 s.c o m*/
 * @param fileStream
 * @param handler
 * @param validating
 * @return <code>true</code> on success.
 * @throws Exception
 */
public static int parseXmlFileSAX(File sourceFile, ConverterHandler handler, boolean validating)
        throws Exception {
    // Create a builder factory
    SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setValidating(validating);
    factory.setNamespaceAware(true);

    InputStream fileStream = new BufferedInputStream(new FileInputStream(sourceFile), 1024 * 1024);

    if (fileStream == null) {
        throw new Exception("Null input XML file stream");
    }
    if (handler == null) {
        throw new Exception("Null XML handler");
    }
    try {
        // Create the builder and parse the file
        SAXParser newSAXParser = factory.newSAXParser();
        if (newSAXParser == null) {
            throw new Exception("null SAX parser");
        }
        newSAXParser.parse(fileStream, handler);
    } catch (Exception e) {
        System.err.println("\n" + "*****************************************************\n"
                + "EXCEPTION OCCURRED in file " + sourceFile.getCanonicalPath() + "\n" + "at line "
                + handler.getDocumentLocator().getLineNumber() + ", column "
                + handler.getDocumentLocator().getColumnNumber());
        e.printStackTrace();
        System.err.println("\n" + "TRYING TO CLOSE FILES GRACEFULLY \n"
                + "*****************************************************\n");
        return -1;
    }
    return 0;
}

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  .ja va  2 s .  co  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:net.sf.ehcache.config.ConfigurationFactory.java

/**
 * Configures a bean from an XML input stream.
 *//*  w ww  .ja v  a  2s.co m*/
public static Configuration parseConfiguration(final InputStream inputStream) throws CacheException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Configuring ehcache from InputStream");
    }

    Configuration configuration = new Configuration();
    try {
        InputStream translatedInputStream = translateSystemProperties(inputStream);
        final SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
        final BeanHandler handler = new BeanHandler(configuration);
        parser.parse(translatedInputStream, handler);
    } catch (Exception e) {
        throw new CacheException("Error configuring from input stream. Initial cause was " + e.getMessage(), e);
    }
    return configuration;
}