Web service for translating between two languages

Jersey REST service for translating between two languages. It's just a basic concept but it's working.

web.xml
1:  <?xml version="1.0" encoding="UTF-8"?>  
2:    
3:  <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"  
4:        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
5:        xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"  
6:        version="3.1">  
7:   <display-name>TranslateWS</display-name>  
8:   <servlet>  
9:    <servlet-name>Jersey REST Service</servlet-name>  
10:    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>  
11:     <!-- Register resources and providers under com.vogella.jersey.first package. -->  
12:    <init-param>  
13:      <param-name>jersey.config.server.provider.packages</param-name>  
14:      <param-value>service</param-value>  
15:    </init-param>  
16:    <load-on-startup>1</load-on-startup>  
17:   </servlet>  
18:   <servlet-mapping>  
19:    <servlet-name>Jersey REST Service</servlet-name>  
20:    <url-pattern>/rest/*</url-pattern>  
21:   </servlet-mapping>  
22:  </web-app>  
23:    

index.jsp
1:  <%--   
2:    Document  : index  
3:    Created on : Jul 7, 2017, 3:39:48 PM  
4:    Author   : Djordje Gavrilovic  
5:  --%>  
6:    
7:  <%@page contentType="text/html" pageEncoding="UTF-8"%>  
8:  <!DOCTYPE html>  
9:  <html>  
10:    <head>  
11:      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
12:      <title>Translator index page</title>  
13:      <script type="text/javascript">  
14:        function go() {  
15:          url = 'http://localhost:8080/TranslateWS/rest/TranslatorService/' + document.getElementById("word").value;  
16:          window.open(url, '_blank');  
17:        }  
18:      </script>  
19:    </head>  
20:    <body>  
21:      <h1>Hello Translator!</h1>  
22:      <br/>  
23:      <form>  
24:        Your word:  
25:        <input type="text" name="word" id="word"/>  
26:      </form>  
27:      <button onclick="go()">Translate</button>  
28:    </body>  
29:  </html>  

dictionary.xml
1:  <?xml version="1.0" encoding="UTF-8"?>  
2:    
3:  <words>  
4:    <word en="book">knjiga</word>  
5:    <word en="dog">pas</word>  
6:    <word en="horse">konj</word>  
7:    <word en="pencil">olovka</word>  
8:    <word en="door">vrata</word>  
9:  </words>  
10:    

service.Translator.java
1:  package service;  
2:    
3:  import java.io.File;  
4:  import java.io.FileInputStream;  
5:  import java.io.FileNotFoundException;  
6:  import javax.ws.rs.*;  
7:  import javax.xml.xpath.XPath;  
8:  import javax.xml.xpath.XPathConstants;  
9:  import javax.xml.xpath.XPathExpression;  
10:  import javax.xml.xpath.XPathExpressionException;  
11:  import javax.xml.xpath.XPathFactory;  
12:  import org.w3c.dom.NodeList;  
13:  import org.xml.sax.InputSource;  
14:    
15:  @Path("/TranslatorService/{word}")  
16:  public class Translator {  
17:      
18:    @GET  
19:    public String translate (@PathParam("word") String word) throws XPathExpressionException, FileNotFoundException{  
20:        
21:      String output = null;  
22:            
23:      XPathFactory factory = XPathFactory.newInstance();  
24:      XPath path = factory.newXPath();  
25:      XPathExpression xPE = path.compile("//word[@en='"+ word.trim().toLowerCase() +"']");  
26:        
27:      File xmlDoc = new File("dictionary.xml");  
28:      InputSource source = new InputSource(new FileInputStream(xmlDoc));  
29:        
30:      Object result = xPE.evaluate(source, XPathConstants.NODESET);  
31:      NodeList nList = (NodeList)result;  
32:        
33:      if (nList.getLength() < 1)  
34:        output = "Word " + word + " does not exist in our little dictionary";  
35:      else {  
36:        for (int i = 0; i < nList.getLength(); i++) {  
37:          output = "Word " + word + " translates as: " + nList.item(i).getTextContent();  
38:        }  
39:      }  
40:        
41:      return output;  
42:    }  
43:      
44:  }  
45:    

Comments

  1. except that the dictionary.xml file has not correct form... it contains only one parameter, just for english...

    ReplyDelete

Post a Comment