Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //File Inflector.java
- package od.igor.petrov.lang.util;
- import java.io.BufferedInputStream;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.net.MalformedURLException;
- import java.net.URL;
- public class Inflector {
- public Inflector(String name) {
- try {
- URL url = new URL(inputUrl + name);
- in = url.openStream();
- in = new BufferedInputStream(in);
- tempFile = new File(".\\" + name + ".xml");
- fout = new FileOutputStream(tempFile);
- int symbol = 0;
- while ((symbol = in.read()) != -1) {
- fout.write(symbol);
- }
- InflectParser pars = new InflectParser(tempFile);
- String[] str = pars.getInflections();
- for (int i = 0; i < str.length; i++) {
- System.out.println(str[i]);
- }
- } catch (MalformedURLException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- if (tempFile.exists()) {
- tempFile.delete();
- }
- }
- }
- private static final String inputUrl = "http://export.yandex.ru/inflect.xml?name=";
- private InputStream in = null;
- private File tempFile = null;
- private FileOutputStream fout = null;
- }
- //File InflectParser.java
- package od.igor.petrov.lang.util;
- import java.io.File;
- import java.io.IOException;
- import javax.xml.parsers.DocumentBuilder;
- import javax.xml.parsers.DocumentBuilderFactory;
- import javax.xml.parsers.ParserConfigurationException;
- import org.w3c.dom.Document;
- import org.w3c.dom.Element;
- import org.w3c.dom.Node;
- import org.w3c.dom.NodeList;
- import org.xml.sax.SAXException;
- public class InflectParser {
- public InflectParser(File xmlFile) {
- try {
- factory = DocumentBuilderFactory.newInstance();
- factory.setIgnoringComments(true);
- factory.setCoalescing(true);
- factory.setNamespaceAware(false);
- factory.setValidating(false);
- parser = factory.newDocumentBuilder();
- document = parser.parse(xmlFile);
- sections = document.getElementsByTagName("inflection");
- int numberOfSections = sections.getLength();
- for (int i = 0; i < numberOfSections; i++) {
- section = (Element) sections.item(i);
- node = section.getFirstChild();
- inflections[i] = node.getNodeValue();
- }
- } catch (ParserConfigurationException e) {
- e.printStackTrace();
- } catch (SAXException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public String[] getInflections() {
- return inflections;
- }
- private DocumentBuilderFactory factory = null;
- private DocumentBuilder parser = null;
- private Document document = null;
- private NodeList sections = null;
- private Element section = null;
- private Node node = null;
- private String[] inflections = new String[6];
- }
- //File Starter.java
- package od.igor.petrov.lang.util;
- public class Starter implements Runnable{
- public Starter(String[] input){
- this.input = input;
- }
- @Override
- public void run() {
- for(int i = 0; i < input.length; i++){
- new Inflector(input[i]);
- try {
- Thread.sleep(500);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- private String[] input = null;
- }
- //File MainTest.java
- package od.igor.petrov.lang.util;
- public class MainTest {
- public static void main(String[] args) {
- new Thread(new Starter(new String[]{"утюг", "хрен", "поршень"})).start();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement