]> git.argeo.org Git - lgpl/argeo-commons.git/blobdiff - org.argeo.cms/src/org/argeo/cms/acr/TypesManager.java
Introduce directory content provider
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / acr / TypesManager.java
index f9077f0a6da9414b016caa25d0a6142c461cb376..731c6d5624488d07ac04277471fe815c52489954 100644 (file)
@@ -10,6 +10,7 @@ import java.util.Set;
 import java.util.SortedMap;
 import java.util.TreeMap;
 
+import javax.xml.XMLConstants;
 import javax.xml.namespace.QName;
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
@@ -22,6 +23,7 @@ import javax.xml.validation.Validator;
 
 import org.apache.xerces.impl.xs.XSImplementationImpl;
 import org.apache.xerces.impl.xs.util.StringListImpl;
+import org.apache.xerces.jaxp.DocumentBuilderFactoryImpl;
 import org.apache.xerces.xs.StringList;
 import org.apache.xerces.xs.XSAttributeDeclaration;
 import org.apache.xerces.xs.XSAttributeUse;
@@ -40,6 +42,7 @@ import org.apache.xerces.xs.XSSimpleTypeDefinition;
 import org.apache.xerces.xs.XSTerm;
 import org.apache.xerces.xs.XSTypeDefinition;
 import org.argeo.api.acr.CrAttributeType;
+import org.argeo.api.acr.NamespaceUtils;
 import org.argeo.api.cms.CmsLog;
 import org.xml.sax.ErrorHandler;
 import org.xml.sax.SAXException;
@@ -58,23 +61,19 @@ class TypesManager {
 
        // cached
        private Schema schema;
-       DocumentBuilderFactory documentBuilderFactory;
+       private DocumentBuilderFactory documentBuilderFactory;
        private XSModel xsModel;
        private SortedMap<QName, Map<QName, CrAttributeType>> types;
 
        private boolean validating = true;
 
+       private final static boolean limited = false;
+
        public TypesManager() {
-               schemaFactory = SchemaFactory.newDefaultInstance();
+               schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
 
                // types
-               types = new TreeMap<>((qn1, qn2) -> {
-                       if (Objects.equals(qn1.getNamespaceURI(), qn2.getNamespaceURI())) {// same namespace
-                               return qn1.getLocalPart().compareTo(qn2.getLocalPart());
-                       } else {
-                               return qn1.getNamespaceURI().compareTo(qn2.getNamespaceURI());
-                       }
-               });
+               types = new TreeMap<>(NamespaceUtils.QNAME_COMPARATOR);
 
        }
 
@@ -122,11 +121,14 @@ class TypesManager {
                        schema = schemaFactory.newSchema(sources.toArray(new Source[sources.size()]));
 
                        // document builder factory
-                       documentBuilderFactory = DocumentBuilderFactory.newInstance();
+                       // we force usage of Xerces for predictability
+                       documentBuilderFactory = limited ? DocumentBuilderFactory.newInstance() : new DocumentBuilderFactoryImpl();
                        documentBuilderFactory.setNamespaceAware(true);
-                       documentBuilderFactory.setXIncludeAware(true);
-                       documentBuilderFactory.setSchema(getSchema());
-                       documentBuilderFactory.setValidating(validating);
+                       if (!limited) {
+                               documentBuilderFactory.setXIncludeAware(true);
+                               documentBuilderFactory.setSchema(getSchema());
+                               documentBuilderFactory.setValidating(validating);
+                       }
 
                        // XS model
                        // TODO use JVM implementation?
@@ -150,7 +152,7 @@ class TypesManager {
 //                     }
                        collectTypes();
                } catch (XSException | SAXException e) {
-                       throw new IllegalStateException("Cannot relaod types");
+                       throw new IllegalStateException("Cannot reload types", e);
                }
        }