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();
}
}
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;
}
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);
}
}
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);
}
}
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;
}
Aggregations