update copyright
[fedora-idea.git] / xml / dom-openapi / src / com / intellij / patterns / DomElementPattern.java
blobe68cf0d0e89913716d95e9fab4e904ca8bc207d8
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.util.xml.DomElement;
19 import com.intellij.util.xml.DomElementVisitor;
20 import com.intellij.util.xml.reflect.AbstractDomChildrenDescription;
21 import com.intellij.util.xml.reflect.DomChildrenDescription;
22 import com.intellij.util.ProcessingContext;
23 import org.jetbrains.annotations.NonNls;
24 import org.jetbrains.annotations.NotNull;
26 import java.util.ArrayList;
27 import java.util.List;
29 /**
30 * @author peter
32 public class DomElementPattern<T extends DomElement,Self extends DomElementPattern<T,Self>> extends TreeElementPattern<DomElement,T,Self> {
33 protected DomElementPattern(final Class<T> aClass) {
34 super(aClass);
37 protected DomElementPattern(@NotNull final InitialPatternCondition<T> condition) {
38 super(condition);
41 protected DomElement getParent(@NotNull DomElement t) {
42 return t.getParent();
45 protected DomElement[] getChildren(@NotNull final DomElement domElement) {
46 final List<DomElement> children = new ArrayList<DomElement>();
47 domElement.acceptChildren(new DomElementVisitor() {
48 public void visitDomElement(final DomElement element) {
49 children.add(element);
51 });
52 return children.toArray(new DomElement[children.size()]);
55 public static class Capture<T extends DomElement> extends DomElementPattern<T, Capture<T>> {
56 protected Capture(final Class<T> aClass) {
57 super(aClass);
62 public Self withChild(@NonNls @NotNull final String localName, final ElementPattern pattern) {
63 return with(new PatternCondition<T>("withChild") {
64 public boolean accepts(@NotNull final T t, final ProcessingContext context) {
65 for (final AbstractDomChildrenDescription description : t.getGenericInfo().getChildrenDescriptions()) {
66 if (!(description instanceof DomChildrenDescription) || localName.equals(((DomChildrenDescription)description).getXmlElementName())) {
67 for (final DomElement element : description.getValues(t)) {
68 if (localName.equals(element.getXmlElementName()) && pattern.getCondition().accepts(element, context)) {
69 return true;
74 return false;
76 });