Home | History | Annotate | Download | only in domts
      1 /*
      2  * Copyright (c) 2001-2004 World Wide Web Consortium, (Massachusetts Institute
      3  * of Technology, Institut National de Recherche en Informatique et en
      4  * Automatique, Keio University). All Rights Reserved. This program is
      5  * distributed under the W3C's Software Intellectual Property License. This
      6  * program is distributed in the hope that it will be useful, but WITHOUT ANY
      7  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
      8  * FOR A PARTICULAR PURPOSE. See W3C License
      9  * http://www.w3.org/Consortium/Legal/ for more details.
     10  */
     11 
     12 package org.w3c.domts;
     13 
     14 import javax.xml.parsers.DocumentBuilderFactory;
     15 
     16 /**
     17  * This class is an parser setting, such as non-validating or entity-expanding.
     18  *
     19  * @author Curt Arnold @date 2 Feb 2002
     20  */
     21 public final class DocumentBuilderSetting {
     22   /**
     23    * property name.
     24    */
     25   private final String property;
     26 
     27   /**
     28    *   property value.
     29    */
     30   private final boolean value;
     31 
     32   /**
     33    * strategy used to set or get property value.
     34    */
     35   private final DocumentBuilderSettingStrategy strategy;
     36 
     37   /**
     38    * coalescing = true.
     39    */
     40   public static final DocumentBuilderSetting coalescing =
     41       new DocumentBuilderSetting(
     42       "coalescing",
     43       true,
     44       DocumentBuilderSettingStrategy.coalescing);
     45 
     46   /**
     47    * coalescing = false.
     48    */
     49   public static final DocumentBuilderSetting notCoalescing =
     50       new DocumentBuilderSetting(
     51       "coalescing",
     52       false,
     53       DocumentBuilderSettingStrategy.coalescing);
     54 
     55   /**
     56    * expandEntityReferences = false.
     57    */
     58   public static final DocumentBuilderSetting expandEntityReferences =
     59       new DocumentBuilderSetting(
     60       "expandEntityReferences",
     61       true,
     62       DocumentBuilderSettingStrategy.expandEntityReferences);
     63 
     64   /**
     65    * expandEntityReferences = true.
     66    */
     67   public static final DocumentBuilderSetting notExpandEntityReferences =
     68       new DocumentBuilderSetting(
     69       "expandEntityReferences",
     70       false,
     71       DocumentBuilderSettingStrategy.expandEntityReferences);
     72 
     73   /**
     74    * ignoringElementContentWhitespace = true.
     75    */
     76   public static final DocumentBuilderSetting ignoringElementContentWhitespace =
     77       new DocumentBuilderSetting(
     78       "ignoringElementContentWhitespace",
     79       true,
     80       DocumentBuilderSettingStrategy.ignoringElementContentWhitespace);
     81 
     82   /**
     83    * ignoringElementContentWhitespace = false.
     84    */
     85   public static final DocumentBuilderSetting
     86       notIgnoringElementContentWhitespace =
     87       new DocumentBuilderSetting(
     88       "ignoringElementContentWhitespace",
     89       false,
     90       DocumentBuilderSettingStrategy.ignoringElementContentWhitespace);
     91 
     92   /**
     93    * namespaceAware = true.
     94    */
     95   public static final DocumentBuilderSetting namespaceAware =
     96       new DocumentBuilderSetting(
     97       "namespaceAware",
     98       true,
     99       DocumentBuilderSettingStrategy.namespaceAware);
    100 
    101   /**
    102    * namespaceAware = false.
    103    */
    104   public static final DocumentBuilderSetting notNamespaceAware =
    105       new DocumentBuilderSetting(
    106       "namespaceAware",
    107       false,
    108       DocumentBuilderSettingStrategy.namespaceAware);
    109 
    110   /**
    111    * validating = true.
    112    */
    113   public static final DocumentBuilderSetting validating =
    114       new DocumentBuilderSetting(
    115       "validating",
    116       true,
    117       DocumentBuilderSettingStrategy.validating);
    118 
    119   /**
    120    * validating = false.
    121    */
    122   public static final DocumentBuilderSetting notValidating =
    123       new DocumentBuilderSetting(
    124       "validating",
    125       false,
    126       DocumentBuilderSettingStrategy.validating);
    127 
    128   /**
    129    * signed = true.
    130    */
    131   public static final DocumentBuilderSetting signed =
    132       new DocumentBuilderSetting(
    133       "signed",
    134       true,
    135       DocumentBuilderSettingStrategy.signed);
    136 
    137   /**
    138    * signed = false.
    139    */
    140   public static final DocumentBuilderSetting notSigned =
    141       new DocumentBuilderSetting(
    142       "signed",
    143       false,
    144       DocumentBuilderSettingStrategy.signed);
    145 
    146   /**
    147    * hasNullString = true.
    148    */
    149   public static final DocumentBuilderSetting hasNullString =
    150       new DocumentBuilderSetting(
    151       "hasNullString",
    152       true,
    153       DocumentBuilderSettingStrategy.hasNullString);
    154 
    155   /**
    156    * hasNullString = false.
    157    */
    158   public static final DocumentBuilderSetting notHasNullString =
    159       new DocumentBuilderSetting(
    160       "hasNullString",
    161       false,
    162       DocumentBuilderSettingStrategy.hasNullString);
    163 
    164   /**
    165    * Schema validating enabled.
    166    */
    167   public static final DocumentBuilderSetting schemaValidating =
    168       new DocumentBuilderSetting(
    169       "schemaValidating",
    170       true,
    171       DocumentBuilderSettingStrategy.schemaValidating);
    172 
    173   /**
    174    * Schema validating disabled.
    175    */
    176   public static final DocumentBuilderSetting notSchemaValidating =
    177       new DocumentBuilderSetting(
    178       "schemaValidating",
    179       false,
    180       DocumentBuilderSettingStrategy.schemaValidating);
    181 
    182   /**
    183    * Comments ignored.
    184    */
    185   public static final DocumentBuilderSetting ignoringComments =
    186       new DocumentBuilderSetting(
    187       "ignoringComments",
    188       true,
    189       DocumentBuilderSettingStrategy.ignoringComments);
    190 
    191   /**
    192    * Comments preserved.
    193    */
    194   public static final DocumentBuilderSetting notIgnoringComments =
    195       new DocumentBuilderSetting(
    196       "ignoringComments",
    197       false,
    198       DocumentBuilderSettingStrategy.ignoringComments);
    199 
    200   /**
    201    * Protected constructor, use static members for supported settings.
    202    * @param property property name, follows JAXP.
    203    * @param value property value
    204    * @param strategy strategy, may not be null
    205    */
    206   protected DocumentBuilderSetting(
    207       String property,
    208       boolean value,
    209       DocumentBuilderSettingStrategy strategy) {
    210     if (property == null) {
    211       throw new NullPointerException("property");
    212     }
    213     this.property = property;
    214     this.value = value;
    215     this.strategy = strategy;
    216   }
    217 
    218   /**
    219    * Returns true if the settings have a conflict or are identical.
    220    *
    221    * @param other
    222    *            other setting, may not be null.
    223    * @return true if this setting and the specified setting conflict
    224    */
    225   public final boolean hasConflict(DocumentBuilderSetting other) {
    226     if (other == null) {
    227       throw new NullPointerException("other");
    228     }
    229     if (other == this) {
    230       return true;
    231     }
    232     return strategy.hasConflict(other.strategy);
    233   }
    234 
    235   /**
    236    * Determines current value of setting.
    237    * @param factory DOMTestDocumentBuilderFactory factory
    238    * @return boolean true if property enabled.
    239    */
    240   public final boolean hasSetting(DOMTestDocumentBuilderFactory factory) {
    241     return strategy.hasSetting(factory) == value;
    242   }
    243 
    244   /**
    245    * Attempts to change builder to have this setting.
    246    * @param factory DocumentBuilderFactory Factory for DOM builders
    247    * @throws DOMTestIncompatibleException
    248    *      if factory does not support the setting
    249    */
    250   public final void applySetting(DocumentBuilderFactory factory) throws
    251       DOMTestIncompatibleException {
    252     strategy.applySetting(factory, value);
    253   }
    254 
    255   /**
    256    * Gets the property name.
    257    * @return property name
    258    */
    259   public final String getProperty() {
    260     return property;
    261   }
    262 
    263   /**
    264    * Gets the property value.
    265    * @return property value
    266    */
    267   public final boolean getValue() {
    268     return value;
    269   }
    270 
    271   /**
    272    * Gets a string representation of the setting.
    273    * @return string representation
    274    */
    275   public final String toString() {
    276     StringBuffer builder = new StringBuffer(property);
    277     builder.append('=');
    278     builder.append(String.valueOf(value));
    279     return builder.toString();
    280   }
    281 
    282 }
    283