update copyright
[fedora-idea.git] / xml / openapi / src / com / intellij / patterns / XmlNamedElementPattern.java
blob530e798520e28dc9e505018a5d3af507ea5074fc
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.psi.PsiNamedElement;
19 import com.intellij.psi.xml.XmlAttribute;
20 import com.intellij.psi.xml.XmlElement;
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 /**
27 * @author peter
29 public abstract class XmlNamedElementPattern<T extends XmlElement & PsiNamedElement,Self extends XmlNamedElementPattern<T,Self>> extends XmlElementPattern<T,Self>{
31 public XmlNamedElementPattern(@NotNull final InitialPatternCondition<T> condition) {
32 super(condition);
35 protected abstract String getLocalName(T t);
36 protected abstract String getNamespace(T t);
38 public Self withLocalName(@NonNls String localName) {
39 return withLocalName(StandardPatterns.string().equalTo(localName));
42 public Self withLocalName(final ElementPattern<String> localName) {
43 return with(new PsiNamePatternCondition<T>("withLocalName", localName) {
44 public String getPropertyValue(@NotNull final Object o) {
45 return o instanceof XmlElement ? getLocalName((T)o) : null;
47 });
50 public Self withNamespace(@NonNls final String... namespaces) {
51 return withNamespace(PlatformPatterns.string().oneOf(namespaces));
54 public Self withNamespace(final ElementPattern<String> namespace) {
55 return with(new PatternCondition<T>("withNamespace") {
56 public boolean accepts(@NotNull final T s, final ProcessingContext context) {
57 return namespace.accepts(getNamespace(s));
59 });
62 public static class XmlAttributePattern extends XmlNamedElementPattern<XmlAttribute, XmlAttributePattern> {
63 protected XmlAttributePattern() {
64 super(new InitialPatternCondition<XmlAttribute>(XmlAttribute.class) {
65 public boolean accepts(@Nullable final Object o, final ProcessingContext context) {
66 return o instanceof XmlAttribute;
68 });
71 protected String getLocalName(XmlAttribute xmlAttribute) {
72 return xmlAttribute.getLocalName();
75 protected String getNamespace(XmlAttribute xmlAttribute) {
76 return xmlAttribute.getNamespace();