public class StandardAttributeRewriter extends Object implements AttributeRewriter
AttributeRewriter
.
The StandardAttributeRewriter
is an re-implementation of
attribute-rewrite process that used to be embedded in
FastArchivalUrlReplayParseEventHandler
, under
AttributeRewriter
interface.
It also offers simple configuration mechanism for adding/changing
attribute rewrite rules through Properties
object. New rules
can be added easily through SpringFramework's support for Properties
object. Well-known rewrite rules for attributes, such as A/@HREF
,
LINK/@HREF
and */@ONLOAD
, are provided by default and
implemented with the same mechanism.
Use setConfigProperties(Properties)
to add more rewrite rules.
Just in case you need an entirely different set of rewrite rules, it is possible to
disable default rule set by setting true
to defaultRulesDisabled
property.
If you need to configure rewrite rules from different sources than Properties
object, it'd be easiest to override loadRules()
method.
Note: caller of this class may implement more complex rewrite process, and may by-pass this class for certain tags/attributes.
While this class is similar to AttributeModifyingRule
class in functionality,
interface is more specific to attribute rewriting and it is easier to customize.
FastArchivalUrlReplayParseEventHandler
Modifier and Type | Class and Description |
---|---|
protected class |
StandardAttributeRewriter.TransformAttr |
protected class |
StandardAttributeRewriter.TransformAttrIfAttrValue |
Modifier and Type | Field and Description |
---|---|
static String |
CONFIG_PROPERTIES
name of properties file with default rewrite rules.
|
protected static String |
RE_ATTR |
protected static String |
RE_TAG
Regular expression for parsing tag name part of rule keys.
|
protected static Pattern |
RE_TAG_ATTR_TYPE |
Constructor and Description |
---|
StandardAttributeRewriter() |
Modifier and Type | Method and Description |
---|---|
void |
init() |
protected void |
initTransformers()
Initialize
StringTransformer s, register them to
transformers map by their name. |
boolean |
isDefaultRulesDisabled() |
protected void |
loadRules() |
protected void |
loadRulesFromProperties(Properties p)
build rewrite rule set from textual description in
p . |
void |
rewrite(ReplayParseContext context,
org.htmlparser.nodes.TagNode tag)
Rewrite attributes of HTML tag
tag . |
void |
setConfigProperties(Properties configProperties)
set
Properties object with additional attribute rewrite rules. |
void |
setCustomTransformers(Map<String,StringTransformer> customTransformers)
add more
StringTransformer s. |
void |
setDefaultRulesDisabled(boolean defaultRulesDisabled)
set
true if you want to disable loading default rewrite
rules from in-class-path properties file. |
void |
setJsBlockTrans(StringTransformer jsBlockTrans)
set StringTransformer for rewriting JavaScript attribute values.
|
void |
setUnescapeAttributeValues(boolean unescapeAttributeValues)
set this property false if you want to disable unescaping
(and corresponding re-escaping) of attribute values.
|
public static final String CONFIG_PROPERTIES
protected static final String RE_TAG
protected static final String RE_ATTR
protected static final Pattern RE_TAG_ATTR_TYPE
public void setJsBlockTrans(StringTransformer jsBlockTrans)
HREF="javascript:..."
.)
Note changing this property after bean initialization is not supported.jsBlockTrans
- StringTransformerpublic void setConfigProperties(Properties configProperties)
Properties
object with additional attribute rewrite rules.
if you want this to replace entire rewrite rule, set defaultRulesDisabled
to true
.configProperties
- public void setDefaultRulesDisabled(boolean defaultRulesDisabled)
true
if you want to disable loading default rewrite
rules from in-class-path properties file.defaultRulesDisabled
- public boolean isDefaultRulesDisabled()
public void setCustomTransformers(Map<String,StringTransformer> customTransformers)
StringTransformer
s.
Caveat: change has effect only before calling init()
.
customTransformers
- a Map with transformer name
as keys and corresponding StringTransformer
object as values.public void setUnescapeAttributeValues(boolean unescapeAttributeValues)
By default, HTML entities (such as &
)
in attribute values are unescaped before translation attempt,
and then escaped back before writing out. Although this is
supposedly the right thing to do, it has a side-effect: all
bare "&
" (not escaped as "&
")
will be replaced by "&
". Setting this property
to false
disables it.
As URL rewrite does neither parse nor modify query part, it
should mostly work without unescaping. But there may be some
corner cases where escaping is crucial. Don't set this to false
unless it's absolutely necessary.
unescapeAttributeValues
- false
to disable unescapingpublic void init() throws IOException
IOException
protected void initTransformers()
StringTransformer
s, register them to
transformers
map by their name.protected void loadRules() throws IOException
IOException
protected void loadRulesFromProperties(Properties p)
p
.
rule set is a collection of a sub-tree for each element, stored in rules
Map. rules
maps tag name to a tree of TransformAttr
. TransformAttr
tree is in essence a linked list of TransformAttr
grouped by attribute name. In each
group, TransformAttr
s are sorted by their specificity. Within the same
specificity, the TransformAttr
added last comes on top (so that rules added through
Properties
override default rules.). As the order of keys in a single Properties
is unspecified, there's no guarantee later line overrides former lines with the same key.p
- Properties with rewrite rules.public void rewrite(ReplayParseContext context, org.htmlparser.nodes.TagNode tag)
AttributeRewriter
tag
.
This method checks all attributes of tag
it concerned
with, and modify their value in-place. context
provides
an interface for rewriting URL for replay, etc.rewrite
in interface AttributeRewriter
context
- provides access to various context informationtag
- tag whose attributes are to be rewritten.Copyright © 2005–2015 IIPC. All rights reserved.