use of org.jdom2.xpath.XPath in project ddf by codice.
the class OpenSearchSource method createResponseFromEntry.
/**
* Creates a single response from input parameters. Performs XPath operations on the document to
* retrieve data not passed in.
*
* @param entry a single Atom entry
* @return single response
* @throws ddf.catalog.source.UnsupportedQueryException
*/
private List<Result> createResponseFromEntry(SyndEntry entry) throws UnsupportedQueryException {
String id = entry.getUri();
if (id != null && !id.isEmpty()) {
id = id.substring(id.lastIndexOf(':') + 1);
}
List<SyndContent> contents = entry.getContents();
List<SyndCategory> categories = entry.getCategories();
List<Metacard> metacards = new ArrayList<>();
List<Element> foreignMarkup = entry.getForeignMarkup();
String relevance = "";
String source = "";
for (Element element : foreignMarkup) {
if (element.getName().equals("score")) {
relevance = element.getContent(0).getValue();
}
}
//we currently do not support downloading content via an RSS enclosure, this support can be added at a later date if we decide to include it
for (SyndContent content : contents) {
MetacardImpl metacard = getMetacardImpl(parseContent(content.getValue(), id));
metacard.setSourceId(this.shortname);
String title = metacard.getTitle();
if (StringUtils.isEmpty(title)) {
metacard.setTitle(entry.getTitle());
}
if (!source.isEmpty()) {
metacard.setSourceId(source);
}
metacards.add(metacard);
}
for (int i = 0; i < categories.size() && i < metacards.size(); i++) {
SyndCategory category = categories.get(i);
Metacard metacard = metacards.get(i);
if (StringUtils.isBlank(metacard.getContentTypeName())) {
((MetacardImpl) metacard).setContentTypeName(category.getName());
}
}
List<Result> results = new ArrayList<>();
for (Metacard metacard : metacards) {
ResultImpl result = new ResultImpl(metacard);
if (relevance == null || relevance.isEmpty()) {
LOGGER.debug("couldn't find valid relevance. Setting relevance to 0");
relevance = "0";
}
result.setRelevanceScore(new Double(relevance));
results.add(result);
}
return results;
}
use of org.jdom2.xpath.XPath in project jspwiki by apache.
the class CleanTextRenderer method getString.
/**
* {@inheritDoc}
*/
public String getString() throws IOException {
StringBuilder sb = new StringBuilder();
try {
XPath xp = XPath.newInstance(ALL_TEXT_NODES);
List nodes = xp.selectNodes(m_document.getDocument());
for (Iterator i = nodes.iterator(); i.hasNext(); ) {
Object el = i.next();
if (el instanceof Text) {
sb.append(((Text) el).getValue());
}
}
} catch (JDOMException e) {
log.error("Could not parse XPATH expression");
throw new IOException(e.getMessage());
}
return sb.toString();
}
use of org.jdom2.xpath.XPath in project jspwiki by apache.
the class WebContainerAuthorizer method isConstrained.
/**
* <p>
* Protected method that identifies whether a particular webapp URL is
* constrained to a particular Role. The resource is considered constrained
* if:
* </p>
* <ul>
* <li>the web application deployment descriptor contains a
* <code>security-constraint</code> with a child
* <code>web-resource-collection/url-pattern</code> element matching the
* URL, <em>and</em>:</li>
* <li>this constraint also contains an
* <code>auth-constraint/role-name</code> element equal to the supplied
* Role's <code>getName()</code> method. If the supplied Role is Role.ALL,
* it matches all roles</li>
* </ul>
* @param url the web resource
* @param role the role
* @return <code>true</code> if the resource is constrained to the role,
* <code>false</code> otherwise
* @throws JDOMException if elements cannot be parsed correctly
*/
public boolean isConstrained(String url, Role role) throws JDOMException {
Element root = m_webxml.getRootElement();
XPath xpath;
String selector;
// Get all constraints that have our URL pattern
// (Note the crazy j: prefix to denote the 2.4 j2ee schema)
selector = "//j:web-app/j:security-constraint[j:web-resource-collection/j:url-pattern=\"" + url + "\"]";
xpath = XPath.newInstance(selector);
xpath.addNamespace("j", J2EE_SCHEMA_25_NAMESPACE);
List<?> constraints = xpath.selectNodes(root);
// Get all constraints that match our Role pattern
selector = "//j:web-app/j:security-constraint[j:auth-constraint/j:role-name=\"" + role.getName() + "\"]";
xpath = XPath.newInstance(selector);
xpath.addNamespace("j", J2EE_SCHEMA_25_NAMESPACE);
List<?> roles = xpath.selectNodes(root);
// If we can't find either one, we must not be constrained
if (constraints.size() == 0) {
return false;
}
// Shortcut: if the role is ALL, we are constrained
if (role.equals(Role.ALL)) {
return true;
}
// If no roles, we must not be constrained
if (roles.size() == 0) {
return false;
}
// If a constraint is contained in both lists, we must be constrained
for (Iterator<?> c = constraints.iterator(); c.hasNext(); ) {
Element constraint = (Element) c.next();
for (Iterator<?> r = roles.iterator(); r.hasNext(); ) {
Element roleConstraint = (Element) r.next();
if (constraint.equals(roleConstraint)) {
return true;
}
}
}
return false;
}
use of org.jdom2.xpath.XPath in project mycore by MyCoRe-Org.
the class MCRMigrationCommands method fixDerivateLinks.
@MCRCommand(syntax = "fix invalid derivate links {0} for {1}", help = "Fixes the paths of all derivate links " + "({0} -> xpath -> e.g. /mycoreobject/metadata/derivateLinks/derivateLink) for object {1}. (MCR-1267)", order = 15)
public static void fixDerivateLinks(String xpath, String id) throws IOException, JDOMException, SAXException {
// get mcr object
MCRObjectID objectID = MCRObjectID.getInstance(id);
// find derivate links
Document xml = MCRXMLMetadataManager.instance().retrieveXML(objectID);
Element mcrObjectXML = xml.getRootElement();
XPathExpression<Element> expression = XPathFactory.instance().compile(xpath, Filters.element());
List<Element> derivateLinkElements = expression.evaluate(mcrObjectXML);
// check them
boolean changedObject = false;
for (Element derivateLinkElement : derivateLinkElements) {
String href = derivateLinkElement.getAttributeValue("href", MCRConstants.XLINK_NAMESPACE);
MCRMetaDerivateLink link = new MCRMetaDerivateLink();
link.setReference(href, null, null);
String owner = link.getOwner();
try {
String path = link.getPath();
MCRPath mcrPath = MCRPath.getPath(owner, path);
if (!Files.exists(mcrPath)) {
// -> e.g. a?c.tif -> path (a), query (c.tif) which is obvious wrong
if (tryRawPath(objectID, derivateLinkElement, href, link, owner)) {
changedObject = true;
} else {
LOGGER.warn("{} of {}cannot be found on file system. This is most likly a dead link.", href, objectID);
}
}
} catch (URISyntaxException uriExc) {
// not encoded properly
if (tryRawPath(objectID, derivateLinkElement, href, link, owner)) {
changedObject = true;
} else {
LOGGER.warn("{} of {} isn't URI encoded and cannot be found on file system. This is most likly a dead link.", href, objectID);
}
}
}
// store the mcr object if its changed
if (changedObject) {
// we use MCRXMLMetadataMananger because we don't want to validate the old mcr object
MCRXMLMetadataManager.instance().update(objectID, xml, new Date());
// manually fire update event
MCRObject newObject = MCRMetadataManager.retrieveMCRObject(objectID);
newObject.setImportMode(true);
MCRMetadataManager.fireUpdateEvent(newObject);
}
}
use of org.jdom2.xpath.XPath in project mycore by MyCoRe-Org.
the class MCRMODSWrapper method removeElements.
public void removeElements(String xPath) {
Iterator<Element> selected;
try {
selected = buildXPath(xPath).evaluate(getMODS()).iterator();
} catch (JDOMException ex) {
String msg = "Could not remove elements at " + xPath;
throw new MCRException(msg, ex);
}
while (selected.hasNext()) {
Element element = selected.next();
element.detach();
}
}
Aggregations