use of org.apache.sling.scripting.jsp.jasper.xmlparser.ParserUtils in project sling by apache.
the class JspConfig method processWebDotXml.
private void processWebDotXml(ServletContext ctxt) throws JasperException {
InputStream is = null;
try {
URL uri = ctxt.getResource(WEB_XML);
if (uri == null) {
// no web.xml
return;
}
is = uri.openStream();
InputSource ip = new InputSource(is);
ip.setSystemId(uri.toExternalForm());
ParserUtils pu = new ParserUtils();
TreeNode webApp = pu.parseXMLDocument(WEB_XML, ip);
if (webApp == null || getVersion(webApp) < 2.4) {
defaultIsELIgnored = "true";
return;
}
TreeNode jspConfig = webApp.findChild("jsp-config");
if (jspConfig == null) {
return;
}
jspProperties = new Vector();
Iterator jspPropertyList = jspConfig.findChildren("jsp-property-group");
while (jspPropertyList.hasNext()) {
TreeNode element = (TreeNode) jspPropertyList.next();
Iterator list = element.findChildren();
Vector urlPatterns = new Vector();
String pageEncoding = null;
String scriptingInvalid = null;
String elIgnored = null;
String isXml = null;
Vector includePrelude = new Vector();
Vector includeCoda = new Vector();
String deferredSyntaxAllowedAsLiteral = null;
String trimDirectiveWhitespaces = null;
while (list.hasNext()) {
element = (TreeNode) list.next();
String tname = element.getName();
if ("url-pattern".equals(tname))
urlPatterns.addElement(element.getBody());
else if ("page-encoding".equals(tname))
pageEncoding = element.getBody();
else if ("is-xml".equals(tname))
isXml = element.getBody();
else if ("el-ignored".equals(tname))
elIgnored = element.getBody();
else if ("scripting-invalid".equals(tname))
scriptingInvalid = element.getBody();
else if ("include-prelude".equals(tname))
includePrelude.addElement(element.getBody());
else if ("include-coda".equals(tname))
includeCoda.addElement(element.getBody());
else if ("deferred-syntax-allowed-as-literal".equals(tname))
deferredSyntaxAllowedAsLiteral = element.getBody();
else if ("trim-directive-whitespaces".equals(tname))
trimDirectiveWhitespaces = element.getBody();
}
if (urlPatterns.size() == 0) {
continue;
}
// the matching logic easier.
for (int p = 0; p < urlPatterns.size(); p++) {
String urlPattern = (String) urlPatterns.elementAt(p);
String path = null;
String extension = null;
if (urlPattern.indexOf('*') < 0) {
// Exact match
path = urlPattern;
} else {
int i = urlPattern.lastIndexOf('/');
String file;
if (i >= 0) {
path = urlPattern.substring(0, i + 1);
file = urlPattern.substring(i + 1);
} else {
file = urlPattern;
}
// pattern must be "*", or of the form "*.jsp"
if (file.equals("*")) {
extension = "*";
} else if (file.startsWith("*.")) {
extension = file.substring(file.indexOf('.') + 1);
}
// The url patterns are reconstructed as the follwoing:
// path != null, extension == null: / or /foo/bar.ext
// path == null, extension != null: *.ext
// path != null, extension == "*": /foo/*
boolean isStar = "*".equals(extension);
if ((path == null && (extension == null || isStar)) || (path != null && !isStar)) {
if (log.isWarnEnabled()) {
log.warn(Localizer.getMessage("jsp.warning.bad.urlpattern.propertygroup", urlPattern));
}
continue;
}
}
JspProperty property = new JspProperty(isXml, elIgnored, scriptingInvalid, pageEncoding, includePrelude, includeCoda, deferredSyntaxAllowedAsLiteral, trimDirectiveWhitespaces);
JspPropertyGroup propertyGroup = new JspPropertyGroup(path, extension, property);
jspProperties.addElement(propertyGroup);
}
}
} catch (Exception ex) {
throw new JasperException(ex);
} finally {
if (is != null) {
try {
is.close();
} catch (Throwable t) {
}
}
}
}
use of org.apache.sling.scripting.jsp.jasper.xmlparser.ParserUtils in project sling by apache.
the class TagLibraryInfoImpl method parseTLD.
/*
* @param ctxt The JSP compilation context @param uri The TLD's uri @param
* in The TLD's input stream @param jarFileUrl The JAR file containing the
* TLD, or null if the tag library is not packaged in a JAR
*/
private void parseTLD(JspCompilationContext ctxt, String uri, InputStream in, URL jarFileUrl) throws JasperException {
Vector tagVector = new Vector();
Vector tagFileVector = new Vector();
Hashtable functionTable = new Hashtable();
// Create an iterator over the child elements of our <taglib> element
ParserUtils pu = new ParserUtils();
TreeNode tld = pu.parseXMLDocument(uri, in);
// Check to see if the <taglib> root element contains a 'version'
// attribute, which was added in JSP 2.0 to replace the <jsp-version>
// subelement
this.jspversion = tld.findAttribute("version");
// Process each child element of our <taglib> element
Iterator list = tld.findChildren();
while (list.hasNext()) {
TreeNode element = (TreeNode) list.next();
String tname = element.getName();
if (// JSP 1.1
"tlibversion".equals(tname) || "tlib-version".equals(tname)) {
// JSP 1.2
this.tlibversion = element.getBody();
} else if ("jspversion".equals(tname) || "jsp-version".equals(tname)) {
this.jspversion = element.getBody();
} else if ("shortname".equals(tname) || "short-name".equals(tname))
this.shortname = element.getBody();
else if ("uri".equals(tname))
this.urn = element.getBody();
else if ("info".equals(tname) || "description".equals(tname))
this.info = element.getBody();
else if ("validator".equals(tname))
this.tagLibraryValidator = createValidator(element);
else if ("tag".equals(tname))
tagVector.addElement(createTagInfo(element, jspversion));
else if ("tag-file".equals(tname)) {
TagFileInfo tagFileInfo = createTagFileInfo(element, uri, jarFileUrl);
tagFileVector.addElement(tagFileInfo);
} else if ("function".equals(tname)) {
// JSP2.0
FunctionInfo funcInfo = createFunctionInfo(element);
String funcName = funcInfo.getName();
if (functionTable.containsKey(funcName)) {
err.jspError("jsp.error.tld.fn.duplicate.name", funcName, uri);
}
functionTable.put(funcName, funcInfo);
} else if (// Ignored elements
"display-name".equals(tname) || "small-icon".equals(tname) || "large-icon".equals(tname) || "listener".equals(tname)) {
;
} else if ("taglib-extension".equals(tname)) {
// Recognized but ignored
} else {
if (log.isWarnEnabled()) {
log.warn(Localizer.getMessage("jsp.warning.unknown.element.in.taglib", tname));
}
}
}
if (tlibversion == null) {
err.jspError("jsp.error.tld.mandatory.element.missing", "tlib-version");
}
if (jspversion == null) {
err.jspError("jsp.error.tld.mandatory.element.missing", "jsp-version");
}
this.tags = new TagInfo[tagVector.size()];
tagVector.copyInto(this.tags);
this.tagFiles = new TagFileInfo[tagFileVector.size()];
tagFileVector.copyInto(this.tagFiles);
this.functions = new FunctionInfo[functionTable.size()];
int i = 0;
Enumeration enumeration = functionTable.elements();
while (enumeration.hasMoreElements()) {
this.functions[i++] = (FunctionInfo) enumeration.nextElement();
}
}
use of org.apache.sling.scripting.jsp.jasper.xmlparser.ParserUtils in project sling by apache.
the class TagPluginManager method init.
private void init(ErrorDispatcher err) throws JasperException {
if (initialized)
return;
InputStream is = ctxt.getResourceAsStream(TAG_PLUGINS_XML);
if (is == null)
return;
TreeNode root = (new ParserUtils()).parseXMLDocument(TAG_PLUGINS_XML, is);
if (root == null) {
return;
}
if (!TAG_PLUGINS_ROOT_ELEM.equals(root.getName())) {
err.jspError("jsp.error.plugin.wrongRootElement", TAG_PLUGINS_XML, TAG_PLUGINS_ROOT_ELEM);
}
tagPlugins = new HashMap();
Iterator pluginList = root.findChildren("tag-plugin");
while (pluginList.hasNext()) {
TreeNode pluginNode = (TreeNode) pluginList.next();
TreeNode tagClassNode = pluginNode.findChild("tag-class");
if (tagClassNode == null) {
// Error
return;
}
String tagClass = tagClassNode.getBody().trim();
TreeNode pluginClassNode = pluginNode.findChild("plugin-class");
if (pluginClassNode == null) {
// Error
return;
}
String pluginClassStr = pluginClassNode.getBody();
TagPlugin tagPlugin = null;
try {
Class pluginClass = this.getClass().getClassLoader().loadClass(pluginClassStr);
tagPlugin = (TagPlugin) pluginClass.newInstance();
} catch (Exception e) {
throw new JasperException(e);
}
if (tagPlugin == null) {
return;
}
tagPlugins.put(tagClass, tagPlugin);
}
initialized = true;
}
use of org.apache.sling.scripting.jsp.jasper.xmlparser.ParserUtils in project sling by apache.
the class OriginalTldLocationsCache method getUriFromTld.
/*
* Returns the value of the uri element of the given TLD, or null if the
* given TLD does not contain any such element.
*/
private String getUriFromTld(String resourcePath, InputStream in) throws JasperException {
// Parse the tag library descriptor at the specified resource path
TreeNode tld = new ParserUtils().parseXMLDocument(resourcePath, in);
TreeNode uri = tld.findChild("uri");
if (uri != null) {
String body = uri.getBody();
if (body != null)
return body;
}
return null;
}
use of org.apache.sling.scripting.jsp.jasper.xmlparser.ParserUtils in project sling by apache.
the class SlingTldLocationsCache method getUriFromTld.
/*
* Returns the value of the uri element of the given TLD, or null if the
* given TLD does not contain any such element.
*/
private String getUriFromTld(final URL resource) {
InputStream stream = null;
try {
stream = resource.openStream();
// Parse the tag library descriptor at the specified resource path
TreeNode tld = new ParserUtils().parseXMLDocument(resource.toString(), stream);
TreeNode uri = tld.findChild("uri");
if (uri != null) {
String body = uri.getBody();
if (body != null) {
return body;
}
}
} catch (Exception e) {
// TODO: handle
} finally {
if (stream != null) {
try {
stream.close();
} catch (Throwable t) {
// do nothing
}
}
}
return null;
}
Aggregations