/** * Adds a restriction this handler should be restricted to. * @param field metadata property/field * @param regex regular expression * @param caseSensitive whether regular expression should be case sensitive */ public synchronized void addRestriction( String field, String regex, boolean caseSensitive) { restrictions.add(new PropertyMatcher(field, regex, caseSensitive)); }
/** * Removes all restrictions on a given field. * @param field the field to remove restrictions on * @return how many elements were removed * @since 2.4.0 */ public synchronized int removeRestriction(String field) { Iterator<PropertyMatcher> it = restrictions.iterator(); int count = 0; while (it.hasNext()) { PropertyMatcher r = it.next(); if (r.isCaseSensitive() && r.getKey().equals(field) || !r.isCaseSensitive() && r.getKey().equalsIgnoreCase(field)) { it.remove(); count++; } } return count; }
@Override public final void saveToXML(Writer out) throws IOException { try { EnhancedXMLStreamWriter writer = new EnhancedXMLStreamWriter(out); writer.writeStartElement(xmltag); writer.writeAttribute("class", getClass().getCanonicalName()); saveHandlerToXML(writer); for (PropertyMatcher restriction : restrictions) { writer.writeStartElement("restrictTo"); if (restriction.getKey() != null) { writer.writeAttribute("field", restriction.getKey()); } writer.writeAttribute("caseSensitive", Boolean.toString(restriction.isCaseSensitive())); if (restriction.getRegex() != null) { writer.writeCharacters(restriction.getRegex()); } writer.writeEndElement(); } writer.writeEndElement(); writer.flush(); writer.close(); } catch (XMLStreamException e) { throw new IOException("Cannot save as XML.", e); } }
/** * Class to invoke by subclasses to find out if this handler should be * rejected or not based on the metadata restriction provided. * @param reference document reference * @param metadata document metadata. * @param parsed if the document was parsed (i.e. imported) already * @return <code>true</code> if this handler is applicable to the document */ protected final boolean isApplicable( String reference, ImporterMetadata metadata, boolean parsed) { if (restrictions.isEmpty()) { return true; } for (PropertyMatcher restriction : restrictions) { if (restriction.matches(metadata)) { return true; } } if (LOG.isDebugEnabled()) { LOG.debug(getClass() + " handler does not apply to: " + reference); } return false; }
private static List<PropertyMatcher> build( String field, String... regexes) { List<PropertyMatcher> list = new ArrayList<>(); for (String regex : regexes) { list.add(new PropertyMatcher(field, regex, false)); } return list; } }