Selector selector, String toBeReplacedSelectorName,
SimpleSelector candidateSelector) {
if (!toString(selector).contains(toBeReplacedSelectorName)) {
return selector;
}
SelectorFactory factory = new SelectorFactoryImpl();
if (selector instanceof SimpleSelector) {
return createSimpleSelectorWithSomePartReplaced(
(SimpleSelector) selector, toBeReplacedSelectorName,
candidateSelector);
} else if (selector instanceof DescendantSelector) {
DescendantSelector descendantSelector = (DescendantSelector) selector;
Selector ancestor = descendantSelector.getAncestorSelector();
SimpleSelector simpleSelector = descendantSelector
.getSimpleSelector();
return factory.createDescendantSelector(
createSelectorWithSomePartReplaced(ancestor,
toBeReplacedSelectorName, candidateSelector),
createSimpleSelectorWithSomePartReplaced(simpleSelector,
toBeReplacedSelectorName, candidateSelector));
} else if (selector instanceof DirectAdjacentSelectorImpl) {
SiblingSelector siblingSelector = (SiblingSelector) selector;
Selector ancestor = siblingSelector.getSelector();
SimpleSelector simpleSelector = siblingSelector
.getSiblingSelector();
return factory.createDirectAdjacentSelector(
Selector.SAC_DIRECT_ADJACENT_SELECTOR, ancestor,
simpleSelector);
} else if (selector instanceof CompositeSelector) {
CompositeSelector compositeSelector = (CompositeSelector) selector;
Selector first = compositeSelector.getFirst();