]> git.argeo.org Git - lgpl/argeo-commons.git/blob - ContentTypesManager.java
23f2d90018beb9668e0be2e8ade946fd55429314
[lgpl/argeo-commons.git] / ContentTypesManager.java
1 package org.argeo.cms.acr;
2
3 import java.util.ArrayList;
4 import java.util.List;
5 import java.util.Map;
6 import java.util.TreeMap;
7
8 import javax.xml.transform.Source;
9 import javax.xml.transform.stream.StreamSource;
10 import javax.xml.validation.Schema;
11 import javax.xml.validation.SchemaFactory;
12
13 import org.apache.xerces.impl.xs.XSImplementationImpl;
14 import org.apache.xerces.impl.xs.util.StringListImpl;
15 import org.apache.xerces.xs.StringList;
16 import org.apache.xerces.xs.XSAttributeDeclaration;
17 import org.apache.xerces.xs.XSConstants;
18 import org.apache.xerces.xs.XSElementDeclaration;
19 import org.apache.xerces.xs.XSException;
20 import org.apache.xerces.xs.XSImplementation;
21 import org.apache.xerces.xs.XSLoader;
22 import org.apache.xerces.xs.XSModel;
23 import org.apache.xerces.xs.XSNamedMap;
24 import org.apache.xerces.xs.XSTypeDefinition;
25 import org.argeo.api.acr.CrName;
26 import org.argeo.api.cms.CmsLog;
27 import org.xml.sax.SAXException;
28
29 public class ContentTypesManager {
30 private final static CmsLog log = CmsLog.getLog(ContentTypesManager.class);
31 private Map<String, String> prefixes = new TreeMap<>();
32
33 private List<Source> sources = new ArrayList<>();
34
35 private SchemaFactory schemaFactory;
36 private Schema schema;
37
38 public ContentTypesManager() {
39 schemaFactory = SchemaFactory.newDefaultInstance();
40
41 }
42
43 public synchronized void init() {
44 // prefixes.put(CrName.CR_DEFAULT_PREFIX, CrName.CR_NAMESPACE_URI);
45 prefixes.put("basic", CrName.CR_NAMESPACE_URI);
46 prefixes.put("owner", CrName.CR_NAMESPACE_URI);
47 prefixes.put("posix", CrName.CR_NAMESPACE_URI);
48
49 try {
50 for (CmsContentTypes cs : CmsContentTypes.values()) {
51 StreamSource source = new StreamSource(cs.getResource().toExternalForm());
52 sources.add(source);
53 if (prefixes.containsKey(cs.getDefaultPrefix()))
54 throw new IllegalStateException("Prefix " + cs.getDefaultPrefix() + " is already mapped with "
55 + prefixes.get(cs.getDefaultPrefix()));
56 prefixes.put(cs.getDefaultPrefix(), cs.getNamespace());
57 }
58
59 schema = schemaFactory.newSchema(sources.toArray(new Source[sources.size()]));
60 } catch (SAXException e) {
61 throw new IllegalStateException("Cannot initialise types", e);
62 }
63
64 }
65
66 public synchronized void registerTypes(String defaultPrefix, String namespace, String xsdSystemId) {
67 try {
68 if (prefixes.containsKey(defaultPrefix))
69 throw new IllegalStateException(
70 "Prefix " + defaultPrefix + " is already mapped with " + prefixes.get(defaultPrefix));
71 prefixes.put(defaultPrefix, namespace);
72
73 sources.add(new StreamSource(xsdSystemId));
74 schema = schemaFactory.newSchema(sources.toArray(new Source[sources.size()]));
75 } catch (SAXException e) {
76 throw new IllegalStateException("Cannot initialise types " + namespace + " based on " + xsdSystemId, e);
77 }
78
79 }
80
81 public void listTypes() {
82 try {
83 // Find an XMLSchema loader instance
84 // DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
85 // XSImplementation implementation = (XSImplementation) registry.getDOMImplementation("XS-Loader");
86 XSImplementation implementation = new XSImplementationImpl();
87 XSLoader loader = implementation.createXSLoader(null);
88
89 // Load the XML Schema
90 List<String> systemIds = new ArrayList<>();
91 for (Source source : sources) {
92 systemIds.add(source.getSystemId());
93 }
94 StringList sl = new StringListImpl(systemIds.toArray(new String[systemIds.size()]), systemIds.size());
95 XSModel xsModel = loader.loadURIList(sl);
96
97 // Convert top level complex type definitions to node types
98 log.debug("\n## TYPES");
99 XSNamedMap map = xsModel.getComponents(XSConstants.TYPE_DEFINITION);
100 for (int i = 0; i < map.getLength(); i++) {
101 XSTypeDefinition tDef = (XSTypeDefinition) map.item(i);
102 log.debug(tDef);
103 }
104 // Convert local (anonymous) complex type defs found in top level
105 // element declarations
106 log.debug("\n## ELEMENTS");
107 map = xsModel.getComponents(XSConstants.ELEMENT_DECLARATION);
108 for (int i = 0; i < map.getLength(); i++) {
109 XSElementDeclaration eDec = (XSElementDeclaration) map.item(i);
110 XSTypeDefinition tDef = eDec.getTypeDefinition();
111 log.debug(eDec + ", " + tDef);
112 }
113 log.debug("\n## ATTRIBUTES");
114 map = xsModel.getComponents(XSConstants.ATTRIBUTE_DECLARATION);
115 for (int i = 0; i < map.getLength(); i++) {
116 XSAttributeDeclaration eDec = (XSAttributeDeclaration) map.item(i);
117 XSTypeDefinition tDef = eDec.getTypeDefinition();
118 log.debug(eDec.getNamespace() + ":" + eDec.getName() + ", " + tDef);
119 }
120 } catch (ClassCastException | XSException e) {
121 throw new RuntimeException(e);
122 }
123
124 }
125
126 public Map<String, String> getPrefixes() {
127 return prefixes;
128 }
129
130 public List<Source> getSources() {
131 return sources;
132 }
133
134 public Schema getSchema() {
135 return schema;
136 }
137
138 }