Coverage Report - org.omegat.filters3.xml.DefaultXMLDialect
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultXMLDialect
80%
57/71
62%
10/16
1.235
 
 1  
 /**************************************************************************
 2  
  OmegaT - Computer Assisted Translation (CAT) tool
 3  
           with fuzzy matching, translation memory, keyword search,
 4  
           glossaries, and translation leveraging into updated projects.
 5  
 
 6  
  Copyright (C) 2000-2006 Keith Godfrey and Maxym Mykhalchuk
 7  
                2008 Martin Fleurke
 8  
                2009 Didier Briel
 9  
                2010 Antonio Vilei
 10  
                Home page: http://www.omegat.org/
 11  
                Support center: http://groups.yahoo.com/group/OmegaT/
 12  
 
 13  
  This program is free software; you can redistribute it and/or modify
 14  
  it under the terms of the GNU General Public License as published by
 15  
  the Free Software Foundation; either version 2 of the License, or
 16  
  (at your option) any later version.
 17  
 
 18  
  This program is distributed in the hope that it will be useful,
 19  
  but WITHOUT ANY WARRANTY; without even the implied warranty of
 20  
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 21  
  GNU General Public License for more details.
 22  
 
 23  
  You should have received a copy of the GNU General Public License
 24  
  along with this program; if not, write to the Free Software
 25  
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 26  
  **************************************************************************/
 27  
 
 28  
 package org.omegat.filters3.xml;
 29  
 
 30  
 import java.util.HashMap;
 31  
 import java.util.HashSet;
 32  
 import java.util.Map;
 33  
 import java.util.Set;
 34  
 import java.util.regex.Pattern;
 35  
 
 36  
 import org.omegat.filters3.Attributes;
 37  
 import org.omegat.util.MultiMap;
 38  
 import org.xml.sax.InputSource;
 39  
 
 40  
 /**
 41  
  * Helper class for describing a certain XML dialect.
 42  
  * 
 43  
  * @author Maxym Mykhalchuk
 44  
  * @author Martin Fleurke
 45  
  * @author Didier Briel
 46  
  */
 47  30569648
 public class DefaultXMLDialect implements XMLDialect {
 48  
     /** The set of defined paragraph tags. */
 49  30569648
     private Set<String> paragraphTags = new HashSet<String>();
 50  
 
 51  
     /** Defines paragraph tag. Allows duplicates. */
 52  
     public void defineParagraphTag(String tag) {
 53  290223728
         paragraphTags.add(tag);
 54  290223728
     }
 55  
 
 56  
     /** Defines a set of paragraph tags from an array. Allows duplicates. */
 57  
     public void defineParagraphTags(String[] tags) {
 58  317779062
         for (String tag : tags)
 59  290223728
             defineParagraphTag(tag);
 60  27555334
     }
 61  
 
 62  
     /** The set of defined tags that surround preformatted text. */
 63  30569648
     private Set<String> preformatTags = new HashSet<String>();
 64  
 
 65  
     /** Defines preformat tag. Allows duplicates. */
 66  
     public void definePreformatTag(String tag) {
 67  6135324
         preformatTags.add(tag);
 68  6135324
     }
 69  
 
 70  
     /** Defines a set of preformat tags from an array. Allows duplicates. */
 71  
     public void definePreformatTags(String[] tags) {
 72  9202986
         for (String tag : tags)
 73  6135324
             definePreformatTag(tag);
 74  3067662
     }
 75  
 
 76  
     /** The set of defined tags that surround intact text. */
 77  30569648
     private Set<String> intactTags = new HashSet<String>();
 78  
 
 79  
     /** Defines intact tag. Allows duplicates. */
 80  
     public void defineIntactTag(String tag) {
 81  54417604
         intactTags.add(tag);
 82  54417604
     }
 83  
 
 84  
     /** Defines a set of intact tags from an array. Allows duplicates. */
 85  
     public void defineIntactTags(String[] tags) {
 86  66634812
         for (String tag : tags)
 87  54417604
             defineIntactTag(tag);
 88  12217208
     }
 89  
 
 90  
     /** The set of defined paragraph tags. */
 91  30569648
     private MultiMap<String, String> translatableTagAttributes = new MultiMap<String, String>();
 92  
 
 93  
     /** Defines translatable attribute of a tag. */
 94  
     public void defineTranslatableTagAttribute(String tag, String attribute) {
 95  0
         translatableTagAttributes.put(tag, attribute);
 96  0
     }
 97  
 
 98  
     /** Defines translatable attributes of a tag. */
 99  
     public void defineTranslatableTagAttributes(String tag, String[] attributes) {
 100  0
         for (String attr : attributes)
 101  0
             defineTranslatableTagAttribute(tag, attr);
 102  0
     }
 103  
 
 104  
     /** Defines translatable attribute of several tags. */
 105  
     public void defineTranslatableTagsAttribute(String[] tags, String attribute) {
 106  0
         for (String tag : tags)
 107  0
             defineTranslatableTagAttribute(tag, attribute);
 108  0
     }
 109  
 
 110  
     /** The set of defined paragraph tags. */
 111  30569648
     private Set<String> translatableAttributes = new HashSet<String>();
 112  
 
 113  
     /**
 114  
      * Defines always translatable attribute (no matter what tag it belongs to).
 115  
      */
 116  
     public void defineTranslatableAttribute(String attribute) {
 117  9202986
         translatableAttributes.add(attribute);
 118  9202986
     }
 119  
 
 120  
     /**
 121  
      * Defines always translatable attributes (no matter what tag it belongs
 122  
      * to).
 123  
      */
 124  
     public void defineTranslatableAttributes(String[] attributes) {
 125  12270648
         for (String attr : attributes)
 126  9202986
             defineTranslatableAttribute(attr);
 127  3067662
     }
 128  
 
 129  
     /**
 130  
      * The set of defined out of turn tags that surround chunks of text that
 131  
      * should be translated separately, not breaking currently collected text.
 132  
      */
 133  30569648
     private Set<String> outOfTurnTags = new HashSet<String>();
 134  
 
 135  
     /**
 136  
      * Defines out of turn tag. Such tag surrounds chunk of text that should be
 137  
      * translated separately, not breaking currently collected text.
 138  
      */
 139  
     public void defineOutOfTurnTag(String tag) {
 140  6081976
         outOfTurnTags.add(tag);
 141  6081976
     }
 142  
 
 143  
     /**
 144  
      * Defines out of turn tags. Such tags surround chunks of text that should
 145  
      * be translated separately, not breaking currently collected text.
 146  
      */
 147  
     public void defineOutOfTurnTags(String[] tags) {
 148  12163952
         for (String tag : tags)
 149  6081976
             defineOutOfTurnTag(tag);
 150  6081976
     }
 151  
 
 152  30569648
     Map<Integer, Pattern> constraints = new HashMap<Integer, Pattern>();
 153  
 
 154  
     /**
 155  
      * Defines a constraint to restrict supported subset of XML files. There can
 156  
      * be only one constraint of each type.
 157  
      * 
 158  
      * @param constraintType
 159  
      *            Type of constraint, see CONSTRAINT_... constants.
 160  
      * @param template
 161  
      *            Regular expression for a specified constrained string.
 162  
      */
 163  
     public void defineConstraint(Integer constraintType, Pattern template) {
 164  24541020
         constraints.put(constraintType, template);
 165  24541020
     }
 166  
 
 167  30569648
     Map<String, String> shortcuts = new HashMap<String, String>();
 168  
 
 169  
     /**
 170  
      * Defines a shortcut for a tag, useful for formatting tags. Shortcut is a
 171  
      * short form of a tag visible to translator, and stored in OmegaT's flavor
 172  
      * of TMX files.
 173  
      * 
 174  
      * @param tag
 175  
      *            Tag name.
 176  
      * @param shortcut
 177  
      *            The shortcut for a tag.
 178  
      */
 179  
     public void defineShortcut(String tag, String shortcut) {
 180  3120918
         shortcuts.put(tag, shortcut);
 181  3120918
     }
 182  
 
 183  
     /**
 184  
      * Defines shortcuts for formatting tags. An alternative to calling
 185  
      * {@link #defineShortcut(String,String)} multiple times.
 186  
      * 
 187  
      * @param mappings
 188  
      *            Array of strings, where even elements (0th, 2nd, etc) are
 189  
      *            tags, and odd elements are their corresponding shortcuts.
 190  
      */
 191  
     public void defineShortcuts(String[] mappings) {
 192  0
         for (int i = 0; i < mappings.length / 2; i++)
 193  0
             defineShortcut(mappings[2 * i], mappings[2 * i + 1]);
 194  0
     }
 195  
 
 196  
     // /////////////////////////////////////////////////////////////////////////
 197  
     // XMLDialect Interface Implementation
 198  
     // /////////////////////////////////////////////////////////////////////////
 199  
 
 200  
     /**
 201  
      * Returns the set of defined paragraph tags.
 202  
      * <p>
 203  
      * Each entry in a set should be a String class.
 204  
      */
 205  
     public Set<String> getParagraphTags() {
 206  284032
         return paragraphTags;
 207  
     }
 208  
 
 209  
     /**
 210  
      * Returns the set of tags that surround preformatted text.
 211  
      * <p>
 212  
      * Each entry in a set should be a String class.
 213  
      */
 214  
     public Set<String> getPreformatTags() {
 215  390544
         return preformatTags;
 216  
     }
 217  
 
 218  
     /**
 219  
      * Returns the set of tags that surround intact portions of document, that
 220  
      * should not be translated at all.
 221  
      * <p>
 222  
      * Each entry in a set should be a String class.
 223  
      */
 224  
     public Set<String> getIntactTags() {
 225  142016
         return intactTags;
 226  
     }
 227  
 
 228  
     /**
 229  
      * Returns the multimap of translatable attributes of each tag.
 230  
      * <p>
 231  
      * Each entry should map from a String to a set of Strings.
 232  
      */
 233  
     public MultiMap<String, String> getTranslatableTagAttributes() {
 234  88760
         return translatableTagAttributes;
 235  
     }
 236  
 
 237  
     /**
 238  
      * Returns for a given attribute of a given tag if the attribute should be
 239  
      * translated with the given other attributes present. If the tagAttribute
 240  
      * is returned by getTranslatable(Tag)Attributes(), this function is called
 241  
      * to further test the attribute within its context. This allows for example
 242  
      * the XHTML filter to not translate the value attribute of an
 243  
      * input-element, except when it is a button or submit or reset.
 244  
      */
 245  
     public Boolean validateTranslatableTagAttribute(String tag, String attribute, Attributes atts) {
 246  0
         return true;
 247  
     }
 248  
 
 249  
     /**
 250  
      * For a given tag, return wether the content of this tag should be
 251  
      * translated, depending on the content of one attribute and the presence or
 252  
      * absence of other attributes. For instance, in the ResX filter, tags
 253  
      * should not be translated when they contain the attribute "type", or when
 254  
      * the attribute "name" starts with "&amp;gt";
 255  
      * 
 256  
      * @param tag
 257  
      *            The tag that could be translated
 258  
      * @param atts
 259  
      *            The list of the tag attributes
 260  
      * @return <code>true</code> or <code>false</code>
 261  
      */
 262  
     public Boolean validateIntactTag(String tag, Attributes atts) {
 263  71008
         return false;
 264  
     }
 265  
 
 266  
     /**
 267  
      * For a given tag, return wether the content of this tag should be
 268  
      * translated, depending on the content of one attribute and the presence or
 269  
      * absence of other attributes. For instance, in the Typo3 filter, tags
 270  
      * should be translated when the attribute locazible="1". Contrary to
 271  
      * validateIntactTag, this applies only to the current tag, and the tags
 272  
      * contained in it are not affected.
 273  
      * 
 274  
      * @param tag
 275  
      *            The tag that could be translated
 276  
      * @param atts
 277  
      *            The list of the tag attributes
 278  
      * @return <code>true</code> or <code>false</code>
 279  
      */
 280  
     public Boolean validateTranslatableTag(String tag, Attributes atts) {
 281  53256
         return true;
 282  
     }
 283  
 
 284  
     /**
 285  
      * For a given tag, return wether the content of this tag is a paragraph
 286  
      * tag, depending on the content of one attribute (and/or the presence or
 287  
      * absence of other attributes). For instance, in the XLIFF filter, the
 288  
      * &lt;mark&gt; tag should start a new paragraph when the attribute "mtype"
 289  
      * contains "seg".
 290  
      * 
 291  
      * @param tag
 292  
      *            The tag that could be a paragraph tag
 293  
      * @param atts
 294  
      *            The list of the tag attributes
 295  
      * @return <code>true</code> or <code>false</code>
 296  
      */
 297  
     public Boolean validateParagraphTag(String tag, Attributes atts) {
 298  142016
         return false;
 299  
     }
 300  
 
 301  
     /**
 302  
      * For a given tag, return wether the content of this tag is a preformat
 303  
      * tag, depending on the content of one attribute (and/or the presence or
 304  
      * absence of other attributes). For instance, in the XLIFF filter, the
 305  
      * &lt;mark&gt; tag should be a preformat tag when the attribute "mtype"
 306  
      * contains "seg".
 307  
      * 
 308  
      * @param tag
 309  
      *            The tag that could be a preformat tag
 310  
      * @param atts
 311  
      *            The list of the tag attributes
 312  
      * @return <code>true</code> or <code>false</code>
 313  
      */
 314  
     public Boolean validatePreformatTag(String tag, Attributes atts) {
 315  195272
         return false;
 316  
     }
 317  
 
 318  
     /**
 319  
      * Returns the set of translatable attributes (no matter what tag they
 320  
      * belong to).
 321  
      * <p>
 322  
      * Each entry in a set should be a String class.
 323  
      */
 324  
     public Set<String> getTranslatableAttributes() {
 325  88760
         return translatableAttributes;
 326  
     }
 327  
 
 328  
     /**
 329  
      * Returns the set of "out-of-turn" tags. Such tags specify chunks of text
 330  
      * that should be translated separately, not breaking currently collected
 331  
      * text entry. For example, footnotes in OpenDocument.
 332  
      * <p>
 333  
      * Each entry in a set should be a String class.
 334  
      */
 335  
     public Set<String> getOutOfTurnTags() {
 336  142016
         return outOfTurnTags;
 337  
     }
 338  
 
 339  
     /**
 340  
      * Returns defined constraints to restrict supported subset of XML files.
 341  
      * There can be only one constraint of each type, see CONSTRAINT_...
 342  
      * constants.
 343  
      * <p>
 344  
      * Each entry should map an {@link Integer} to a {@link Pattern} -- regular
 345  
      * expression for a specified constrained string.
 346  
      */
 347  
     public Map<Integer, Pattern> getConstraints() {
 348  2185520
         return constraints;
 349  
     }
 350  
 
 351  
     /**
 352  
      * Resolves external entites if child filter needs it. Default
 353  
      * implementation returns <code>null</code>.
 354  
      */
 355  
     public InputSource resolveEntity(String publicId, String systemId) {
 356  53256
         return null;
 357  
     }
 358  
 
 359  
     /**
 360  
      * Returns the map of tags to their shortcuts.
 361  
      * <p>
 362  
      * Each entry should map a {@link String} to a {@link String} -- a tag to
 363  
      * its shortcut.
 364  
      */
 365  
     public Map<String, String> getShortcuts() {
 366  177520
         return shortcuts;
 367  
     }
 368  
 
 369  
     /**
 370  
      * The parameter setting wether closing tags should be used
 371  
      */
 372  30569648
     private boolean closingTagRequired = false;
 373  
 
 374  
     /**
 375  
      * Sets closingTag to <code>true</code> or <code>false</code>
 376  
      * 
 377  
      * @param onOff
 378  
      *            The parameter setting wether closing tags should be used or
 379  
      *            not for empty tags.
 380  
      */
 381  
     public void setClosingTagRequired(boolean onOff) {
 382  6188580
         closingTagRequired = onOff;
 383  6188580
     }
 384  
 
 385  
     /**
 386  
      * Gives the value of closingTag
 387  
      */
 388  
     public Boolean getClosingTagRequired() {
 389  53256
         return closingTagRequired;
 390  
     }
 391  
 
 392  
     /**
 393  
      * The parameter setting whether tags aggregation can be enabled
 394  
      */
 395  30569648
     private boolean tagsAggregationEnabled = false;
 396  
 
 397  
     /**
 398  
      * {@inheritDoc}
 399  
      */
 400  
     public void setTagsAggregationEnabled(boolean onOff) {
 401  0
         tagsAggregationEnabled = onOff;
 402  0
     }
 403  
 
 404  
     /**
 405  
      * {@inheritDoc}
 406  
      */
 407  
     public Boolean getTagsAggregationEnabled() {
 408  53256
         return tagsAggregationEnabled;
 409  
     }
 410  
 
 411  
 }