Class TranslationEngine

  • All Implemented Interfaces:
    ITranslationEngine
    Direct Known Subclasses:
    EditorTranslationEngine, TestEngine

    public abstract class TranslationEngine
    extends java.lang.Object
    implements ITranslationEngine
    The translation engine: two current translators are provided: Google and Microsoft, but the interface allows others. This engine is built to provide support to use multiple translators in parallel, with its own language capabilities and this engine would select the most suitable one to use.
    Author:
    Christopher Mindus
    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and TypeMethodDescription
      voidcancel()
      Cancels pending or running translation job.
      booleancanTranslate​(java.lang.String sourceLanguageCode, java.lang.String targetLanguageCode)
      Checks if a translation is handled between two language codes.
      protected voidclearLanguageQueue()
      Clears the language queue after reconfiguration.
      static ITranslationEnginegetInstance()
      Gets the translation engine instance.
      TranslationQueue[]getQueues()
      Gets the translation queues.
      protected abstract TranslationQueuenewTranslationQueue​(java.lang.String sourceLanguageCode, java.lang.String targetLanguageCode, ITranslator translator)
      Creates a new TranslationQueue instance.
      java.lang.StringpostRequestJSON​(java.lang.String httpRequest, java.util.Map<java.lang.String,​java.lang.String> headers, java.lang.String data)
      Requests a function using HTTPS POST, configured for proxy as specified in Eclipse or the Server.
      JSONObjectrequestJSON​(java.lang.String httpRequest, java.lang.String params, java.util.Map<java.lang.String,​java.lang.String> headers, int type, int sizeLimit)
      Requests a function using HTTPS GET or POST with "X-HTTP-Method-Override: GET", configured for proxy as specified in Eclipse or the Server.
      JSONObjectrequestJSON​(java.lang.String httpRequest, java.util.Map<java.lang.String,​java.lang.String> params, java.util.Map<java.lang.String,​java.lang.String> headers, int type, int sizeLimit)
      Requests a function using HTTPS GET or POST with "X-HTTP-Method-Override: GET", configured for proxy as specified in Eclipse or the Server.
      java.lang.StringrequestString​(java.lang.String httpRequest, java.lang.String params, java.util.Map<java.lang.String,​java.lang.String> headers, int type, int sizeLimit)
      Requests a function using HTTPS GET or POST with "X-HTTP-Method-Override: GET", configured for proxy as specified in Eclipse or the Server.
      java.lang.StringrequestString​(java.lang.String httpRequest, java.util.Map<java.lang.String,​java.lang.String> params, java.util.Map<java.lang.String,​java.lang.String> headers, int type, int sizeLimit)
      Requests a function using HTTPS GET or POST with "X-HTTP-Method-Override: GET", configured for proxy as specified in Eclipse or the Server.
      booleantranslate​(KString sourceText, java.lang.String sourceLanguageCode, java.lang.String targetLanguageCode, ITranslationReply replyCallback)
      Translates a text in plain text or HTML.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • ENCODING

        public static final java.nio.charset.Charset ENCODING
        Always UTF-8 encoded.
      • ENCODING_NAME

        public static final java.lang.String ENCODING_NAME
        The "UTF-8" string.
      • DEBUG

        public static boolean DEBUG
        Debug mode.
    • Constructor Detail

      • TranslationEngine

        protected TranslationEngine()
        Constructor.
        Throws:
        java.lang.InternalError - if the engine is already instantiated.
      • TranslationEngine

        protected TranslationEngine​(boolean recreate)
        Constructor.
        Throws:
        java.lang.InternalError - if the engine is already instantiated.
    • Method Detail

      • getInstance

        public static ITranslationEngine getInstance()
        Gets the translation engine instance.
        Returns:
        The engine.
        Throws:
        java.lang.NullPointerException - If the engine is not created yet.
      • clearLanguageQueue

        protected void clearLanguageQueue()
        Clears the language queue after reconfiguration.
      • canTranslate

        public boolean canTranslate​(java.lang.String sourceLanguageCode,
                                    java.lang.String targetLanguageCode)
        Checks if a translation is handled between two language codes.
        Specified by:
        canTranslate in interface ITranslationEngine
        Parameters:
        sourceLanguageCode - The source language code, null for automatic detection.
        targetLanguageCode - The target language code.
        Returns:
        true if supported, false if not.
      • cancel

        public void cancel()
        Cancels pending or running translation job.
        Specified by:
        cancel in interface ITranslationEngine
      • translate

        public boolean translate​(KString sourceText,
                                 java.lang.String sourceLanguageCode,
                                 java.lang.String targetLanguageCode,
                                 ITranslationReply replyCallback)
        Translates a text in plain text or HTML.
        Specified by:
        translate in interface ITranslationEngine
        Parameters:
        sourceText - The source text to translate.
        sourceLanguageCode - The source language code, null for automatic detection.
        targetLanguageCode - The target language code.
        replyCallback - Callback when translation completes.
        Returns:
        true for success, false for failure (error is logged).
        Throws:
        java.lang.NullPointerException - If sourceText, targetLanguageCode or replyCallback is null.
      • newTranslationQueue

        protected abstract TranslationQueue newTranslationQueue​(java.lang.String sourceLanguageCode,
                                                                java.lang.String targetLanguageCode,
                                                                ITranslator translator)
        Creates a new TranslationQueue instance.
        Parameters:
        sourceLanguageCode - The source language code, null for auto-detect.
        targetLanguageCode - The target language code.
        translator - The translator.
      • requestJSON

        public JSONObject requestJSON​(java.lang.String httpRequest,
                                      java.util.Map<java.lang.String,​java.lang.String> params,
                                      java.util.Map<java.lang.String,​java.lang.String> headers,
                                      int type,
                                      int sizeLimit)
                               throws java.io.IOException,
                                      JSONException
        Requests a function using HTTPS GET or POST with "X-HTTP-Method-Override: GET", configured for proxy as specified in Eclipse or the Server.
        Specified by:
        requestJSON in interface ITranslationEngine
        Parameters:
        httpRequest - The request string.
        params - The parameters, null for none.
        headers - The HTTP headers, null for none.
        type - Type of request: REQTYPE_* value.
        sizeLimit - The size limit imposed, 5120 for Google, 10K for Microsoft.
        Returns:
        The parsed JSON object.
        Throws:
        java.io.IOException - For I/O errors.
        JSONException - For JSON exceptions.
      • requestJSON

        public JSONObject requestJSON​(java.lang.String httpRequest,
                                      java.lang.String params,
                                      java.util.Map<java.lang.String,​java.lang.String> headers,
                                      int type,
                                      int sizeLimit)
                               throws java.io.IOException,
                                      JSONException
        Requests a function using HTTPS GET or POST with "X-HTTP-Method-Override: GET", configured for proxy as specified in Eclipse or the Server.
        Specified by:
        requestJSON in interface ITranslationEngine
        Parameters:
        httpRequest - The request string.
        params - The parameters, null for none.
        headers - The HTTP headers, null for none.
        type - Type of request: REQTYPE_* value.
        sizeLimit - The size limit imposed, 5120 for Google, 10K for Microsoft.
        Returns:
        The parsed JSON object.
        Throws:
        java.io.IOException - For I/O errors.
        JSONException - For JSON exceptions.
      • requestString

        public java.lang.String requestString​(java.lang.String httpRequest,
                                              java.util.Map<java.lang.String,​java.lang.String> params,
                                              java.util.Map<java.lang.String,​java.lang.String> headers,
                                              int type,
                                              int sizeLimit)
                                       throws java.io.IOException
        Requests a function using HTTPS GET or POST with "X-HTTP-Method-Override: GET", configured for proxy as specified in Eclipse or the Server.
        Specified by:
        requestString in interface ITranslationEngine
        Parameters:
        httpRequest - The request string.
        params - The parameters, null for none.
        headers - The HTTP headers, null for none.
        type - Type of request: REQTYPE_* value.
        sizeLimit - The size limit imposed, 5120 for Google, 10K for Microsoft.
        Returns:
        The reply string.
        Throws:
        java.io.IOException - For I/O errors.
      • requestString

        public java.lang.String requestString​(java.lang.String httpRequest,
                                              java.lang.String params,
                                              java.util.Map<java.lang.String,​java.lang.String> headers,
                                              int type,
                                              int sizeLimit)
                                       throws java.io.IOException
        Requests a function using HTTPS GET or POST with "X-HTTP-Method-Override: GET", configured for proxy as specified in Eclipse or the Server.
        Specified by:
        requestString in interface ITranslationEngine
        Parameters:
        httpRequest - The request string.
        params - The parameters, null for none.
        headers - The HTTP headers, null for none.
        type - Type of request: REQTYPE_* value.
        sizeLimit - The size limit imposed, 5120 for Google, 10K for Microsoft.
        Returns:
        The reply string.
        Throws:
        java.io.IOException - For I/O errors.
      • postRequestJSON

        public java.lang.String postRequestJSON​(java.lang.String httpRequest,
                                                java.util.Map<java.lang.String,​java.lang.String> headers,
                                                java.lang.String data)
                                         throws java.io.IOException
        Requests a function using HTTPS POST, configured for proxy as specified in Eclipse or the Server.
        Specified by:
        postRequestJSON in interface ITranslationEngine
        Parameters:
        httpRequest - The request string.
        headers - The HTTP headers, never null.
        data - The data string.
        Returns:
        The reply string.
        Throws:
        java.io.IOException - For I/O errors.