Line 0
Link Here
|
|
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 2012 Oracle and/or its affiliates. All rights reserved. |
5 |
* |
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates. |
7 |
* Other names may be trademarks of their respective owners. |
8 |
* |
9 |
* The contents of this file are subject to the terms of either the GNU |
10 |
* General Public License Version 2 only ("GPL") or the Common |
11 |
* Development and Distribution License("CDDL") (collectively, the |
12 |
* "License"). You may not use this file except in compliance with the |
13 |
* License. You can obtain a copy of the License at |
14 |
* http://www.netbeans.org/cddl-gplv2.html |
15 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
16 |
* specific language governing permissions and limitations under the |
17 |
* License. When distributing the software, include this License Header |
18 |
* Notice in each file and include the License file at |
19 |
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this |
20 |
* particular file as subject to the "Classpath" exception as provided |
21 |
* by Oracle in the GPL Version 2 section of the License file that |
22 |
* accompanied this code. If applicable, add the following below the |
23 |
* License Header, with the fields enclosed by brackets [] replaced by |
24 |
* your own identifying information: |
25 |
* "Portions Copyrighted [year] [name of copyright owner]" |
26 |
* |
27 |
* If you wish your version of this file to be governed by only the CDDL |
28 |
* or only the GPL Version 2, indicate your decision by adding |
29 |
* "[Contributor] elects to include this software in this distribution |
30 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
31 |
* single choice of license, a recipient has the option to distribute |
32 |
* your version of this file under either the CDDL, the GPL Version 2 or |
33 |
* to extend the choice of license to its licensees as provided above. |
34 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
35 |
* Version 2 license, then the option applies only if the new code is |
36 |
* made subject to such option by the copyright holder. |
37 |
* |
38 |
* Contributor(s): |
39 |
* |
40 |
* Portions Copyrighted 2012 Sun Microsystems, Inc. |
41 |
*/ |
42 |
package org.netbeans.modules.apisupport.hints; |
43 |
|
44 |
import com.sun.source.tree.ModifiersTree; |
45 |
import java.io.ByteArrayInputStream; |
46 |
import java.io.IOException; |
47 |
import java.net.URI; |
48 |
import java.net.URL; |
49 |
import java.util.ArrayList; |
50 |
import java.util.HashMap; |
51 |
import java.util.List; |
52 |
import java.util.Map; |
53 |
import javax.lang.model.element.Element; |
54 |
import org.netbeans.api.java.classpath.ClassPath; |
55 |
import org.netbeans.api.java.source.GeneratorUtilities; |
56 |
import org.netbeans.api.java.source.WorkingCopy; |
57 |
import static org.netbeans.modules.apisupport.hints.Bundle.*; |
58 |
import org.netbeans.spi.editor.hints.ChangeInfo; |
59 |
import org.netbeans.spi.editor.hints.Fix; |
60 |
import org.netbeans.spi.editor.hints.Severity; |
61 |
import org.openide.DialogDisplayer; |
62 |
import org.openide.NotifyDescriptor; |
63 |
import org.openide.filesystems.FileObject; |
64 |
import org.openide.filesystems.MIMEResolver; |
65 |
import org.openide.filesystems.URLMapper; |
66 |
import org.openide.util.Exceptions; |
67 |
import org.openide.util.NbBundle.Messages; |
68 |
import org.openide.util.NbCollections; |
69 |
import org.openide.util.lookup.ServiceProvider; |
70 |
import org.openide.xml.EntityCatalog; |
71 |
import org.openide.xml.XMLUtil; |
72 |
import org.w3c.dom.Document; |
73 |
import org.w3c.dom.Node; |
74 |
import org.w3c.dom.NodeList; |
75 |
import org.xml.sax.EntityResolver; |
76 |
import org.xml.sax.InputSource; |
77 |
import org.xml.sax.SAXException; |
78 |
|
79 |
/** |
80 |
* |
81 |
* #207960: convert MimeResolver |
82 |
*/ |
83 |
@ServiceProvider(service = Hinter.class) |
84 |
public class MIMEResolverRegistrationHinter implements Hinter { |
85 |
|
86 |
public static final String ACTIONS_FOLDER = "Actions"; //NOI18N |
87 |
public static final String METHOD_DOPOOL_FACTORY = "method:org.openide.loaders.DataLoaderPool.factory"; //NOI18N |
88 |
private static final String LOADERS_FOLDER = "Loaders/"; //NOI18N |
89 |
private static final String FACTORIES_FOLDER = "Factories"; //NOI18N |
90 |
private static final String SERVICES_MIME_RESOLVER = "Services/MIMEResolver"; //NOI18N |
91 |
|
92 |
@Override |
93 |
@Messages({"MIMEResolverRegistrationHinter.noTarget=Cannot find a reasonable target for refactory", |
94 |
"MIMEResolverRegistrationHinter.urlInvalid=URL not valid", "MIMEResolverRegistrationHinter.allInOne=into respective loader"}) |
95 |
public void process(final Context ctx) throws Exception { |
96 |
final FileObject file = ctx.file(); |
97 |
final Object instanceCreate = ctx.instanceAttribute(file); |
98 |
// cannot be folder |
99 |
if (!file.isData()) { |
100 |
return; |
101 |
} |
102 |
// must start with Services/MimeResolver |
103 |
if (file.getPath().startsWith(SERVICES_MIME_RESOLVER)) { |
104 |
if (checkAttributes(file, ctx)) { |
105 |
if (annotationsMIMEResolverAvailable(ctx)) { |
106 |
Map<String, List<String>> visibleTarget = DataObjectRegistrationHinter.Utility.getVisibleLoader(ctx, file); |
107 |
if (visibleTarget.isEmpty()) { |
108 |
ctx.addHint(Severity.ERROR, MIMEResolverRegistrationHinter_noTarget()); |
109 |
} else { |
110 |
List<Fix> fixes = new ArrayList<Fix>(); // prepare list of fixes |
111 |
// get information for strategy |
112 |
final String url = (String) file.getAttribute("WritableXMLFileSystem.url"); //NOI18N |
113 |
if (url == null && file.getSize() > 0) { |
114 |
// XXX cannot handle inline content |
115 |
ctx.addHint(Severity.WARNING, MIMEResolverRegistrationHinter_urlInvalid()); |
116 |
return; |
117 |
} |
118 |
|
119 |
if (url != null) { |
120 |
FileObject xmlMimeFile = null; |
121 |
URI u = new URI(url); |
122 |
if (!u.isAbsolute()) { |
123 |
URL[] layers = (URL[]) file.getAttribute("layers"); //NOI18N |
124 |
assert layers != null && layers.length == 1; |
125 |
FileObject layer = URLMapper.findFileObject(layers[0]); |
126 |
if (layer != null) { |
127 |
ClassPath src = ClassPath.getClassPath(layer, ClassPath.SOURCE); |
128 |
String path = src.getResourceName(layer); |
129 |
if (path != null) { |
130 |
u = new URI("nbres", "/" + path, null).resolve(u); //NOI18N //NOI18N |
131 |
} |
132 |
xmlMimeFile = src.findResource("/" + u.getPath()); //NOI18N |
133 |
} |
134 |
} |
135 |
|
136 |
MIMEResolverStrategyManager sm = null; |
137 |
if (xmlMimeFile != null) { |
138 |
sm = new MIMEResolverStrategyManager(xmlMimeFile.toURL()); |
139 |
} |
140 |
|
141 |
boolean allLoaderAccessible = false; |
142 |
if (sm != null && !sm.isUseComplex()) { |
143 |
// warn for use NameSpaceRegistration |
144 |
// all mimetype at once |
145 |
final Map<Object, Context.ModifyDeclarationTask> modifications = sm.checkLoaderAccessibility(visibleTarget, ctx); |
146 |
allLoaderAccessible = !modifications.isEmpty(); |
147 |
|
148 |
if (allLoaderAccessible) { |
149 |
ctx.addHint(Severity.WARNING, ctx.standardAnnotationDescription(), new Fix() { |
150 |
|
151 |
@Override |
152 |
public String getText() { |
153 |
return ctx.standardAnnotationFixDescription() + " " + MIMEResolverRegistrationHinter_allInOne(); //NOI18N |
154 |
} |
155 |
|
156 |
@Override |
157 |
public ChangeInfo implement() throws Exception { |
158 |
ctx.findAndModifyMultipleDeclaration(modifications); |
159 |
return null; |
160 |
} |
161 |
}); |
162 |
} |
163 |
|
164 |
} |
165 |
if (sm == null || !allLoaderAccessible || sm.isUseComplex()) { |
166 |
for (Map.Entry<String, List<String>> loader : visibleTarget.entrySet()) { // |
167 |
StringBuilder sbMime = new StringBuilder(); |
168 |
for (String aMime : loader.getValue()) { |
169 |
sbMime.append(aMime); |
170 |
sbMime.append(","); //NOI18N |
171 |
} |
172 |
sbMime.deleteCharAt(sbMime.length() - 1); |
173 |
|
174 |
String fname = loader.getKey().substring(loader.getKey().lastIndexOf(".") + 1) + ".java"; //NOI18N |
175 |
final String text = DataObjectRegistrationHinter_fix_special(fname, sbMime); |
176 |
final String fixParam = loader.getKey(); |
177 |
|
178 |
fixes.add(new Fix() { |
179 |
|
180 |
@Override |
181 |
public String getText() { |
182 |
return ctx.standardAnnotationFixDescription() + " " + text; //NOI18N |
183 |
} |
184 |
|
185 |
@Override |
186 |
public ChangeInfo implement() throws Exception { |
187 |
ctx.findAndModifyDeclaration(fixParam, new RegisterMIMEGenericResolver(ctx, url)); |
188 |
return null; |
189 |
} |
190 |
}); |
191 |
} |
192 |
if (!fixes.isEmpty()) { |
193 |
ctx.addHint(Severity.WARNING, ctx.standardAnnotationDescription(), fixes.toArray(new Fix[fixes.size()])); |
194 |
|
195 |
} |
196 |
} |
197 |
} |
198 |
} |
199 |
} |
200 |
} |
201 |
} |
202 |
} |
203 |
|
204 |
@Messages("MIMEResolverRegistrationHinter.missing_dep=You must add a dependency on org.openide.filesystems (7.58+) before using this fix.") |
205 |
private boolean annotationsMIMEResolverAvailable(Context ctx) { |
206 |
if (ctx.canAccess(MIMEResolver.Registration.class.getName()) |
207 |
&& ctx.canAccess(MIMEResolver.ExtensionRegistration.class.getName()) |
208 |
&& ctx.canAccess(MIMEResolver.NamespaceRegistration.class.getName())) { |
209 |
return true; |
210 |
} else { |
211 |
DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(MIMEResolverRegistrationHinter_missing_dep(), NotifyDescriptor.WARNING_MESSAGE)); |
212 |
return false; |
213 |
} |
214 |
} |
215 |
|
216 |
@Messages({ |
217 |
"# {0} - file attribute name", "MIMEResolverRegistrationHinter_unrecognized_attr=Unrecognized MimeResolver attribute: {0}", |
218 |
"MIMEResolverRegistrationHinter.pleaseConvert=Please convert to displayName to be able to use MimeResolver hinter"}) |
219 |
private boolean checkAttributes(FileObject file, Context ctx) { |
220 |
boolean attributesCompatible = true; |
221 |
for (String attr : NbCollections.iterable(file.getAttributes())) { |
222 |
if (!attr.matches("position|displayName|SystemFileSystem.localizingBundle")) { //NOI18N |
223 |
|
224 |
ctx.addHint(Severity.WARNING, MIMEResolverRegistrationHinter_unrecognized_attr(attr)); |
225 |
attributesCompatible = false; |
226 |
} |
227 |
} |
228 |
if (file.getAttribute("literal:SystemFileSystem.localizingBundle") != null) { //NOI18N |
229 |
attributesCompatible = false; |
230 |
ctx.addHint(Severity.HINT, MIMEResolverRegistrationHinter_pleaseConvert()); |
231 |
} |
232 |
return attributesCompatible; |
233 |
} |
234 |
|
235 |
private static class RegisterMIMEGenericResolver implements Context.ModifyDeclarationTask { |
236 |
|
237 |
private static final String MIMERESOLVER_REGISTRATION = "org.openide.filesystems.MIMEResolver.Registration"; //NOI18N |
238 |
private final Context ctx; |
239 |
private String url; |
240 |
|
241 |
public RegisterMIMEGenericResolver(Context ctx, String url) { |
242 |
this.ctx = ctx; |
243 |
this.url = url; |
244 |
} |
245 |
|
246 |
@Override |
247 |
public void run(WorkingCopy wc, Element declaration, ModifiersTree modifiers) throws Exception { |
248 |
Map<String, Object> params = new HashMap<String, Object>(); |
249 |
FileObject file = ctx.file(); |
250 |
String displayName = ctx.bundlevalue(file.getAttribute("literal:displayName"), declaration); //NOI18N |
251 |
|
252 |
if (displayName == null) { |
253 |
// checkAttributes method tries to warn user to avoid this fallback |
254 |
// unaware if fix can be chained |
255 |
displayName = "#TODO"; //NOI18N |
256 |
} |
257 |
params.put("position", file.getAttribute("position")); //NOI18N |
258 |
params.put("displayName", displayName); //NOI18N |
259 |
if (url != null) { |
260 |
URI u = new URI(url); |
261 |
if (!u.isAbsolute()) { |
262 |
URL[] layers = (URL[]) file.getAttribute("layers"); //NOI18N |
263 |
assert layers != null && layers.length == 1; |
264 |
FileObject layer = URLMapper.findFileObject(layers[0]); |
265 |
if (layer != null) { |
266 |
ClassPath src = ClassPath.getClassPath(layer, ClassPath.SOURCE); |
267 |
String path = src.getResourceName(layer); |
268 |
if (path != null) { |
269 |
u = new URI("nbres", "/" + path, null).resolve(u); //NOI18N //NOI18N |
270 |
} |
271 |
} |
272 |
} |
273 |
if (u.getScheme() != null && u.getScheme().matches("nbres(loc)?")) { //NOI18N |
274 |
params.put("resource", u.getPath()); //NOI18N |
275 |
} |
276 |
} |
277 |
ModifiersTree mt = ctx.addAnnotation(wc, modifiers, MIMERESOLVER_REGISTRATION, null, params); |
278 |
wc.rewrite(modifiers, GeneratorUtilities.get(wc).importFQNs(mt)); |
279 |
ctx.delete(file); |
280 |
} |
281 |
} |
282 |
|
283 |
private static abstract class AbstractMimeSpecificResolver implements Context.ModifyDeclarationTask { |
284 |
|
285 |
private final Context ctx; |
286 |
private final String annotation; |
287 |
final Registration registration; |
288 |
|
289 |
public AbstractMimeSpecificResolver(Context ctx, Registration registration, String annotation) { |
290 |
this.ctx = ctx; |
291 |
this.annotation = annotation; |
292 |
this.registration = registration; |
293 |
} |
294 |
|
295 |
@Override |
296 |
public void run(WorkingCopy wc, Element declaration, ModifiersTree modifiers) throws Exception { |
297 |
Map<String, Object> params = new HashMap<String, Object>(); |
298 |
FileObject file = ctx.file(); |
299 |
String displayName = ctx.bundlevalue(file.getAttribute("literal:displayName"), declaration); //NOI18N |
300 |
|
301 |
if (displayName == null) { |
302 |
// checkAttributes method tries to warn user to avoid this fallback |
303 |
// unaware if fix can be chained |
304 |
displayName = "#TODO"; //NOI18N |
305 |
} |
306 |
params.put("position", file.getAttribute("position")); //NOI18N |
307 |
params.put("displayName", displayName); //NOI18N |
308 |
specificRun(wc, declaration, modifiers, params); |
309 |
|
310 |
ModifiersTree mt = ctx.addAnnotation(wc, modifiers, annotation, null, params); |
311 |
wc.rewrite(modifiers, GeneratorUtilities.get(wc).importFQNs(mt)); |
312 |
} |
313 |
|
314 |
public abstract void specificRun(WorkingCopy wc, Element declaration, ModifiersTree modifiers, Map<String, Object> params); |
315 |
} |
316 |
|
317 |
private static class RegisterMIMENamespaceResolver extends AbstractMimeSpecificResolver { |
318 |
|
319 |
private static final String MIMERESOLVER_NAMESPACEREGISTRATION = "org.openide.filesystems.MIMEResolver.NamespaceRegistration"; //NOI18N |
320 |
|
321 |
public RegisterMIMENamespaceResolver(Context ctx, NamespaceRegistration nr) { |
322 |
super(ctx, nr, MIMERESOLVER_NAMESPACEREGISTRATION); |
323 |
} |
324 |
|
325 |
@Override |
326 |
public void specificRun(WorkingCopy wc, Element declaration, ModifiersTree modifiers, Map<String, Object> params) { |
327 |
params.put("mimeType", registration.mime); //NOI18N |
328 |
if (!registration.getExt().isEmpty()) { |
329 |
params.put("acceptedExtension", registration.getExt().toArray()); //NOI18N |
330 |
} |
331 |
//params.put("checkedExtension", ((NamespaceRegistration)registration).getExt()); |
332 |
if (!((NamespaceRegistration) registration).getRootElement().isEmpty()) { |
333 |
params.put("elementName", ((NamespaceRegistration) registration).getRootElement()); //NOI18N |
334 |
} |
335 |
if (!((NamespaceRegistration) registration).getElementNS().isEmpty()) { |
336 |
params.put("elementNS", ((NamespaceRegistration) registration).getElementNS().toArray()); //NOI18N |
337 |
} |
338 |
if (!((NamespaceRegistration) registration).getPublicID().isEmpty()) { |
339 |
params.put("doctypePublicId", ((NamespaceRegistration) registration).getPublicID().toArray()); //NOI18N |
340 |
} |
341 |
|
342 |
} |
343 |
} |
344 |
|
345 |
private static class RegisterMIMEExtensionResolver extends AbstractMimeSpecificResolver { |
346 |
|
347 |
private static final String MIMERESOLVER_EXTENSIONREGISTRATION = "org.openide.filesystems.MIMEResolver.ExtensionRegistration"; //NOI18N |
348 |
|
349 |
public RegisterMIMEExtensionResolver(Context ctx, ExtensionRegistration er) { |
350 |
super(ctx, er, MIMERESOLVER_EXTENSIONREGISTRATION); |
351 |
} |
352 |
|
353 |
@Override |
354 |
public void specificRun(WorkingCopy wc, Element declaration, ModifiersTree modifiers, Map<String, Object> params) { |
355 |
params.put("mimeType", registration.mime); //NOI18N |
356 |
if (!registration.getExt().isEmpty()) { |
357 |
params.put("extension", registration.getExt().toArray()); //NOI18N |
358 |
} |
359 |
} |
360 |
} |
361 |
|
362 |
protected abstract class Registration { |
363 |
|
364 |
List<String> ext = new ArrayList<String>(); |
365 |
String mime; |
366 |
|
367 |
public Registration(String mime) { |
368 |
this.mime = mime; |
369 |
} |
370 |
|
371 |
public void addExt(String mimeType) { |
372 |
ext.add(mimeType); |
373 |
} |
374 |
|
375 |
public List<String> getExt() { |
376 |
return ext; |
377 |
} |
378 |
} |
379 |
|
380 |
protected class ExtensionRegistration extends Registration { |
381 |
|
382 |
public ExtensionRegistration(String mime) { |
383 |
super(mime); |
384 |
} |
385 |
} |
386 |
|
387 |
protected class NamespaceRegistration extends Registration { |
388 |
|
389 |
List<String> elementNS = new ArrayList<String>(); |
390 |
List<String> doctype = new ArrayList<String>(); |
391 |
String rootElement = ""; //NOI18N |
392 |
|
393 |
public NamespaceRegistration(String mime) { |
394 |
super(mime); |
395 |
} |
396 |
|
397 |
private void addPublicId(String publicid) { |
398 |
doctype.add(publicid); |
399 |
} |
400 |
|
401 |
public List<String> getPublicID() { |
402 |
return doctype; |
403 |
} |
404 |
|
405 |
public void setRootElement(String rootElement) { |
406 |
this.rootElement = rootElement; |
407 |
} |
408 |
|
409 |
public String getRootElement() { |
410 |
return this.rootElement; |
411 |
} |
412 |
|
413 |
private void addElementNS(String attribute) { |
414 |
elementNS.add(attribute); |
415 |
} |
416 |
|
417 |
public List<String> getElementNS() { |
418 |
return elementNS; |
419 |
} |
420 |
} |
421 |
|
422 |
class MIMEResolverStrategyManager { |
423 |
|
424 |
private URL file; |
425 |
protected Map<String, ExtensionRegistration> extensionsRegistrations = new HashMap<String, ExtensionRegistration>(); |
426 |
protected Map<String, NamespaceRegistration> nameSpaceRegistrations = new HashMap<String, NamespaceRegistration>(); |
427 |
private boolean useComplex; //private List<> ; |
428 |
|
429 |
MIMEResolverStrategyManager(URL mimeXmlUrl) { |
430 |
file = mimeXmlUrl; |
431 |
useComplex = false; |
432 |
// parse file |
433 |
try { |
434 |
Document doc = XMLUtil.parse(new InputSource(file.toString()), false, false, null, new EntityResolver() { |
435 |
|
436 |
@Override |
437 |
public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException { |
438 |
if (publicId.contains("-//NetBeans//DTD MIME Resolver")) { //NOI18N |
439 |
return new InputSource(new ByteArrayInputStream(new byte[0])); |
440 |
} else { |
441 |
return EntityCatalog.getDefault().resolveEntity(publicId, systemId); |
442 |
|
443 |
} |
444 |
} |
445 |
}); |
446 |
NodeList nl = doc.getElementsByTagName("file"); //NOI18N |
447 |
for (int i = 0; |
448 |
i < nl.getLength(); |
449 |
i++) { |
450 |
useComplex = useComplex || filterAFile(nl.item(i)); |
451 |
} |
452 |
} catch (SAXException ex) { |
453 |
Exceptions.printStackTrace(ex); |
454 |
} catch (IOException ex) { |
455 |
Exceptions.printStackTrace(ex); |
456 |
} |
457 |
} |
458 |
|
459 |
private boolean filterAFile(Node nl) { |
460 |
int invalidTags = 0; // according to dtd 1.1 |
461 |
// |
462 |
// ext is not invalid |
463 |
invalidTags += ((org.w3c.dom.Element) nl).getElementsByTagName("mime").getLength(); //NOI18N |
464 |
invalidTags += ((org.w3c.dom.Element) nl).getElementsByTagName("magic").getLength(); //NOI18N |
465 |
invalidTags += ((org.w3c.dom.Element) nl).getElementsByTagName("fattr").getLength(); //NOI18N |
466 |
invalidTags += ((org.w3c.dom.Element) nl).getElementsByTagName("pattern").getLength(); //NOI18N |
467 |
invalidTags += ((org.w3c.dom.Element) nl).getElementsByTagName("name").getLength(); //NOI18N |
468 |
// |
469 |
// resolver is not invalid |
470 |
invalidTags += ((org.w3c.dom.Element) nl).getElementsByTagName("exit").getLength(); //NOI18N |
471 |
|
472 |
boolean registrationForcedToXML = invalidTags > 0; |
473 |
if (!registrationForcedToXML) { |
474 |
NodeList resolver = ((org.w3c.dom.Element) nl).getElementsByTagName("resolver"); //NOI18N |
475 |
assert (resolver.getLength() == 1); |
476 |
org.w3c.dom.Element resolverElement = (org.w3c.dom.Element) resolver.item(0); |
477 |
String mimeType = resolverElement.getAttribute("mime"); //NOI18N |
478 |
assert (!mimeType.isEmpty()); |
479 |
if (((org.w3c.dom.Element) nl).getElementsByTagName("xml-rule").getLength() > 0) { //NOI18N |
480 |
registrationForcedToXML = registrationForcedToXML || addNamespaceRegistration(mimeType, nl); |
481 |
} else { |
482 |
addExtRegistration(mimeType, nl); |
483 |
} |
484 |
} |
485 |
return registrationForcedToXML; |
486 |
} |
487 |
|
488 |
private boolean addNamespaceRegistration(String mime, Node nl) { |
489 |
NamespaceRegistration namespaceRegistation = nameSpaceRegistrations.get(mime); |
490 |
if (namespaceRegistation == null) { |
491 |
namespaceRegistation = new NamespaceRegistration(mime); |
492 |
nameSpaceRegistrations.put(mime, namespaceRegistation); |
493 |
} |
494 |
|
495 |
addExtension(namespaceRegistation, nl); |
496 |
// pi not taken into account |
497 |
boolean registrationForcedToXML = ((org.w3c.dom.Element) nl).getElementsByTagName("pi").getLength() > 0; //NOI18N |
498 |
if (!registrationForcedToXML) { // no pi |
499 |
// check for doctype |
500 |
// DOCTYPE part |
501 |
NodeList doctype = ((org.w3c.dom.Element) nl).getElementsByTagName("doctype"); //NOI18N |
502 |
assert (doctype.getLength() <= 1); |
503 |
if (doctype.getLength() > 0) { |
504 |
org.w3c.dom.Element docElement = (org.w3c.dom.Element) doctype.item(0); |
505 |
if (docElement.getAttribute("public-id").isEmpty()) { //NOI18N |
506 |
// getall publicidtag |
507 |
NodeList publictype = ((org.w3c.dom.Element) docElement).getElementsByTagName("public-id"); //NOI18N |
508 |
for (int i = 0; i < publictype.getLength(); i++) { |
509 |
org.w3c.dom.Element publicElement = (org.w3c.dom.Element) publictype.item(i); |
510 |
if (publicElement.getAttribute("id").isEmpty()) { //NOI18N |
511 |
registrationForcedToXML = true; |
512 |
} else { |
513 |
namespaceRegistation.addPublicId(publicElement.getAttribute("id")); //NOI18N |
514 |
} |
515 |
} |
516 |
} else { |
517 |
namespaceRegistation.addPublicId(docElement.getAttribute("public-id")); //NOI18N |
518 |
} |
519 |
} |
520 |
|
521 |
// ELEMENT part |
522 |
NodeList elementList = ((org.w3c.dom.Element) nl).getElementsByTagName("element"); //NOI18N |
523 |
assert (elementList.getLength() <= 1); |
524 |
if (elementList.getLength() > 0) { |
525 |
org.w3c.dom.Element element = (org.w3c.dom.Element) elementList.item(0); |
526 |
if (element.getAttribute("name").isEmpty()) { //NOI18N |
527 |
registrationForcedToXML = true; // no name force |
528 |
} else { |
529 |
if (namespaceRegistation.getRootElement().isEmpty()) { |
530 |
namespaceRegistation.setRootElement(element.getAttribute("name")); //NOI18N |
531 |
registrationForcedToXML = ((org.w3c.dom.Element) nl).getElementsByTagName("attr").getLength() > 0; // attr attribute found => cannot handle //NOI18N |
532 |
|
533 |
NodeList nsList = ((org.w3c.dom.Element) element).getElementsByTagName("ns"); //NOI18N |
534 |
if (nsList.getLength() > 0) { |
535 |
for (int i = 0; i < nsList.getLength(); i++) { |
536 |
org.w3c.dom.Element nsElement = (org.w3c.dom.Element) nsList.item(i); |
537 |
if (!nsElement.getAttribute("ns").isEmpty()) { //NOI18N |
538 |
namespaceRegistation.addElementNS(nsElement.getAttribute("ns")); //NOI18N |
539 |
} else { |
540 |
registrationForcedToXML = true; |
541 |
} |
542 |
} |
543 |
} else { |
544 |
if (!element.getAttribute("ns").isEmpty()) { //NOI18N |
545 |
namespaceRegistation.addElementNS(element.getAttribute("ns")); //NOI18N |
546 |
} |
547 |
} |
548 |
|
549 |
} else { |
550 |
registrationForcedToXML = true; // allready an element => fail |
551 |
} |
552 |
} |
553 |
} |
554 |
|
555 |
} |
556 |
return registrationForcedToXML; |
557 |
} |
558 |
|
559 |
private void addExtRegistration(String mime, Node nl) { |
560 |
ExtensionRegistration extRegistration = extensionsRegistrations.get(mime); |
561 |
if (extRegistration == null) { |
562 |
extRegistration = new ExtensionRegistration(mime); |
563 |
extensionsRegistrations.put(mime, extRegistration); |
564 |
} |
565 |
addExtension(extRegistration, nl); |
566 |
// thats all for extension based |
567 |
} |
568 |
|
569 |
private void addExtension(Registration registration, Node nl) { |
570 |
NodeList ext = ((org.w3c.dom.Element) nl).getElementsByTagName("ext"); //NOI18N |
571 |
for (int i = 0; i < ext.getLength(); i++) { |
572 |
registration.addExt((((org.w3c.dom.Element) ext.item(i)).getAttribute("name"))); //NOI18N |
573 |
} |
574 |
} |
575 |
|
576 |
/** |
577 |
* @return the useComplex |
578 |
*/ |
579 |
public boolean isUseComplex() { |
580 |
return useComplex; |
581 |
} |
582 |
|
583 |
private Map<Object, Context.ModifyDeclarationTask> checkLoaderAccessibility(Map<String, List<String>> visibleTarget, Context ctx) { |
584 |
Map<Object, Context.ModifyDeclarationTask> tmp = new HashMap<Object, Context.ModifyDeclarationTask>(); |
585 |
boolean loaderok = true; |
586 |
for (Map.Entry<String, ExtensionRegistration> extregistration : extensionsRegistrations.entrySet()) { |
587 |
boolean findforthis = false; |
588 |
String mime = extregistration.getKey(); |
589 |
for (Map.Entry<String, List<String>> mime2 : visibleTarget.entrySet()) { |
590 |
if (mime2.getValue().contains(mime)) { |
591 |
tmp.put(mime2.getKey(), new RegisterMIMEExtensionResolver(ctx, extregistration.getValue())); |
592 |
findforthis = true; |
593 |
} |
594 |
} |
595 |
loaderok = loaderok && findforthis; |
596 |
} |
597 |
for (Map.Entry<String, NamespaceRegistration> namespaceregistration : nameSpaceRegistrations.entrySet()) { |
598 |
boolean findforthis = false; |
599 |
String mime = namespaceregistration.getKey(); |
600 |
for (Map.Entry<String, List<String>> mime2 : visibleTarget.entrySet()) { |
601 |
if (mime2.getValue().contains(mime)) { |
602 |
tmp.put(mime2.getKey(), new RegisterMIMENamespaceResolver(ctx, namespaceregistration.getValue())); |
603 |
findforthis = true; |
604 |
} |
605 |
} |
606 |
loaderok = loaderok && findforthis; |
607 |
} |
608 |
if (!loaderok) { // one missing |
609 |
tmp.clear(); |
610 |
} |
611 |
return tmp; |
612 |
} |
613 |
} |
614 |
} |