|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| package org.qedeq.kernel.xml.loader; |
|
19 |
| |
|
20 |
| import java.io.File; |
|
21 |
| import java.io.IOException; |
|
22 |
| import java.io.Reader; |
|
23 |
| |
|
24 |
| import javax.xml.parsers.ParserConfigurationException; |
|
25 |
| |
|
26 |
| import org.qedeq.kernel.base.module.Qedeq; |
|
27 |
| import org.qedeq.kernel.bo.control.InternalKernelServices; |
|
28 |
| import org.qedeq.kernel.bo.control.KernelQedeqBo; |
|
29 |
| import org.qedeq.kernel.bo.control.ModuleFileNotFoundException; |
|
30 |
| import org.qedeq.kernel.bo.control.ModuleLoader; |
|
31 |
| import org.qedeq.kernel.common.DefaultSourceFileExceptionList; |
|
32 |
| import org.qedeq.kernel.common.LoadingState; |
|
33 |
| import org.qedeq.kernel.common.ModuleContext; |
|
34 |
| import org.qedeq.kernel.common.ModuleDataException; |
|
35 |
| import org.qedeq.kernel.common.SourceArea; |
|
36 |
| import org.qedeq.kernel.common.SourceFileExceptionList; |
|
37 |
| import org.qedeq.kernel.trace.Trace; |
|
38 |
| import org.qedeq.kernel.xml.handler.module.QedeqHandler; |
|
39 |
| import org.qedeq.kernel.xml.mapper.Context2SimpleXPath; |
|
40 |
| import org.qedeq.kernel.xml.parser.SaxDefaultHandler; |
|
41 |
| import org.qedeq.kernel.xml.parser.SaxParser; |
|
42 |
| import org.qedeq.kernel.xml.tracker.SimpleXPath; |
|
43 |
| import org.qedeq.kernel.xml.tracker.XPathLocationParser; |
|
44 |
| import org.xml.sax.SAXException; |
|
45 |
| |
|
46 |
| import com.sun.syndication.io.XmlReader; |
|
47 |
| |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
| |
|
54 |
| |
|
55 |
| public class XmlModuleLoader implements ModuleLoader { |
|
56 |
| |
|
57 |
| |
|
58 |
| private static final Class CLASS = XmlModuleLoader.class; |
|
59 |
| |
|
60 |
| |
|
61 |
| private InternalKernelServices services; |
|
62 |
| |
|
63 |
| |
|
64 |
| |
|
65 |
| |
|
66 |
45
| public XmlModuleLoader() {
|
|
67 |
| } |
|
68 |
| |
|
69 |
45
| public void setServices(final InternalKernelServices services) {
|
|
70 |
45
| this.services = services;
|
|
71 |
| } |
|
72 |
| |
|
73 |
0
| public InternalKernelServices getServices() {
|
|
74 |
0
| return this.services;
|
|
75 |
| } |
|
76 |
| |
|
77 |
| |
|
78 |
| |
|
79 |
| |
|
80 |
| |
|
81 |
| |
|
82 |
| |
|
83 |
| |
|
84 |
| |
|
85 |
187
| public Qedeq loadLocalModule(final KernelQedeqBo prop,
|
|
86 |
| final File localFile) |
|
87 |
| throws ModuleFileNotFoundException, SourceFileExceptionList { |
|
88 |
187
| final String method = "loadLocalModule";
|
|
89 |
187
| prop.setLoader(this);
|
|
90 |
187
| final File file;
|
|
91 |
187
| try {
|
|
92 |
187
| file = localFile.getCanonicalFile();
|
|
93 |
| } catch (IOException e) { |
|
94 |
0
| Trace.trace(CLASS, this, method, e);
|
|
95 |
0
| throw new ModuleFileNotFoundException("file path not correct: " + localFile);
|
|
96 |
| } |
|
97 |
187
| if (!file.canRead()) {
|
|
98 |
32
| Trace.trace(CLASS, this, method, "file not readable=" + file);
|
|
99 |
32
| throw new ModuleFileNotFoundException("file not found: " + file);
|
|
100 |
| } |
|
101 |
155
| prop.setLoadingProgressState(LoadingState.STATE_LOADING_FROM_BUFFER);
|
|
102 |
155
| SaxDefaultHandler handler = new SaxDefaultHandler();
|
|
103 |
155
| QedeqHandler simple = new QedeqHandler(handler);
|
|
104 |
155
| handler.setBasisDocumentHandler(simple);
|
|
105 |
155
| SaxParser parser = null;
|
|
106 |
155
| try {
|
|
107 |
155
| parser = new SaxParser(handler);
|
|
108 |
| } catch (SAXException e) { |
|
109 |
0
| Trace.trace(CLASS, this, method, e);
|
|
110 |
0
| prop.setLoadingFailureState(LoadingState.STATE_LOADING_FROM_BUFFER_FAILED,
|
|
111 |
| new DefaultSourceFileExceptionList(e)); |
|
112 |
0
| throw createXmlFileExceptionList(e);
|
|
113 |
| } catch (ParserConfigurationException e) { |
|
114 |
0
| Trace.trace(CLASS, this, method, e);
|
|
115 |
0
| prop.setLoadingFailureState(LoadingState.STATE_LOADING_FROM_BUFFER_FAILED,
|
|
116 |
| new DefaultSourceFileExceptionList(new RuntimeException( |
|
117 |
| "XML parser configuration error", e))); |
|
118 |
0
| throw createXmlFileExceptionList(e);
|
|
119 |
| } |
|
120 |
155
| try {
|
|
121 |
155
| parser.parse(file, prop.getUrl());
|
|
122 |
| } catch (SourceFileExceptionList e) { |
|
123 |
20
| Trace.trace(CLASS, this, method, e);
|
|
124 |
20
| prop.setEncoding(parser.getEncoding());
|
|
125 |
20
| prop.setLoadingFailureState(LoadingState.STATE_LOADING_FROM_BUFFER_FAILED, e);
|
|
126 |
20
| throw e;
|
|
127 |
| } |
|
128 |
135
| prop.setEncoding(parser.getEncoding());
|
|
129 |
135
| return simple.getQedeq();
|
|
130 |
| } |
|
131 |
| |
|
132 |
| |
|
133 |
| |
|
134 |
| |
|
135 |
| |
|
136 |
| |
|
137 |
| |
|
138 |
| |
|
139 |
158
| public SourceArea createSourceArea(final Qedeq qedeq, final ModuleContext context) {
|
|
140 |
| |
|
141 |
158
| final String method = "createSourceArea(Qedeq, ModuleContext)";
|
|
142 |
158
| if (qedeq == null || context == null) {
|
|
143 |
72
| return null;
|
|
144 |
| } |
|
145 |
86
| ModuleContext ctext = new ModuleContext(context);
|
|
146 |
86
| final String xpath;
|
|
147 |
86
| try {
|
|
148 |
86
| xpath = Context2SimpleXPath.getXPath(ctext, qedeq).toString();
|
|
149 |
| } catch (ModuleDataException e) { |
|
150 |
0
| Trace.trace(CLASS, method, e);
|
|
151 |
0
| return null;
|
|
152 |
| }; |
|
153 |
| |
|
154 |
86
| SimpleXPath find = null;
|
|
155 |
86
| try {
|
|
156 |
86
| find = XPathLocationParser.getXPathLocation(
|
|
157 |
| services.getLocalFilePath(ctext.getModuleLocation()), |
|
158 |
| xpath, |
|
159 |
| ctext.getModuleLocation().getURL()); |
|
160 |
86
| if (find.getStartLocation() == null) {
|
|
161 |
0
| return null;
|
|
162 |
| } |
|
163 |
86
| return new SourceArea(ctext.getModuleLocation().getURL(), find.getStartLocation(),
|
|
164 |
| find.getEndLocation()); |
|
165 |
| } catch (ParserConfigurationException e) { |
|
166 |
0
| Trace.trace(CLASS, method, e);
|
|
167 |
| } catch (SAXException e) { |
|
168 |
0
| Trace.trace(CLASS, method, e);
|
|
169 |
| } catch (IOException e) { |
|
170 |
0
| Trace.trace(CLASS, method, e);
|
|
171 |
| } |
|
172 |
0
| return null;
|
|
173 |
| } |
|
174 |
| |
|
175 |
| |
|
176 |
| |
|
177 |
| |
|
178 |
| |
|
179 |
| |
|
180 |
| |
|
181 |
| |
|
182 |
389
| public Reader getModuleReader(final KernelQedeqBo bo)
|
|
183 |
| throws IOException { |
|
184 |
389
| return new XmlReader(services.getLocalFilePath(bo.getModuleAddress()));
|
|
185 |
| } |
|
186 |
| |
|
187 |
0
| private SourceFileExceptionList createXmlFileExceptionList(
|
|
188 |
| final ParserConfigurationException e) { |
|
189 |
0
| return new DefaultSourceFileExceptionList(e);
|
|
190 |
| } |
|
191 |
| |
|
192 |
0
| private DefaultSourceFileExceptionList createXmlFileExceptionList(final SAXException e) {
|
|
193 |
0
| return new DefaultSourceFileExceptionList(e);
|
|
194 |
| } |
|
195 |
| } |