Clarify and refactor content namespaces
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / acr / TypesManager.java
index 731c6d5624488d07ac04277471fe815c52489954..c60849355ecf9207512d23dda63ac3d38471ef84 100644 (file)
@@ -5,7 +5,6 @@ import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
-import java.util.Objects;
 import java.util.Set;
 import java.util.SortedMap;
 import java.util.TreeMap;
@@ -43,6 +42,8 @@ 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.acr.RuntimeNamespaceContext;
+import org.argeo.api.acr.spi.ContentNamespace;
 import org.argeo.api.cms.CmsLog;
 import org.xml.sax.ErrorHandler;
 import org.xml.sax.SAXException;
@@ -51,7 +52,7 @@ import org.xml.sax.SAXParseException;
 /** Register content types. */
 class TypesManager {
        private final static CmsLog log = CmsLog.getLog(TypesManager.class);
-       private Map<String, String> prefixes = new TreeMap<>();
+//     private Map<String, String> prefixes = new TreeMap<>();
 
        // immutable factories
        private SchemaFactory schemaFactory;
@@ -77,31 +78,33 @@ class TypesManager {
 
        }
 
-       public synchronized void init() {
-//             prefixes.put(CrName.CR_DEFAULT_PREFIX, CrName.CR_NAMESPACE_URI);
-//             prefixes.put("basic", CrName.CR_NAMESPACE_URI);
-//             prefixes.put("owner", CrName.CR_NAMESPACE_URI);
-//             prefixes.put("posix", CrName.CR_NAMESPACE_URI);
-
-               for (CmsContentTypes cs : CmsContentTypes.values()) {
-                       StreamSource source = new StreamSource(cs.getResource().toExternalForm());
-                       sources.add(source);
-                       if (prefixes.containsKey(cs.getDefaultPrefix()))
-                               throw new IllegalStateException("Prefix " + cs.getDefaultPrefix() + " is already mapped with "
-                                               + prefixes.get(cs.getDefaultPrefix()));
-                       prefixes.put(cs.getDefaultPrefix(), cs.getNamespace());
-               }
+       public void init() {
+//             for (CmsContentTypes cs : CmsContentTypes.values()) {
+//                     if (cs.getSchemaResource() != null) {
+//                             StreamSource source = new StreamSource(cs.getSchemaResource().toExternalForm());
+//                             sources.add(source);
+//                     }
+//                     RuntimeNamespaceContext.register(cs.getNamespace(), cs.getDefaultPrefix());
+//             }
 
-               reload();
+               registerTypes(CmsContentNamespace.values());
+//             reload();
        }
 
-       public synchronized void registerTypes(String defaultPrefix, String namespace, String xsdSystemId) {
-               if (prefixes.containsKey(defaultPrefix))
-                       throw new IllegalStateException(
-                                       "Prefix " + defaultPrefix + " is already mapped with " + prefixes.get(defaultPrefix));
-               prefixes.put(defaultPrefix, namespace);
-
-               sources.add(new StreamSource(xsdSystemId));
+       public void registerTypes(ContentNamespace... namespaces) {
+//             if (prefixes.containsKey(defaultPrefix))
+//                     throw new IllegalStateException(
+//                                     "Prefix " + defaultPrefix + " is already mapped with " + prefixes.get(defaultPrefix));
+//             prefixes.put(defaultPrefix, namespace);
+               for (ContentNamespace contentNamespace : namespaces) {
+                       RuntimeNamespaceContext.register(contentNamespace.getNamespaceURI(), contentNamespace.getDefaultPrefix());
+
+                       if (contentNamespace.getSchemaResource() != null) {
+                               sources.add(new StreamSource(contentNamespace.getSchemaResource().toExternalForm()));
+                               log.debug(() -> "Registered types " + contentNamespace.getNamespaceURI() + " from "
+                                               + contentNamespace.getSchemaResource().toExternalForm());
+                       }
+               }
                reload();
        }
 
@@ -455,13 +458,14 @@ class TypesManager {
                try {
                        validator.validate(source);
                } catch (SAXException e) {
-                       throw new IllegalArgumentException("Provided source is not valid", e);
+                       log.error(source + " is not valid " + e);
+                       // throw new IllegalArgumentException("Provided source is not valid", e);
                }
        }
 
-       public Map<String, String> getPrefixes() {
-               return prefixes;
-       }
+//     public Map<String, String> getPrefixes() {
+//             return prefixes;
+//     }
 
        public List<Source> getSources() {
                return sources;