public class ModifyPropertyFilter extends AbstractDocumentFilter
Document
filter that alters the values of the specified
Properties
. The filter will scrutinize the
Values
returned by the supplied Properties
.
If the value (as a string) matches the regular expression pattern
,
then all matching regions of the value will be replaced with the
replacement
string.
If the overwrite
flag is true
, the modified
property values replace any matching values of the target property.
Otherwise, the modified property values supplement any existing values
of the target property.
Example documentFilters.xml
Configurations:
The following example replaces all instances of the word "Foo" with "Bar"
in the Category
property.
<bean id="FooToBar"
class="com.google.enterprise.connector.util.filter.ModifyPropertyFilter">
<property name="propertyName" value="Category"/>
<property name="pattern" value="Foo"/>
<property name="replacement" value="Bar"/>
<property name="overwrite" value="true"/>
</bean>
The following example adds "Paul Erdös" to the list of Authors
of documents for which I am also an author. This will give me an Erdös
Number of 1!
<!-- Add Erdös as co-author of all my documents. -->
<bean id="AddErdosAuthor"
class="com.google.enterprise.connector.util.filter.AddPropertyFilter">
<property name="propertyName" value="Author"/>
<property name="pattern" value="C'est Moi"/>
<property name="replacement" value="Paul Erdös"/>
<property name="overwrite" value="false"/>
</bean>
The following example replaces one or more instances of the characters
'.' or '_' with a single space for all values of the Foo
and
Bar
properties. The original values are kept, and new values
with whitespace delimiters are added to the properties.
<!-- Replace '.' and '_' with a space. -->
<bean id="DotUnderscoreToWhiteSpace"
class="com.google.enterprise.connector.util.filter.ModifyPropertyFilter">
<property name="propertyNames">
<set>
<value>Foo</value>
<value>Bar</value>
</set>
</property>
<property name="pattern" value="[_.]+"/>
<property name="replacement" value=" "/>
<property name="overwrite" value="false"/>
</bean>
When used with binary values, the entire value is buffered and the
modified value is stored in a byte
array.
Constructor and Description |
---|
ModifyPropertyFilter() |
Modifier and Type | Method and Description |
---|---|
Property |
findProperty(Document source,
java.lang.String name)
Finds a
Property by name . |
void |
setEncoding(java.lang.String encoding)
Sets the the name of the character encoding type to be used.
|
void |
setMimeType(java.lang.String mimeType)
Sets the media types of the
Document objects to modify. |
void |
setMimeTypes(java.util.Set<java.lang.String> mimeTypes)
Sets the media types of the
Document objects to modify. |
void |
setOverwrite(boolean overwrite)
Sets the
overwrite values flag. |
void |
setPattern(java.lang.String pattern)
Sets the regular expression pattern to match in the values.
|
void |
setPropertyName(java.lang.String propertyName)
Sets the the name of the
Property to filter. |
void |
setPropertyNames(java.util.Set<java.lang.String> propertyNames)
Sets the the names of the
Properties to filter. |
void |
setReplacement(java.lang.String replacement)
Sets the replacement string for matching regions in the values.
|
java.lang.String |
toString() |
getPropertyNames, getPropertyValues, newDocumentFilter
public void setPropertyName(java.lang.String propertyName)
Property
to filter.
A convenience method that is equivalent to calling
setPropertyNames(Collections.singleton(propertyName)
.
propertyName
- the name of the Property
to filterjava.lang.IllegalArgumentException
- if propertyName
is null
or emptypublic void setPropertyNames(java.util.Set<java.lang.String> propertyNames)
Properties
to filter.propertyNames
- a Set
of names of the
Properties
to filterjava.lang.NullPointerException
- if propertyNames
is null
public void setMimeTypes(java.util.Set<java.lang.String> mimeTypes)
Document
objects to modify.mimeTypes
- a Set
of names of the media types to filterjava.lang.NullPointerException
- if mimeTypes
is null
public void setMimeType(java.lang.String mimeType)
Document
objects to modify.mimeType
- the name of the media type to filterjava.lang.NullPointerException
- if mimeType
is null
public void setPattern(java.lang.String pattern) throws java.util.regex.PatternSyntaxException
pattern
must conform to the syntax defined in
java.util.regex.Pattern
.pattern
- the regular expression pattern to match in the valuesjava.util.regex.PatternSyntaxException
- if pattern
's syntax is invalidjava.lang.IllegalArgumentException
- if pattern
is null
or emptypublic void setReplacement(java.lang.String replacement)
replacement
string may refer to
capturing groups from the pattern
as $1, $2
, etc.
Therefore, literal instances of '\'
and '$'
in the
replacement string need to be properly
escaped.replacement
- the replacement String for matching regions in the
valuespublic void setOverwrite(boolean overwrite)
overwrite
values flag. If true
, matching values
are overwritten with the modified value. If false
, matching
values are augmented by adding an additional modified value.
Default overwrite
is false
.overwrite
- the overwrite flagpublic void setEncoding(java.lang.String encoding)
encoding
- name of encoding typepublic Property findProperty(Document source, java.lang.String name) throws RepositoryException
Property
by name
. If the source
Document
has a property of that name, then that property
is returned.
If any of the Property's values (as a string) match the regular
expression pattern
, then all matching regions of the value
will be replaced with the replacement
string.
The modified value may either augment or overwrite the original value,
based upon the overwrite
flag.
findProperty
in class AbstractDocumentFilter
source
- the source Document
for this filtername
- the name of the property to findnull
otherwiseRepositoryException
- if a repository access error occursRepositoryDocumentException
- if a document has fatal
processing errorspublic java.lang.String toString()
toString
in class AbstractDocumentFilter