1 package tests.org.w3c.dom;
2 
3 import java.lang.reflect.Method;
4 
5 import javax.xml.parsers.DocumentBuilderFactory;
6 
7 public abstract class DocumentBuilderSettingStrategy {
DocumentBuilderSettingStrategy()8     protected DocumentBuilderSettingStrategy() {
9     }
10 
11     private static final String JAXP_SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
12 
13     private static final String W3C_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema";
14 
hasConflict(DocumentBuilderSettingStrategy other)15     public boolean hasConflict(DocumentBuilderSettingStrategy other) {
16         return (other == this);
17     }
18 
applySetting(DocumentBuilderFactory factory, boolean value)19     public abstract void applySetting(DocumentBuilderFactory factory,
20             boolean value);
21 
hasSetting(DOMDocumentBuilderFactory factory)22     public abstract boolean hasSetting(DOMDocumentBuilderFactory factory);
23 
24     public static final DocumentBuilderSettingStrategy coalescing = new DocumentBuilderSettingStrategy() {
25         public void applySetting(DocumentBuilderFactory factory,
26                 boolean value) {
27             factory.setCoalescing(value);
28         }
29 
30         public boolean hasSetting(DOMDocumentBuilderFactory factory) {
31             return factory.isCoalescing();
32         }
33 
34     };
35 
36     public static final DocumentBuilderSettingStrategy expandEntityReferences = new DocumentBuilderSettingStrategy() {
37         public void applySetting(DocumentBuilderFactory factory, boolean value) {
38             factory.setExpandEntityReferences(value);
39         }
40 
41         public boolean hasSetting(DOMDocumentBuilderFactory factory) {
42             return factory.isExpandEntityReferences();
43         }
44     };
45 
46     public static final DocumentBuilderSettingStrategy ignoringElementContentWhitespace = new DocumentBuilderSettingStrategy() {
47         public void applySetting(DocumentBuilderFactory factory, boolean value) {
48             factory.setIgnoringElementContentWhitespace(value);
49         }
50 
51         public boolean hasSetting(DOMDocumentBuilderFactory factory) {
52             return factory.isIgnoringElementContentWhitespace();
53         }
54     };
55 
56     public static final DocumentBuilderSettingStrategy ignoringComments = new DocumentBuilderSettingStrategy() {
57         public void applySetting(DocumentBuilderFactory factory, boolean value) {
58             if (value) {
59                 System.out.println("ignoreComments=true not supported");
60             }
61         }
62 
63         public boolean hasSetting(DOMDocumentBuilderFactory factory) {
64             return false;
65         }
66     };
67 
68     public static final DocumentBuilderSettingStrategy namespaceAware = new DocumentBuilderSettingStrategy() {
69         public void applySetting(DocumentBuilderFactory factory, boolean value)
70                 {
71             factory.setNamespaceAware(value);
72         }
73 
74         public boolean hasSetting(DOMDocumentBuilderFactory factory) {
75             return factory.isNamespaceAware();
76         }
77     };
78 
79     public static final DocumentBuilderSettingStrategy validating = new DocumentBuilderSettingStrategy() {
80         public void applySetting(DocumentBuilderFactory factory, boolean value)
81                 {
82             factory.setValidating(value);
83         }
84 
85         public boolean hasSetting(DOMDocumentBuilderFactory factory) {
86             return factory.isValidating();
87         }
88     };
89 
90     public static final DocumentBuilderSettingStrategy signed = new DocumentBuilderSettingStrategy() {
91         public void applySetting(DocumentBuilderFactory factory, boolean value)
92                  {
93             if (!value) {
94                 System.out.println("DocumentBuilderSetting.notSigned");
95             }
96         }
97 
98         public boolean hasSetting(DOMDocumentBuilderFactory factory) {
99             return true;
100         }
101     };
102 
103     public static final DocumentBuilderSettingStrategy hasNullString = new DocumentBuilderSettingStrategy() {
104         public void applySetting(DocumentBuilderFactory factory, boolean value)
105                  {
106             if (!value) {
107                 System.out.println("DocumentBuilderSetting.notHasNullString");
108             }
109         }
110 
111         public boolean hasSetting(DOMDocumentBuilderFactory factory) {
112             return true;
113         }
114     };
115 
116     public static final DocumentBuilderSettingStrategy schemaValidating = new DocumentBuilderSettingStrategy() {
117         public void applySetting(DocumentBuilderFactory factory, boolean value)
118                  {
119             if (value) {
120                 factory.setNamespaceAware(true);
121                 factory.setValidating(true);
122                 factory.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
123             } else {
124                 factory.setAttribute(JAXP_SCHEMA_LANGUAGE,
125                         "http://www.w3.org/TR/REC-xml");
126             }
127         }
128 
129         public boolean hasSetting(DOMDocumentBuilderFactory factory) {
130             try {
131                 if (factory.isValidating()) {
132                     Method getAttrMethod = factory.getClass().getMethod(
133                             "getAttribute", new Class[] { String.class });
134                     String val = (String) getAttrMethod.invoke(factory,
135                             new Object[] { JAXP_SCHEMA_LANGUAGE });
136                     return W3C_XML_SCHEMA.equals(val);
137                 }
138             } catch (Exception ex) {
139             }
140             return false;
141         }
142 
143         //
144         // schema validating conflicts with namespaceAware
145         // and validating
146         //
147         public boolean hasConflict(DocumentBuilderSettingStrategy other) {
148             if (other == this
149                     || other == DocumentBuilderSettingStrategy.namespaceAware
150                     || other == DocumentBuilderSettingStrategy.validating) {
151                 return true;
152             }
153             return false;
154         }
155 
156     };
157 
158 }
159