use of org.jdom2.filter.Filters in project jspwiki by apache.
the class ModuleBean method doGet.
/**
* {@inheritDoc}
*/
public String doGet(WikiContext context) {
Collection<T> filters = modules();
Element root = title();
Element tb = containerForModuleDetail(root);
Element trHead = heading();
tb.addContent(trHead);
for (T info : filters) {
Element tr = rowBody(info);
tb.addContent(tr);
}
return XhtmlUtil.serialize(root, XhtmlUtil.EXPAND_EMPTY_NODES);
}
use of org.jdom2.filter.Filters in project jspwiki by apache.
the class XmlUtil method parse.
/**
* Parses the given XML file and returns the requested nodes. If there's an error accessing or parsing the file, an
* empty list is returned.
*
* @param xml file to parse; matches all resources from classpath, filters repeated items.
* @param requestedNodes requestd nodes on the xml file
* @return the requested nodes of the XML file.
*/
public static List<Element> parse(String xml, String requestedNodes) {
if (StringUtils.isNotEmpty(xml) && StringUtils.isNotEmpty(requestedNodes)) {
Set<Element> readed = new HashSet<Element>();
SAXBuilder builder = new SAXBuilder();
try {
Enumeration<URL> resources = XmlUtil.class.getClassLoader().getResources(xml);
while (resources.hasMoreElements()) {
URL resource = resources.nextElement();
log.debug("reading " + resource.toString());
Document doc = builder.build(resource);
XPathFactory xpfac = XPathFactory.instance();
XPathExpression<Element> xp = xpfac.compile(requestedNodes, Filters.element());
// filter out repeated items
readed.addAll(xp.evaluate(doc));
}
return new ArrayList<Element>(readed);
} catch (IOException ioe) {
log.error("Couldn't load all " + xml + " resources", ioe);
} catch (JDOMException jdome) {
log.error("error parsing " + xml + " resources", jdome);
}
}
return Collections.<Element>emptyList();
}
use of org.jdom2.filter.Filters in project mycore by MyCoRe-Org.
the class MCRAutoDeploy method registerWebFragment.
private void registerWebFragment(final ServletContext servletContext, final MCRComponent comp) {
if (!isHandledByServletContainer(servletContext, comp)) {
try {
final JarFile jar = new JarFile(comp.getJarFile());
Collections.list(jar.entries()).stream().filter(file -> file.getName().equals(WEB_FRAGMENT)).findFirst().ifPresent(file -> {
final SAXBuilder builder = new SAXBuilder();
try {
final InputStream is = jar.getInputStream(file);
final Document doc = builder.build(is);
final Element root = doc.getRootElement();
final Namespace ns = root.getNamespace();
final List<Element> filters = root.getChildren("filter", ns);
final List<Element> fmaps = root.getChildren("filter-mapping", ns);
filters.forEach(filter -> {
final String name = filter.getChildText("filter-name", ns);
final String className = filter.getChildText("filter-class", ns);
fmaps.stream().filter(mapping -> mapping.getChildText("filter-name", ns).equals(name)).findFirst().ifPresent(mapping -> {
LOGGER.info("Register Filter {} ({})...", name, className);
Optional.ofNullable(servletContext.addFilter(name, className)).<Runnable>map(fr -> () -> {
final List<Element> dispatchers = mapping.getChildren("dispatcher", ns);
final EnumSet<DispatcherType> eDT = dispatchers.isEmpty() ? null : dispatchers.stream().map(d -> DispatcherType.valueOf(d.getTextTrim())).collect(Collectors.toCollection(() -> EnumSet.noneOf(DispatcherType.class)));
final List<Element> servletNames = mapping.getChildren("servlet-name", ns);
if (!servletNames.isEmpty()) {
fr.addMappingForServletNames(eDT, false, servletNames.stream().map(sn -> {
LOGGER.info("...add servlet mapping: {}", sn.getTextTrim());
return sn.getTextTrim();
}).toArray(String[]::new));
}
final List<Element> urlPattern = mapping.getChildren("url-pattern", ns);
if (!urlPattern.isEmpty()) {
fr.addMappingForUrlPatterns(eDT, false, urlPattern.stream().map(url -> {
LOGGER.info("...add url mapping: {}", url.getTextTrim());
return url.getTextTrim();
}).toArray(String[]::new));
}
}).orElse(() -> LOGGER.warn("Filter {} already registered!", name)).run();
});
});
final List<Element> servlets = root.getChildren("servlet", ns);
final List<Element> smaps = root.getChildren("servlet-mapping", ns);
servlets.forEach(servlet -> {
final String name = servlet.getChildText("servlet-name", ns);
final String className = servlet.getChildText("servlet-class", ns);
smaps.stream().filter(mapping -> mapping.getChildText("servlet-name", ns).equals(name)).findFirst().ifPresent(mapping -> {
LOGGER.info("Register Servlet {} ({})...", name, className);
Optional.ofNullable(servletContext.addServlet(name, className)).<Runnable>map(sr -> () -> mapping.getChildren("url-pattern", ns).stream().forEach(url -> {
LOGGER.info("...add url mapping: {}", url.getTextTrim());
sr.addMapping(url.getTextTrim());
})).orElse(() -> LOGGER.error("Servlet{} already registered!", name)).run();
});
});
} catch (IOException | JDOMException e) {
LOGGER.error("Couldn't parse " + WEB_FRAGMENT, e);
}
});
jar.close();
} catch (final IOException e) {
LOGGER.error("Couldn't parse JAR!", e);
}
}
}
Aggregations