summaryrefslogtreecommitdiffstats
path: root/dom/src/test/java/org/w3c/domts/DocumentBuilderSettingStrategy.java
blob: 6df1a92c3e095d48eeda465c9dc8ffe2a945d7a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
 * Copyright (c) 2001-2004 World Wide Web Consortium, (Massachusetts Institute of
 * Technology, Institut National de Recherche en Informatique et en
 * Automatique, Keio University). All Rights Reserved. This program is
 * distributed under the W3C's Software Intellectual Property License. This
 * program is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See W3C License
 * http://www.w3.org/Consortium/Legal/ for more details.
 */

package org.w3c.domts;

import java.lang.reflect.Method;

import javax.xml.parsers.DocumentBuilderFactory;

/**
 * This class is a strategy that provides the mapping from an abstract setting
 * (such as DocumentBuilderSetting.validating) to a specific DOM implementation
 *
 * @author Curt Arnold @date 2 Feb 2002
 */
public abstract class DocumentBuilderSettingStrategy {
  protected DocumentBuilderSettingStrategy() {
  }

  private static final String JAXP_SCHEMA_LANGUAGE =
      "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
  private static final String W3C_XML_SCHEMA =
      "http://www.w3.org/2001/XMLSchema";

  public boolean hasConflict(DocumentBuilderSettingStrategy other) {
    return (other == this);
  }

  public abstract void applySetting(
      DocumentBuilderFactory factory,
      boolean value) throws DOMTestIncompatibleException;

  public abstract boolean hasSetting(DOMTestDocumentBuilderFactory factory);

  public static final DocumentBuilderSettingStrategy coalescing =
      new DocumentBuilderSettingStrategy() {
    public void applySetting(DocumentBuilderFactory factory, boolean value)
        throws DOMTestIncompatibleException {
      factory.setCoalescing(value);
    }

    public boolean hasSetting(DOMTestDocumentBuilderFactory factory) {
      return factory.isCoalescing();
    }

  };

  public static final DocumentBuilderSettingStrategy
      expandEntityReferences =
      new DocumentBuilderSettingStrategy() {
    public void applySetting(DocumentBuilderFactory factory, boolean value)
        throws DOMTestIncompatibleException {
      factory.setExpandEntityReferences(value);
    }

    public boolean hasSetting(DOMTestDocumentBuilderFactory factory) {
      return factory.isExpandEntityReferences();
    }
  };

  public static final DocumentBuilderSettingStrategy
      ignoringElementContentWhitespace =
      new DocumentBuilderSettingStrategy() {
    public void applySetting(DocumentBuilderFactory factory, boolean value)
        throws DOMTestIncompatibleException {
      factory.setIgnoringElementContentWhitespace(value);
    }

    public boolean hasSetting(DOMTestDocumentBuilderFactory factory) {
      return factory.isIgnoringElementContentWhitespace();
    }
  };

  public static final DocumentBuilderSettingStrategy ignoringComments =
      new DocumentBuilderSettingStrategy() {
    public void applySetting(DocumentBuilderFactory factory, boolean value)
        throws DOMTestIncompatibleException {
      if (value) {
        throw new DOMTestIncompatibleException(
            new Exception("ignoreComments=true not supported"),
            DocumentBuilderSetting.ignoringComments);
      }
    }

    public boolean hasSetting(DOMTestDocumentBuilderFactory factory) {
      return false;
    }
  };

  public static final DocumentBuilderSettingStrategy namespaceAware =
      new DocumentBuilderSettingStrategy() {
    public void applySetting(DocumentBuilderFactory factory, boolean value) throws
        DOMTestIncompatibleException {
      factory.setNamespaceAware(value);
    }

    public boolean hasSetting(DOMTestDocumentBuilderFactory factory) {
      return factory.isNamespaceAware();
    }
  };

  public static final DocumentBuilderSettingStrategy validating =
      new DocumentBuilderSettingStrategy() {
    public void applySetting(DocumentBuilderFactory factory, boolean value) throws
        DOMTestIncompatibleException {
      factory.setValidating(value);
    }

    public boolean hasSetting(DOMTestDocumentBuilderFactory factory) {
      return factory.isValidating();
    }
  };

  public static final DocumentBuilderSettingStrategy signed =
      new DocumentBuilderSettingStrategy() {
    public void applySetting(DocumentBuilderFactory factory, boolean value) throws
        DOMTestIncompatibleException {
      if (!value) {
        throw new DOMTestIncompatibleException(
            null,
            DocumentBuilderSetting.notSigned);
      }
    }

    public boolean hasSetting(DOMTestDocumentBuilderFactory factory) {
      return true;
    }
  };

  public static final DocumentBuilderSettingStrategy hasNullString =
      new DocumentBuilderSettingStrategy() {
    public void applySetting(DocumentBuilderFactory factory, boolean value) throws
        DOMTestIncompatibleException {
      if (!value) {
        throw new DOMTestIncompatibleException(
            null,
            DocumentBuilderSetting.notHasNullString);
      }
    }

    public boolean hasSetting(DOMTestDocumentBuilderFactory factory) {
      return true;
    }
  };

  public static final DocumentBuilderSettingStrategy schemaValidating =
      new DocumentBuilderSettingStrategy() {
    public void applySetting(DocumentBuilderFactory factory, boolean value) throws
        DOMTestIncompatibleException {
      if (value) {
        factory.setNamespaceAware(true);
        factory.setValidating(true);
        factory.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
      }
      else {
        factory.setAttribute(JAXP_SCHEMA_LANGUAGE,
                             "http://www.w3.org/TR/REC-xml");
      }
    }

    public boolean hasSetting(DOMTestDocumentBuilderFactory factory) {
      try {
        if (factory.isValidating()) {
          Method getAttrMethod = factory.getClass().getMethod("getAttribute",
              new Class[] {String.class});
          String val = (String) getAttrMethod.invoke(factory,
              new Object[] {JAXP_SCHEMA_LANGUAGE});
          return W3C_XML_SCHEMA.equals(val);
        }
      }
      catch (Exception ex) {
      }
      return false;
    }

    //
    //   schema validating conflicts with namespaceAware
    //        and validating
    //
    public boolean hasConflict(DocumentBuilderSettingStrategy other) {
      if (other == this ||
          other == DocumentBuilderSettingStrategy.namespaceAware ||
          other == DocumentBuilderSettingStrategy.validating) {
        return true;
      }
      return false;
    }

  };

}