1 package org.daisy.pipeline.modules.impl;
2
3 import java.net.URI;
4 import java.net.URL;
5 import java.nio.file.NoSuchFileException;
6 import java.util.LinkedList;
7
8 import org.daisy.pipeline.modules.Component;
9 import org.daisy.pipeline.modules.Module;
10 import org.daisy.pipeline.modules.ModuleRegistry;
11 import org.daisy.pipeline.modules.ResolutionException;
12 import org.daisy.pipeline.xmlcatalog.XmlCatalogParser;
13 import org.osgi.service.component.annotations.Activate;
14 import org.osgi.service.component.annotations.Reference;
15 import org.osgi.service.component.annotations.ReferenceCardinality;
16 import org.osgi.service.component.annotations.ReferencePolicy;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19 import org.slf4j.helpers.NOPLogger;
20
21 @org.osgi.service.component.annotations.Component(
22 name = "org.daisy.pipeline.modules.impl.Module_html_to_dtbook",
23 service = { Module.class }
24 )
25 public class Module_html_to_dtbook extends Module {
26
27 private static final Logger logger = LoggerFactory.getLogger(Module_html_to_dtbook.class);
28
29 private boolean initialized = false;
30 private XmlCatalogParser catalogParser;
31 private ModuleRegistry moduleRegistry;
32 private final LinkedList<Component> componentsToAdd;
33 private final LinkedList<Component> componentsBeingAdded;
34
35 public Module_html_to_dtbook() {
36 super("html-to-dtbook",
37 "2.0.7",
38 "DAISY Pipeline 2 module :: HTML to DTBook");
39 componentsToAdd = new LinkedList<>();
40 componentsBeingAdded = new LinkedList<>();
41 }
42
43 @Reference(
44 name = "XmlCatalogParser",
45 unbind = "-",
46 service = XmlCatalogParser.class,
47 cardinality = ReferenceCardinality.MANDATORY,
48 policy = ReferencePolicy.STATIC
49 )
50 public void setParser(XmlCatalogParser parser) {
51 catalogParser = parser;
52 }
53
54 @Reference(
55 name = "ModuleRegistry",
56 unbind = "-",
57 service = ModuleRegistry.class,
58 cardinality = ReferenceCardinality.MANDATORY,
59 policy = ReferencePolicy.STATIC
60 )
61 public void setModuleRegistry(ModuleRegistry registry) {
62 moduleRegistry = registry;
63 }
64
65 private void tryResolveComponent(String uri, String versionRange) throws ResolutionException {
66 if (moduleRegistry.getModuleByComponent(URI.create(uri), versionRange) == null)
67 throw new ResolutionException("Unresolved dependency " + uri + " with version=" + versionRange);
68 }
69
70 @Activate
71 protected void init() {
72 logger.debug("Initializing module " + getName());
73 Module tmpModule = new Module(getName(), getVersion(), getTitle()) {
74 @Override public void resolveDependencies() {}
75 @Override public Logger getLogger() { return NOPLogger.NOP_LOGGER; }
76 };
77 Module.parseCatalog(tmpModule, catalogParser);
78 for (String e : tmpModule.getEntities()) {
79 addEntity(tmpModule.getEntity(e));
80 logger.debug("Loaded entity: " + e);
81 }
82 for (URI c : tmpModule.getComponents()) {
83 components.put(c, () -> null);
84 componentsToAdd.add(tmpModule.getComponent(c));
85 }
86 logger.debug("Module " + getName() + " initialized, but dependencies have not been resolved yet");
87 }
88
89
90
91
92
93
94 @Override
95 public void resolveDependencies() {
96 if (!initialized) {
97 boolean recursive = !componentsBeingAdded.isEmpty();
98 if (recursive)
99 logger.debug("Resolving dependencies of module " + getName() + " (continuing in recursive call)");
100 else
101 logger.debug("Resolving dependencies of module " + getName());
102 while (true) {
103 if (componentsToAdd.isEmpty()) {
104 if (componentsBeingAdded.isEmpty())
105 break;
106 componentsToAdd.addAll(componentsBeingAdded);
107 componentsBeingAdded.clear();
108 }
109 Component c = componentsToAdd.poll();
110 componentsBeingAdded.add(c);
111 if (addComponent(c))
112 logger.debug("Loaded component: " + c.getURI());
113 componentsBeingAdded.remove(c);
114 componentsToAdd.remove(c);
115 }
116 if (!initialized)
117 logger.debug("Done resolving dependencies of module " + getName());
118 initialized = true;
119 }
120 }
121
122 @Override
123 protected boolean addComponent(Component component) {
124 String name = component.getURI().toString();
125 try {
126 if ("http://www.daisy.org/pipeline/modules/html-to-dtbook/library.xpl".equals(name)) {
127 tryResolveComponent("http://www.daisy.org/pipeline/modules/common-utils/numeral-conversion.xsl",
128 "[3.1.2,4.0.0)");
129 tryResolveComponent("http://www.daisy.org/pipeline/modules/css-utils/library.xpl",
130 "[6.0.0,7.0.0)");
131 tryResolveComponent("http://www.daisy.org/pipeline/modules/file-utils/library.xpl",
132 "[4.3.1,5.0.0)");
133 tryResolveComponent("http://www.daisy.org/pipeline/modules/file-utils/library.xsl",
134 "[4.3.1,5.0.0)");
135 tryResolveComponent("http://www.daisy.org/pipeline/modules/common-utils/generate-id.xsl",
136 "[3.1.2,4.0.0)");
137 tryResolveComponent("http://www.daisy.org/pipeline/modules/fileset-utils/library.xpl",
138 "[6.1.4,7.0.0)");
139 tryResolveComponent("http://www.daisy.org/pipeline/modules/html-utils/library.xpl",
140 "[6.4.1,7.0.0)");
141 tryResolveComponent("http://www.daisy.org/pipeline/modules/epub-utils/library.xsl",
142 "[2.2.1,3.0.0)");
143 tryResolveComponent("http://www.daisy.org/pipeline/modules/common-utils/library.xpl",
144 "[3.1.2,4.0.0)");
145 } else if ("http://www.daisy.org/pipeline/modules/html-to-dtbook/format-list.xsl".equals(name)) {
146 tryResolveComponent("http://www.daisy.org/pipeline/modules/common-utils/numeral-conversion.xsl",
147 "[3.1.2,4.0.0)");
148 } else if ("http://www.daisy.org/pipeline/modules/html-to-dtbook/epub3-to-dtbook.xsl".equals(name)) {
149 tryResolveComponent("http://www.daisy.org/pipeline/modules/common-utils/numeral-conversion.xsl",
150 "[3.1.2,4.0.0)");
151 tryResolveComponent("http://www.daisy.org/pipeline/modules/epub-utils/library.xsl",
152 "[2.2.1,3.0.0)");
153 }
154 } catch (ResolutionException re) {
155 logger.warn("Component " + component.getURI() + " can not be loaded: " + re.getMessage());
156 return false;
157 }
158 return super.addComponent(component);
159 }
160
161 private URL resource1 = null;
162
163 @Override
164 public URL getResource(String path) throws NoSuchFileException {
165 URL url = super.getResource(path);
166 try {
167 if (resource1 == null)
168 resource1 = super.getResource("../xml/xproc/html-to-dtbook.script.xpl");
169 if (url.equals(resource1)) {
170 tryResolveComponent("http://www.daisy.org/pipeline/modules/common-utils/numeral-conversion.xsl",
171 "[3.1.2,4.0.0)");
172 tryResolveComponent("http://www.daisy.org/pipeline/modules/css-utils/library.xpl",
173 "[6.0.0,7.0.0)");
174 tryResolveComponent("http://www.daisy.org/pipeline/modules/file-utils/library.xpl",
175 "[4.3.1,5.0.0)");
176 tryResolveComponent("http://www.daisy.org/pipeline/modules/file-utils/library.xsl",
177 "[4.3.1,5.0.0)");
178 tryResolveComponent("http://www.daisy.org/pipeline/modules/common-utils/generate-id.xsl",
179 "[3.1.2,4.0.0)");
180 tryResolveComponent("http://www.daisy.org/pipeline/modules/fileset-utils/library.xpl",
181 "[6.1.4,7.0.0)");
182 tryResolveComponent("http://www.daisy.org/pipeline/modules/html-utils/library.xpl",
183 "[6.4.1,7.0.0)");
184 tryResolveComponent("http://www.daisy.org/pipeline/modules/epub-utils/library.xsl",
185 "[2.2.1,3.0.0)");
186 tryResolveComponent("http://www.daisy.org/pipeline/modules/common-utils/library.xpl",
187 "[3.1.2,4.0.0)");
188 return url;
189 }
190 } catch (ResolutionException re) {
191 throw new NoSuchFileException(
192 "Resource " + path + " in module " + getName() + " can not be loaded: " + re.getMessage());
193 }
194 return url;
195 }
196 }