org.lockss.util.AuXpathMatcher Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of lockss-core Show documentation
Show all versions of lockss-core Show documentation
The Slimmed Down LOCKSS Daemon Core
The newest version!
/*
Copyright (c) 2000-2017 Board of Trustees of Leland Stanford Jr. University,
all rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
STANFORD UNIVERSITY BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Except as contained in this notice, the name of Stanford University shall not
be used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from Stanford University.
*/
package org.lockss.util;
import java.util.*;
import org.apache.commons.jxpath.*;
import org.lockss.app.*;
import org.lockss.config.*;
import org.lockss.plugin.*;
/**
* Matches an AU against an XPath predicate. The object to which the XPath
* is applied is a restricted view of an AU, with the attributes
* name
, auId
, and
tdbAu, from which
* the AU's config params, tdb attributes, title, publisher, etc. may be
* accessed. Examples:
*
-
*
[name='AuName']
- True if the AU name is "AuName"
* [tdbAu/year >= 1996 and tdbAu/year <= 2005]
- True if * the year (see below) is between 1996 and 2005
* [RE:isMatchRe(tdbAu/journalTitle, '^JAMA')]
- True if * the journal title starts with "JAMA"
* [RE:isMatchRe(tdbAu/params/base_url, * '\.univ\.edu/(path1|path2)')]
- True if the base_url config param * matches the regexp
*
* Expressions may access the tdb attribute and AU config param maps, * various scalar data and a number of convenience accessors. For a * complete list see the getters in {@link org.lockss.config.TdbAu}. *
-
*
name
- AU name
* auId
- AUID
* tdbAu/name
- AU name in tdb
* tdbAu/params/param_name
- named AU config parameter
* tdbAu/attrs/attr_name
- named tdb attribute
* tdbAu/year
- Theyear
attribute if present, else theyear
AU config param, else null
* tdbAu/journalTitle
* tdbAu/publisherName
* tdbAu/publicationType
* tdbAu/issn
* - ... *
-
*
$myhost
- the value of {@value * org.lockss.config.ConfigManager#PARAM_PLATFORM_FQDN} *
RE:isMatchRe(string, regexp)
* performs a regexp match against the string.
* @param xpathPred an XPath predicate (eg,
* [journalTitle='Dog Journal']
)
*/
AuXpathMatcher(String xpathPred) {
if (!(xpathPred.startsWith("[") && xpathPred.endsWith("]"))) {
throw new IllegalArgumentException("XPath predicate must be enclosed in \"[\" ... \"]\"");
}
xpath = xpathPred;
expr = JXPathContext.compile("." + xpath);
}
/** Return true if the XPath predicate returns true when applied to the
* AU */
public boolean isMatch(ArchivalUnit au) {
return isMatch(new AuXpathAccessor(au));
}
/** Return true if the XPath predicate returns true when applied to the
* AU */
public boolean isMatch(AuXpathAccessor auxa) {
JXPathContext context = newContext(sharedContext, auxa);
Iterator iter = expr.iteratePointers(context);
return iter.hasNext();
}
private JXPathContext newContext(JXPathContext sharedContext,
AuXpathAccessor auxa) {
JXPathContext context = JXPathContext.newContext(sharedContext, auxa);
context.getVariables().declareVariable("myhost",
ConfigManager.getPlatformHostname());
return context;
}
public String getXpath() {
return xpath;
}
public boolean equals(Object o) {
if (! (o instanceof AuXpathMatcher)) {
return false;
}
AuXpathMatcher oset = (AuXpathMatcher)o;
return xpath.equals(oset.getXpath());
}
public int hashCode() {
int hash = 0x227035;
hash += xpath.hashCode();
return hash;
}
public String toString() {
StringBuilder sb = new StringBuilder(40);
sb.append("[AuXpath: ");
sb.append(xpath);
sb.append("]");
return sb.toString();
}
/** Return the object located by the XPath. Useful for debugging. */
public Object eval(ArchivalUnit au) {
return eval(new AuXpathAccessor(au));
}
/** Return the object located by the XPath. Useful for debugging. */
public Object eval(AuXpathAccessor auxa) {
JXPathContext context = newContext(sharedContext, auxa);
return context.getValue("" + xpath.substring(1, xpath.length()-1));
}
/** Factory method to create an AuXpathMatcher */
public static AuXpathMatcher create(String xpathPred) {
return new AuXpathMatcher(xpathPred);
}
/** Bean to provide access to a limited set of AU features, for XPath
* matching */
public static class AuXpathAccessor {
private ArchivalUnit au;
public AuXpathAccessor(ArchivalUnit au) {
this.au = au;
}
/** Return the tdb, from which */
public TdbAu getTdbAu() {
return au.getTdbAu();
}
public String getAuId() {
return au.getAuId();
}
public String getPluginId() {
return au.getPluginId();
}
public String getName() {
return au.getName();
}
public ArchivalUnit getAu() {
return au;
}
public String toString() {
StringBuilder sb = new StringBuilder(40);
sb.append("[AuA: ");
sb.append(au.toString());
sb.append("]");
return sb.toString();
}
}
}