All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.intellij.patterns.XmlNamedElementPattern Maven / Gradle / Ivy

/*
 * Copyright 2000-2014 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.patterns;

import com.intellij.psi.PsiNamedElement;
import com.intellij.psi.xml.XmlAttribute;
import com.intellij.psi.xml.XmlElement;
import com.intellij.util.PairProcessor;
import com.intellij.util.ProcessingContext;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
 * @author peter
 */
public abstract class XmlNamedElementPattern> extends XmlElementPattern{

  public XmlNamedElementPattern(@NotNull final InitialPatternCondition condition) {
    super(condition);
  }

  protected abstract String getLocalName(T t);
  protected abstract String getNamespace(T t);

  public Self withLocalName(@NonNls String localName) {
    return withLocalName(StandardPatterns.string().equalTo(localName));
  }

  public Self withLocalName(@NonNls String... localNames) {
    return withLocalName(StandardPatterns.string().oneOf(localNames));
  }

  public Self withLocalName(final ElementPattern localName) {
    return with(new PsiNamePatternCondition("withLocalName", localName) {
      @Override
      public String getPropertyValue(@NotNull final Object o) {
        return o instanceof XmlElement ? getLocalName((T)o) : null;
      }
    });
  }

  public Self withNamespace(@NonNls final String namespace) {
    return withNamespace(StandardPatterns.string().equalTo(namespace));
  }

  public Self withNamespace(@NonNls final String... namespaces) {
    return withNamespace(StandardPatterns.string().oneOf(namespaces));
  }

  public Self withNamespace(final ElementPattern namespace) {
    return with(new PatternConditionPlus("withNamespace", namespace) {
      @Override
      public boolean processValues(T t,
                                   ProcessingContext context,
                                   PairProcessor processor) {
        return processor.process(getNamespace(t), context);
      }
    });
  }

  public static class XmlAttributePattern extends XmlNamedElementPattern {
    protected XmlAttributePattern() {
      super(new InitialPatternCondition(XmlAttribute.class) {
        @Override
        public boolean accepts(@Nullable final Object o, final ProcessingContext context) {
          return o instanceof XmlAttribute;
        }
      });
    }

    @Override
    protected String getLocalName(XmlAttribute xmlAttribute) {
      return xmlAttribute.getLocalName();
    }

    @Override
    protected String getNamespace(XmlAttribute xmlAttribute) {
      return xmlAttribute.getNamespace();
    }
    
    public XmlAttributePattern withValue(final StringPattern pattern) {
      return with(new PatternConditionPlus("withValue", pattern) {
        @Override
        public boolean processValues(XmlAttribute t,
                                     ProcessingContext context,
                                     PairProcessor processor) {
          return processor.process(t.getValue(), context);
        }
      });
    }

  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy