Example usage for javax.xml.parsers SAXParser getXMLReader

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

Introduction

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

Prototype


public abstract org.xml.sax.XMLReader getXMLReader() throws SAXException;

Source Link

Document

Returns the org.xml.sax.XMLReader that is encapsulated by the implementation of this class.

Usage

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

private static XMLReader createXMLReader() throws ParserConfigurationException, SAXException {
    final SAXParserFactory saxFac = SAXParserFactory.newInstance();
    saxFac.setNamespaceAware(true);/*  w  w w  .ja va  2  s  . c om*/

    final SAXParser saxParser = saxFac.newSAXParser();
    // make namespace-prefixes visible to content handler
    // used to allow necessary schemas from gml document
    final XMLReader xmlReader = saxParser.getXMLReader();
    xmlReader.setFeature("http://xml.org/sax/features/namespace-prefixes", Boolean.TRUE); //$NON-NLS-1$
    return xmlReader;
}

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 . j  a v  a 2 s .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 w  w .  java 2s .co  m*/
        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 ww  w . ja v a 2s  . c  o  m

    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.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;//  w w  w . j  a va 2 s . c o  m
    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.languagetool.rules.patterns.FalseFriendRuleLoader.java

public final List<PatternRule> getRules(final InputStream file, final Language textLanguage,
        final Language motherTongue) throws ParserConfigurationException, SAXException, IOException {
    final FalseFriendRuleHandler handler = new FalseFriendRuleHandler(textLanguage, motherTongue);
    final SAXParserFactory factory = SAXParserFactory.newInstance();
    final SAXParser saxParser = factory.newSAXParser();
    saxParser.getXMLReader().setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd",
            false);/* w  ww  .  jav  a 2s  .c om*/
    saxParser.parse(file, handler);
    final List<PatternRule> rules = handler.getRules();
    // Add suggestions to each rule:
    final ResourceBundle messages = ResourceBundle.getBundle(JLanguageTool.MESSAGE_BUNDLE,
            motherTongue.getLocale());
    for (final PatternRule rule : rules) {
        final List<String> suggestionMap = handler.getSuggestionMap().get(rule.getId());
        if (suggestionMap != null) {
            final MessageFormat msgFormat = new MessageFormat(messages.getString("false_friend_suggestion"));
            final Object[] msg = { formatSuggestions(suggestionMap) };
            rule.setMessage(rule.getMessage() + " " + msgFormat.format(msg));
        }
    }
    return rules;
}

From source file:org.mule.config.spring.MuleDocumentLoader.java

protected XMLReader createSaxAnnotator(Document doc) throws ParserConfigurationException, SAXException {
    SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
    SAXParser saxParser = saxParserFactory.newSAXParser();
    XMLReader documentReader = saxParser.getXMLReader();
    documentReader.setContentHandler(new XmlMetadataAnnotator(doc));
    return documentReader;
}

From source file:org.olat.ims.qti.qpool.ItemFileResourceValidator.java

private boolean validateDocument(Document in) {
    try {/*ww w .j  ava2 s  .  c o m*/
        SAXParserFactory factory = SAXParserFactory.newInstance();
        factory.setValidating(true);
        factory.setNamespaceAware(true);

        SimpleErrorHandler errorHandler = new SimpleErrorHandler();
        ItemContentHandler contentHandler = new ItemContentHandler();

        SAXParser parser = factory.newSAXParser();
        XMLReader reader = parser.getXMLReader();
        reader.setEntityResolver(new IMSEntityResolver());
        reader.setErrorHandler(errorHandler);
        reader.setContentHandler(contentHandler);

        SAXValidator validator = new SAXValidator(reader);
        validator.validate(in);

        return errorHandler.isValid() && contentHandler.isItem();
    } catch (ParserConfigurationException e) {
        return false;
    } catch (SAXException e) {
        return false;
    } catch (Exception e) {
        return false;
    }
}

From source file:org.openbravo.test.webservice.BaseWSTest.java

/**
 * Validates the xml against the generated schema.
 * /*from  w w  w .  j  a  v  a  2  s.c o  m*/
 * @param xml
 *          the xml to validate
 */
protected void validateXML(String xml) {
    final Reader schemaReader = new StringReader(getXMLSchema());
    final Reader xmlReader = new StringReader(xml);
    try {
        SAXParserFactory factory = SAXParserFactory.newInstance();
        factory.setValidating(false);
        factory.setNamespaceAware(true);

        SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");

        factory.setSchema(schemaFactory.newSchema(new Source[] { new StreamSource(schemaReader) }));

        SAXParser parser = factory.newSAXParser();

        XMLReader reader = parser.getXMLReader();
        reader.setErrorHandler(new SimpleErrorHandler());
        reader.parse(new InputSource(xmlReader));
    } catch (Exception e) {
        throw new OBException(e);
    }

}

From source file:org.owasp.dependencycheck.xml.pom.PomParser.java

/**
 * Parses the given XML file and returns a Model object containing only the
 * fields dependency-check requires.// w  ww.j av  a 2s . co  m
 *
 * @param inputStream an InputStream containing suppression rues
 * @return a list of suppression rules
 * @throws PomParseException if the XML cannot be parsed
 */
public Model parse(InputStream inputStream) throws PomParseException {
    try {
        final PomHandler handler = new PomHandler();
        final SAXParser saxParser = XmlUtils.buildSecureSaxParser();
        final XMLReader xmlReader = saxParser.getXMLReader();
        xmlReader.setContentHandler(handler);

        final BOMInputStream bomStream = new BOMInputStream(new XmlInputStream(inputStream));
        final ByteOrderMark bom = bomStream.getBOM();
        final String defaultEncoding = "UTF-8";
        final String charsetName = bom == null ? defaultEncoding : bom.getCharsetName();
        final Reader reader = new InputStreamReader(bomStream, charsetName);
        final InputSource in = new InputSource(reader);
        xmlReader.parse(in);
        return handler.getModel();
    } catch (ParserConfigurationException | SAXException | FileNotFoundException ex) {
        LOGGER.debug("", ex);
        throw new PomParseException(ex);
    } catch (IOException ex) {
        LOGGER.debug("", ex);
        throw new PomParseException(ex);
    }
}