1
17 package org.apache.tomcat.util.descriptor.tld;
18
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.security.AccessController;
22
23 import org.apache.juli.logging.Log;
24 import org.apache.juli.logging.LogFactory;
25 import org.apache.tomcat.util.descriptor.Constants;
26 import org.apache.tomcat.util.descriptor.DigesterFactory;
27 import org.apache.tomcat.util.descriptor.XmlErrorHandler;
28 import org.apache.tomcat.util.digester.Digester;
29 import org.apache.tomcat.util.digester.RuleSet;
30 import org.apache.tomcat.util.security.PrivilegedGetTccl;
31 import org.apache.tomcat.util.security.PrivilegedSetTccl;
32 import org.xml.sax.InputSource;
33 import org.xml.sax.SAXException;
34
35
38 public class TldParser {
39 private final Log log = LogFactory.getLog(TldParser.class);
40 private final Digester digester;
41
42 public TldParser(boolean namespaceAware, boolean validation,
43 boolean blockExternal) {
44 this(namespaceAware, validation, new TldRuleSet(), blockExternal);
45 }
46
47 public TldParser(boolean namespaceAware, boolean validation, RuleSet ruleSet,
48 boolean blockExternal) {
49 digester = DigesterFactory.newDigester(
50 validation, namespaceAware, ruleSet, blockExternal);
51 }
52
53 public TaglibXml parse(TldResourcePath path) throws IOException, SAXException {
54 ClassLoader original;
55 if (Constants.IS_SECURITY_ENABLED) {
56 PrivilegedGetTccl pa = new PrivilegedGetTccl();
57 original = AccessController.doPrivileged(pa);
58 } else {
59 original = Thread.currentThread().getContextClassLoader();
60 }
61 try (InputStream is = path.openStream()) {
62 if (Constants.IS_SECURITY_ENABLED) {
63 PrivilegedSetTccl pa = new PrivilegedSetTccl(TldParser.class.getClassLoader());
64 AccessController.doPrivileged(pa);
65 } else {
66 Thread.currentThread().setContextClassLoader(TldParser.class.getClassLoader());
67 }
68 XmlErrorHandler handler = new XmlErrorHandler();
69 digester.setErrorHandler(handler);
70
71 TaglibXml taglibXml = new TaglibXml();
72 digester.push(taglibXml);
73
74 InputSource source = new InputSource(path.toExternalForm());
75 source.setByteStream(is);
76 digester.parse(source);
77 if (!handler.getWarnings().isEmpty() || !handler.getErrors().isEmpty()) {
78 handler.logFindings(log, source.getSystemId());
79 if (!handler.getErrors().isEmpty()) {
80
81 throw handler.getErrors().iterator().next();
82 }
83 }
84 return taglibXml;
85 } finally {
86 digester.reset();
87 if (Constants.IS_SECURITY_ENABLED) {
88 PrivilegedSetTccl pa = new PrivilegedSetTccl(original);
89 AccessController.doPrivileged(pa);
90 } else {
91 Thread.currentThread().setContextClassLoader(original);
92 }
93 }
94 }
95
96 public void setClassLoader(ClassLoader classLoader) {
97 digester.setClassLoader(classLoader);
98 }
99 }
100