update copyright
[fedora-idea.git] / xml / openapi / src / com / intellij / patterns / XmlTagPattern.java
blob5be06fa8472e53da7ba67e896f4fe1a1aa89d4e6
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.patterns;
18 import com.intellij.openapi.util.Comparing;
19 import com.intellij.psi.xml.XmlTag;
20 import com.intellij.psi.meta.PsiMetaData;
21 import com.intellij.util.ProcessingContext;
22 import org.jetbrains.annotations.NonNls;
23 import org.jetbrains.annotations.NotNull;
24 import org.jetbrains.annotations.Nullable;
26 import java.util.Arrays;
27 import java.util.Collection;
29 /**
30 * @author spleaner
32 public class XmlTagPattern<Self extends XmlTagPattern<Self>> extends XmlNamedElementPattern<XmlTag, Self> {
33 protected XmlTagPattern() {
34 super(new InitialPatternCondition<XmlTag>(XmlTag.class) {
35 public boolean accepts(@Nullable final Object o, final ProcessingContext context) {
36 return o instanceof XmlTag;
38 });
41 protected XmlTagPattern(@NotNull final InitialPatternCondition<XmlTag> condition) {
42 super(condition);
45 protected String getLocalName(XmlTag tag) {
46 return tag.getLocalName();
49 protected String getNamespace(XmlTag tag) {
50 return tag.getNamespace();
53 public Self withAttributeValue(@NotNull @NonNls final String attributeName, @NotNull final String attributeValue) {
54 return with(new PatternCondition<XmlTag>("withAttributeValue") {
55 public boolean accepts(@NotNull final XmlTag xmlTag, final ProcessingContext context) {
56 return Comparing.equal(xmlTag.getAttributeValue(attributeName), attributeValue);
58 });
61 public Self withAnyAttribute(@NotNull @NonNls final String... attributeNames) {
62 return with(new PatternCondition<XmlTag>("withAnyAttribute") {
63 public boolean accepts(@NotNull final XmlTag xmlTag, final ProcessingContext context) {
64 for (String attributeName : attributeNames) {
65 if (xmlTag.getAttribute(attributeName) != null) {
66 return true;
69 return false;
71 });
74 public Self withDescriptor(@NotNull final ElementPattern<? extends PsiMetaData> metaDataPattern) {
75 return with(new PatternCondition<XmlTag>("withDescriptor") {
76 public boolean accepts(@NotNull final XmlTag xmlTag, final ProcessingContext context) {
77 return metaDataPattern.accepts(xmlTag.getDescriptor());
79 });
82 public Self isFirstSubtag(@NotNull final ElementPattern pattern) {
83 return with(new PatternCondition<XmlTag>("isFirstSubtag") {
84 public boolean accepts(@NotNull final XmlTag xmlTag, final ProcessingContext context) {
85 final XmlTag parent = xmlTag.getParentTag();
86 return parent != null &&
87 pattern.getCondition().accepts(parent, context) && parent.getSubTags()[0] == xmlTag;
89 });
92 public Self withFirstSubTag(@NotNull final ElementPattern<? extends XmlTag> pattern) {
93 return withSubTags(StandardPatterns.<XmlTag>collection().first(pattern));
96 public Self withSubTags(@NotNull final ElementPattern<? extends Collection<XmlTag>> pattern) {
97 return with(new PatternCondition<XmlTag>("withSubTags") {
98 public boolean accepts(@NotNull final XmlTag xmlTag, final ProcessingContext context) {
99 return pattern.getCondition().accepts(Arrays.asList(xmlTag.getSubTags()), context);
104 public Self withoutAttributeValue(@NotNull @NonNls final String attributeName, @NotNull final String attributeValue) {
105 return and(StandardPatterns.not(withAttributeValue(attributeName, attributeValue)));
108 public static class Capture extends XmlTagPattern<Capture> {