/*
 * Copyright 2000-2010 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.lang.ant.dom;

import com.intellij.lang.ant.AntBundle;
import com.intellij.lang.ant.AntSupport;
import com.intellij.pom.references.PomService;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiReference;
import com.intellij.util.ArrayUtil;
import com.intellij.util.xml.*;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.LinkedHashSet;
import java.util.Set;

/**
 * @author Eugene Zhuravlev
 *         Date: Apr 16, 2010
 */
public class AntDomRefIdConverter extends Converter<AntDomElement> implements CustomReferenceConverter<AntDomElement>{
  public AntDomElement fromString(@Nullable @NonNls String s, ConvertContext context) {
    if (s != null) {
      final AntDomElement element = AntSupport.getInvocationAntDomElement(context);
      if (element != null) {
        return findElementById(element.getContextAntProject(), s);
      }
    }
    return null;
  }

  public String toString(@Nullable AntDomElement antDomElement, ConvertContext context) {
    return antDomElement != null? antDomElement.getId().getRawText() : null;
  }

  @NotNull
  public PsiReference[] createReferences(final GenericDomValue<AntDomElement> genericDomValue, final PsiElement element, ConvertContext context) {
    final AntDomElement invocationElement = AntSupport.getInvocationAntDomElement(context);
    return new PsiReference[] {new AntDomReferenceBase(element, true) {
      public PsiElement resolve() {
        final AntDomElement value = genericDomValue.getValue();
        if (value == null) {
          return null;
        }
        final DomTarget target = DomTarget.getTarget(value, value.getId());
        if (target == null) {
          return null;
        }
        return PomService.convertToPsi(element.getProject(), target);
      }
      @NotNull 
      public Object[] getVariants() {
        if (invocationElement == null) {
          return ArrayUtil.EMPTY_OBJECT_ARRAY;
        }
        final Set<String> variants = new LinkedHashSet<String>();
        invocationElement.getContextAntProject().accept(new AntDomRecursiveVisitor() {
          public void visitAntDomElement(AntDomElement element) {
            final String variant = element.getId().getRawText();
            if (variant != null) {
              variants.add(variant);
            }
            super.visitAntDomElement(element);
          }
        });
        return variants.size() > 0? variants.toArray(new Object[variants.size()]) : ArrayUtil.EMPTY_OBJECT_ARRAY;
      }

      public String getUnresolvedMessagePattern() {
        return AntBundle.message("cannot.resolve.refid", getCanonicalText());
      }
    }};
  }

  @Nullable
  private static AntDomElement findElementById(AntDomElement from, final String id) {
    if (id.equals(from.getId().getRawText())) {
      return from;
    }
    for (AntDomElement child : from.getAntChildren()) {
      final AntDomElement result = findElementById(child, id);
      if (result != null) {
        return result;
      }
    }
    return null;
  }
}

@@ -711,16 +711,54 @@
 upport;%0A
+import com.intellij.openapi.util.Ref;%0A
 import c
@@ -3550,171 +3550,399 @@
    f
-or (AntDomElement child : from.getAntChildren()) %7B%0A      final AntDomElement result = findElementById(child, id);
+inal Ref%3CAntDomElement%3E result = new Ref%3CAntDomElement%3E(null);%0A    from.accept(new AntDomRecursiveVisitor() %7B%0A      public void visitAntDomElement(AntDomElement element) %7B%0A        if (result.get() != null) %7B%0A          return;%0A        %7D%0A        if (id.equals(element.getId().getRawText())) %7B
 %0A      
-if (
+    
 result
- != null) %7B%0A        return result
+.set(element);%0A          return;%0A        %7D%0A        super.visitAntDomElement(element)
 ;%0A  
@@ -3944,32 +3944,39 @@
 );%0A      %7D%0A    %7D
+);%0A    
 %0A    return null
@@ -3967,24 +3967,32 @@
 %0A    return 
-null
+result.get()
 ;%0A  %7D%0A%7D%0A

change recursion to visitor