Class Translator


  • public class Translator
    extends java.lang.Object
    This is main class for translator functionality. After creating TranslatorConfiguration with all required configuration it can be used in Translator constructor. Then it is ready to translate text.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addAllContextProperties​(java.util.HashMap<java.lang.String,​java.lang.Object> additionalContextProperties)
      Add a property to the translators context properties
      void addContextProperty​(java.lang.String propertyName, java.lang.Object propertyValue)
      Add a property to the translators context properties
      AbstractNode createNodesTree​(java.lang.String text)
      search for parts of text that would be changed to special nodes (by using parsers from TranslatorConfiguration) and return nodes tree (AbstractNode) that can be translated in next phase to output text
      java.util.HashMap<java.lang.String,​java.lang.Object> getContextProperties()
      Returns the translators context properties
      java.lang.Object getContextProperty​(java.lang.String propertyName)
      Returns a translators context property for a given name
      java.util.List<AbstractNode> parseText​(java.lang.String text)
      convert text to nodes list (if find any recognized text that should be changed to node)
      AbstractNode prepareNode​(AbstractNode node)
      This is an optional method during translate process.
      java.lang.String renderContent​(AbstractNode node)
      generate output text from childNodes if they exists otherwise from nodeText from node itself
      java.lang.String renderTextFromNode​(AbstractNode node)
      search for proper renderer and use it to render node content if render doesn't exist, get default renderer.
      java.lang.String[] searchForClosingTag​(AbstractParser abstractParser, java.lang.String startTag, java.lang.String textToSearch)
      search in textToSearch closing tag for given startTag.
      void setContextProperties​(java.util.HashMap<java.lang.String,​java.lang.Object> contextProperties)
      Set additional properties that shall be accessale while translating
      java.lang.String translate​(java.lang.String htmlInput)
      This method translate text from input to output format.
      • Methods inherited from class java.lang.Object

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

    • Method Detail

      • createNodesTree

        public AbstractNode createNodesTree​(java.lang.String text)
        search for parts of text that would be changed to special nodes (by using parsers from TranslatorConfiguration) and return nodes tree (AbstractNode) that can be translated in next phase to output text
        Parameters:
        text - for translation
        Returns:
        SimpleNode with name mainNode, with tree structure of nodes that were found in text
      • parseText

        public java.util.List<AbstractNode> parseText​(java.lang.String text)
        convert text to nodes list (if find any recognized text that should be changed to node)
        Returns:
        if text is null or none nodes were found in text then return null
      • renderTextFromNode

        public java.lang.String renderTextFromNode​(AbstractNode node)
        search for proper renderer and use it to render node content if render doesn't exist, get default renderer. This method doesn't check if node has children
        Parameters:
        node -
        Returns:
        String that node was translated to
      • translate

        public java.lang.String translate​(java.lang.String htmlInput)
        This method translate text from input to output format. All translator phases are done after this one method call. To go trough the phases one by one use different Translator methods.
        Parameters:
        htmlInput - text that should be translated
        Returns:
        output text depending on TranslatorConfiguration
      • searchForClosingTag

        public java.lang.String[] searchForClosingTag​(AbstractParser abstractParser,
                                                      java.lang.String startTag,
                                                      java.lang.String textToSearch)
        search in textToSearch closing tag for given startTag. Mechanism find first closing tag in textToSearch and create initial tagText, then check if all opening tags in this textTag are closed, if not then mechanism search for next closing tag (next loop)
        Returns:
        String[] startTag, endTag ,tagText
      • renderContent

        public java.lang.String renderContent​(AbstractNode node)
        generate output text from childNodes if they exists otherwise from nodeText from node itself
        Parameters:
        node - AbstractNode that should be translated to output text
        Returns:
        String that was node was translated to
      • getContextProperties

        public java.util.HashMap<java.lang.String,​java.lang.Object> getContextProperties()
        Returns the translators context properties
        Returns:
        A String:Object map
      • getContextProperty

        public java.lang.Object getContextProperty​(java.lang.String propertyName)
        Returns a translators context property for a given name
        Parameters:
        propertyName - The name of the property for which to get the property value
        Returns:
        The property value
      • setContextProperties

        public void setContextProperties​(java.util.HashMap<java.lang.String,​java.lang.Object> contextProperties)
        Set additional properties that shall be accessale while translating
        Parameters:
        contextProperties - A String:Object map
      • addContextProperty

        public void addContextProperty​(java.lang.String propertyName,
                                       java.lang.Object propertyValue)
        Add a property to the translators context properties
        Parameters:
        propertyName - The name of the property for which to add a property value
        propertyValue - The property value
      • addAllContextProperties

        public void addAllContextProperties​(java.util.HashMap<java.lang.String,​java.lang.Object> additionalContextProperties)
        Add a property to the translators context properties
        Parameters:
        additionalContextProperties - A String:Object map of properties that shall be added to the translators context properties
      • prepareNode

        public AbstractNode prepareNode​(AbstractNode node)
        This is an optional method during translate process. It gives the possibility to do some changes on the nodes tree created by the createNodesTree method. The method can be used before the rendererContent method starts rendering the output. By using the prepareNode(AbstractNode) the Translator calls prepareNode on each prerenderer included in the in prerendersList of the TranslatorConfiguration. For example this mechanism is used when translating HTML text to InDesign. It replaces all angle brackets (> and < symbols), because they are not allowed in InDesign.
        Parameters:
        node - usually mainNode created by the createNodesTree method
        Returns:
        usually the same node as this one in method input just modified however this is not a rule