X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=org.argeo.cms%2Fsrc%2Forg%2Fargeo%2Fcms%2Facr%2FTypesManager.java;h=c3bea5b60fae239c550179641d004f383730c2c6;hb=55870eba50d8b28e72a3102fd18a17a6f23f7bad;hp=c5c3fd12833235a56cd956d881c3b46cc96d9c07;hpb=eb4cc3db3bf141c229f0f7ff929daff108bee6d2;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.cms/src/org/argeo/cms/acr/TypesManager.java b/org.argeo.cms/src/org/argeo/cms/acr/TypesManager.java index c5c3fd128..c3bea5b60 100644 --- a/org.argeo.cms/src/org/argeo/cms/acr/TypesManager.java +++ b/org.argeo.cms/src/org/argeo/cms/acr/TypesManager.java @@ -1,11 +1,11 @@ package org.argeo.cms.acr; import java.io.IOException; +import java.net.URL; 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; @@ -42,6 +42,9 @@ 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.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; @@ -50,13 +53,13 @@ import org.xml.sax.SAXParseException; /** Register content types. */ class TypesManager { private final static CmsLog log = CmsLog.getLog(TypesManager.class); - private Map prefixes = new TreeMap<>(); +// private Map prefixes = new TreeMap<>(); // immutable factories private SchemaFactory schemaFactory; /** Schema sources. */ - private List sources = new ArrayList<>(); + private List sources = new ArrayList<>(); // cached private Schema schema; @@ -64,49 +67,37 @@ class TypesManager { private XSModel xsModel; private SortedMap> types; - private boolean validating = true; + private boolean validating = false; + private boolean creatingXsModel = false; - private final static boolean limited = true; + private final static boolean limited = false; public TypesManager() { 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); } - 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()); - } - - reload(); + public void init() { + registerTypes(CmsContentNamespace.values()); } - 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(contentNamespace.getSchemaResource()); + log.debug(() -> "Registered types " + contentNamespace.getNamespaceURI() + " from " + + contentNamespace.getSchemaResource().toExternalForm()); + } + } reload(); } @@ -123,7 +114,21 @@ class TypesManager { private synchronized void reload() { try { // schema - schema = schemaFactory.newSchema(sources.toArray(new Source[sources.size()])); + if (validating) { + List sourcesToUse = new ArrayList<>(); + for (URL sourceUrl : sources) { + sourcesToUse.add(new StreamSource(sourceUrl.toExternalForm())); + } + schema = schemaFactory.newSchema(sourcesToUse.toArray(new Source[sourcesToUse.size()])); +// for (StreamSource source : sourcesToUse) { +// try { +// source.getInputStream().close(); +// } catch (IOException e) { +// // TODO Auto-generated catch block +// e.printStackTrace(); +// } +// } + } // document builder factory // we force usage of Xerces for predictability @@ -131,33 +136,39 @@ class TypesManager { documentBuilderFactory.setNamespaceAware(true); if (!limited) { documentBuilderFactory.setXIncludeAware(true); - documentBuilderFactory.setSchema(getSchema()); - documentBuilderFactory.setValidating(validating); + if (validating) { + documentBuilderFactory.setSchema(getSchema()); + documentBuilderFactory.setValidating(validating); + } } - // XS model - // TODO use JVM implementation? + if (creatingXsModel) { + // XS model + // TODO use JVM implementation? // DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); // XSImplementation implementation = (XSImplementation) registry.getDOMImplementation("XS-Loader"); - XSImplementation xsImplementation = new XSImplementationImpl(); - XSLoader xsLoader = xsImplementation.createXSLoader(null); - List systemIds = new ArrayList<>(); - for (Source source : sources) { - systemIds.add(source.getSystemId()); - } - StringList sl = new StringListImpl(systemIds.toArray(new String[systemIds.size()]), systemIds.size()); - xsModel = xsLoader.loadURIList(sl); + XSImplementation xsImplementation = new XSImplementationImpl(); + XSLoader xsLoader = xsImplementation.createXSLoader(null); + List systemIds = new ArrayList<>(); + for (URL sourceUrl : sources) { + systemIds.add(sourceUrl.toExternalForm()); + } + StringList sl = new StringListImpl(systemIds.toArray(new String[systemIds.size()]), systemIds.size()); + xsModel = xsLoader.loadURIList(sl); - // types + // types // XSNamedMap map = xsModel.getComponents(XSConstants.ELEMENT_DECLARATION); // for (int i = 0; i < map.getLength(); i++) { // XSElementDeclaration eDec = (XSElementDeclaration) map.item(i); // QName type = new QName(eDec.getNamespace(), eDec.getName()); // types.add(type); // } - collectTypes(); + collectTypes(); + + log.debug("Created XS model"); + } } catch (XSException | SAXException e) { - throw new IllegalStateException("Cannot relaod types"); + throw new IllegalStateException("Cannot reload types", e); } } @@ -419,34 +430,35 @@ class TypesManager { } public void printTypes() { - try { - - // Convert top level complex type definitions to node types - log.debug("\n## TYPES"); - XSNamedMap map = xsModel.getComponents(XSConstants.TYPE_DEFINITION); - for (int i = 0; i < map.getLength(); i++) { - XSTypeDefinition tDef = (XSTypeDefinition) map.item(i); - log.debug(tDef); - } - // Convert local (anonymous) complex type defs found in top level - // element declarations - log.debug("\n## ELEMENTS"); - map = xsModel.getComponents(XSConstants.ELEMENT_DECLARATION); - for (int i = 0; i < map.getLength(); i++) { - XSElementDeclaration eDec = (XSElementDeclaration) map.item(i); - XSTypeDefinition tDef = eDec.getTypeDefinition(); - log.debug(eDec + ", " + tDef); - } - log.debug("\n## ATTRIBUTES"); - map = xsModel.getComponents(XSConstants.ATTRIBUTE_DECLARATION); - for (int i = 0; i < map.getLength(); i++) { - XSAttributeDeclaration eDec = (XSAttributeDeclaration) map.item(i); - XSTypeDefinition tDef = eDec.getTypeDefinition(); - log.debug(eDec.getNamespace() + ":" + eDec.getName() + ", " + tDef); + if (xsModel != null) + try { + + // Convert top level complex type definitions to node types + log.debug("\n## TYPES"); + XSNamedMap map = xsModel.getComponents(XSConstants.TYPE_DEFINITION); + for (int i = 0; i < map.getLength(); i++) { + XSTypeDefinition tDef = (XSTypeDefinition) map.item(i); + log.debug(tDef); + } + // Convert local (anonymous) complex type defs found in top level + // element declarations + log.debug("\n## ELEMENTS"); + map = xsModel.getComponents(XSConstants.ELEMENT_DECLARATION); + for (int i = 0; i < map.getLength(); i++) { + XSElementDeclaration eDec = (XSElementDeclaration) map.item(i); + XSTypeDefinition tDef = eDec.getTypeDefinition(); + log.debug(eDec + ", " + tDef); + } + log.debug("\n## ATTRIBUTES"); + map = xsModel.getComponents(XSConstants.ATTRIBUTE_DECLARATION); + for (int i = 0; i < map.getLength(); i++) { + XSAttributeDeclaration eDec = (XSAttributeDeclaration) map.item(i); + XSTypeDefinition tDef = eDec.getTypeDefinition(); + log.debug(eDec.getNamespace() + ":" + eDec.getName() + ", " + tDef); + } + } catch (ClassCastException | XSException e) { + throw new RuntimeException(e); } - } catch (ClassCastException | XSException e) { - throw new RuntimeException(e); - } } @@ -460,17 +472,18 @@ 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 getPrefixes() { - return prefixes; - } +// public Map getPrefixes() { +// return prefixes; +// } - public List getSources() { - return sources; - } +// public List getSources() { +// return sources; +// } public Schema getSchema() { return schema;