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
.xml
.XmlAttribute
;
19 import com
.intellij
.psi
.xml
.XmlElement
;
20 import com
.intellij
.psi
.xml
.XmlTag
;
21 import com
.intellij
.util
.ProcessingContext
;
22 import com
.intellij
.util
.xml
.DomElement
;
23 import com
.intellij
.util
.xml
.DomManager
;
24 import com
.intellij
.util
.xml
.DomTarget
;
25 import com
.intellij
.pom
.PomTargetPsiElement
;
26 import org
.jetbrains
.annotations
.NotNull
;
31 public class DomPatterns
{
33 public static <T
extends DomElement
> DomElementPattern
.Capture
<T
> domElement(Class
<T
> aClass
) {
34 return new DomElementPattern
.Capture
<T
>(aClass
);
37 public static DomElementPattern
.Capture
<DomElement
> domElement() {
38 return domElement(DomElement
.class);
41 public static GenericDomValuePattern
<?
> genericDomValue() {
42 return new GenericDomValuePattern();
45 public static <T
> GenericDomValuePattern
<T
> genericDomValue(ElementPattern
<?
> valuePattern
) {
46 return ((GenericDomValuePattern
)genericDomValue()).withValue(valuePattern
);
49 public static <T
> GenericDomValuePattern
<T
> genericDomValue(Class
<T
> aClass
) {
50 return new GenericDomValuePattern
<T
>(aClass
);
53 public static XmlElementPattern
.Capture
withDom(final ElementPattern
<?
extends DomElement
> pattern
) {
54 return new XmlElementPattern
.Capture().with(new PatternCondition
<XmlElement
>("tagWithDom") {
55 public boolean accepts(@NotNull final XmlElement xmlElement
, final ProcessingContext context
) {
56 final DomManager manager
= DomManager
.getDomManager(xmlElement
.getProject());
57 if (xmlElement
instanceof XmlAttribute
) {
58 return pattern
.getCondition().accepts(manager
.getDomElement((XmlAttribute
)xmlElement
), context
);
60 return xmlElement
instanceof XmlTag
&& pattern
.getCondition().accepts(manager
.getDomElement((XmlTag
)xmlElement
), context
);
65 public static XmlTagPattern
.Capture
tagWithDom(String tagName
, Class
<?
extends DomElement
> aClass
) {
66 return tagWithDom(tagName
, domElement(aClass
));
69 public static XmlTagPattern
.Capture
tagWithDom(String tagName
, ElementPattern
<?
extends DomElement
> domPattern
) {
70 return XmlPatterns
.xmlTag().withLocalName(tagName
).and(withDom(domPattern
));
73 public static PsiElementPattern
.Capture
<PomTargetPsiElement
> domTargetElement(final ElementPattern
<?
extends DomElement
> pattern
) {
74 return PlatformPatterns
.pomElement(withDomTarget(pattern
));
77 public static ElementPattern
<DomTarget
> withDomTarget(final ElementPattern
<?
extends DomElement
> pattern
) {
78 return new ObjectPattern
.Capture
<DomTarget
>(DomTarget
.class).with(new PatternCondition
<DomTarget
>("withDomTarget") {
80 public boolean accepts(@NotNull final DomTarget target
, final ProcessingContext context
) {
81 return pattern
.accepts(target
.getDomElement(), context
);