Search in sources :

Example 21 with SAXParser

use of javax.xml.parsers.SAXParser in project Cloud9 by lintool.

the class ParallelCorpusReader method parseXMLDocument.

public static void parseXMLDocument(String file, PChunkCallback cb) {
    //get a factory
    ParallelCorpusReader pcr = new ParallelCorpusReader(cb);
    final SAXParserFactory spf = SAXParserFactory.newInstance();
    try {
        //get a new instance of parser
        final SAXParser sp = spf.newSAXParser();
        //parse the file and also register this class for call backs
        sp.parse(file, pcr);
    } catch (final SAXException se) {
        se.printStackTrace();
    } catch (final ParserConfigurationException pce) {
        pce.printStackTrace();
    } catch (final IOException ie) {
        ie.printStackTrace();
    }
}
Also used : SAXParser(javax.xml.parsers.SAXParser) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IOException(java.io.IOException) SAXParserFactory(javax.xml.parsers.SAXParserFactory) SAXException(org.xml.sax.SAXException)

Example 22 with SAXParser

use of javax.xml.parsers.SAXParser in project languagetool by languagetool-org.

the class FalseFriendRuleLoader method getRules.

public final List<AbstractPatternRule> getRules(InputStream stream, Language textLanguage, Language motherTongue) throws ParserConfigurationException, SAXException, IOException {
    FalseFriendRuleHandler handler = new FalseFriendRuleHandler(textLanguage, motherTongue);
    SAXParserFactory factory = SAXParserFactory.newInstance();
    SAXParser saxParser = factory.newSAXParser();
    saxParser.getXMLReader().setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
    saxParser.parse(stream, handler);
    List<AbstractPatternRule> rules = handler.getRules();
    // Add suggestions to each rule:
    ResourceBundle messages = ResourceBundle.getBundle(JLanguageTool.MESSAGE_BUNDLE, motherTongue.getLocale());
    MessageFormat msgFormat = new MessageFormat(messages.getString("false_friend_suggestion"));
    for (AbstractPatternRule rule : rules) {
        List<String> suggestions = handler.getSuggestionMap().get(rule.getId());
        if (suggestions != null) {
            String[] msg = { formatSuggestions(suggestions) };
            rule.setMessage(rule.getMessage() + " " + msgFormat.format(msg));
        }
    }
    return rules;
}
Also used : MessageFormat(java.text.MessageFormat) SAXParser(javax.xml.parsers.SAXParser) ResourceBundle(java.util.ResourceBundle) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 23 with SAXParser

use of javax.xml.parsers.SAXParser in project languagetool by languagetool-org.

the class PatternRuleLoader method getRules.

/**
   * @param is stream with the XML rules
   * @param filename used only for verbose exception message - should refer to where the stream comes from
   */
public final List<AbstractPatternRule> getRules(InputStream is, String filename) throws IOException {
    try {
        PatternRuleHandler handler = new PatternRuleHandler();
        handler.setRelaxedMode(relaxedMode);
        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser saxParser = factory.newSAXParser();
        Tools.setPasswordAuthenticator();
        saxParser.getXMLReader().setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
        saxParser.parse(is, handler);
        return handler.getRules();
    } catch (Exception e) {
        throw new IOException("Cannot load or parse input stream of '" + filename + "'", e);
    }
}
Also used : SAXParser(javax.xml.parsers.SAXParser) IOException(java.io.IOException) IOException(java.io.IOException) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 24 with SAXParser

use of javax.xml.parsers.SAXParser in project languagetool by languagetool-org.

the class BitextPatternRuleLoader method getRules.

public final List<BitextPatternRule> getRules(InputStream is, String filename) throws IOException {
    List<BitextPatternRule> rules;
    try {
        BitextPatternRuleHandler handler = new BitextPatternRuleHandler();
        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser saxParser = factory.newSAXParser();
        saxParser.getXMLReader().setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
        saxParser.parse(is, handler);
        rules = handler.getBitextRules();
        return rules;
    } catch (Exception e) {
        throw new IOException("Cannot load or parse '" + filename + "'", e);
    }
}
Also used : SAXParser(javax.xml.parsers.SAXParser) IOException(java.io.IOException) IOException(java.io.IOException) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 25 with SAXParser

use of javax.xml.parsers.SAXParser in project android-async-http by loopj.

the class SaxAsyncHttpResponseHandler method getResponseData.

/**
     * Deconstructs response into given content handler
     *
     * @param entity returned HttpEntity
     * @return deconstructed response
     * @throws java.io.IOException if there is problem assembling SAX response from stream
     * @see cz.msebera.android.httpclient.HttpEntity
     */
@Override
protected byte[] getResponseData(HttpEntity entity) throws IOException {
    if (entity != null) {
        InputStream instream = entity.getContent();
        InputStreamReader inputStreamReader = null;
        if (instream != null) {
            try {
                SAXParserFactory sfactory = SAXParserFactory.newInstance();
                SAXParser sparser = sfactory.newSAXParser();
                XMLReader rssReader = sparser.getXMLReader();
                rssReader.setContentHandler(handler);
                inputStreamReader = new InputStreamReader(instream, getCharset());
                rssReader.parse(new InputSource(inputStreamReader));
            } catch (SAXException e) {
                AsyncHttpClient.log.e(LOG_TAG, "getResponseData exception", e);
            } catch (ParserConfigurationException e) {
                AsyncHttpClient.log.e(LOG_TAG, "getResponseData exception", e);
            } finally {
                AsyncHttpClient.silentCloseInputStream(instream);
                if (inputStreamReader != null) {
                    try {
                        inputStreamReader.close();
                    } catch (IOException e) {
                    /*ignore*/
                    }
                }
            }
        }
    }
    return null;
}
Also used : InputSource(org.xml.sax.InputSource) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) SAXParser(javax.xml.parsers.SAXParser) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IOException(java.io.IOException) XMLReader(org.xml.sax.XMLReader) SAXParserFactory(javax.xml.parsers.SAXParserFactory) SAXException(org.xml.sax.SAXException)

Aggregations

SAXParser (javax.xml.parsers.SAXParser)235 SAXParserFactory (javax.xml.parsers.SAXParserFactory)142 SAXException (org.xml.sax.SAXException)112 InputSource (org.xml.sax.InputSource)95 IOException (java.io.IOException)80 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)71 DefaultHandler (org.xml.sax.helpers.DefaultHandler)37 XMLReader (org.xml.sax.XMLReader)36 File (java.io.File)35 ByteArrayInputStream (java.io.ByteArrayInputStream)28 StringReader (java.io.StringReader)27 InputStream (java.io.InputStream)24 Attributes (org.xml.sax.Attributes)22 SAXSource (javax.xml.transform.sax.SAXSource)17 SAXParseException (org.xml.sax.SAXParseException)17 ArrayList (java.util.ArrayList)13 JAXBContext (javax.xml.bind.JAXBContext)12 Unmarshaller (javax.xml.bind.Unmarshaller)12 FileNotFoundException (java.io.FileNotFoundException)10 ValidationEvent (javax.xml.bind.ValidationEvent)9