Search in sources :

Example 16 with SAXParserFactory

use of javax.xml.parsers.SAXParserFactory in project robovm by robovm.

the class SimpleParserTest method testWorkingFile2.

public void testWorkingFile2() throws Exception {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setValidating(false);
    factory.setNamespaceAware(false);
    factory.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
    SAXParser parser = factory.newSAXParser();
    parser.getXMLReader().setContentHandler(contentHandler);
    parser.parse(getClass().getResourceAsStream("/SimpleParserTest.xml"), (DefaultHandler) null);
    assertFalse(parser.isNamespaceAware());
    assertEquals("The:quick,brown:fox", instructions.toString());
    assertEquals("t:stuff,nestedStuff,nestedStuff,nestedStuff", elements2.toString());
    assertEquals("Some text here,some more here...", text.toString());
    assertEquals("eins", attributes2.get("one"));
    assertEquals("zwei", attributes2.get("two"));
    assertEquals("drei", attributes2.get("three"));
    assertEquals(0, namespaces2.size());
}
Also used : SAXParser(javax.xml.parsers.SAXParser) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 17 with SAXParserFactory

use of javax.xml.parsers.SAXParserFactory 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 18 with SAXParserFactory

use of javax.xml.parsers.SAXParserFactory 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 19 with SAXParserFactory

use of javax.xml.parsers.SAXParserFactory 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 20 with SAXParserFactory

use of javax.xml.parsers.SAXParserFactory 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)

Aggregations

SAXParserFactory (javax.xml.parsers.SAXParserFactory)183 SAXParser (javax.xml.parsers.SAXParser)141 InputSource (org.xml.sax.InputSource)76 SAXException (org.xml.sax.SAXException)75 IOException (java.io.IOException)62 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)53 XMLReader (org.xml.sax.XMLReader)37 DefaultHandler (org.xml.sax.helpers.DefaultHandler)27 InputStream (java.io.InputStream)22 File (java.io.File)21 SAXSource (javax.xml.transform.sax.SAXSource)21 ByteArrayInputStream (java.io.ByteArrayInputStream)16 StringReader (java.io.StringReader)15 Unmarshaller (javax.xml.bind.Unmarshaller)13 Attributes (org.xml.sax.Attributes)13 JAXBContext (javax.xml.bind.JAXBContext)12 SAXParseException (org.xml.sax.SAXParseException)10 InputStreamReader (java.io.InputStreamReader)9 ArrayList (java.util.ArrayList)9 ValidationEvent (javax.xml.bind.ValidationEvent)9